Cherry pick 52725 into trunk (#52743)

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 <danielwrobert@users.noreply.github.com>
Co-authored-by: Luigi Teschio <gigitux@gmail.com>
Co-authored-by: Albert Juhé Lluveras <contact@albertjuhe.com>
Co-authored-by: Barry Hughes <3594411+barryhughes@users.noreply.github.com>
This commit is contained in:
github-actions[bot] 2024-11-12 13:19:56 -08:00 committed by GitHub
parent 6f92796727
commit 0a858e5453
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 2 deletions

View File

@ -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' ),
);

View File

@ -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;
}