Php 了解wordpress中的cron&;一段代码

Php 了解wordpress中的cron&;一段代码,php,wordpress,Php,Wordpress,我正在寻找一些有助于理解这段代码的注释,如果可能的话,我正在试图弄清楚它到底是如何工作的 它位于一个插件中,我已经看过Wordpress的codex,但它并没有给我带来多大帮助 我看到的页面是: 而且: 代码段: add_action('wp','prefix_setup_schedule'); function prefix_setup_schedule() { if (!wp_next_scheduled('prefix_hourly_event')){ wp_s

我正在寻找一些有助于理解这段代码的注释,如果可能的话,我正在试图弄清楚它到底是如何工作的

它位于一个插件中,我已经看过Wordpress的codex,但它并没有给我带来多大帮助

我看到的页面是:

而且:

代码段:

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');
}
add_action('wp','prefix_setup_schedule');
函数前缀\u设置\u计划(){
如果(!wp\u next\u scheduled('prefix\u hourly\u event')){
wp_计划_事件(time(),“hourly”,“prefix_hourly_event”);
}
如果(!wp_next_scheduled('daily_prefix_hourly_event')){
//wp_计划_事件(time(),“daily”,“daily_前缀_hourly_事件”);
wp_时间表_事件(time(),“wpo_daily”,“daily_prefix_hourly_event”);
}
}
添加动作(“前缀每小时事件”、“过滤器成员”);
$t=时间();
$hour=日期('G');
if(get_选项('cronhour')!=null){
$hcron=(int)get_选项('cronhour');
如果($hcron>0&&$hcron=$on){
添加动作(“每日前缀”、“每小时事件”、“过滤温度”);
}
据我所知,它似乎在将当前时间与“cronhour”进行比较,并以某种方式添加cron

我还注意到插件没有unschedule/clear-schedule钩子,所以即使插件被禁用,它也会继续启动吗

我看了下面的内容


不确定我应该使用哪一个,它不是很清楚。我非常感谢一些人帮助我理解它在做什么,并提供一些注释和解释差异。

在进入代码之前,我想知道一些关于代码中引用的选项
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');
}

+1很好的解释。我可能错过了它,但值得一提的是,引用的代码不包括使用挂钩调用的所有函数(例如,
filter\u temp
filter\u mem
),所以可能无法提供它正在做什么的全貌。@drew010感谢您的精彩解释。您是对的
cronhour
是一个数字0-23,表示它要运行的时间,它是从插件选项页调用的。据我所知,如果未设置,则整个if-else区域将默认为午夜,如果未设置,则默认为午夜他说,
24
23201
将默认为午夜。你能解释一下日期('g'是什么意思吗因为我在代码中的任何地方都看不到。我安装了一个cron查看器/运行程序插件来查看cron。如果我在这两个cron上运行一个非计划程序,它会破坏其他任何东西吗?比如其他prg是否可能与那些非常通用的cron名称相同。最后,但并非最不重要的是
wpo_daily
因为我在wordpress中找不到它codex@Ryflex我所指的
日期('G')
是设置为
$hour
的日期。查找
$hour=date('G'))
在代码中。@Ryflex您关于
wpo_daily
的观点很好。上面正确的一行被注释掉了。除非他们创建了一个筛选器将wpo_daily定义为有效的计划条目,否则它是无效的,不会起作用,所以可能这就是为什么他们运行下面的代码来进行日常处理。据我所知,这两个项目是不计划的事件不会影响其他任何东西。它们不是我所知道的任何东西的标准,但就我个人而言,当我存储这样的东西时,我总是尝试在它前面加上我的插件特有的东西,这样它看起来就不那么通用了,其他人使用相同值的可能性也很小