From 0a858e5453961113eb0af9561ecfdfa265e33a2c Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 12 Nov 2024 13:19:56 -0800 Subject: [PATCH] Cherry pick 52725 into trunk (#52743) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PTK: Check to confirm `as_has_scheduled_action` exists before using to conditionally fire logger (#52725) * Check to confirm as_has_scheduled_action exists Add a check to confirm that `as_has_scheduled_action` exists in the condition that uses it to conditionally run the warning log. * improve fallback logic * Update plugins/woocommerce/src/Blocks/BlockPatterns.php --------- Co-authored-by: Daniel W. Robert Co-authored-by: Luigi Teschio Co-authored-by: Albert Juhé Lluveras Co-authored-by: Barry Hughes <3594411+barryhughes@users.noreply.github.com> --- plugins/woocommerce/src/Blocks/BlockPatterns.php | 6 +++++- .../woocommerce/src/Blocks/Patterns/PTKPatternsStore.php | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/plugins/woocommerce/src/Blocks/BlockPatterns.php b/plugins/woocommerce/src/Blocks/BlockPatterns.php index 3699243258f..7eeb2797eba 100644 --- a/plugins/woocommerce/src/Blocks/BlockPatterns.php +++ b/plugins/woocommerce/src/Blocks/BlockPatterns.php @@ -205,12 +205,16 @@ class BlockPatterns { return; } + // The most efficient way to check for an existing action is to use `as_has_scheduled_action`, but in unusual + // cases where another plugin has loaded a very old version of Action Scheduler, it may not be available to us. + $has_scheduled_action = function_exists( 'as_has_scheduled_action' ) ? 'as_has_scheduled_action' : 'as_next_scheduled_action'; + $patterns = $this->ptk_patterns_store->get_patterns(); if ( empty( $patterns ) ) { // By only logging when patterns are empty and no fetch is scheduled, // we ensure that warnings are only generated in genuinely problematic situations, // such as when the pattern fetching mechanism has failed entirely. - if ( ! as_has_scheduled_action( 'fetch_patterns' ) ) { + if ( ! call_user_func( $has_scheduled_action, 'fetch_patterns' ) ) { wc_get_logger()->warning( __( 'Empty patterns received from the PTK Pattern Store', 'woocommerce' ), ); diff --git a/plugins/woocommerce/src/Blocks/Patterns/PTKPatternsStore.php b/plugins/woocommerce/src/Blocks/Patterns/PTKPatternsStore.php index 1ce873b39f8..dd2c4735771 100644 --- a/plugins/woocommerce/src/Blocks/Patterns/PTKPatternsStore.php +++ b/plugins/woocommerce/src/Blocks/Patterns/PTKPatternsStore.php @@ -94,7 +94,11 @@ class PTKPatternsStore { */ private function schedule_action_if_not_pending( $action ) { $last_request = get_transient( 'last_fetch_patterns_request' ); - if ( as_has_scheduled_action( $action ) || false !== $last_request ) { + // The most efficient way to check for an existing action is to use `as_has_scheduled_action`, but in unusual + // cases where another plugin has loaded a very old version of Action Scheduler, it may not be available to us. + + $has_scheduled_action = function_exists( 'as_has_scheduled_action' ) ? 'as_has_scheduled_action' : 'as_next_scheduled_action'; + if ( call_user_func( $has_scheduled_action, $action ) || false !== $last_request ) { return; }