self::HOOK, 'args' => array( $order_id ), 'group' => self::GROUP, 'status' => ActionScheduler_Store::STATUS_PENDING, 'per_page' => 1, ), 'ids' ); if ( $waiting ) { return; } as_enqueue_async_action( self::HOOK, array( $order_id ), self::GROUP ); } /** * Queues the parent of a deleted refund. * * @param int $refund_id Refund id. * @param int $order_id Parent order id. */ public static function queue_refund_parent( $refund_id, $order_id ): void { self::queue( $order_id ); } /** * Queues orders whose items for a product have no cost snapshot. * * @param int $product_id Product or variation id. */ public static function queue_orders_for_product( $product_id ): void { global $wpdb; $product_id = absint( $product_id ); $table = NOTASTAN_Ledger::items_table(); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- the plugin's own table; its content changes with every order. $order_ids = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT order_id FROM %i WHERE ( product_id = %d OR variation_id = %d ) AND cost_source <> 'snapshot'", $table, $product_id, $product_id ) ); foreach ( $order_ids as $order_id ) { self::queue( $order_id ); } } /** * Removes every scheduled action of the plugin. */ public static function deactivate(): void { if ( function_exists( 'as_unschedule_all_actions' ) ) { as_unschedule_all_actions( '', array(), self::GROUP ); } } /** * Queues every order that has a ledger row without an exchange rate. * * @return int Number of orders queued. */ public static function queue_orders_missing_rate(): int { global $wpdb; $table = NOTASTAN_Ledger::table(); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- the plugin's own table; its content changes with every order. $order_ids = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT order_id FROM %i WHERE rate_status = %s", $table, 'missing' ) ); foreach ( $order_ids as $order_id ) { self::queue( $order_id ); } return count( $order_ids ); } /** * How many of the plugin's background jobs are due and have not run yet. */ public static function pending_jobs(): int { if ( ! function_exists( 'as_get_scheduled_actions' ) ) { return 0; } $ids = as_get_scheduled_actions( array( 'group' => self::GROUP, 'status' => ActionScheduler_Store::STATUS_PENDING, 'date' => as_get_datetime_object(), 'date_compare' => '<=', 'per_page' => -1, ), 'ids' ); return count( $ids ); } }