了解wordpress&中的crons一段代码

时间:2015-09-26 01:05:07

标签: php wordpress

如果可能的话,我正在寻找帮助理解这段代码的一些评论,我试图弄清楚它是如何工作的。

它位于一个插件中,我看过Wordpress的手抄本,但它对我没什么帮助。

我看过的页面是:

https://codex.wordpress.org/Function_Reference/wp_schedule_event https://codex.wordpress.org/Function_Reference/wp_next_scheduled

还有:http://codex.wordpress.org/Plugin_API/Action_Reference/wp

代码段:

add_action('wp','prefix_setup_schedule');
function prefix_setup_schedule() {
    if (!wp_next_scheduled('prefix_hourly_event')){
        wp_schedule_event(time(), 'hourly', 'prefix_hourly_event');
    }
    if (!wp_next_scheduled('daily_prefix_hourly_event')){
        //wp_schedule_event(time(), 'daily', 'daily_prefix_hourly_event');
        wp_schedule_event(time(), 'wpo_daily', 'daily_prefix_hourly_event');
    }
}

add_action('prefix_hourly_event','filter_mem');

$t = time();
$hour = date('G');
if(get_option('cronhour') != null){
    $hcron = (int)get_option('cronhour');
    if($hcron > 0 && $hcron <= 23){
        if($hour < $hcron){
            $on = mktime($hcron, 0, 0, date('m'), date('d'), date('Y'));
        }else{
            $on = mktime($hcron, 0, 0, date('m'), date('d')+1, date('Y'));
        }
    }else{
        $hcron = 0;
        if($hour < $hcron){
            $on = mktime($hcron, 0, 0, date('m'), date('d'), date('Y'));
        }else{
            $on = mktime($hcron, 0, 0, date('m'), date('d')+1, date('Y'));
        }
    }
} else {
    $hcron = 0;
    if($hour < $hcron){
        $on = mktime($hcron, 0, 0, date('m'), date('d'), date('Y'));
    }else{
        $on = mktime($hcron, 0, 0, date('m'), date('d')+1, date('Y'));
    }
}

if ($t>=$on){
    add_action('daily_prefix_hourly_event', 'filter_temp');
}

据我所知,似乎是将当前时间与&#34; cronhour&#34;并以某种方式添加了cron。

我还注意到插件没有非计划/清除计划挂钩所以即使插件被禁用它肯定会继续触发?

我查看了以下内容

https://codex.wordpress.org/Function_Reference/wp_unschedule_event https://codex.wordpress.org/Function_Reference/wp_clear_scheduled_hook

不确定我应该使用什么,它不是很清楚。我非常感谢帮助理解这是做什么的,有些评论和解释差异。

1 个答案:

答案 0 :(得分:3)

在进入代码之前,我想知道代码中引用的选项cronhour。我不确定它代表什么或它是如何使用的(即插件是否更改/更新它[可能在某些触发的事件中]或者是否由网站管理员设置在选项的某处并保持固定)?

该变量会影响此代码的一部分,这就是我提到它的原因。也就是说,我必须对第二部分进行一些猜测,这可能会引起你的大部分困惑。

此外,正如您所见,此代码中的某些逻辑很差。有重复的代码不需要和一个永远不会执行的语句,因为永远不会满足条件。

以下是我解释一些代码的工作原理:

<?php

// this causes the 'prefix_setup_schedule' function to run on all WP requests
add_action('wp','prefix_setup_schedule');

// the function referenced above - essentially gets run on every request
function prefix_setup_schedule() {
    // checks to see if WP's scheduler has an hourly event for
    // prefix_hourly_event set to run, if not, schedules it to run "now"
    // and then every hour going forward
    if (!wp_next_scheduled('prefix_hourly_event')){
        wp_schedule_event(time(), 'hourly', 'prefix_hourly_event');
    }
    // same as above, except for the daily event, and every day at this
    // time going forward
    if (!wp_next_scheduled('daily_prefix_hourly_event')){
        //wp_schedule_event(time(), 'daily', 'daily_prefix_hourly_event');
        wp_schedule_event(time(), 'wpo_daily', 'daily_prefix_hourly_event');
    }
}

// tells WP to run the filter_mem function when the prefix_hourly_event
// hook runs if that hook is called based on the schedule
add_action('prefix_hourly_event','filter_mem');

$t = time(); // current time
$hour = date('G'); // current hour in 24h format

// again, not sure about this variable, but it represents an hour of the day
// for this example, pretend it equals 5 (5 AM)
if(get_option('cronhour') != null) {
    $hcron = (int)get_option('cronhour');
    if($hcron > 0 && $hcron <= 23){ // range check
        if($hour < $hcron){
            // if current hour is less than 5, set timestamp to 5 AM today
            $on = mktime($hcron, 0, 0, date('m'), date('d'), date('Y'));
        }else{
            // else timestamp is 5 am tomorrow
            $on = mktime($hcron, 0, 0, date('m'), date('d')+1, date('Y'));
        }
    }else{
        // invalid range, they just set hour to midnight
        $hcron = 0;
        if($hour < $hcron){
            // NOOP: not possible, date('G') cannot be less than 0
            $on = mktime($hcron, 0, 0, date('m'), date('d'), date('Y'));
        }else{
            // set time to midnight tomorrow (hcron was set to 0)
            $on = mktime($hcron, 0, 0, date('m'), date('d')+1, date('Y'));
        }
    }
} else {
    // cronhour option not set, set to midnight
    // this is essentially duplicate to code above.
    // written properly, this block could have been avoided

    // option was not set, so set hour to midnight
    $hcron = 0;
    if($hour < $hcron){
        // again, $hour cannot be less than 0
        $on = mktime($hcron, 0, 0, date('m'), date('d'), date('Y'));
    }else{
        // midnight tomorrow
        $on = mktime($hcron, 0, 0, date('m'), date('d')+1, date('Y'));
    }
}

if ($t>=$on){
    // if current time is later than $on calculated above, runs the daily action
    add_action('daily_prefix_hourly_event', 'filter_temp');
}

在我看到它之后(除非选项cronhour像我之前提到的那样经常更改),一旦cronhour通过,这段代码将基本上运行&#34;每日&#34;在cronhour通过后,每个请求都会挂钩(如果我读错了,有人会纠正我。)

add_action('prefix_hourly_event','filter_mem');之后的所有代码似乎都是不必要的(如果WP调度程序无法运行那些不应该运行的挂钩,则打算作为故障保护)。

考虑到一些冗余代码,并且两个if语句永远不能运行,因为date('G')永远不会小于0,我认为编写它的人并不完全理解它们是什么做。

并评论你所说的关于&#34;插件[没有非计划/清除计划挂钩]所以肯定即使插件被禁用也会继续触发&#34 ;;当一个插件被禁用时,WordPress不会调用它的任何代码,因此当插件被禁用时,这些代码都不会运行。

即使WP的事件调度程序有每日和每小时事件,因为它将调用的那些函数是由此插件定义的,因此当插件被禁用时不可用,因此它们将被忽略(是的,当插件被禁用/删除时,将这些值留在调度程序中是很草率的,因为它只会导致WP无法完成的额外不必要的处理。

我希望我所说的有意义 - 如果您想澄清任何事情或有其他问题,请随时发表评论。