AjaxTracker.php
56 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class AjaxTracker |
| 4 | */ |
| 5 | |
| 6 | namespace WPDesk\FS\ProFeatures\Tracker; |
| 7 | |
| 8 | use FSVendor\WPDesk\PluginBuilder\Plugin\Hookable; |
| 9 | |
| 10 | /** |
| 11 | * Can handle Ajax requests for pro features. |
| 12 | */ |
| 13 | class AjaxTracker implements Hookable { |
| 14 | |
| 15 | const AJAX_ACTION = 'flexible_shipping_pro_features_tracking'; |
| 16 | |
| 17 | /** |
| 18 | * @var TrackingData |
| 19 | */ |
| 20 | private $tracking_data; |
| 21 | |
| 22 | /** |
| 23 | * @param TrackingData $tracking_data . |
| 24 | */ |
| 25 | public function __construct( TrackingData $tracking_data ) { |
| 26 | $this->tracking_data = $tracking_data; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * @return void |
| 31 | */ |
| 32 | public function hooks() { |
| 33 | add_action( 'wp_ajax_' . self::AJAX_ACTION, [ $this, 'handle_ajax' ] ); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * @return void |
| 38 | */ |
| 39 | public function handle_ajax(): void { |
| 40 | check_ajax_referer( self::AJAX_ACTION ); |
| 41 | if ( ! current_user_can( 'manage_woocommerce' ) ) { |
| 42 | wp_send_json_error(); |
| 43 | } |
| 44 | |
| 45 | $status = wc_string_to_bool( sanitize_text_field( wp_unslash( $_REQUEST['status'] ?? 'false' ) ) ); |
| 46 | |
| 47 | if ( $status ) { |
| 48 | $this->tracking_data->increase_show_count(); |
| 49 | } else { |
| 50 | $this->tracking_data->increase_hide_count(); |
| 51 | } |
| 52 | |
| 53 | wp_send_json_success(); |
| 54 | } |
| 55 | } |
| 56 |