views
1 month ago
FinishOption.php
1 month ago
Onboarding.php
1 month ago
OptionAjaxUpdater.php
1 month ago
PopupData.php
1 month ago
Tracker.php
1 month ago
OptionAjaxUpdater.php
115 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Onboarding option AJAX updater. |
| 4 | * |
| 5 | * @package WPDesk\FS\TableRate\NewRulesTableBanner |
| 6 | */ |
| 7 | |
| 8 | namespace WPDesk\FS\Onboarding\TableRate; |
| 9 | |
| 10 | use FSVendor\WPDesk\PluginBuilder\Plugin\Hookable; |
| 11 | |
| 12 | /** |
| 13 | * Can update option when onboarding is clicked. |
| 14 | */ |
| 15 | class OptionAjaxUpdater implements Hookable { |
| 16 | const AJAX_ACTION_CLICK = 'flexible_shipping_onboarding_table_rate_click'; |
| 17 | const AJAX_ACTION_EVENT = 'flexible_shipping_onboarding_table_rate_event'; |
| 18 | const AJAX_ACTION_AUTO_SHOP_POPUP = 'flexible_shipping_onboarding_table_rate_auto_show_popup'; |
| 19 | |
| 20 | const NONCE_ACTION = 'flexible_shipping_onboarding_table_rate'; |
| 21 | |
| 22 | /** |
| 23 | * @var FinishOption |
| 24 | */ |
| 25 | private $option; |
| 26 | |
| 27 | /** |
| 28 | * OptionAjaxUpdater constructor. |
| 29 | * |
| 30 | * @param FinishOption $option . |
| 31 | */ |
| 32 | public function __construct( FinishOption $option ) { |
| 33 | $this->option = $option; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Hooks. |
| 38 | */ |
| 39 | public function hooks() { |
| 40 | add_action( 'wp_ajax_' . self::AJAX_ACTION_CLICK, [ $this, 'handle_ajax_action_click' ] ); |
| 41 | add_action( 'wp_ajax_' . self::AJAX_ACTION_EVENT, [ $this, 'handle_ajax_action_event' ] ); |
| 42 | add_action( 'wp_ajax_' . self::AJAX_ACTION_AUTO_SHOP_POPUP, [ $this, 'handle_ajax_action_auto_show_popup' ] ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Handle AJAX action OK. |
| 47 | * |
| 48 | * @internal |
| 49 | */ |
| 50 | public function handle_ajax_action_event() { |
| 51 | check_ajax_referer( self::NONCE_ACTION ); |
| 52 | if ( ! current_user_can( 'manage_woocommerce' ) ) { |
| 53 | wp_send_json_error(); |
| 54 | } |
| 55 | |
| 56 | $event = $this->filter_input( INPUT_POST, 'event' ); |
| 57 | $step = (int) $this->filter_input( INPUT_POST, 'step' ); |
| 58 | |
| 59 | if ( $event ) { |
| 60 | $this->option->update_option( 'event', sanitize_text_field( $event ) ); |
| 61 | $this->option->update_option( 'step', $step ); |
| 62 | |
| 63 | wp_send_json_success(); |
| 64 | } else { |
| 65 | wp_send_json_error(); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Handle AJAX action Click. |
| 71 | * |
| 72 | * @internal |
| 73 | */ |
| 74 | public function handle_ajax_action_click() { |
| 75 | check_ajax_referer( self::NONCE_ACTION ); |
| 76 | if ( ! current_user_can( 'manage_woocommerce' ) ) { |
| 77 | wp_send_json_error(); |
| 78 | } |
| 79 | |
| 80 | $clicks = (int) $this->option->get_option_value( 'clicks' ); |
| 81 | |
| 82 | $this->option->update_option( 'clicks', $clicks + 1 ); |
| 83 | $this->option->update_option( 'step', 0 ); |
| 84 | |
| 85 | wp_send_json_success(); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Handle AJAX action Click. |
| 90 | * |
| 91 | * @internal |
| 92 | */ |
| 93 | public function handle_ajax_action_auto_show_popup() { |
| 94 | check_ajax_referer( self::NONCE_ACTION ); |
| 95 | if ( ! current_user_can( 'manage_woocommerce' ) ) { |
| 96 | wp_send_json_error(); |
| 97 | } |
| 98 | |
| 99 | $this->option->update_option( 'auto_show_popup', 1 ); |
| 100 | |
| 101 | wp_send_json_success(); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * @param int $type . |
| 106 | * @param string $var_name . |
| 107 | * |
| 108 | * @return mixed |
| 109 | * @codeCoverageIgnore |
| 110 | */ |
| 111 | protected function filter_input( int $type, string $var_name ) { |
| 112 | return filter_input( $type, $var_name ); |
| 113 | } |
| 114 | } |
| 115 |