| 1 |
<?php |
| 2 |
|
| 3 |
namespace YayMail\Notices; |
| 4 |
|
| 5 |
use YayMail\Utils\SingletonTrait; |
| 6 |
|
| 7 |
/** |
| 8 |
* |
| 9 |
* @method static Ajax get_instance() |
| 10 |
*/ |
| 11 |
class Ajax { |
| 12 |
use SingletonTrait; |
| 13 |
|
| 14 |
protected function __construct() { |
| 15 |
$this->init_hooks(); |
| 16 |
} |
| 17 |
|
| 18 |
protected function init_hooks() { |
| 19 |
add_action( 'wp_ajax_yaymail_dismiss_suggest_addons_notice', [ $this, 'yaymail_dismiss_suggest_addons_notice' ] ); |
| 20 |
add_action( 'wp_ajax_yaymail_dismiss_upgrade_notice', [ $this, 'yaymail_dismiss_upgrade_notice' ] ); |
| 21 |
} |
| 22 |
|
| 23 |
public function yaymail_dismiss_suggest_addons_notice() { |
| 24 |
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; |
| 25 |
if ( ! wp_verify_nonce( $nonce, 'yaymail_nonce' ) ) { |
| 26 |
return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] ); |
| 27 |
} |
| 28 |
try { |
| 29 |
// The Notice should comeback after 60 days |
| 30 |
update_option( 'yaymail_next_recommendation_suggest_addons_notice_time', time() + 60 * 60 * 24 * 60 ); |
| 31 |
wp_send_json_success(); |
| 32 |
} catch ( \Error $error ) { |
| 33 |
yaymail_get_logger( $error ); |
| 34 |
wp_send_json_error( [ 'mess' => $error->getMessage() ] ); |
| 35 |
} catch ( \Exception $exception ) { |
| 36 |
yaymail_get_logger( $exception ); |
| 37 |
wp_send_json_error( [ 'mess' => $exception->getMessage() ] ); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
public function yaymail_dismiss_upgrade_notice() { |
| 42 |
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; |
| 43 |
if ( ! wp_verify_nonce( $nonce, 'yaymail_nonce' ) ) { |
| 44 |
return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] ); |
| 45 |
} |
| 46 |
try { |
| 47 |
// The Notice should comeback after 60 days |
| 48 |
update_option( 'yaymail_next_recommendation_upgrade_notice_time', time() + 60 * 60 * 24 * 60 ); |
| 49 |
wp_send_json_success(); |
| 50 |
} catch ( \Error $error ) { |
| 51 |
yaymail_get_logger( $error ); |
| 52 |
wp_send_json_error( [ 'mess' => $error->getMessage() ] ); |
| 53 |
} catch ( \Exception $exception ) { |
| 54 |
yaymail_get_logger( $exception ); |
| 55 |
wp_send_json_error( [ 'mess' => $exception->getMessage() ] ); |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
|