BulkAction
1 month ago
views
1 month ago
AdminNotices.php
1 month ago
BulkAction.php
1 month ago
DispatchLabelFile.php
1 month ago
FilterOrders.php
1 month ago
ModifyOrderTable.php
1 month ago
ModifyStatuses.php
1 month ago
SubscriptionsIntegration.php
1 month ago
BulkAction.php
140 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class BulkAction |
| 4 | */ |
| 5 | |
| 6 | namespace WPDesk\FS\Shipment; |
| 7 | |
| 8 | use Exception; |
| 9 | use FSVendor\WPDesk\PluginBuilder\Plugin\Hookable; |
| 10 | use FSVendor\WPDesk\Session\SessionFactory; |
| 11 | use WPDesk\FS\Shipment\BulkAction\HandleAction; |
| 12 | use WPDesk\FS\Shipment\BulkAction\HandleActionStrategy; |
| 13 | use WPDesk\FS\Shipment\BulkAction\HandleActionStrategyInterface; |
| 14 | |
| 15 | /** |
| 16 | * Bulk actions on order screen. |
| 17 | */ |
| 18 | class BulkAction implements Hookable { |
| 19 | |
| 20 | /** |
| 21 | * @var SessionFactory |
| 22 | */ |
| 23 | private $session_factory; |
| 24 | |
| 25 | /** |
| 26 | * @param SessionFactory $session_factory . |
| 27 | */ |
| 28 | public function __construct( SessionFactory $session_factory ) { |
| 29 | $this->session_factory = $session_factory; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * @return void |
| 34 | */ |
| 35 | public function hooks() { |
| 36 | // Old Screen. |
| 37 | add_filter( 'bulk_actions-edit-shop_order', [ $this, 'add_bulk_action_options' ] ); |
| 38 | add_filter( 'handle_bulk_actions-edit-shop_order', [ $this, 'handle_bulk_action' ], 10, 3 ); |
| 39 | |
| 40 | // New Screen. |
| 41 | add_filter( 'bulk_actions-woocommerce_page_wc-orders', [ $this, 'add_bulk_action_options' ] ); |
| 42 | add_action( 'admin_init', [ $this, 'handle_bulk_action_new_screen' ] ); |
| 43 | } |
| 44 | |
| 45 | public function handle_bulk_action_new_screen() { |
| 46 | if ( ! isset( $_GET['page'], $_GET['action'], $_GET['id'] ) || empty( $_GET['id'] ) || 'wc-orders' !== wp_unslash( $_GET['page'] ) ) { //phpcs:ignore |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | $action = wp_unslash( $_GET['action'] ); //phpcs:ignore |
| 51 | $orders = wp_parse_id_list( wp_unslash( $_GET['id'] ) ); |
| 52 | |
| 53 | try { |
| 54 | $this->get_handle_action_strategy( $action ); |
| 55 | } catch ( Exception $e ) { |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | check_admin_referer( 'bulk-orders' ); |
| 60 | wp_safe_redirect( $this->handle_action( wp_get_referer(), $action, $orders ) ); |
| 61 | die(); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @param array $bulk_actions . |
| 66 | * |
| 67 | * @return mixed |
| 68 | */ |
| 69 | public function add_bulk_action_options( $bulk_actions ) { |
| 70 | $integrations = apply_filters( 'flexible_shipping_integration_options', [] ); |
| 71 | |
| 72 | if ( count( $integrations ) ) { |
| 73 | $bulk_actions['flexible_shipping_send'] = __( 'Send shipment', 'flexible-shipping' ); |
| 74 | $bulk_actions['flexible_shipping_labels'] = __( 'Get labels', 'flexible-shipping' ); |
| 75 | |
| 76 | if ( apply_filters( 'flexible_shipping_has_manifests', false ) ) { |
| 77 | $bulk_actions['flexible_shipping_manifest'] = __( 'Create shipping manifest', 'flexible-shipping' ); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return $bulk_actions; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @param string $redirect_to . |
| 86 | * @param string $action . |
| 87 | * @param array $post_ids . |
| 88 | * |
| 89 | * @return string |
| 90 | */ |
| 91 | public function handle_bulk_action( $redirect_to, $action, $post_ids ): string { |
| 92 | $redirect_to = is_string( $redirect_to ) ? $redirect_to : add_query_arg( 'post_type', 'shop_order', admin_url( 'edit.php' ) ); |
| 93 | |
| 94 | return $this->handle_action( $redirect_to, $action, $post_ids ); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * @param string $redirect_to . |
| 99 | * @param string $action . |
| 100 | * @param array $ids . |
| 101 | * |
| 102 | * @return string |
| 103 | */ |
| 104 | private function handle_action( string $redirect_to, string $action, array $ids ): string { |
| 105 | $redirect_to = remove_query_arg( 'bulk_flexible_shipping_send', $redirect_to ); |
| 106 | $redirect_to = remove_query_arg( 'bulk_flexible_shipping_labels', $redirect_to ); |
| 107 | $redirect_to = remove_query_arg( 'bulk_flexible_shipping_manifests', $redirect_to ); |
| 108 | |
| 109 | try { |
| 110 | $handle_action = $this->get_handle_action(); |
| 111 | |
| 112 | $handle_action->set_strategy( $this->get_handle_action_strategy( $action ) ); |
| 113 | |
| 114 | return $handle_action->handle( $redirect_to, wp_parse_id_list( $ids ) ); |
| 115 | } catch ( Exception $e ) { // phpcs:ignore |
| 116 | // Do nothing. |
| 117 | } |
| 118 | |
| 119 | return $redirect_to; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @return HandleAction |
| 124 | */ |
| 125 | protected function get_handle_action(): HandleAction { |
| 126 | return new HandleAction(); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * @param string $action |
| 131 | * |
| 132 | * @return HandleActionStrategyInterface |
| 133 | * @throws Exception |
| 134 | */ |
| 135 | protected function get_handle_action_strategy( string $action ): HandleActionStrategyInterface { |
| 136 | return ( new HandleActionStrategy( $this->session_factory ) )->get( $action ); |
| 137 | } |
| 138 | |
| 139 | } |
| 140 |