| 1 |
<?php |
| 2 |
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' ); |
| 3 |
|
| 4 |
/** |
| 5 |
* Class handling background processes. |
| 6 |
* |
| 7 |
* @since 1.8.1 |
| 8 |
* @author Grégory Viguier |
| 9 |
*/ |
| 10 |
abstract class Imagify_Abstract_Background_Process extends Imagify_WP_Background_Process { |
| 11 |
|
| 12 |
/** |
| 13 |
* Class version. |
| 14 |
* |
| 15 |
* @var string |
| 16 |
* @since 1.8.1 |
| 17 |
* @author Grégory Viguier |
| 18 |
*/ |
| 19 |
const VERSION = '1.1'; |
| 20 |
|
| 21 |
/** |
| 22 |
* Prefix used to build the global process identifier. |
| 23 |
* |
| 24 |
* @var string |
| 25 |
* @since 1.8.1 |
| 26 |
* @access protected |
| 27 |
* @author Grégory Viguier |
| 28 |
*/ |
| 29 |
protected $prefix = 'imagify'; |
| 30 |
|
| 31 |
/** |
| 32 |
* Set to true to automatically displatch at the end of the page. |
| 33 |
* |
| 34 |
* @var bool |
| 35 |
* @since 1.9 |
| 36 |
* @access protected |
| 37 |
* @see $this->save() |
| 38 |
* @see $this->maybe_save_and_dispatch() |
| 39 |
* @author Grégory Viguier |
| 40 |
*/ |
| 41 |
protected $auto_dispatch = false; |
| 42 |
|
| 43 |
/** |
| 44 |
* The single instance of the class. |
| 45 |
* |
| 46 |
* @var object |
| 47 |
* @since 1.8.1 |
| 48 |
* @access protected |
| 49 |
* @author Grégory Viguier |
| 50 |
*/ |
| 51 |
protected static $_instance; |
| 52 |
|
| 53 |
|
| 54 |
/** |
| 55 |
* Get the main Instance. |
| 56 |
* |
| 57 |
* @since 1.8.1 |
| 58 |
* @access public |
| 59 |
* @author Grégory Viguier |
| 60 |
* |
| 61 |
* @return object Main instance. |
| 62 |
*/ |
| 63 |
public static function get_instance() { |
| 64 |
if ( ! isset( static::$_instance ) ) { |
| 65 |
static::$_instance = new static(); |
| 66 |
} |
| 67 |
|
| 68 |
return static::$_instance; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Init: launch a hook that will clear the scheduled events and empty the queue when the plugin is disabled. |
| 73 |
* This is only a precaution in case something went wrong. |
| 74 |
* |
| 75 |
* @since 1.8.1 |
| 76 |
* @access public |
| 77 |
* @author Grégory Viguier |
| 78 |
*/ |
| 79 |
public function init() { |
| 80 |
$this->query_url = admin_url( 'admin-ajax.php' ); |
| 81 |
|
| 82 |
/** |
| 83 |
* Filter the URL to use for background processes. |
| 84 |
* |
| 85 |
* @since 1.9.5 |
| 86 |
* @author Grégory Viguier |
| 87 |
* |
| 88 |
* @param string $query_url An URL. |
| 89 |
* @param object $this This class instance. |
| 90 |
*/ |
| 91 |
$this->query_url = apply_filters( 'imagify_background_process_url', $this->query_url, $this ); |
| 92 |
|
| 93 |
if ( ! $this->query_url || ! is_string( $this->query_url ) || ! preg_match( '@^https?://@', $this->query_url ) ) { |
| 94 |
$this->query_url = admin_url( 'admin-ajax.php' ); |
| 95 |
} |
| 96 |
|
| 97 |
// Deactivation hook. |
| 98 |
if ( did_action( static::get_deactivation_hook_name() ) ) { |
| 99 |
$this->cancel_process(); |
| 100 |
} else { |
| 101 |
add_action( static::get_deactivation_hook_name(), [ $this, 'cancel_process' ] ); |
| 102 |
} |
| 103 |
|
| 104 |
// Automatically save and dispatch at the end of the page if the queue is not empty. |
| 105 |
add_action( 'shutdown', [ $this, 'maybe_save_and_dispatch' ], 666 ); // Evil magic number. |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
/** ----------------------------------------------------------------------------------------- */ |
| 110 |
/** OVERRIDES =============================================================================== */ |
| 111 |
/** ----------------------------------------------------------------------------------------- */ |
| 112 |
|
| 113 |
/** |
| 114 |
* Cancel Process. |
| 115 |
* Stop processing queue items, clear cronjob and delete batch. |
| 116 |
* This is a copy of the parent's method, in case an older version of WP_Background_Process is loaded instead of this one (an old version without this method). |
| 117 |
* |
| 118 |
* @since 1.8.1 |
| 119 |
* @access public |
| 120 |
* @author Grégory Viguier |
| 121 |
*/ |
| 122 |
public function cancel_process() { |
| 123 |
if ( method_exists( $this, 'cancel_process' ) ) { |
| 124 |
parent::cancel_process(); |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
if ( ! $this->is_queue_empty() ) { |
| 129 |
$batch = $this->get_batch(); |
| 130 |
|
| 131 |
$this->delete( $batch->key ); |
| 132 |
|
| 133 |
wp_clear_scheduled_hook( $this->get_event_name() ); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Save the queen. No, I meant the queue. |
| 139 |
* Also empty the queue to avoid to create several batches with the same items. |
| 140 |
* |
| 141 |
* @since 1.9 |
| 142 |
* @access public |
| 143 |
* @author Grégory Viguier |
| 144 |
* |
| 145 |
* @return $this |
| 146 |
*/ |
| 147 |
public function save() { |
| 148 |
if ( empty( $this->data ) ) { |
| 149 |
return $this; |
| 150 |
} |
| 151 |
|
| 152 |
parent::save(); |
| 153 |
|
| 154 |
$this->auto_dispatch = true; |
| 155 |
$this->data = []; |
| 156 |
|
| 157 |
return $this; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
/** ----------------------------------------------------------------------------------------- */ |
| 162 |
/** TOOLS =================================================================================== */ |
| 163 |
/** ----------------------------------------------------------------------------------------- */ |
| 164 |
|
| 165 |
/** |
| 166 |
* Save and dispatch if the queue is not empty. |
| 167 |
* |
| 168 |
* @since 1.9 |
| 169 |
* @access public |
| 170 |
* @author Grégory Viguier |
| 171 |
*/ |
| 172 |
public function maybe_save_and_dispatch() { |
| 173 |
$this->save(); |
| 174 |
|
| 175 |
if ( $this->auto_dispatch ) { |
| 176 |
$this->dispatch(); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Get the cron name. |
| 182 |
* |
| 183 |
* @since 1.8.1 |
| 184 |
* @access public |
| 185 |
* @author Grégory Viguier |
| 186 |
* |
| 187 |
* @return string |
| 188 |
*/ |
| 189 |
public function get_event_name() { |
| 190 |
return $this->cron_hook_identifier; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Get the deactivation hook name. |
| 195 |
* |
| 196 |
* @since 1.8.1 |
| 197 |
* @access public |
| 198 |
* @author Grégory Viguier |
| 199 |
* |
| 200 |
* @return string |
| 201 |
*/ |
| 202 |
public static function get_deactivation_hook_name() { |
| 203 |
static $deactivation_hook; |
| 204 |
|
| 205 |
if ( ! isset( $deactivation_hook ) ) { |
| 206 |
$deactivation_hook = 'deactivate_' . plugin_basename( IMAGIFY_FILE ); |
| 207 |
} |
| 208 |
|
| 209 |
return $deactivation_hook; |
| 210 |
} |
| 211 |
} |
| 212 |
|