这段PHP函数代码是什么意思?

时间:2012-03-28 作者:fritids

有人能帮我理解这段代码的每一部分吗?你能评论每一行吗?这样我就能理解并根据我的目的定制代码。以下是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);
 }
此链接中的其余代码:pastebin.com/30GCNhLv

非常感谢

UPDATE 1 :

第一部分明白了,这部分怎么样。我在人类语言中的每一行是什么意思;

 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);
    }
}

1 个回复
最合适的回答,由SO网友:EAMann 整理而成

基本上,这是WordPress插件中命名空间代码的编程模式。通常,只能调用一个函数init() 但不止一位作者会尝试使用这个名称。将函数名放在类中是绕过此限制的一种方法。

例如:

class Towfiq_Person {
    static function on_load() { }

    static function init() { }

    static function profile_update() { }
}
与其直接调用它们,还可以使用类名调用它们。因此:

Towfiq_Person::on_load();
Towfiq_Person::init();
Towfiq_Person::profile_update();
问题中包含的代码部分是此类的引导程序。链接的PHP文件的最底部是Towfiq_Person::on_load(), 它告诉WordPress执行静态on_load() 的功能Towfiq_Person

此函数连接WordPress中的各种事件和过滤器。WordPress启动时init 行动,它还将调用Towfiq_Person::init(). 当WordPress插入帖子并激发其wp_insert_post 行动,它还将调用Towfiq_Person::wp_insert_post().

上面列出的每一行都将类中的特定函数与WordPress触发的特定事件联系起来。

我建议你读一读Actions and Filters 要确切了解发生了什么,但这是一个基本概述。

Update

根据您对信息的进一步要求。。。

我们无法为您定义每个函数。您应该与原始开发人员联系并索取文档。但这里有一个快速概述。

这个get_email_key() 函数对字符串“\\u email”应用WordPress筛选器本质上,它接受“\\u email”,并在将其返回给用户之前通过与该筛选器关联的任何函数。如果你打电话get_email_key() 现在,你会收到“电子邮件”

这个profile_update() 函数绑定到profile_updateuser_register WordPress中的操作。当这些动作被触发时,WordPress将调用您的函数并传入被更改的用户ID和(可选)被更改的数据。

然后,您的函数执行以下操作:

// 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);
    }

结束

相关推荐

Update all posts at once

我已经在我的帖子中添加了一个自定义分类法,它将在post\\u update操作中获得iteself的值。一次更新所有帖子的SQL查询是什么,以便为每个帖子添加分类法值。从SQL更新还会调用wordpress的update\\u post操作吗?