Advertisement
Guest User

Untitled

a guest
Mar 28th, 2012
108
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Towfiq_Person {
  2.     static function on_load() {
  3.      add_action('init',array(__CLASS__,'init'));
  4. add_action('wp_insert_post',array(__CLASS__,'wp_insert_post'),10,2);
  5. add_action('profile_update',array(__CLASS__,'profile_update'),10,2);
  6. add_action('user_register',array(__CLASS__,'profile_update'));
  7. add_filter('author_link',array(__CLASS__,'author_link'),10,2);
  8. add_filter('get_the_author_url',array(__CLASS__,'author_link'),10,2);
  9. }
  10. static function init() {
  11. register_post_type('towfiq-person',
  12.   array(
  13.     'labels'          => array('name'=>'People','singular_name'=>'Person'),
  14.     'public'          => true,
  15.     'show_ui'         => true,
  16.     'rewrite'         => array('slug' => 'people'),
  17.     'hierarchical'    => false,
  18.     //'supports'        => array('title','editor','custom-fields'),
  19.   )
  20. );
  21. }
  22. static function get_email_key() {
  23. return apply_filters( 'person_email_key', '_email' );
  24. }
  25.  static function profile_update($user_id,$old_user_data=false) {
  26.  global $wpdb;
  27.  $is_new_person = false;
  28.  $user = get_userdata($user_id);
  29.  $user_email = ($old_user_data ? $old_user_data->user_email : $user->user_email);
  30. $email_key = self::get_email_key();
  31. $person_id = $wpdb->get_var($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE     meta_key='%s' AND meta_value='%s'",$email_key,$user_email));
  32.   if (!is_numeric($person_id)) {
  33.    $person_id = $is_new_person = wp_insert_post(array(
  34.     'post_type' => 'towfiq-person',
  35.     'post_status' => 'publish',   // Maybe this should be pending or draft?
  36.     'post_title' => $user->display_name,
  37.   ));
  38. }
  39.  update_user_meta($user_id,'_person_id',$person_id);
  40.  update_post_meta($person_id,'_user_id',$user_id);
  41.  if ($is_new_person || ($old_user_data && $user->user_email!=$old_user_data-  >user_email)) {
  42.   update_post_meta($person_id,$email_key,$user->user_email);
  43.   }
  44.   }
  45.    static function wp_insert_post($person_id,$person) {
  46.     if ($person->post_type=='towfiq-person') {
  47.     $email = get_post_meta($person_id,self::get_email_key(),true);
  48.     if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
  49.     $user = get_user_by('email',$email);
  50.     if ($user) { // Associate the user IF there is an user with the same email address
  51.       update_user_meta($user->ID,'_person_id',$person_id);
  52.       update_post_meta($person_id,'_user_id',$user->ID);
  53.       } else {
  54.       delete_post_meta($person_id,'_user_id');
  55.       }
  56.      }
  57.     }
  58.    }
  59.    static function get_user_id($person_id) {
  60.    return get_user_meta($user_id,'_user_id',true);
  61.     }
  62.   static function get_user($person_id) {
  63.   $user_id = self::get_user_id($person_id);
  64.    return get_userdata($user_id);
  65.    }
  66.   static function get_person_id($user_id) {
  67.   return get_user_meta($user_id,'_person_id',true);
  68.   }
  69.   static function get_person($user_id) {
  70.   $person_id = self::get_person_id($user_id);
  71.   return get_post($person_id);
  72.   }
  73.   static function author_link($permalink, $user_id) {
  74.   $author_id = get_user_meta($user_id,'_person_id',true);
  75.   if ($author_id) // If an associate is found, use it
  76.   $permalink = get_post_permalink($author_id);
  77.   return $permalink;
  78.   }
  79.   }
  80.   Towfiq_Person::on_load();
Advertisement
RAW Paste Data Copied
Advertisement