HandleAction.php
1 month ago
HandleActionLabels.php
1 month ago
HandleActionManifest.php
1 month ago
HandleActionSend.php
1 month ago
HandleActionStrategy.php
1 month ago
HandleActionStrategyInterface.php
1 month ago
HandleActionLabels.php
76 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class HandleActionLabels |
| 4 | */ |
| 5 | |
| 6 | namespace WPDesk\FS\Shipment\BulkAction; |
| 7 | |
| 8 | use Exception; |
| 9 | use FSVendor\WPDesk\FS\Shipment\Exception\UnableToCreateTmpFileException; |
| 10 | use FSVendor\WPDesk\FS\Shipment\Exception\UnableToCreateTmpZipFileException; |
| 11 | use FSVendor\WPDesk\FS\Shipment\Label\LabelsBulkActionHandler; |
| 12 | use FSVendor\WPDesk\FS\Shipment\Label\LabelsFileCreator; |
| 13 | use FSVendor\WPDesk\Session\SessionFactory; |
| 14 | |
| 15 | /** |
| 16 | * . |
| 17 | */ |
| 18 | class HandleActionLabels implements HandleActionStrategyInterface { |
| 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 | * @param string $redirect_to . |
| 34 | * @param array $post_ids . |
| 35 | * |
| 36 | * @return string |
| 37 | */ |
| 38 | public function handle( string $redirect_to, array $post_ids ): string { |
| 39 | try { |
| 40 | $labels_bulk_actions_handler = LabelsBulkActionHandler::get_labels_bulk_actions_handler(); |
| 41 | $labels_bulk_actions_handler->bulk_process_orders( $post_ids ); |
| 42 | |
| 43 | $labels = $labels_bulk_actions_handler->get_labels_for_shipments(); |
| 44 | if ( 0 === count( $labels ) ) { |
| 45 | $redirect_to = add_query_arg( 'bulk_flexible_shipping_labels', count( $post_ids ), $redirect_to ); |
| 46 | |
| 47 | return add_query_arg( 'bulk_flexible_shipping_no_labels_created', 1, $redirect_to ); |
| 48 | } |
| 49 | |
| 50 | $labels_file_creator = new LabelsFileCreator( $labels ); |
| 51 | $labels_file_creator->create_labels_file(); |
| 52 | $labels['tmp_file'] = $labels_file_creator->get_tmp_file_name(); |
| 53 | $labels['client_file'] = $labels_file_creator->get_file_name(); |
| 54 | } catch ( UnableToCreateTmpZipFileException $zip_file_exception ) { |
| 55 | $labels['error'] = __( 'Unable to create temporary zip archive for labels. Check temporary folder configuration on server.', 'flexible-shipping' ); |
| 56 | } catch ( UnableToCreateTmpFileException $tmp_file_exception ) { |
| 57 | $labels['error'] = __( 'Unable to create temporary file for labels. Check temporary folder configuration on server.', 'flexible-shipping' ); |
| 58 | } catch ( \Exception $e ) { |
| 59 | $labels['error'] = $e->getMessage(); |
| 60 | } catch ( \Throwable $e ) { |
| 61 | $labels['error'] = $e->getMessage(); |
| 62 | } |
| 63 | |
| 64 | foreach ( $labels as $key => $label ) { |
| 65 | if ( is_array( $labels[ $key ] ) && isset( $labels[ $key ]['content'] ) ) { |
| 66 | unset( $labels[ $key ]['content'] ); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | $this->session_factory->get_woocommerce_session_adapter()->set( 'flexible_shipping_bulk_labels', $labels ); |
| 71 | |
| 72 | return add_query_arg( 'bulk_flexible_shipping_labels', count( $post_ids ), $redirect_to ); |
| 73 | } |
| 74 | |
| 75 | } |
| 76 |