脚本宝典收集整理的这篇文章主要介绍了php – 理解wordpress中的crons和一段代码,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
它位于一个插件中,我看过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,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
https://codex.wordpress.org/Function_Reference/wp_clear_scheduled_hook
该变量会影响此代码的一部分,这就是我提到它的原因.也就是说,我必须在第二部分猜一点,这可能会引起你的大部分困惑.
此外,正如您将看到的,此代码中的一些逻辑很差.有重复的代码不需要和一个永远不会执行的语句,因为永远不会满足条件.
这是我如何解释一些工作的代码:
<?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执行的额外不必要的处理(没有结果).
以上是脚本宝典为你收集整理的php – 理解wordpress中的crons和一段代码全部内容,希望文章能够帮你解决php – 理解wordpress中的crons和一段代码所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。