PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 3.8.2
WP STAGING – WordPress Backup, Restore, Migration & Clone v3.8.2
4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Framework / BackgroundProcessing / WithQueueAwareness.php
wp-staging / Framework / BackgroundProcessing Last commit date
Exceptions 5 years ago Action.php 2 years ago BackgroundProcessingServiceProvider.php 2 years ago Demo.php 4 years ago FeatureDetection.php 2 years ago Queue.php 2 years ago QueueActionAware.php 4 years ago QueueProcessor.php 2 years ago WithQueueAwareness.php 3 years ago
WithQueueAwareness.php
128 lines
1 <?php
2
3 /**
4 * Provides methods to be aware of the queue system and its inner workings.
5 *
6 * @package WPStaging\Framework\BackgroundProcessing
7 */
8
9 namespace WPStaging\Framework\BackgroundProcessing;
10
11 use WP_Error;
12
13 use function WPStaging\functions\debug_log;
14
15 /**
16 * Trait WithQueueAwareness
17 *
18 * @package WPStaging\Framework\BackgroundProcessing
19 */
20 trait WithQueueAwareness
21 {
22
23 /**
24 * Whether this Queue instance did fire the AJAX action request or not.
25 *
26 * @var bool
27 */
28 private $didFireAjaxAction = false;
29
30 /**
31 * Returns the Queue default priority that will be used to schedule actions when the
32 * priority is not specified or is specified as an invalid value.
33 *
34 * @return int The Queue default priority.
35 */
36 public static function getDefaultPriority()
37 {
38 return 0;
39 }
40
41
42 /**
43 * Fires a non-blocking request to the WordPress admin AJAX endpoint that will,
44 * in turn, trigger the processing of more Actions.
45 *
46 * @param mixed|null $bodyData An optional set of data to customize the processing request
47 * for. If not provided, then the request will be fired for the
48 * next available Actions (normal operations).
49 *
50 * @return bool A value that will indicate whether the request was correctly dispatched
51 * or not.
52 */
53 public function fireAjaxAction($bodyData = null)
54 {
55 if ($this->didFireAjaxAction) {
56 // Let's not fire the AJAX request more than once per HTTP request, per Queue.
57 return false;
58 }
59
60 $ajaxUrl = add_query_arg([
61 'action' => QueueProcessor::QUEUE_PROCESS_ACTION,
62 '_ajax_nonce' => wp_create_nonce(QueueProcessor::QUEUE_PROCESS_ACTION)
63 ], admin_url('admin-ajax.php'));
64
65 $response = wp_remote_post(esc_url_raw($ajaxUrl), [
66 'headers' => [
67 'X-WPSTG-Request' => QueueProcessor::QUEUE_PROCESS_ACTION,
68 ],
69 'blocking' => false,
70 'timeout' => 0.01,
71 'cookies' => !empty($_COOKIE) ? $_COOKIE : [],
72 'sslverify' => apply_filters('https_local_ssl_verify', false),
73 'body' => $this->normalizeAjaxRequestBody($bodyData),
74 ]);
75
76 //debug_log('fireAjaxAction: ' . wp_json_encode($response, JSON_PRETTY_PRINT));
77
78 /*
79 * A non-blocking request will either return a WP_Error instance, or
80 * a mock response. The response is a mock as we cannot really build
81 * a good response without waiting for it to be processed from the server.
82 */
83 if ($response instanceof WP_Error) {
84 \WPStaging\functions\debug_log(json_encode([
85 'root' => 'Queue processing admin-ajax request failed.',
86 'class' => get_class($this),
87 'code' => $response->get_error_code(),
88 'message' => $response->get_error_message(),
89 'data' => $response->get_error_data()
90 ], JSON_PRETTY_PRINT));
91
92 return false;
93 }
94
95 $this->didFireAjaxAction = true;
96
97 /**
98 * Fires an Action to indicate the Queue did fire the AJAX request that will
99 * trigger side-processing in another PHP process.
100 *
101 * @param Queue $this A reference to the instance of the Queue that actually fired
102 * the AJAX request.
103 */
104 do_action('wpstg_queue_fire_ajax_request', $this);
105
106 return true;
107 }
108
109 /**
110 * Normalizes the data to be sent along the non-blocking AJAX request
111 * that will trigger the Queue processing of an Action.
112 *
113 * @param mixed|null $bodyData The data to normlize to a format suitable for
114 * the remote request.
115 *
116 * @return array The normalized body data to be sent along the non-blocking
117 * AJAX request.
118 */
119 private function normalizeAjaxRequestBody($bodyData)
120 {
121 $normalized = (array)$bodyData;
122
123 $normalized['_referer'] = __CLASS__;
124
125 return $normalized;
126 }
127 }
128