PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.3.0
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.3.0
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / inc / classes / class-imagify-abstract-background-process.php

class-imagify-abstract-background-process.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 2.3.0, at inc/classes/class-imagify-abstract-background-process.php

166 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use Imagify\Traits\InstanceGetterTrait;
4
5 /**
6 * Class handling background processes.
7 *
8 * @since 1.8.1
9 */
10 abstract class Imagify_Abstract_Background_Process extends Imagify_WP_Background_Process {
11 use InstanceGetterTrait;
12
13 /**
14 * Prefix used to build the global process identifier.
15 *
16 * @var string
17 * @since 1.8.1
18 */
19 protected $prefix = 'imagify';
20
21 /**
22 * URL to query on.
23 *
24 * @var string
25 */
26 protected $query_url = '';
27
28 /**
29 * Set to true to automatically displatch at the end of the page.
30 *
31 * @var bool
32 * @since 1.9
33 * @see $this->save()
34 * @see $this->maybe_save_and_dispatch()
35 */
36 protected $auto_dispatch = false;
37
38 /**
39 * Init: launch a hook that will clear the scheduled events and empty the queue when the plugin is disabled.
40 * This is only a precaution in case something went wrong.
41 *
42 * @since 1.8.1
43 */
44 public function init() {
45 $this->query_url = admin_url( 'admin-ajax.php' );
46
47 /**
48 * Filter the URL to use for background processes.
49 *
50 * @since 1.9.5
51 *
52 * @param string $query_url An URL.
53 * @param object $this This class instance.
54 */
55 $this->query_url = apply_filters( 'imagify_background_process_url', $this->query_url, $this );
56
57 if ( ! $this->query_url || ! is_string( $this->query_url ) || ! preg_match( '@^https?://@', $this->query_url ) ) {
58 $this->query_url = admin_url( 'admin-ajax.php' );
59 }
60
61 // Deactivation hook.
62 if ( did_action( static::get_deactivation_hook_name() ) ) {
63 $this->cancel_process();
64 } else {
65 add_action( static::get_deactivation_hook_name(), [ $this, 'cancel_process' ] );
66 }
67
68 // Automatically save and dispatch at the end of the page if the queue is not empty.
69 add_action( 'shutdown', [ $this, 'maybe_save_and_dispatch' ], 666 ); // Evil magic number.
70 }
71
72
73 /** ----------------------------------------------------------------------------------------- */
74 /** OVERRIDES =============================================================================== */
75 /** ----------------------------------------------------------------------------------------- */
76
77 /**
78 * Cancel Process.
79 * Stop processing queue items, clear cronjob and delete batch.
80 * 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).
81 *
82 * @since 1.8.1
83 */
84 public function cancel_process() {
85 if ( method_exists( $this, 'cancel_process' ) ) {
86 parent::cancel_process();
87 return;
88 }
89
90 if ( ! $this->is_queue_empty() ) {
91 $batch = $this->get_batch();
92
93 $this->delete( $batch->key );
94
95 wp_clear_scheduled_hook( $this->get_event_name() );
96 }
97 }
98
99 /**
100 * Save the queen. No, I meant the queue.
101 * Also empty the queue to avoid to create several batches with the same items.
102 *
103 * @since 1.9
104 *
105 * @return $this
106 */
107 public function save() {
108 if ( empty( $this->data ) ) {
109 return $this;
110 }
111
112 parent::save();
113
114 $this->auto_dispatch = true;
115 $this->data = [];
116
117 return $this;
118 }
119
120
121 /** ----------------------------------------------------------------------------------------- */
122 /** TOOLS =================================================================================== */
123 /** ----------------------------------------------------------------------------------------- */
124
125 /**
126 * Save and dispatch if the queue is not empty.
127 *
128 * @since 1.9
129 */
130 public function maybe_save_and_dispatch() {
131 $this->save();
132
133 if ( $this->auto_dispatch ) {
134 $this->dispatch();
135 }
136 }
137
138 /**
139 * Get the cron name.
140 *
141 * @since 1.8.1
142 *
143 * @return string
144 */
145 public function get_event_name() {
146 return $this->cron_hook_identifier;
147 }
148
149 /**
150 * Get the deactivation hook name.
151 *
152 * @since 1.8.1
153 *
154 * @return string
155 */
156 public static function get_deactivation_hook_name() {
157 static $deactivation_hook;
158
159 if ( ! isset( $deactivation_hook ) ) {
160 $deactivation_hook = 'deactivate_' . plugin_basename( IMAGIFY_FILE );
161 }
162
163 return $deactivation_hook;
164 }
165 }
166