php – 理解wordpress中的crons和一段代码
如果可能的话,我正在寻找帮助理解这段代码的一些评论,我正在试图弄清楚它是如何工作的.
它位于一个插件中,我看过Wordpress的手抄本,但它并没有真正帮助我. 我看过的页面是: https://codex.wordpress.org/Function_Reference/wp_schedule_event 还有: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,date('m'),date('d'),date('Y')); }else{ $on = mktime($hcron,date('d')+1,date('Y')); } }else{ $hcron = 0; if($hour < $hcron){ $on = mktime($hcron,date('Y')); } } } else { $hcron = 0; if($hour < $hcron){ $on = mktime($hcron,date('Y')); }else{ $on = mktime($hcron,date('Y')); } } if ($t>=$on){ add_action('daily_prefix_hourly_event','filter_temp'); } 据我所知,它似乎是将当前时间与“cronhour”进行比较,并以某种方式添加了cron. 我还注意到插件没有非计划/清除计划挂钩,所以即使插件被禁用它也会继续触发? 我看了下面的内容 https://codex.wordpress.org/Function_Reference/wp_unschedule_event 不确定我应该使用什么,不是很清楚.我非常感谢帮助理解这是做什么的,有些评论和解释差异. 解决方法
在进入代码之前,我想知道关于代码中引用的选项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(),'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_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,date('Y')); }else{ // else timestamp is 5 am tomorrow $on = mktime($hcron,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,date('Y')); }else{ // set time to midnight tomorrow (hcron was set to 0) $on = mktime($hcron,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,date('Y')); }else{ // midnight tomorrow $on = mktime($hcron,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调度程序无法运行它不应该运行的那些挂钩,则打算作为故障保护). 鉴于一些冗余代码,并且两个if语句永远不能运行,因为date(‘G’)永远不会小于0,我的意见是编写它的人并不完全理解他们在做什么. 并评论你所说的关于“插件[没有非计划/清除计划挂钩”的内容,所以即使插件被禁用它也会继续触发“;当一个插件被禁用时,WordPress不会调用它的任何代码,所以当插件被禁用时,这一切都不会运行. 即使WP的事件调度程序具有每日和每小时事件,因为它将调用的那些函数是由此插件定义的,因此当插件被禁用时不可用,因此它们将被忽略(是的,保留这些值是草率的在调度程序中,当插件被禁用/删除时,因为它只会导致将由WP执行的额外不必要的处理(没有结果). 我希望我说的是有道理的 – 如果你想澄清任何事情或有其他问题,请随时发表评论. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |