| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Actions; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Helpers\User_Helper; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class Alert_Dismissal_Action. |
| 9 |
*/ |
| 10 |
class Alert_Dismissal_Action { |
| 11 |
|
| 12 |
public const USER_META_KEY = '_yoast_alerts_dismissed'; |
| 13 |
|
| 14 |
/** |
| 15 |
* Holds the user helper instance. |
| 16 |
* |
| 17 |
* @var User_Helper |
| 18 |
*/ |
| 19 |
protected $user; |
| 20 |
|
| 21 |
/** |
| 22 |
* Constructs Alert_Dismissal_Action. |
| 23 |
* |
| 24 |
* @param User_Helper $user User helper. |
| 25 |
*/ |
| 26 |
public function __construct( User_Helper $user ) { |
| 27 |
$this->user = $user; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Dismisses an alert. |
| 32 |
* |
| 33 |
* @param string $alert_identifier Alert identifier. |
| 34 |
* |
| 35 |
* @return bool Whether the dismiss was successful or not. |
| 36 |
*/ |
| 37 |
public function dismiss( $alert_identifier ) { |
| 38 |
$user_id = $this->user->get_current_user_id(); |
| 39 |
if ( $user_id === 0 ) { |
| 40 |
return false; |
| 41 |
} |
| 42 |
|
| 43 |
if ( $this->is_allowed( $alert_identifier ) === false ) { |
| 44 |
return false; |
| 45 |
} |
| 46 |
|
| 47 |
$dismissed_alerts = $this->get_dismissed_alerts( $user_id ); |
| 48 |
if ( $dismissed_alerts === false ) { |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
if ( \array_key_exists( $alert_identifier, $dismissed_alerts ) === true ) { |
| 53 |
// The alert is already dismissed. |
| 54 |
return true; |
| 55 |
} |
| 56 |
|
| 57 |
// Add this alert to the dismissed alerts. |
| 58 |
$dismissed_alerts[ $alert_identifier ] = true; |
| 59 |
|
| 60 |
// Save. |
| 61 |
return $this->user->update_meta( $user_id, static::USER_META_KEY, $dismissed_alerts ) !== false; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Resets an alert. |
| 66 |
* |
| 67 |
* @param string $alert_identifier Alert identifier. |
| 68 |
* |
| 69 |
* @return bool Whether the reset was successful or not. |
| 70 |
*/ |
| 71 |
public function reset( $alert_identifier ) { |
| 72 |
$user_id = $this->user->get_current_user_id(); |
| 73 |
if ( $user_id === 0 ) { |
| 74 |
return false; |
| 75 |
} |
| 76 |
|
| 77 |
if ( $this->is_allowed( $alert_identifier ) === false ) { |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
$dismissed_alerts = $this->get_dismissed_alerts( $user_id ); |
| 82 |
if ( $dismissed_alerts === false ) { |
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
$amount_of_dismissed_alerts = \count( $dismissed_alerts ); |
| 87 |
if ( $amount_of_dismissed_alerts === 0 ) { |
| 88 |
// No alerts: nothing to reset. |
| 89 |
return true; |
| 90 |
} |
| 91 |
|
| 92 |
if ( \array_key_exists( $alert_identifier, $dismissed_alerts ) === false ) { |
| 93 |
// Alert not found: nothing to reset. |
| 94 |
return true; |
| 95 |
} |
| 96 |
|
| 97 |
if ( $amount_of_dismissed_alerts === 1 ) { |
| 98 |
// The 1 remaining dismissed alert is the alert to reset: delete the alerts user meta row. |
| 99 |
return $this->user->delete_meta( $user_id, static::USER_META_KEY, $dismissed_alerts ); |
| 100 |
} |
| 101 |
|
| 102 |
// Remove this alert from the dismissed alerts. |
| 103 |
unset( $dismissed_alerts[ $alert_identifier ] ); |
| 104 |
|
| 105 |
// Save. |
| 106 |
return $this->user->update_meta( $user_id, static::USER_META_KEY, $dismissed_alerts ) !== false; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Returns if an alert is dismissed or not. |
| 111 |
* |
| 112 |
* @param string $alert_identifier Alert identifier. |
| 113 |
* |
| 114 |
* @return bool Whether the alert has been dismissed. |
| 115 |
*/ |
| 116 |
public function is_dismissed( $alert_identifier ) { |
| 117 |
$user_id = $this->user->get_current_user_id(); |
| 118 |
if ( $user_id === 0 ) { |
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
if ( $this->is_allowed( $alert_identifier ) === false ) { |
| 123 |
return false; |
| 124 |
} |
| 125 |
|
| 126 |
$dismissed_alerts = $this->get_dismissed_alerts( $user_id ); |
| 127 |
if ( $dismissed_alerts === false ) { |
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
return \array_key_exists( $alert_identifier, $dismissed_alerts ); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Returns an object with all alerts dismissed by current user. |
| 136 |
* |
| 137 |
* @return array|false An array with the keys of all Alerts that have been dismissed |
| 138 |
* by the current user or `false`. |
| 139 |
*/ |
| 140 |
public function all_dismissed() { |
| 141 |
$user_id = $this->user->get_current_user_id(); |
| 142 |
if ( $user_id === 0 ) { |
| 143 |
return false; |
| 144 |
} |
| 145 |
|
| 146 |
$dismissed_alerts = $this->get_dismissed_alerts( $user_id ); |
| 147 |
if ( $dismissed_alerts === false ) { |
| 148 |
return false; |
| 149 |
} |
| 150 |
|
| 151 |
return $dismissed_alerts; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Returns if an alert is allowed or not. |
| 156 |
* |
| 157 |
* @param string $alert_identifier Alert identifier. |
| 158 |
* |
| 159 |
* @return bool Whether the alert is allowed. |
| 160 |
*/ |
| 161 |
public function is_allowed( $alert_identifier ) { |
| 162 |
return \in_array( $alert_identifier, $this->get_allowed_dismissable_alerts(), true ); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Retrieves the dismissed alerts. |
| 167 |
* |
| 168 |
* @param int $user_id User ID. |
| 169 |
* |
| 170 |
* @return string[]|false The dismissed alerts. False for an invalid $user_id. |
| 171 |
*/ |
| 172 |
protected function get_dismissed_alerts( $user_id ) { |
| 173 |
$dismissed_alerts = $this->user->get_meta( $user_id, static::USER_META_KEY, true ); |
| 174 |
if ( $dismissed_alerts === false ) { |
| 175 |
// Invalid user ID. |
| 176 |
return false; |
| 177 |
} |
| 178 |
|
| 179 |
if ( $dismissed_alerts === '' ) { |
| 180 |
/* |
| 181 |
* When no database row exists yet, an empty string is returned because of the `single` parameter. |
| 182 |
* We do want a single result returned, but the default should be an empty array instead. |
| 183 |
*/ |
| 184 |
return []; |
| 185 |
} |
| 186 |
|
| 187 |
return $dismissed_alerts; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Retrieves the allowed dismissable alerts. |
| 192 |
* |
| 193 |
* @return string[] The allowed dismissable alerts. |
| 194 |
*/ |
| 195 |
protected function get_allowed_dismissable_alerts() { |
| 196 |
/** |
| 197 |
* Filter: 'wpseo_allowed_dismissable_alerts' - List of allowed dismissable alerts. |
| 198 |
* |
| 199 |
* @param string[] $allowed_dismissable_alerts Allowed dismissable alerts list. |
| 200 |
*/ |
| 201 |
$allowed_dismissable_alerts = \apply_filters( 'wpseo_allowed_dismissable_alerts', [] ); |
| 202 |
|
| 203 |
if ( \is_array( $allowed_dismissable_alerts ) === false ) { |
| 204 |
return []; |
| 205 |
} |
| 206 |
|
| 207 |
// Only allow strings. |
| 208 |
$allowed_dismissable_alerts = \array_filter( $allowed_dismissable_alerts, 'is_string' ); |
| 209 |
|
| 210 |
// Filter unique and reorder indices. |
| 211 |
$allowed_dismissable_alerts = \array_values( \array_unique( $allowed_dismissable_alerts ) ); |
| 212 |
|
| 213 |
return $allowed_dismissable_alerts; |
| 214 |
} |
| 215 |
} |
| 216 |
|