1
votes

I tried to declare a new time interval for a WP_CRON. When I do it in a PHP file it works. When I do it in a PHP Class it doesn't.

Can someone see what I'm doing wrong ?

I'm using the plugin cron view to check if the declaration is working or not.

If I solve this problem I think it will also solve my problem to know why my cron job is not triggered in the class but works properly when not in a class.

=> File myplugin.php

function set_up_option_page() {
    add_options_page( [...]);
}
add_action( 'admin_menu', 'set_up_option_page' );

function do_some_rock() {

    $instance = My_Plugin_Class::instance();

    if ( isset($_POST['action']) && 'do-magic' == $_POST['action'] ) {
        $instance->do_stuff();
    }else{
        // Display the form.
    <?
    }
}

=> File My_Plugin_Class.php

<?php

if ( ! defined( 'ABSPATH' ) ) exit;

class My_Plugin_Class {

    private static $_instance = null;

    public function __construct () {

[...] 

        add_filter( 'cron_schedules', array($this,'cron_time_intervals'));

    } 

    public function cron_time_intervals( $schedules ) {
        echo "——— cron time intervals —— ";
        $schedules['minutes_1'] = array(
            'interval' => 10*60,
            'display'   => 'Once 10 minutes'
        );

        return $schedules;
    }

    public static function instance () {
        if ( is_null( self::$_instance ) ) {
            self::$_instance = new self();
        }
        return self::$_instance;
    } // End instance ()

Best regards.

3
Do you instantiate your class every time when user visits your page? - Lukas Pawlik
Hello @lukas I'm pretty sure this the case. Well I first check is there is an existing instance of the class. $instance = My_Class_Name::instance( FILE, '1.0.0' ); $instance->do_things(); - Kaizoku Gambare
Can you paste the whole class? - Lukas Pawlik
I see it now. You need to call add_filter( 'cron_shedules', .... ); every request - is that do_some_rock() function always called? - Lukas Pawlik
do_some_rock() is the call back of add_options_page( ); I'm not sure it's call all the time, I guess not. So where it make more sense to put my cron code ? and what is the best acording to the wordpress standard ? In the My_Plugin_Class, in a My_Plugin_Cron_class, or myplugin.php file (not a class) ? Regards, - Kaizoku Gambare

3 Answers

2
votes

I am almost sure that do_some_rock() is not called every time - only when someone goes to that page. You can move add_filter( 'cron_schedules', array($this,'cron_time_intervals')); from your class constructor to main plugin file myplugin.php and do more or less something like this

add_action( 'admin_menu', 'set_up_option_page' );
$instance = My_Plugin_Class::instance();
add_filter( 'cron_schedules', array($instance ,'cron_time_intervals'));

One extra question. Does My_Plugin_Class constructor contain any code you would like to prevent being executed during each request?

2
votes

I think the proper way to do this, is to have a specific class for my Cron. This class is instanciated on each request this is why the code shouldn't be in a class with a static instance as I did before. Also I think it's better to have the Cron outside the plugin class for logic and cleaner code purpose. Thanks to Lukas Pawlik for the help.

 if (!defined('ABSPATH')) exit;

    new My_Cron();

    class My_Cron {

        public function __construct() {
            add_filter('cron_schedules', array($this, 'cron_time_intervals'));
            add_action( 'wp',  array($this, 'cron_scheduler'));
            add_action( 'cast_my_spell', array( $this, 'auto_spell_cast' ) );
        }

        public function cron_time_intervals($schedules)
        {
            $schedules['minutes_10'] = array(
                'interval' => 10 * 60,
                'display' => 'Once 10 minutes'
            );
            return $schedules;
        }

        function cron_scheduler() {
            if ( ! wp_next_scheduled( 'cast_my_spell' ) ) {
                wp_schedule_event( time(), 'minutes_10', 'cast_my_spell');
            }
        }

        function auto_spell_cast(){
            My_Plugin_Class::instance()->launch_spell();
        }
    }
1
votes

I am not Sure But Try this.

   add_filter( 'cron_schedules', array($this,'cron_time_intervals'),1);

And Check cron_time_intervals() Function is Call or Not.