Beacon.php
1 week ago
BeaconClickedAjax.php
1 week ago
BeaconDeactivationTracker.php
1 week ago
BeaconDisplayStrategy.php
1 week ago
BeaconClickedAjax.php
102 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class BeaconAjax |
| 4 | * |
| 5 | * @package WPDesk\FS\TableRate |
| 6 | */ |
| 7 | |
| 8 | namespace WPDesk\FS\TableRate\Beacon; |
| 9 | |
| 10 | use FSVendor\WPDesk\PluginBuilder\Plugin\Hookable; |
| 11 | |
| 12 | /** |
| 13 | * Can register Beacon click vi AJAX. |
| 14 | */ |
| 15 | class BeaconClickedAjax implements Hookable { |
| 16 | |
| 17 | const OPTION_NAME = 'flexible_shipping_beacon_clicked'; |
| 18 | const NONCE_ACTION = 'flexible_shipping_beacon_clicked'; |
| 19 | const AJAX_ACTION = 'flexible_shipping_beacon_clicked'; |
| 20 | |
| 21 | /** |
| 22 | * @var BeaconDisplayStrategy |
| 23 | */ |
| 24 | private $strategy; |
| 25 | |
| 26 | /** |
| 27 | * @var string |
| 28 | */ |
| 29 | private $assets_url; |
| 30 | |
| 31 | /** |
| 32 | * @var string |
| 33 | */ |
| 34 | private $scripts_version; |
| 35 | |
| 36 | /** |
| 37 | * BeaconAjax constructor. |
| 38 | * |
| 39 | * @param BeaconDisplayStrategy $strategy . |
| 40 | * @param string $assets_url . |
| 41 | * @param string $scripts_version . |
| 42 | */ |
| 43 | public function __construct( BeaconDisplayStrategy $strategy, $assets_url, $scripts_version ) { |
| 44 | $this->strategy = $strategy; |
| 45 | $this->assets_url = $assets_url; |
| 46 | $this->scripts_version = $scripts_version; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Hooks. |
| 51 | */ |
| 52 | public function hooks() { |
| 53 | if ( 0 === (int) get_option( self::OPTION_NAME, 0 ) ) { |
| 54 | if ( $this->strategy->shouldDisplay() ) { |
| 55 | add_action( 'admin_enqueue_scripts', [ $this, 'add_script' ] ); |
| 56 | } |
| 57 | add_action( 'wp_ajax_' . self::AJAX_ACTION, [ $this, 'handle_ajax_action' ] ); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @internal |
| 63 | */ |
| 64 | public function add_script() { |
| 65 | $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 66 | $handle = 'fs_beacon_clicked'; |
| 67 | |
| 68 | wp_register_script( |
| 69 | $handle, |
| 70 | trailingslashit( $this->assets_url ) . 'js/beacon-clicked' . $suffix . '.js', |
| 71 | [ 'jquery' ], |
| 72 | $this->scripts_version |
| 73 | ); |
| 74 | |
| 75 | wp_localize_script( |
| 76 | $handle, |
| 77 | 'fs_beacon_clicked', |
| 78 | [ |
| 79 | 'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 80 | 'action' => self::AJAX_ACTION, |
| 81 | 'nonce' => wp_create_nonce( self::NONCE_ACTION ), |
| 82 | ] |
| 83 | ); |
| 84 | |
| 85 | wp_enqueue_script( $handle ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Handle AJAX action. |
| 90 | * |
| 91 | * @internal |
| 92 | */ |
| 93 | public function handle_ajax_action() { |
| 94 | check_ajax_referer( self::AJAX_ACTION, 'nonce' ); |
| 95 | if ( ! current_user_can( 'manage_woocommerce' ) ) { |
| 96 | wp_send_json_error(); |
| 97 | } |
| 98 | update_option( self::OPTION_NAME, 1 ); |
| 99 | wp_send_json_success(); |
| 100 | } |
| 101 | } |
| 102 |