logger = $logger; } /** * Mark that an admin bulk status action is in progress. * * @param mixed $redirect_to Redirect URL (passed through). * @param string $action Bulk action name. * @param array $ids Affected ids. * @return mixed */ public function flag_bulk( $redirect_to, $action, $ids ) { if ( is_string( $action ) && 0 === strpos( $action, 'mark_' ) ) { self::$bulk = true; } return $redirect_to; } /** * Handle an order status change. * * @param int $order_id Order id. * @param string $old_status Previous status slug. * @param string $new_status New status slug. * @param \WC_Order $order Order object. */ public function on_status_changed( $order_id, $old_status, $new_status, $order ) { if ( self::$bulk && Options::suppress_bulk() ) { return; } // Nothing to send until the API token is set; avoids polluting every order // with failure notes/meta on a freshly-activated, unconfigured install. if ( '' === Options::token() ) { return; } if ( ! Options::is_status_enabled( $new_status ) ) { return; } if ( Options::optout_enabled() && $order instanceof \WC_Order && ! OptOut::is_allowed( $order ) ) { $this->logger->debug( 'Customer opted out, skipping', array( 'order_id' => $order_id, 'status' => $new_status ) ); return; } // No 'attempt' in the payload: retries reuse identical args so the dedup // guard matches; the attempt counter lives in order meta (see Dispatcher). $payload = array( 'order_id' => (int) $order_id, 'status' => $new_status, ); // Fallback when Action Scheduler is unavailable: run synchronously. if ( ! function_exists( 'as_enqueue_async_action' ) ) { do_action( SENDLY_HOOK, $payload ); return; } // Queue-level dedup guard. if ( function_exists( 'as_has_scheduled_action' ) && as_has_scheduled_action( SENDLY_HOOK, array( $payload ), SENDLY_GROUP ) ) { return; } as_enqueue_async_action( SENDLY_HOOK, array( $payload ), SENDLY_GROUP ); } }