scheduler = $scheduler ?? new BulkSyncScheduler(); } public function register(): void { foreach ($this->screenIds() as $screen) { add_filter("bulk_actions-{$screen}", [$this, 'registerBulkAction']); add_filter("handle_bulk_actions-{$screen}", [$this, 'handleBulkAction'], 10, 3); } add_action('admin_notices', [$this, 'renderNotice']); } /** * @param array $actions * @return array */ public function registerBulkAction(array $actions): array { $actions[self::BULK_ACTION] = __('Sync to Aivie', 'aivie-for-woocommerce'); return $actions; } /** * @param array $ids */ public function handleBulkAction(string $redirectTo, string $action, array $ids): string { if (self::BULK_ACTION !== $action) { return $redirectTo; } if (! current_user_can('manage_woocommerce')) { wp_die(esc_html__('Insufficient permissions.', 'aivie-for-woocommerce')); } $queued = 0; foreach ($ids as $id) { $orderId = absint($id); if ($orderId > 0 && $this->scheduler->enqueue($orderId)) { $queued++; } } Logger::info('Bulk order sync enqueued from list view.', [ 'requested' => count($ids), 'queued' => $queued, ]); return add_query_arg(self::NOTICE_ARG, $queued, $redirectTo); } public function renderNotice(): void { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only count for a confirmation notice; no state is changed. if (! isset($_GET[self::NOTICE_ARG])) { return; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only count for a confirmation notice; no state is changed. $count = absint(wp_unslash($_GET[self::NOTICE_ARG])); if ($count < 1) { printf( '

%s

', esc_html__('No new orders were queued for sync to Aivie (they may already be queued).', 'aivie-for-woocommerce') ); return; } $message = sprintf( /* translators: %d is the number of orders queued for sync. */ _n( '%d order queued for sync to Aivie.', '%d orders queued for sync to Aivie.', $count, 'aivie-for-woocommerce' ), $count ); printf( '

%s

', esc_html($message) ); } /** * Resolve the order list screen IDs for both legacy posts storage and HPOS. * * @return array */ private function screenIds(): array { $screens = ['edit-shop_order']; if (function_exists('wc_get_page_screen_id')) { $hpos = wc_get_page_screen_id('shop-order'); if (is_string($hpos) && '' !== $hpos) { $screens[] = $hpos; } } return array_values(array_unique($screens)); } }