flexible-shipping
/
src
/
WPDesk
/
FS
/
TableRate
/
ShippingMethod
/
Duplicate
/
DuplicateAction.php
DuplicateAction.php
1 month ago
DuplicateNotice.php
1 month ago
DuplicateScript.php
1 month ago
DuplicateTracker.php
1 month ago
DuplicatorChecker.php
1 month ago
DuplicateAction.php
139 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class DuplicateAction |
| 4 | * |
| 5 | * @package WPDesk\FS\TableRate\ShippingMethod\Duplicate |
| 6 | */ |
| 7 | |
| 8 | namespace WPDesk\FS\TableRate\ShippingMethod\Duplicate; |
| 9 | |
| 10 | use FSVendor\WPDesk\PluginBuilder\Plugin\Hookable; |
| 11 | use WPDesk\FS\TableRate\ShippingMethod\Management\ShippingMethodManagement; |
| 12 | use WPDesk\FS\TableRate\ShippingMethodSingle; |
| 13 | |
| 14 | /** |
| 15 | * Duplicate Action. |
| 16 | */ |
| 17 | class DuplicateAction implements Hookable { |
| 18 | const ACTION = 'fs_duplicate_method'; |
| 19 | const OPTION = 'fs_duplicate_method'; |
| 20 | const PARAM_ID = 'instance_id'; |
| 21 | |
| 22 | /** |
| 23 | * @var ShippingMethodManagement |
| 24 | */ |
| 25 | private $shipping_method_management; |
| 26 | |
| 27 | /** |
| 28 | * @var DuplicatorChecker |
| 29 | */ |
| 30 | private $duplicator_checker; |
| 31 | |
| 32 | /** |
| 33 | * @param DuplicatorChecker $duplicator_checker . |
| 34 | * @param ShippingMethodManagement $shipping_method_management . |
| 35 | */ |
| 36 | public function __construct( DuplicatorChecker $duplicator_checker, ShippingMethodManagement $shipping_method_management ) { |
| 37 | $this->duplicator_checker = $duplicator_checker; |
| 38 | $this->shipping_method_management = $shipping_method_management; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Init hooks (actions and filters). |
| 43 | * |
| 44 | * @return void |
| 45 | */ |
| 46 | public function hooks() { |
| 47 | add_action( 'admin_post_' . self::ACTION, [ $this, 'action_duplicate' ] ); |
| 48 | } |
| 49 | |
| 50 | public function action_duplicate() { |
| 51 | check_admin_referer( self::ACTION ); |
| 52 | |
| 53 | $instance_id = (int) ( sanitize_text_field( wp_unslash( $_GET[ self::PARAM_ID ] ?? 0 ) ) ); |
| 54 | |
| 55 | if ( ! $instance_id ) { |
| 56 | wp_die( __( 'Shipping method duplication error. Please try again later.', 'flexible-shipping' ) ); // phpcs:ignore |
| 57 | |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | $title = $this->shipping_method_management->get_shipping_method( $instance_id )->get_title(); |
| 62 | |
| 63 | if ( ! $this->duplicator_checker->should_duplicate( $instance_id ) ) { |
| 64 | wp_redirect( $this->get_redirect_url( 'error', $title ) ); |
| 65 | $this->end_request(); |
| 66 | |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | $zone = $this->shipping_method_management->get_shipping_zone( $instance_id ); |
| 71 | $new_instance_id = $zone->add_shipping_method( ShippingMethodSingle::SHIPPING_METHOD_ID ); |
| 72 | |
| 73 | $options = $this->get_instance_settings( $instance_id ); |
| 74 | |
| 75 | if ( empty( $options['method_title'] ?? '' ) ) { |
| 76 | $options['method_title'] = __( 'Flexible Shipping', 'flexible-shipping' ); |
| 77 | } |
| 78 | |
| 79 | $options['method_title'] .= ' ' . __( '(Copy)', 'flexible-shipping' ); |
| 80 | |
| 81 | add_option( $this->get_option_settings_field( $new_instance_id ), $options ); |
| 82 | |
| 83 | $this->shipping_method_management->set_shipping_method_status( $new_instance_id, false, $zone ); |
| 84 | |
| 85 | $this->update_usage_functionality(); |
| 86 | |
| 87 | wp_redirect( $this->get_redirect_url( 'success', $title ) ); |
| 88 | $this->end_request(); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @codeCoverageIgnore |
| 93 | */ |
| 94 | protected function end_request() { |
| 95 | die(); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @param int $instance_id . |
| 100 | * |
| 101 | * @return array |
| 102 | */ |
| 103 | private function get_instance_settings( int $instance_id ): array { |
| 104 | return (array) get_option( $this->get_option_settings_field( $instance_id ), [] ); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @param int $instance_id . |
| 109 | * |
| 110 | * @return string |
| 111 | */ |
| 112 | private function get_option_settings_field( int $instance_id ): string { |
| 113 | return sprintf( 'woocommerce_%s_%d_settings', ShippingMethodSingle::SHIPPING_METHOD_ID, $instance_id ); |
| 114 | } |
| 115 | |
| 116 | private function update_usage_functionality() { |
| 117 | $current = (int) get_option( self::OPTION, 0 ); |
| 118 | |
| 119 | update_option( self::OPTION, ++$current ); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @param string $status . |
| 124 | * @param string $method_title . |
| 125 | * |
| 126 | * @return string |
| 127 | */ |
| 128 | private function get_redirect_url( string $status, string $method_title ): string { |
| 129 | return add_query_arg( |
| 130 | [ |
| 131 | self::ACTION => true, |
| 132 | 'status' => $status, |
| 133 | 'method_title' => urlencode( $method_title ), |
| 134 | ], |
| 135 | wp_get_referer() |
| 136 | ); |
| 137 | } |
| 138 | } |
| 139 |