= self::MAX_ATTEMPTS ) { self::unschedule( self::HOOK_CATALOG_KEY ); Aha_Is_Woocommerce_Logger::warning( 'aha-is-for-woocommerce-connect: run_catalog_key paused after ' . self::MAX_ATTEMPTS . ' attempts' ); } self::ensure_scheduled(); } /** Log only a fixed prefix, the job name and a plugin reason code; never traces, bodies or keys. */ private static function log_failure( $job, $error ) { $reason = preg_match( '/^[a-z0-9_]{1,64}$/D', $error->getMessage() ) ? $error->getMessage() : 'unexpected'; Aha_Is_Woocommerce_Logger::warning( 'aha-is-for-woocommerce-connect: ' . $job . ' failed: ' . $reason ); } /** Whether the catalog-key job still has work: no catalog or order key while connected, or keys awaiting deletion. */ private static function catalog_key_needed() { // An owner who lost access cannot be fixed by retrying: no work, so no endless job. return ( 'connected' === Aha_Is_Woocommerce_Connect_Credentials::phase() && ! Aha_Is_Woocommerce_Connect_Credentials::catalog_key_owner_lost() && ( ! Aha_Is_Woocommerce_Connect_Credentials::catalog_key_registered() || ! Aha_Is_Woocommerce_Connect_Credentials::order_key_registered() ) ) || Aha_Is_Woocommerce_Connect_Credentials::catalog_key_cleanup_pending(); } /** Consecutive unsuccessful catalog-key runs for the current connection. */ private static function catalog_key_attempts() { $stored = get_option( self::ATTEMPTS_OPTION, array() ); if ( ! is_array( $stored ) || ! isset( $stored['count'], $stored['bootstrap_id'] ) || ! is_int( $stored['count'] ) ) { return 0; } $current = Aha_Is_Woocommerce_Connect_Credentials::current_bootstrap_id(); // An unreadable snapshot keeps the stored count, so read errors cannot defeat the cap. return null === $current || $current === $stored['bootstrap_id'] ? $stored['count'] : 0; } /** Count one run before it starts, so a crash mid-run still counts. Returns the new count. */ private static function record_catalog_key_attempt() { $count = self::catalog_key_attempts() + 1; $current = Aha_Is_Woocommerce_Connect_Credentials::current_bootstrap_id(); if ( null === $current ) { $stored = get_option( self::ATTEMPTS_OPTION, array() ); $current = is_array( $stored ) && isset( $stored['bootstrap_id'] ) && is_string( $stored['bootstrap_id'] ) ? $stored['bootstrap_id'] : ''; } update_option( self::ATTEMPTS_OPTION, array( 'bootstrap_id' => $current, 'count' => $count ), false ); return $count; } /** Clear the counter, writing only when it holds something. */ private static function reset_catalog_key_attempts() { if ( self::catalog_key_attempts() > 0 ) { update_option( self::ATTEMPTS_OPTION, array( 'bootstrap_id' => '', 'count' => 0 ), false ); } } /** Whether unattended catalog-key attempts stopped after MAX_ATTEMPTS. */ public static function catalog_key_paused() { return self::catalog_key_attempts() >= self::MAX_ATTEMPTS; } /** * Seconds before the next unattended attempt: none for the first, then 60, 120, 240 ... capped at one hour. * * $count is the attempts already recorded. run_catalog_key() records one before it does anything, * so a 0 delay cannot repeat: the running job has count >= 1 when ensure_scheduled() runs again. */ public static function catalog_key_delay( $count ) { if ( (int) $count < 1 ) { return 0; } return min( 3600, 60 * ( 1 << max( 0, min( (int) $count, self::MAX_ATTEMPTS ) - 1 ) ) ); } /** Merchant-requested retry: clear the counter and schedule one attempt now. */ public static function retry_catalog_key() { update_option( self::ATTEMPTS_OPTION, array( 'bootstrap_id' => '', 'count' => 0 ), false ); if ( ! function_exists( 'as_schedule_single_action' ) || ! function_exists( 'as_get_scheduled_actions' ) ) { return; } if ( ! self::has_pending( self::HOOK_CATALOG_KEY ) ) { as_schedule_single_action( time(), self::HOOK_CATALOG_KEY, array(), self::GROUP ); } } /** Schedule one near-term retry for a persisted redeem command. */ public static function schedule_poll_retry() { self::schedule_retry( self::HOOK_POLL_RETRY ); } /** Schedule one near-term retry for a persisted refresh command. */ public static function schedule_refresh_retry( $delay = 60 ) { self::schedule_retry( self::HOOK_REFRESH_RETRY, $delay ); } /** Schedule one deduplicated retry for remaining managed catalog keys. */ public static function schedule_catalog_key_retry() { if ( ! self::catalog_key_paused() ) { self::schedule_retry( self::HOOK_CATALOG_KEY, self::catalog_key_delay( self::catalog_key_attempts() ) ); } } /** Clear a pending redeem retry after reconciliation. */ public static function clear_poll_retry() { self::unschedule( self::HOOK_POLL_RETRY ); } /** Clear a pending refresh retry after reconciliation. */ public static function clear_refresh_retry() { self::unschedule( self::HOOK_REFRESH_RETRY ); } /** * Reconcile recurring actions with the readable local phase. * * This runs on admin_init so WooCommerce 4.0's Action Scheduler is ready. */ public static function ensure_scheduled() { if ( ! function_exists( 'as_schedule_recurring_action' ) || ! function_exists( 'as_next_scheduled_action' ) ) { return; } $phase = Aha_Is_Woocommerce_Connect_Credentials::phase(); if ( in_array( $phase, array( 'pending_review', 'reconnect_approved' ), true ) ) { if ( ! as_next_scheduled_action( self::HOOK_POLL, null, self::GROUP ) ) { as_schedule_recurring_action( time() + 60, 60, self::HOOK_POLL, array(), self::GROUP ); } } else { self::unschedule( self::HOOK_POLL ); self::unschedule( self::HOOK_POLL_RETRY ); } if ( 'connected' === $phase ) { if ( ! as_next_scheduled_action( self::HOOK_REFRESH, null, self::GROUP ) ) { as_schedule_recurring_action( time() + DAY_IN_SECONDS, DAY_IN_SECONDS, self::HOOK_REFRESH, array(), self::GROUP ); } } else { self::unschedule( self::HOOK_REFRESH ); self::unschedule( self::HOOK_REFRESH_RETRY ); } if ( self::catalog_key_needed() ) { if ( ! self::catalog_key_paused() ) { self::schedule_retry( self::HOOK_CATALOG_KEY, self::catalog_key_delay( self::catalog_key_attempts() ) ); } } else { self::reset_catalog_key_attempts(); } } /** Clear Connect actions on deactivation. */ public static function clear_scheduled() { self::unschedule( self::HOOK_POLL ); self::unschedule( self::HOOK_POLL_RETRY ); self::unschedule( self::HOOK_REFRESH ); self::unschedule( self::HOOK_REFRESH_RETRY ); self::unschedule( self::HOOK_CATALOG_KEY ); } /** Schedule a single deduplicated retry after $delay seconds. */ private static function schedule_retry( $hook, $delay = 60 ) { if ( ! function_exists( 'as_schedule_single_action' ) || ! function_exists( 'as_get_scheduled_actions' ) || self::has_pending( $hook ) ) { return; } as_schedule_single_action( time() + $delay, $hook, array(), self::GROUP ); } /** * Whether a pending action exists for $hook. * * Not as_next_scheduled_action(): it is also true for the running action, so a job * that fails inside its own run would never queue its retry. */ private static function has_pending( $hook ) { return (bool) as_get_scheduled_actions( array( 'hook' => $hook, 'group' => self::GROUP, 'status' => 'pending', 'per_page' => 1, ), 'ids' ); } /** Unschedule one Action Scheduler hook when the API is available. */ private static function unschedule( $hook ) { if ( function_exists( 'as_unschedule_all_actions' ) ) { as_unschedule_all_actions( $hook, null, self::GROUP ); } } }