the nonce action that screen's list table signs its form with. */ public const SCREENS = [ 'woocommerce_page_wc-orders' => 'bulk-orders', 'edit-shop_order' => 'bulk-posts', ]; /** * Hook registration. */ public function register(): void { foreach ( array_keys( self::SCREENS ) as $screen ) { add_filter( "bulk_actions-{$screen}", [ $this, 'add' ] ); } add_filter( 'handle_bulk_actions-woocommerce_page_wc-orders', [ $this, 'handle_hpos' ], 10, 3 ); add_filter( 'handle_bulk_actions-edit-shop_order', [ $this, 'handle_legacy' ], 10, 3 ); } /** * Offer the action to people who may export. * * @param mixed $actions Bulk actions, label by key. * @return array */ public function add( mixed $actions ): array { $actions = is_array( $actions ) ? $actions : []; if ( current_user_can( Page::CAPABILITY ) ) { $actions[ self::ACTION ] = __( 'Export with Modulary', 'modulary-export' ); } return $actions; } /** * Handler for the HPOS orders screen. * * @param mixed $redirect Where the screen will redirect to. * @param mixed $action Bulk action. * @param mixed $ids Checked order IDs. */ public function handle_hpos( mixed $redirect, mixed $action, mixed $ids ): string { return $this->handle( $redirect, $action, $ids, self::SCREENS['woocommerce_page_wc-orders'] ); } /** * Handler for the legacy (posts) orders screen. * * @param mixed $redirect Where the screen will redirect to. * @param mixed $action Bulk action. * @param mixed $ids Checked order IDs. */ public function handle_legacy( mixed $redirect, mixed $action, mixed $ids ): string { return $this->handle( $redirect, $action, $ids, self::SCREENS['edit-shop_order'] ); } /** * Store the checked orders and send the merchant to the builder. * * Other bulk actions pass through untouched. When none of the IDs is an * exportable order, the merchant stays on the Orders screen. * * @param mixed $redirect Where the screen will redirect to. * @param mixed $action Bulk action. * @param mixed $ids Checked order IDs. * @param string $nonce_action The screen's bulk nonce action. */ private function handle( mixed $redirect, mixed $action, mixed $ids, string $nonce_action ): string { $redirect = is_string( $redirect ) ? $redirect : ''; if ( self::ACTION !== $action ) { return $redirect; } if ( ! current_user_can( Page::CAPABILITY ) ) { wp_die( esc_html__( 'You are not allowed to export orders.', 'modulary-export' ), 403 ); } check_admin_referer( $nonce_action ); $visible = Selection::visible( $ids ); if ( ! $visible ) { return $redirect; } return Selection::builder_url( Selection::store( get_current_user_id(), $visible ) ); } }