1

I am a bit lost while dealing with a open source plugin implementing wp cron.

You can read about the problem on github https://github.com/nekojira/wp-currencies/issues/6#issuecomment-116735909 - I will summarise it also here. Unfortunately there's a bit of code involved to paste it on Stack.

I have a method in one class, which makes a remote request and updates some rows when called: https://github.com/nekojira/wp-currencies/blob/master/includes/rates.php#L64

In short:

// Rates class
public function update() {
   // do wp_remote_get(), do stuff in db, return response results
}

This method is only called by "update_currencies" method in a class in my plugin that deals with Cron: https://github.com/nekojira/wp-currencies/blob/master/includes/cron.php#L41

In short:

// Cron class
public function update_currencies() {
    if ( defined( 'DOING_CRON' ) ) {
        do_action( 'wp_currencies_before_update', time() );
        $rates = new Rates();
        $rates->update();
    }
} 

As you can see it's wrapped with DOING_CRON check... But it seems not to fire, ever. If I remove DOING_CRON it will fire at every second or so.

This last method update_currencies it's only used as callback of a wp cron action: https://github.com/nekojira/wp-currencies/blob/master/includes/cron.php#L29

In short:

// Cron class
public function __construct() {
    add_filter( 'cron_schedules', array( $this, 'cron_schedules' ), 10, 1 ) ;
    add_action( 'wp_currencies_update', array( $this, 'update_currencies' ) );
}

The action is a wp cron scheduled event, which changes only in two occasions: 1) upon plugin installation & activation: https://github.com/nekojira/wp-currencies/blob/master/includes/cron.php#L56 2) When options for my specific plugin are saved: https://github.com/nekojira/wp-currencies/blob/master/includes/settings.php#L211 https://github.com/nekojira/wp-currencies/blob/master/includes/settings.php#L40

In short, this is the action set by the plugin on init:

 add_action( 'update_option_wp_currencies_settings', array( $this, 'updated_option' ), 10, 2 );

and this is the callback that is fired when updating option 'update_option_wp_currencies_settings':

public function updated_option( $old_value, $new_value ) {
    // Is there an update?
    if ( $old_value != $new_value ) {
        wp_clear_scheduled_hook( 'wp_currencies_update' );
        // Makes sure there's an API key (won't be able to tell if valid, but at least is not empty).
        $api_key = isset( $new_value['api_key'] ) ? $new_value['api_key'] : ( isset( $old_value['api_key'] ) ? $old_value['api_key'] : '' );
        if ( ! empty( $api_key ) ) {
            wp_schedule_event( time(), esc_attr( $new_value['update_interval'] ), 'wp_currencies_update' );
            do_action( 'wp_currencies_rescheduled_update', time(), esc_attr( $new_value['update_interval'] ) );
        }
    }
}

The update intervals are set by the user via an option - but I double checked them and the most frequent interval is still 3600 seconds.

I have read both WordPress documentation and looked in Core itself and I am a bit lost as I can't find what's causing the issue... It's a free plugin I made I would like to find a solution.

  • Could you, please, post the relevant code in the question itself? It makes easier to have a full view of the context and it makes the question useful in the future even if the external service goes down. – cybmeta Jun 29 '15 at 16:24
  • Have you defined an alternate cron in your wp-config? – Brandt Solovij Jun 29 '15 at 16:26
  • @cybmeta I'll try to edit my question... I thought it was more readable this way – unfulvio Jun 29 '15 at 16:42
  • @BrandtSolovij no apparently that is not the culprit – unfulvio Jun 29 '15 at 16:43
  • edited with code snippets – unfulvio Jun 29 '15 at 17:02

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.