0

i´m trying to run a function with a schedule but the response is empty. I am developing a plugin where i will send a json encoded array to my IoT service. The service is fine. All other functions are working fine. i want to send the update counts to my iot service.

What i have is this - my function:

    function send_count_updates_to_iobroker() { 
    
    $updates = wp_get_update_data();
    
    //Generate text if title is empty
    if ( empty($updates['title'] )) {
        $updatesTitle = "No updates available";
    } else {
        $updatesTitle = $updates['title'];
    }
    
    $datum_uhrzeit = datum_uhrzeit();

    $serviceURL = get_option( 'iob_iot_service_url_count_updates' );
    $data = wp_json_encode( (object) array( 
        'plugins'         => $updates['counts']['plugins'],
        'themes'          => $updates['counts']['themes'],
        'wordpress'       => $updates['counts']['wordpress'],
        'translations'    => $updates['counts']['translations'],
        'total'           => $updates['counts']['total'],
        'title'           => $updatesTitle,
        'timestamp'       => $datum_uhrzeit
    ), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES );
    
    $response = wp_remote_post( $serviceURL . $data );
    
    return $response; 
}

This function will be executed every five minutes with a schedule:

// own schedule five minutes
add_filter( 'cron_schedules', 'five_minute_cron_interval' );
function five_minute_cron_interval( $schedules ) { 
    $schedules['five_minutes'] = array(
        'interval' => 300,
        'display'  => esc_html__( 'Alle 5 Minuten' ), );
    return $schedules;
}

I deactivated the wp-cron in wp-config.php and activate the serverside cron:

define( 'DISABLE_WP_CRON', true);

*/5 * * * * wget -q -O - https://example.com/wp-cron.php?doing_wp_cron  >/dev/null 2>&1

The Cronjob is working - every five minutes it fires the events... But the response is always 0:

Aktualisiert:   05.10.2021 - 14:00:01 Uhr
Title:  No updates available
Plugins:    0
Themes: 0
Translations:   0
Core:   0
Total:  0

Also i used for tests the plugin Advanced Cron Manager where i can execute the events manually. This works fine. The response is:

Aktualisiert:   05.10.2021 - 14:01:00 Uhr
Title:  2 Plugin-Aktualisierungen
Plugins:    2
Themes: 0
Translations:   0
Core:   0
Total:  2

Can anyone tell me what i´m doing wrong?

Thanks a lot!

nukleuz

//Edit:

Here is the missing code:

register_activation_hook( __FILE__, 'jb_iobroker_iot_activation' );

add_action( 'jb_iobroker_iot_hourly_event', 'do_this_hourly' );
add_action( 'jb_iobroker_iot_5min_event', 'do_this_five_min_interval' );

 
function jb_iobroker_iot_activation() {
    wp_schedule_event( time(), 'hourly', 'jb_iobroker_iot_hourly_event' );
    wp_schedule_event( time(), 'five_minutes', 'jb_iobroker_iot_5min_event' );
}
 
function do_this_hourly() {
    checkPluginUpdates();
    send_bloginfo_to_iobroker();
}

function do_this_five_min_interval() {
    send_count_updates_to_iobroker();
}


register_deactivation_hook( __FILE__, 'jb_iobroker_iot_deactivation' );
 
function jb_iobroker_iot_deactivation() {
    wp_clear_scheduled_hook( 'jb_iobroker_iot_hourly_event' );
    wp_clear_scheduled_hook( 'jb_iobroker_iot_5min_event' );
}

// own schedule five minutes
add_filter( 'cron_schedules', 'five_minute_cron_interval' );
function five_minute_cron_interval( $schedules ) { 
    $schedules['five_minutes'] = array(
        'interval' => 300,
        'display'  => esc_html__( 'Alle 5 Minuten' ), );
    return $schedules;
}
15
  • The response from where? Do you mean the response from wp-cron.php or the response from your IOT service? No cron jobs are registered in the code in your question, did you miss out parts?
    – Tom J Nowell
    Oct 5 at 12:11
  • I mean the response from iot service.
    – nukleuz
    Oct 5 at 13:07
  • is the iot service not a 3rd party? Wouldn't the issue be in the iot service not WP?
    – Tom J Nowell
    Oct 5 at 14:56
  • No,, because other functions are working fine. And this function in the question is working when i execute the event in the Advanced Cron Manager Plugin. The plugin lists all the registered events/crons. Each of them have the option to execute manually - if i do that the data in the json is fine. All updateable plugins are shown. But if the cronjob executes the function all values are zero/0... So the iot-service is not the problem. The iot service is just a url where i send the data/array as json in my homeautomation to visualize the information...
    – nukleuz
    Oct 5 at 17:52
  • are you sure that the value is actually 0? And not PHP trying to marshal undefined into a falsey value that resolves to 0? wp_get_update_data is full of checks to test if the current user can do something, but cron jobs don't have a current user. Wouldn't it make more sense to hook into when these update checks happen and send the data on that hook instead?
    – Tom J Nowell
    Oct 5 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.