| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 4 |
namespace Yoast\WP\SEO\Alerts\User_Interface; |
| 5 |
|
| 6 |
use Yoast\WP\SEO\Conditionals\No_Conditionals; |
| 7 |
use Yoast\WP\SEO\Helpers\Capability_Helper; |
| 8 |
use Yoast\WP\SEO\Helpers\User_Helper; |
| 9 |
use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 10 |
|
| 11 |
/** |
| 12 |
* Registers a route to resolve an alert |
| 13 |
* |
| 14 |
* @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded |
| 15 |
*/ |
| 16 |
class Resolve_Alert_Route implements Integration_Interface { |
| 17 |
|
| 18 |
use No_Conditionals; |
| 19 |
|
| 20 |
/** |
| 21 |
* The user helper. |
| 22 |
* |
| 23 |
* @var User_Helper |
| 24 |
*/ |
| 25 |
private $user_helper; |
| 26 |
|
| 27 |
/** |
| 28 |
* The capability helper. |
| 29 |
* |
| 30 |
* @var Capability_Helper |
| 31 |
*/ |
| 32 |
private $capability_helper; |
| 33 |
|
| 34 |
/** |
| 35 |
* Class constructor. |
| 36 |
* |
| 37 |
* @param User_Helper $user_helper The user helper. |
| 38 |
* @param Capability_Helper $capability_helper The capability helper. |
| 39 |
*/ |
| 40 |
public function __construct( |
| 41 |
User_Helper $user_helper, |
| 42 |
Capability_Helper $capability_helper |
| 43 |
) { |
| 44 |
$this->user_helper = $user_helper; |
| 45 |
$this->capability_helper = $capability_helper; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Registers all hooks to WordPress. |
| 50 |
* |
| 51 |
* @return void |
| 52 |
*/ |
| 53 |
public function register_hooks() { |
| 54 |
\add_action( 'wp_ajax_wpseo_resolve_alert', [ $this, 'resolve_alert' ] ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Runs the callback to resolve an alert for the current user. |
| 59 |
* |
| 60 |
* @return void. |
| 61 |
*/ |
| 62 |
public function resolve_alert() { |
| 63 |
if ( ! \check_ajax_referer( 'wpseo-resolve-alert-nonce', 'nonce', false ) || ! $this->capability_helper->current_user_can( 'wpseo_manage_options' ) ) { |
| 64 |
\wp_send_json_error( |
| 65 |
[ |
| 66 |
'message' => 'Security check failed.', |
| 67 |
], |
| 68 |
); |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
if ( ! isset( $_POST['alertId'] ) ) { |
| 73 |
\wp_send_json_error( |
| 74 |
[ |
| 75 |
'message' => 'Alert ID is missing.', |
| 76 |
], |
| 77 |
); |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
$alert_id = \sanitize_text_field( \wp_unslash( $_POST['alertId'] ) ); |
| 82 |
$user_id = \get_current_user_id(); |
| 83 |
|
| 84 |
$this->user_helper->update_meta( $user_id, $alert_id . '_resolved', true ); |
| 85 |
|
| 86 |
\wp_send_json_success( |
| 87 |
[ |
| 88 |
'message' => 'Alert resolved successfully.', |
| 89 |
], |
| 90 |
); |
| 91 |
} |
| 92 |
} |
| 93 |
|