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: Albert Juhé Lluveras <contact@albertjuhe.com>

---------

Co-authored-by: Luigi Teschio <gigitux@gmail.com>
Co-authored-by: Albert Juhé Lluveras <contact@albertjuhe.com>
This commit is contained in:
Daniel W. Robert 2024-11-12 10:06:13 -05:00 committed by GitHub
parent b0284be31e
commit 9495d7ce98
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 2 deletions

View File

@ -150,12 +150,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;
}