我正在寻找一些可能的帮助,以一些注释来理解此代码,我试图弄清楚它的工作原理。
它位于一个插件中,我看过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');
}
Run Code Online (Sandbox Code Playgroud)
据我所知,似乎正在将当前时间与“ cronhour”进行比较,并以某种方式添加了cron。
我还注意到插件没有取消计划/清除计划挂钩,因此即使禁用了插件,它肯定也会继续触发?
我看了以下
https://codex.wordpress.org/Function_Reference/wp_unschedule_event https://codex.wordpress.org/Function_Reference/wp_clear_scheduled_hook
不知道我应该使用什么,这不是很清楚。我非常感谢您的帮助,并提供了一些评论并解释了不同之处,帮助您理解了它的作用。
在进入代码之前,我想知道有关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');
}
Run Code Online (Sandbox Code Playgroud)
在我看来(除非选项 cronhour 像我之前提到的那样频繁更改),一旦cronhour通过,这段代码实际上将在通过后的每个请求上运行“每日”钩子cronhour(如果我是这样,有人纠正我)读错了)。
之后的所有代码add_action('prefix_hourly_event','filter_mem');似乎都是不必要的(如果 WP 调度程序无法运行那些不应该运行的钩子,则旨在作为故障安全)。
考虑到一些冗余代码,以及两个 if 语句永远无法运行,因为date('G')永远不会小于 0,我认为编写它的人并没有完全理解他们在做什么。
并评论您所说的关于“插件没有取消计划/清除计划挂钩,因此即使插件被禁用它肯定也会继续触发”;当插件被禁用时,WordPress 不会调用其任何代码,因此当插件被禁用时,这些代码都不会运行。
即使 WP 的事件调度程序有每日和每小时的事件,因为它将调用的那些函数是由该插件定义的,因此在禁用插件时不可用,因此它们只会被忽略(是的,保留这些值是很草率的)当插件被禁用/删除时,在调度程序中,因为它只会导致额外的不必要的处理,而这些处理将由没有结果的 WP 完成)。
我希望我说的有道理——如果您想澄清任何事情或有进一步的问题,请随时发表评论。
| 归档时间: |
|
| 查看次数: |
142 次 |
| 最近记录: |