基本上,这是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_update
和user_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);
}