WP_Job_Batch_Handler.php
301 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Plugin Framework |
| 4 | * |
| 5 | * This source file is subject to the GNU General Public License v3.0 |
| 6 | * that is bundled with this package in the file license.txt. |
| 7 | * It is also available through the world-wide-web at this URL: |
| 8 | * http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later |
| 9 | * If you did not receive a copy of the license and are unable to |
| 10 | * obtain it through the world-wide-web, please send an email |
| 11 | * to license@skyverge.com so we can send you a copy immediately. |
| 12 | * |
| 13 | * @since 3.0.0 |
| 14 | * @author WooCommerce / SkyVerge |
| 15 | * @copyright Copyright (c) 2021-2022 WooCommerce. |
| 16 | * @copyright Copyright (c) 2017-2019, SkyVerge, Inc. |
| 17 | * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later |
| 18 | * |
| 19 | * Modified by WooCommerce on 01 December 2021. |
| 20 | */ |
| 21 | |
| 22 | namespace WooCommerce\Square\Framework\Utilities; |
| 23 | use WooCommerce\Square\Framework\Square_Helper; |
| 24 | use WooCommerce\Square\Framework as SquareFramework; |
| 25 | |
| 26 | defined( 'ABSPATH' ) or exit; |
| 27 | |
| 28 | /** |
| 29 | * The job batch handler class. |
| 30 | * |
| 31 | * This provides a way for plugins to process "background" jobs in batches when |
| 32 | * regular background processing isn't available. |
| 33 | * |
| 34 | * @since 3.0.0 |
| 35 | */ |
| 36 | class WP_Job_Batch_Handler { |
| 37 | |
| 38 | |
| 39 | /** @var WP_Background_Job_Handler job handler instance */ |
| 40 | protected $job_handler; |
| 41 | |
| 42 | /** @var SquareFramework\Plugin $plugin WC plugin instance */ |
| 43 | protected $plugin; |
| 44 | |
| 45 | /** @var int default items per batch */ |
| 46 | protected $items_per_batch = 20; |
| 47 | |
| 48 | |
| 49 | /** |
| 50 | * Constructs the class. |
| 51 | * |
| 52 | * @since 3.0.0 |
| 53 | * |
| 54 | * @param SV_WP_Background_Job_Handler $job_handler job handler instance |
| 55 | * @param SquareFramework\Plugin $plugin WC plugin instance |
| 56 | */ |
| 57 | public function __construct( $job_handler, SquareFramework\Plugin $plugin ) { |
| 58 | |
| 59 | if ( ! is_admin() ) { |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | $this->job_handler = $job_handler; |
| 64 | $this->plugin = $plugin; |
| 65 | |
| 66 | $this->add_hooks(); |
| 67 | |
| 68 | $this->render_js(); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | /** |
| 73 | * Adds the necessary action and filter hooks. |
| 74 | * |
| 75 | * @since 3.0.0 |
| 76 | */ |
| 77 | protected function add_hooks() { |
| 78 | |
| 79 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 80 | |
| 81 | add_action( 'wp_ajax_' . $this->get_job_handler()->get_identifier() . '_process_batch', array( $this, 'ajax_process_batch' ) ); |
| 82 | add_action( 'wp_ajax_' . $this->get_job_handler()->get_identifier() . '_cancel_job', array( $this, 'ajax_cancel_job' ) ); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | /** |
| 87 | * Enqueues the scripts. |
| 88 | * |
| 89 | * @since 3.0.0 |
| 90 | */ |
| 91 | public function enqueue_scripts() { |
| 92 | |
| 93 | wp_enqueue_script( $this->get_job_handler()->get_identifier() . '_batch_handler', $this->get_plugin()->get_framework_assets_url() . '/js/admin/sv-wp-admin-job-batch-handler.min.js', array( 'jquery' ), $this->get_plugin()->get_version() ); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | /** |
| 98 | * Renders the inline JavaScript for instantiating the batch handler class. |
| 99 | * |
| 100 | * @since 3.0.0 |
| 101 | */ |
| 102 | protected function render_js() { |
| 103 | |
| 104 | /** |
| 105 | * Filters the JavaScript batch handler arguments. |
| 106 | * |
| 107 | * @since 3.0.0 |
| 108 | * |
| 109 | * @param array $args arguments to pass to the JavaScript batch handler |
| 110 | * @param SV_WP_Job_Batch_Handler $handler handler object |
| 111 | */ |
| 112 | $args = apply_filters( $this->get_job_handler()->get_identifier() . '_batch_handler_js_args', $this->get_js_args(), $this ); |
| 113 | |
| 114 | wc_enqueue_js( sprintf( 'window.%1$s_batch_handler = new %2$s( %3$s );', |
| 115 | esc_js( $this->get_job_handler()->get_identifier() ), |
| 116 | esc_js( $this->get_js_class() ), |
| 117 | wp_json_encode( $args ) |
| 118 | ) ); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | /** |
| 123 | * Gets the JavaScript batch handler arguments. |
| 124 | * |
| 125 | * @since 3.0.0 |
| 126 | * |
| 127 | * @return array |
| 128 | */ |
| 129 | protected function get_js_args() { |
| 130 | |
| 131 | return array( |
| 132 | 'id' => $this->get_job_handler()->get_identifier(), |
| 133 | 'process_nonce' => wp_create_nonce( $this->get_job_handler()->get_identifier() . '_process_batch' ), |
| 134 | 'cancel_nonce' => wp_create_nonce( $this->get_job_handler()->get_identifier() . '_cancel_job' ), |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | /** |
| 140 | * Gets the JavaScript batch handler class name. |
| 141 | * |
| 142 | * Plugins can override this with their own handler that extends the base. |
| 143 | * |
| 144 | * @since 3.0.0 |
| 145 | * |
| 146 | * @return string |
| 147 | */ |
| 148 | protected function get_js_class() { |
| 149 | |
| 150 | return 'Square_WP_Job_Batch_Handler'; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | /** |
| 155 | * Processes a job batch via AJAX. |
| 156 | * |
| 157 | * @internal |
| 158 | * |
| 159 | * @since 3.0.0 |
| 160 | * |
| 161 | * @throws \Exception upon error. |
| 162 | */ |
| 163 | public function ajax_process_batch() { |
| 164 | |
| 165 | check_ajax_referer( $this->get_job_handler()->get_identifier() . '_process_batch', 'security' ); |
| 166 | |
| 167 | if ( empty( $_POST['job_id'] ) ) { |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | try { |
| 172 | |
| 173 | $job = $this->process_batch( $_POST['job_id'] ); |
| 174 | |
| 175 | $job = $this->process_job_status( $job ); |
| 176 | |
| 177 | wp_send_json_success( (array) $job ); |
| 178 | |
| 179 | } catch( \Exception $e ) { |
| 180 | |
| 181 | $data = ( ! empty( $job ) ) ? (array) $job : array(); |
| 182 | |
| 183 | $data['message'] = $e->getMessage(); |
| 184 | |
| 185 | wp_send_json_error( $data ); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | |
| 190 | /** |
| 191 | * Cancels a job via AJAX. |
| 192 | * |
| 193 | * @internal |
| 194 | * |
| 195 | * @since 3.0.0 |
| 196 | */ |
| 197 | public function ajax_cancel_job() { |
| 198 | |
| 199 | check_ajax_referer( $this->get_job_handler()->get_identifier() . '_cancel_job', 'security' ); |
| 200 | |
| 201 | if ( empty( $_POST['job_id'] ) ) { |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | $this->get_job_handler()->delete_job( $_POST['job_id'] ); |
| 206 | |
| 207 | wp_send_json_success(); |
| 208 | } |
| 209 | |
| 210 | |
| 211 | /** |
| 212 | * Handles a job after processing one of its batches. |
| 213 | * |
| 214 | * Allows plugins to add extra job properties and handle certain statuses. |
| 215 | * Implementations may throw a \Exception. |
| 216 | * |
| 217 | * @since 3.0.0 |
| 218 | * |
| 219 | * @param \stdClass|object $job job object |
| 220 | * @return \stdClass|object $job job object |
| 221 | */ |
| 222 | protected function process_job_status( $job ) { |
| 223 | |
| 224 | $job->percentage = Square_Helper::number_format( (int) $job->progress / (int) $job->total * 100 ); |
| 225 | |
| 226 | return $job; |
| 227 | } |
| 228 | |
| 229 | |
| 230 | /** |
| 231 | * Processes a batch of items for the given job. |
| 232 | * |
| 233 | * A batch consists of the number of items defined by self::get_items_per_batch() |
| 234 | * or the number we're able to process before exceeding time or memory limits. |
| 235 | * |
| 236 | * @since 3.0.0 |
| 237 | * |
| 238 | * @param string $job_id job to process |
| 239 | * @return \stdClass|object $job job after processing the batch |
| 240 | * @throws \Exception |
| 241 | */ |
| 242 | public function process_batch( $job_id ) { |
| 243 | |
| 244 | $job = $this->get_job_handler()->get_job( $job_id ); |
| 245 | |
| 246 | if ( ! $job ) { |
| 247 | throw new \Exception( 'Invalid job ID' ); |
| 248 | } |
| 249 | |
| 250 | return $this->get_job_handler()->process_job( $job, $this->get_items_per_batch() ); |
| 251 | } |
| 252 | |
| 253 | |
| 254 | /** |
| 255 | * Gets the number of items to process in a single request when processing job item batches. |
| 256 | * |
| 257 | * @since 3.0.0 |
| 258 | * |
| 259 | * @return int |
| 260 | */ |
| 261 | protected function get_items_per_batch() { |
| 262 | |
| 263 | /** |
| 264 | * Filters the number of items to process in a single request when processing job item batches. |
| 265 | * |
| 266 | * @since 3.0.0 |
| 267 | * |
| 268 | * @param int $items_per_batch |
| 269 | */ |
| 270 | $items_per_batch = absint( apply_filters( $this->get_job_handler()->get_identifier() . '_batch_handler_items_per_batch', $this->items_per_batch ) ); |
| 271 | |
| 272 | return $items_per_batch > 0 ? $items_per_batch : 1; |
| 273 | } |
| 274 | |
| 275 | |
| 276 | /** |
| 277 | * Gets the job handler. |
| 278 | * |
| 279 | * @since 3.0.0 |
| 280 | * |
| 281 | * @return SV_WP_Background_Job_Handler |
| 282 | */ |
| 283 | protected function get_job_handler() { |
| 284 | |
| 285 | return $this->job_handler; |
| 286 | } |
| 287 | |
| 288 | |
| 289 | /** |
| 290 | * Gets the plugin instance. |
| 291 | * |
| 292 | * @since 3.0.0 |
| 293 | * |
| 294 | * @return SquareFramework\Plugin |
| 295 | */ |
| 296 | protected function get_plugin() { |
| 297 | |
| 298 | return $this->plugin; |
| 299 | } |
| 300 | } |
| 301 |