| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Admin\Ajax |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* This class will catch the request to dismiss the target notice (set by notice_name) |
| 10 |
* and will store the dismiss status as an user meta in the database. |
| 11 |
*/ |
| 12 |
class Yoast_Dismissable_Notice_Ajax { |
| 13 |
|
| 14 |
/** |
| 15 |
* Notice type toggle value for user notices. |
| 16 |
* |
| 17 |
* @var string |
| 18 |
*/ |
| 19 |
const FOR_USER = 'user_meta'; |
| 20 |
|
| 21 |
/** |
| 22 |
* Notice type toggle value for network notices. |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
const FOR_NETWORK = 'site_option'; |
| 27 |
|
| 28 |
/** |
| 29 |
* Notice type toggle value for site notices. |
| 30 |
* |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
const FOR_SITE = 'option'; |
| 34 |
|
| 35 |
/** |
| 36 |
* Name of the notice that will be dismissed. |
| 37 |
* |
| 38 |
* @var string |
| 39 |
*/ |
| 40 |
private $notice_name; |
| 41 |
|
| 42 |
/** |
| 43 |
* The type of the current notice. |
| 44 |
* |
| 45 |
* @var string |
| 46 |
*/ |
| 47 |
private $notice_type; |
| 48 |
|
| 49 |
/** |
| 50 |
* Initialize the hooks for the AJAX request. |
| 51 |
* |
| 52 |
* @param string $notice_name The name for the hook to catch the notice. |
| 53 |
* @param string $notice_type The notice type. |
| 54 |
*/ |
| 55 |
public function __construct( $notice_name, $notice_type = self::FOR_USER ) { |
| 56 |
$this->notice_name = $notice_name; |
| 57 |
$this->notice_type = $notice_type; |
| 58 |
|
| 59 |
add_action( 'wp_ajax_wpseo_dismiss_' . $notice_name, [ $this, 'dismiss_notice' ] ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Handles the dismiss notice request. |
| 64 |
*/ |
| 65 |
public function dismiss_notice() { |
| 66 |
check_ajax_referer( 'wpseo-dismiss-' . $this->notice_name ); |
| 67 |
|
| 68 |
$this->save_dismissed(); |
| 69 |
|
| 70 |
wp_die( 'true' ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Storing the dismissed value in the database. The target location is based on the set notification type. |
| 75 |
*/ |
| 76 |
private function save_dismissed() { |
| 77 |
if ( $this->notice_type === self::FOR_SITE ) { |
| 78 |
update_option( 'wpseo_dismiss_' . $this->notice_name, 1 ); |
| 79 |
|
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
if ( $this->notice_type === self::FOR_NETWORK ) { |
| 84 |
update_site_option( 'wpseo_dismiss_' . $this->notice_name, 1 ); |
| 85 |
|
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
update_user_meta( get_current_user_id(), 'wpseo_dismiss_' . $this->notice_name, 1 ); |
| 90 |
} |
| 91 |
} |
| 92 |
|