Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active April 9, 2023 18:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/dfb619c0fa645b5a043707b32212d2ba to your computer and use it in GitHub Desktop.
Save wpmudev-sls/dfb619c0fa645b5a043707b32212d2ba to your computer and use it in GitHub Desktop.
Custom Scheduled Events. A snippet to gather any custom WP cronjobs
<?php
/**
* Plugin Name: Custom Scheduled Events
* Plugin URI: https://premium.wpmudev.org/
* Description: A snippet to gather any custom WP cronjobs
* Task: SLS-56
* Author: Panos Lyrakis @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
return;
}
if ( ! class_exists( 'WPMUDEV_Custom_Scheduled_Events' ) ) {
class WPMUDEV_Custom_Scheduled_Events {
private static $_instance = null;
private $cron_tasks = null;
private $intervals = array();
/**
* Set up variables.
* In this section you can modify the variables according to your needs
*
* In `set_intervals()` you can set the "intervals" you need to have available
*
* In `set_cron_tasks` you can set the tasks that you need to be executed on each cron run
*/
public function set_intervals() {
/**
* The custom intervals. We set here the intervals that will be available to use in our cron events.
*
* @var array $intervals
*
* Array format:
* Event key (interval key) :
* > `interval` : time in seconds
* > `display` : a human readable name to recognize the interval
*/
$intervals = array(
'every_five_minutes' => array(
'interval' => MINUTE_IN_SECONDS * 5,
'display' => __( 'Every Five Minutes' ),
),
'every_ten_minutes' => array(
'interval' => MINUTE_IN_SECONDS * 10,
'display' => __( 'Every Ten Minutes' ),
),
);
$this->intervals = $intervals;
}
public function set_cron_tasks() {
/**
* The cron events.
*
* @var array $events
*
* Array format:
* Event key (action) :
* > `recurrence` : how often
* > `callback` : the callback function/method
* > `site_id` : when on multisite, you can define the site id this cron will be set
*/
$events = array(
'custom_event' => array(
'recurrence' => 'every_five_minutes',
'callback' => array( $this, 'callback_one' ),
'site_id' => 1,
),
'custom_event_two' => array(
'recurrence' => 'every_ten_minutes',
'callback' => array( $this, 'callback_two' ),
),
);
$this->cron_tasks = (object) $events;
}
/**
* Callbacks.
* In the following section you can list your callback methods
*/
public function callback_one() {
/**
* Run some code here
*/
}
public function callback_two() {
/**
* Run some code here
*/
}
/**
* Basic methods
* No need to edit those (ususally)
*/
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new WPMUDEV_Custom_Scheduled_Events();
}
return self::$_instance;
}
private function __construct() {
$this->set_intervals();
$this->set_cron_action();
add_filter( 'cron_schedules', array( $this, 'set_cron_schedules' ) );
add_action( 'wp', array( $this, 'cron_kickstarter' ), 20 );
}
public function set_cron_schedules( $schedules ) {
if ( ! empty( $this->intervals ) ) {
foreach ( $this->intervals as $key => $interval ) {
$schedules[ $key ] = array(
'interval' => $interval['interval'],
'display' => $interval['display'],
);
}
}
return $schedules;
}
public function cron_kickstarter() {
$cron_tasks = $this->get_cron_tasks();
foreach ( $cron_tasks as $cron_action => $_cron_task ) {
$cron_task = (object) $_cron_task;
if ( is_multisite() && property_exists( $cron_task, 'site_id' ) ) {
if ( get_current_blog_id() !== $cron_task->site_id ) {
continue;
}
}
if ( ! wp_next_scheduled( $cron_action ) ) {
wp_schedule_event( time(), $cron_task->recurrence, $cron_action );
}
}
}
private function set_cron_action() {
$cron_tasks = $this->get_cron_tasks();
foreach ( $cron_tasks as $cron_action => $_cron_task ) {
$cron_task = (object) $_cron_task;
add_action( $cron_action, $cron_task->callback );
}
}
public function get_cron_tasks() {
if ( empty( $this->cron_tasks ) ) {
$this->set_cron_tasks();
}
return $this->cron_tasks;
}
}
if ( ! function_exists( 'wpmudev_custom_scheduled_events' ) ) {
function wpmudev_custom_scheduled_events() {
return WPMUDEV_Custom_Scheduled_Events::instance();
};
add_action( 'plugins_loaded', 'wpmudev_custom_scheduled_events', 10 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment