了解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');
}

据我所知,似乎正在将当前时间与“ cronhour”进行比较,并以某种方式添加了cron。

我还注意到插件没有取消计划/清除计划挂钩,因此即使禁用了插件,它肯定也会继续触发?

我看了以下

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

不知道我应该使用哪种,还不太清楚。我非常感谢您的帮助,并提供了一些评论并解释了不同之处,帮助您理解了它的作用。

drew010

在进入代码之前,我想知道关于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通过,这段代码实际上将在cronhour通过之后的每个请求上运行“每日”钩子(如果我读错了)。

后面的所有代码add_action('prefix_hourly_event','filter_mem');似乎都是不必要的(如果WP Scheduler无法运行不应执行的钩子,则将作为故障保护)。

给定一些冗余代码,然后两个if语句永远不会运行,因为date('G')永远不会小于0,我认为编写该代码的人并不完全了解他们在做什么。

并评论您对“插件[没有]没有计划外/清除计划的钩子,因此即使在禁用插件的情况下也可以确保触发”的评论;禁用插件后,WordPress不会调用其任何代码,因此禁用插件时,这些代码均不应运行。

即使WP的事件调度程序有每日和每小时的事件,由于它将要调用的功能都是由该插件定义的,因此在禁用该插件时不可用,因此它们将被忽略(是的,保留这些值是很草率的在调度程序中,当插件被禁用/删除时,因为它只会导致多余的不必要的处理,而该处理将由WP完成,没有结果)。

我希望我所说的是有道理的-如果您想澄清任何问题或有其他疑问,请随时发表评论。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章