← All changes
|
vendor/woocommerce/action-scheduler/classes/ActionScheduler_ActionFactory.php
+24
-3
1.0.2
→
trunk
View file →
| @@ -245,9 +245,9 @@ | ||
| 245 | 245 | * |
| 246 | 246 | * This general purpose method can be used in place of specific methods such as async(), |
| 247 | 247 | * async_unique(), single() or single_unique(), etc. |
| 248 | 248 | * |
| 249 | - * @internal Not intended for public use, should not be overriden by subclasses. | |
| 249 | + * @internal Not intended for public use, should not be overridden by subclasses. | |
| 250 | 250 | * |
| 251 | 251 | * @param array $options { |
| 252 | 252 | * Describes the action we wish to schedule. |
| 253 | 253 | * |
| @@ -306,8 +306,9 @@ | ||
| 306 | 306 | $schedule = new ActionScheduler_SimpleSchedule( $date ); |
| 307 | 307 | break; |
| 308 | 308 | |
| 309 | 309 | default: |
| 310 | + // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log | |
| 310 | 311 | error_log( "Unknown action type '{$options['type']}' specified when trying to create an action for '{$options['hook']}'." ); |
| 311 | 312 | return 0; |
| 312 | 313 | } |
| 313 | 314 | |
| @@ -317,8 +318,9 @@ | ||
| 317 | 318 | $action_id = 0; |
| 318 | 319 | try { |
| 319 | 320 | $action_id = $options['unique'] ? $this->store_unique_action( $action ) : $this->store( $action ); |
| 320 | 321 | } catch ( Exception $e ) { |
| 322 | + // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log | |
| 321 | 323 | error_log( |
| 322 | 324 | sprintf( |
| 323 | 325 | /* translators: %1$s is the name of the hook to be enqueued, %2$s is the exception message. */ |
| 324 | 326 | __( 'Caught exception while enqueuing action "%1$s": %2$s', 'action-scheduler' ), |
| @@ -350,8 +352,27 @@ | ||
| 350 | 352 | * @return int ID of the created action. Will be 0 if action was not created. |
| 351 | 353 | */ |
| 352 | 354 | protected function store_unique_action( ActionScheduler_Action $action ) { |
| 353 | 355 | $store = ActionScheduler_Store::instance(); |
| 354 | - return method_exists( $store, 'save_unique_action' ) ? | |
| 355 | - $store->save_unique_action( $action ) : $store->save_action( $action ); | |
| 356 | + if ( method_exists( $store, 'save_unique_action' ) ) { | |
| 357 | + return $store->save_unique_action( $action ); | |
| 358 | + } else { | |
| 359 | + /** | |
| 360 | + * Fallback to non-unique action if the store doesn't support unique actions. | |
| 361 | + * We try to save the action as unique, accepting that there might be a race condition. | |
| 362 | + * This is likely still better than giving up on unique actions entirely. | |
| 363 | + */ | |
| 364 | + $existing_action_id = (int) $store->find_action( | |
| 365 | + $action->get_hook(), | |
| 366 | + array( | |
| 367 | + 'args' => $action->get_args(), | |
| 368 | + 'status' => ActionScheduler_Store::STATUS_PENDING, | |
| 369 | + 'group' => $action->get_group(), | |
| 370 | + ) | |
| 371 | + ); | |
| 372 | + if ( $existing_action_id > 0 ) { | |
| 373 | + return 0; | |
| 374 | + } | |
| 375 | + return $store->save_action( $action ); | |
| 376 | + } | |
| 356 | 377 | } |
| 357 | 378 | } |