|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace JustCoded\WP\Framework\Objects; |
| 4 | + |
| 5 | +/** |
| 6 | + * Class Cronjob |
| 7 | + * |
| 8 | + * @package JustCoded\WP\Framework\Objects |
| 9 | + * |
| 10 | + * @method Cronjob instance() static |
| 11 | + */ |
| 12 | +abstract class Cronjob { |
| 13 | + use Singleton; |
| 14 | + |
| 15 | + /** |
| 16 | + * Constant for single cron |
| 17 | + */ |
| 18 | + const FREQUENCY_ONCE = 'single'; |
| 19 | + |
| 20 | + /** |
| 21 | + * Constant for hourly cron |
| 22 | + */ |
| 23 | + const FREQUENCY_HOURLY = 'hourly'; |
| 24 | + |
| 25 | + /** |
| 26 | + * Constant for twicedaily cron |
| 27 | + */ |
| 28 | + const FREQUENCY_TWICEDAILY = 'twicedaily'; |
| 29 | + |
| 30 | + /** |
| 31 | + * Constant for daily cron |
| 32 | + */ |
| 33 | + const FREQUENCY_DAILY = 'daily'; |
| 34 | + |
| 35 | + /** |
| 36 | + * Cron id |
| 37 | + * |
| 38 | + * @var string |
| 39 | + */ |
| 40 | + protected $ID; |
| 41 | + |
| 42 | + /** |
| 43 | + * Start time. |
| 44 | + * |
| 45 | + * @var int|string |
| 46 | + */ |
| 47 | + protected $start; |
| 48 | + |
| 49 | + /** |
| 50 | + * Frequency name. |
| 51 | + * |
| 52 | + * @var string |
| 53 | + */ |
| 54 | + protected $frequency; |
| 55 | + |
| 56 | + /** |
| 57 | + * Interval in seconds. |
| 58 | + * |
| 59 | + * @var int |
| 60 | + */ |
| 61 | + protected $interval; |
| 62 | + |
| 63 | + /** |
| 64 | + * Cronjob constructor. |
| 65 | + * |
| 66 | + * @throws \Exception |
| 67 | + */ |
| 68 | + protected function __construct() { |
| 69 | + if ( empty( $this->ID ) ) { |
| 70 | + throw new \Exception( static::class . ' class: $ID property is required' ); |
| 71 | + } |
| 72 | + |
| 73 | + // register action hook. |
| 74 | + add_action( $this->ID, [ $this, 'handle' ] ); |
| 75 | + |
| 76 | + $this->cron_debug(); |
| 77 | + |
| 78 | + if ( static::FREQUENCY_ONCE !== $this->frequency ) { |
| 79 | + // deactivation hook for repeatable event. |
| 80 | + add_action( 'switch_theme', [ $this, 'deactivate' ] ); |
| 81 | + } |
| 82 | + |
| 83 | + if ( ! in_array( $this->frequency, $this->get_standard_frequencies(), true ) ) { |
| 84 | + if ( empty( $this->interval ) || ! is_numeric( $this->interval ) ) { |
| 85 | + throw new \Exception( static::class . ' class: $interval property is required and must be numeric if you use a custom schedule' ); |
| 86 | + } |
| 87 | + |
| 88 | + // register custom schedule. |
| 89 | + add_filter( 'cron_schedules', [ $this, 'register_custom_schedule' ], 99, 1 ); |
| 90 | + } |
| 91 | + |
| 92 | + // register cron. |
| 93 | + add_action( 'init', [ $this, 'register' ] ); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Register custom schedule. |
| 98 | + * |
| 99 | + * @param array $schedules Non-default schedule. |
| 100 | + * |
| 101 | + * @return array |
| 102 | + */ |
| 103 | + public function register_custom_schedule( $schedules ) { |
| 104 | + $schedules[ $this->frequency ] = [ |
| 105 | + 'interval' => $this->interval, |
| 106 | + 'display' => $this->frequency, |
| 107 | + ]; |
| 108 | + |
| 109 | + return $schedules; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Registers cron with interval |
| 114 | + */ |
| 115 | + public function register() { |
| 116 | + if ( empty( $this->start ) ) { |
| 117 | + $this->start = time(); |
| 118 | + } elseif ( ! is_numeric( $this->start ) && is_string( $this->start ) ) { |
| 119 | + $this->start = strtotime( $this->start ); |
| 120 | + } |
| 121 | + |
| 122 | + if ( static::FREQUENCY_ONCE === $this->frequency ) { |
| 123 | + wp_schedule_single_event( $this->start, $this->ID ); |
| 124 | + } elseif ( ! wp_next_scheduled( $this->ID ) ) { |
| 125 | + wp_schedule_event( $this->start, $this->frequency, $this->ID ); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Deactivate cron on theme deactivate. |
| 131 | + */ |
| 132 | + public function deactivate() { |
| 133 | + if ( $start = wp_next_scheduled( $this->ID ) ) { |
| 134 | + wp_unschedule_event( $start, $this->ID ); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Get all frequency |
| 140 | + * |
| 141 | + * @return array |
| 142 | + */ |
| 143 | + protected function get_standard_frequencies() { |
| 144 | + return [ |
| 145 | + self::FREQUENCY_HOURLY, |
| 146 | + self::FREQUENCY_TWICEDAILY, |
| 147 | + self::FREQUENCY_DAILY, |
| 148 | + ]; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Debug |
| 153 | + * |
| 154 | + * @return bool |
| 155 | + */ |
| 156 | + protected function cron_debug() { |
| 157 | + if ( ! defined( 'WP_DEBUG' ) || false === WP_DEBUG ) { |
| 158 | + return false; |
| 159 | + } |
| 160 | + |
| 161 | + if ( ! isset( $_GET['do_cron'] ) || $_GET['do_cron'] !== $this->ID ) { |
| 162 | + return false; |
| 163 | + } |
| 164 | + |
| 165 | + remove_action( $this->ID, [ $this, 'handle' ] ); |
| 166 | + add_action( 'init', function () { |
| 167 | + $this->handle(); |
| 168 | + wp_die( 'Finished cronjob <code>' . esc_attr__( $this->ID ) . '</code>' ); |
| 169 | + }, 99 ); |
| 170 | + |
| 171 | + return true; |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Handle function |
| 176 | + */ |
| 177 | + abstract public function handle(); |
| 178 | +} |
0 commit comments