Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Can someone please help me to understand each part of this code? Can you comment every line so I can understand and custom the code accordind to my purpose. Here is the link in pastebin:

class Towfiq_Person {
    static function on_load() {
        add_action('init',array(__CLASS__,'init'));
        add_action('wp_insert_post',array(__CLASS__,'wp_insert_post'),10,2);
        add_action('profile_update',array(__CLASS__,'profile_update'),10,2);
        add_action('user_register',array(__CLASS__,'profile_update'));
        add_filter('author_link',array(__CLASS__,'author_link'),10,2);
        add_filter('get_the_author_url',array(__CLASS__,'author_link'),10,2);
 }

The rest of the code at this link :pastebin.com/30GCNhLv

many thanks

UPDATE 1 :

1st part up understood, what about this part.what does each line does i mean in human language;

 static function get_email_key() {
     return apply_filters( 'person_email_key', '_email' );
 }
 static function profile_update($user_id,$old_user_data=false) {
     global $wpdb;
     $is_new_person = false;
     $user = get_userdata($user_id);
     $user_email = ($old_user_data ? $old_user_data->user_email : $user->user_email);
     $email_key = self::get_email_key();
     $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));
     if (!is_numeric($person_id)) {
         $person_id = $is_new_person = wp_insert_post(array(
            'post_type' => 'towfiq-person',
            'post_status' => 'publish',   // Maybe this should be pending or draft?
            'post_title' => $user->display_name,
        ));
    }
    update_user_meta($user_id,'_person_id',$person_id);
    update_post_meta($person_id,'_user_id',$user_id);
    if ($is_new_person || ($old_user_data && $user->user_email!=$old_user_data-   >user_email)) {
        update_post_meta($person_id,$email_key,$user->user_email);
    }
}
share|improve this question
1  
Please try to formulate this as manageable question. In current form this is not even a question, but a request for someone to do work documenting third party code and explaining it to you. As such it is not a good for for the QA format of this site. –  Rarst Mar 28 '12 at 22:40
add comment

closed as not a real question by EAMann Mar 28 '12 at 22:45

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

up vote 3 down vote accepted

Basically, this is a programming pattern for namespacing code within a WordPress plugin. Typically, you can only have one function called init() in a program, but more than one author will try to use that name. Placing function names in a class is a way around this limitation.

For example:

class Towfiq_Person {
    static function on_load() { }

    static function init() { }

    static function profile_update() { }
}

Rather than calling these directly, you invoke them with the class name as well. So:

Towfiq_Person::on_load();
Towfiq_Person::init();
Towfiq_Person::profile_update();

The section of code you included in your question is the bootstrapper for this class. At the very bottom of the linked PHP file is Towfiq_Person::on_load(), which tells WordPress to execute the static on_load() function of the Towfiq_Person class.

This function wires up various events and filters within WordPress. When WordPress fires it's init action, it will also call Towfiq_Person::init(). When WordPress inserts a post and fires its wp_insert_post action, it will also call Towfiq_Person::wp_insert_post().

Each line you listed above ties a specific function in the class to a specific event fired by WordPress.

I recommend you read up a bit on Actions and Filters to understand exactly what's going on, but this is a basic overview.

Update

Based on your further request for information ...

We can't define every function for you. You should really contact the original developer and ask for documentation. But here's a quick overview.

The get_email_key() function applies a WordPress filter against the string "_email" Essentially, it takes "_email" and passes it through any function that's tied to that filter before returning it to the user. If you were to call get_email_key() right now, you'd be given back "_email"

The profile_update() function is bound to the profile_update and user_register actions within WordPress. When either of these actions are triggered, WordPress will call your function and pass in the ID of the user being changed and (optionally) the data that was changed.

Your function then does the following:

// Reference the global database object so we can run queries later
global $wpdb;

// We're assuming this is an existing record for now. This will be updated elsewhere
// if necessary.
$is_new_person = false;

// Get data for the user passed in by WordPress
$user = get_userdata($user_id);

// If we have old user data, get the user's email from it. If not, get the user's email
// from the $user object created above.
$user_email = ($old_user_data ? $old_user_data->user_email : $user->user_email);

// Get the email key. Typically this will be "_email"
$email_key = self::get_email_key();

// Get the ID of the person we're working with from the database. We're searching for
// the ID of a post that has meta information matching the email key from above and
// the email address of the user.
$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));

// If we didn't find a user ID (the ID passed back is null or "false"), then we
// insert a new custom post type "towfiq-person" with the data we need.  The title is
// the user's name, the status is published, and we set post meta information so we can
// find it again with the above query.
if (!is_numeric($person_id)) {
         $person_id = $is_new_person = wp_insert_post(array(
            'post_type' => 'towfiq-person',
            'post_status' => 'publish',   // Maybe this should be pending or draft?
            'post_title' => $user->display_name,
        ));
    }
    // Update the user's meta info to map to the new custom post type
    update_user_meta($user_id,'_person_id',$person_id);
    // Update the custom post type's meta info to map to the user
    update_post_meta($person_id,'_user_id',$user_id);
    if ($is_new_person || ($old_user_data && $user->user_email!=$old_user_data-   >user_email)) {
        // Update the custom post type's meta info to map to the user's email
        update_post_meta($person_id,$email_key,$user->user_email);
    }
share|improve this answer
    
thanks i understand 1st part but what about the other part see upadate1. –  fritids Mar 28 '12 at 22:15
    
I'll document that one set of functions, but honestly we won't do all of the work for you. Read the code. If you don't understand what it's doing, contact the original developer and ask for documentation. We won't write your code or documentation for you. –  EAMann Mar 28 '12 at 22:25
    
thank you very much for your efforts i understand .i have treid to contact the writer of this code in this site but no success. dosen't matter it is already a begin. cheers –  fritids Mar 28 '12 at 22:48
    
I contacted the original author of this code directly - if you look, the original answer was submitted by Mike Schinkel. Just Google his name and I'm sure you can find his contact information to get in touch should you need further help. –  EAMann Mar 28 '12 at 22:49
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.