Understanding crowns in wordpress and code snippet

I am looking for some help in understanding this code with some comments, if possible, I am trying to figure out how this works.

It is located in the plugin, and I looked at the Wordpress codex, but that didn't help me much.

The pages that I looked at were / were:

https://codex.wordpress.org/Function_Reference/wp_schedule_event https://codex.wordpress.org/Function_Reference/wp_next_scheduled

And also: http://codex.wordpress.org/Plugin_API/Action_Reference/wp

Code snippet:

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

As far as I can tell, it seems to compare the current time with "cronhour" and somehow add cron.

I also noticed that there is no fuzzy / clear schedule in the plugin, so will it continue to fire even when the plugin is disabled?

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

, , . , , .

+4
1

, - 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 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 , ).

if, , date('G') 0, - , , , .

, " [] / , , "; , WordPress - , , .

WP , , , , , , , ( , , /, , WP, ).

, , , - , , - .

+3

Source: https://habr.com/ru/post/1609056/


All Articles