views
3 days ago
ConvertAction.php
3 days ago
ConvertNotice.php
3 days ago
ConvertTracker.php
3 days ago
ConvertTracker.php
138 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class ConvertTracker |
| 4 | * |
| 5 | * @package WPDesk\FS\TableRate\ShippingMethod\Convert |
| 6 | */ |
| 7 | |
| 8 | namespace WPDesk\FS\TableRate\ShippingMethod\Convert; |
| 9 | |
| 10 | use FSVendor\WPDesk\PluginBuilder\Plugin\Hookable; |
| 11 | use WC_Shipping_Zones; |
| 12 | use WPDesk_Flexible_Shipping; |
| 13 | |
| 14 | /** |
| 15 | * Tracking of convert usages. |
| 16 | */ |
| 17 | class ConvertTracker implements Hookable { |
| 18 | const OPTION_FIRST = 'flexible_shipping_convert_first'; |
| 19 | const OPTION_AGAIN = 'flexible_shipping_convert_again'; |
| 20 | const OPTION_DELETED = 'flexible_shipping_convert_deleted'; |
| 21 | |
| 22 | const PRIORYTY_AFTER_FS_TRACKER = 12; |
| 23 | |
| 24 | /** |
| 25 | * @var array . |
| 26 | */ |
| 27 | private $to_delete_methods = array(); |
| 28 | |
| 29 | /** |
| 30 | * Hooks. |
| 31 | */ |
| 32 | public function hooks() { |
| 33 | add_filter( 'wpdesk_tracker_data', array( $this, 'add_tracking_data' ), self::PRIORYTY_AFTER_FS_TRACKER ); |
| 34 | |
| 35 | // Trackers. |
| 36 | add_action( 'woocommerce_shipping_zone_method_deleted', array( $this, 'track_deleted_method' ) ); |
| 37 | add_action( 'flexible-shipping/group-method/converted-method', array( $this, 'track_convert_method' ) ); |
| 38 | |
| 39 | // Prepare before tracking. |
| 40 | add_action( |
| 41 | 'wp_ajax_woocommerce_shipping_zone_methods_save_changes', |
| 42 | array( |
| 43 | $this, |
| 44 | 'check_deleted_methods', |
| 45 | ), |
| 46 | 5 |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Track running of converting. |
| 52 | */ |
| 53 | public function track_convert_method() { |
| 54 | $converting_again = filter_input( INPUT_GET, 'converting_again' ); |
| 55 | |
| 56 | $this->update_count( $converting_again ? self::OPTION_AGAIN : self::OPTION_FIRST ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Check if FS Group has been deleted. |
| 61 | */ |
| 62 | public function check_deleted_methods() { |
| 63 | if ( ! isset( $_POST['wc_shipping_zones_nonce'], $_POST['zone_id'], $_POST['changes'] ) ) { |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | if ( ! wp_verify_nonce( wp_unslash( $_POST['wc_shipping_zones_nonce'] ), 'wc_shipping_zones_nonce' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | if ( ! current_user_can( 'manage_woocommerce' ) ) { |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | $changes = wp_unslash( $_POST['changes'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 76 | |
| 77 | if ( ! isset( $changes['methods'] ) ) { |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | foreach ( $changes['methods'] as $instance_id => $data ) { |
| 82 | if ( ! isset( $data['deleted'] ) ) { |
| 83 | continue; |
| 84 | } |
| 85 | |
| 86 | $shipping_method = WC_Shipping_Zones::get_shipping_method( $instance_id ); |
| 87 | |
| 88 | if ( ! $shipping_method instanceof WPDesk_Flexible_Shipping ) { |
| 89 | continue; |
| 90 | } |
| 91 | |
| 92 | $this->to_delete_methods[] = $instance_id; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @param int $instance_id . |
| 98 | */ |
| 99 | public function track_deleted_method( $instance_id ) { |
| 100 | if ( ! in_array( $instance_id, $this->to_delete_methods ) ) { |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | $this->update_count( self::OPTION_DELETED ); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @param array $data . |
| 109 | * |
| 110 | * @return array |
| 111 | */ |
| 112 | public function add_tracking_data( $data ) { |
| 113 | $data['flexible_shipping']['convert'] = $this->prepare_data(); |
| 114 | |
| 115 | return $data; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * @return array |
| 120 | */ |
| 121 | private function prepare_data() { |
| 122 | return array( |
| 123 | 'first' => (int) get_option( self::OPTION_FIRST, 0 ), |
| 124 | 'again' => (int) get_option( self::OPTION_AGAIN, 0 ), |
| 125 | 'deleted' => (int) get_option( self::OPTION_DELETED, 0 ), |
| 126 | ); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Update option count. |
| 131 | * |
| 132 | * @param string $option . |
| 133 | */ |
| 134 | private function update_count( $option ) { |
| 135 | update_option( $option, (int) get_option( $option, 0 ) + 1 ); |
| 136 | } |
| 137 | } |
| 138 |