wordpress-seo
/
src
/
dashboard
/
user-interface
/
configuration
/
site-kit-configuration-dismissal-route.php
site-kit-configuration-dismissal-route.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.6, at src/dashboard/user-interface/configuration/site-kit-configuration-dismissal-route.php
| 1 | <?php |
| 2 | |
| 3 | // phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 4 | namespace Yoast\WP\SEO\Dashboard\User_Interface\Configuration; |
| 5 | |
| 6 | use Exception; |
| 7 | use WP_Error; |
| 8 | use WP_REST_Request; |
| 9 | use WP_REST_Response; |
| 10 | use Yoast\WP\SEO\Conditionals\No_Conditionals; |
| 11 | use Yoast\WP\SEO\Dashboard\Infrastructure\Configuration\Permanently_Dismissed_Site_Kit_Configuration_Repository_Interface; |
| 12 | use Yoast\WP\SEO\Helpers\Capability_Helper; |
| 13 | use Yoast\WP\SEO\Main; |
| 14 | use Yoast\WP\SEO\Routes\Route_Interface; |
| 15 | |
| 16 | /** |
| 17 | * Registers a route to set whether the Site Kit configuration is permanently dismissed. |
| 18 | * |
| 19 | * @makePublic |
| 20 | * |
| 21 | * @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded |
| 22 | */ |
| 23 | class Site_Kit_Configuration_Dismissal_Route implements Route_Interface { |
| 24 | |
| 25 | use No_Conditionals; |
| 26 | |
| 27 | /** |
| 28 | * The namespace for this route. |
| 29 | * |
| 30 | * @var string |
| 31 | */ |
| 32 | public const ROUTE_NAMESPACE = Main::API_V1_NAMESPACE; |
| 33 | |
| 34 | /** |
| 35 | * The prefix for this route. |
| 36 | * |
| 37 | * @var string |
| 38 | */ |
| 39 | public const ROUTE_PREFIX = '/site_kit_configuration_permanent_dismissal'; |
| 40 | |
| 41 | /** |
| 42 | * Holds the introductions collector instance. |
| 43 | * |
| 44 | * @var Permanently_Dismissed_Site_Kit_Configuration_Repository_Interface |
| 45 | */ |
| 46 | private $permanently_dismissed_site_kit_configuration_repository; |
| 47 | |
| 48 | /** |
| 49 | * Holds the capabilit helper instance. |
| 50 | * |
| 51 | * @var Capability_Helper |
| 52 | */ |
| 53 | private $capability_helper; |
| 54 | |
| 55 | /** |
| 56 | * Constructs the class. |
| 57 | * |
| 58 | * @param Permanently_Dismissed_Site_Kit_Configuration_Repository_Interface $permanently_dismissed_site_kit_configuration_repository The repository. |
| 59 | * @param Capability_Helper $capability_helper The capability helper. |
| 60 | */ |
| 61 | public function __construct( |
| 62 | Permanently_Dismissed_Site_Kit_Configuration_Repository_Interface $permanently_dismissed_site_kit_configuration_repository, |
| 63 | Capability_Helper $capability_helper |
| 64 | ) { |
| 65 | $this->permanently_dismissed_site_kit_configuration_repository = $permanently_dismissed_site_kit_configuration_repository; |
| 66 | $this->capability_helper = $capability_helper; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Registers routes with WordPress. |
| 71 | * |
| 72 | * @return void |
| 73 | */ |
| 74 | public function register_routes() { |
| 75 | \register_rest_route( |
| 76 | self::ROUTE_NAMESPACE, |
| 77 | self::ROUTE_PREFIX, |
| 78 | [ |
| 79 | [ |
| 80 | 'methods' => 'POST', |
| 81 | 'callback' => [ $this, 'set_site_kit_configuration_permanent_dismissal' ], |
| 82 | 'permission_callback' => [ $this, 'check_capabilities' ], |
| 83 | 'args' => [ |
| 84 | 'is_dismissed' => [ |
| 85 | 'required' => true, |
| 86 | 'type' => 'bool', |
| 87 | 'sanitize_callback' => 'rest_sanitize_boolean', |
| 88 | ], |
| 89 | ], |
| 90 | ], |
| 91 | ], |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Sets whether the Site Kit configuration is permanently dismissed. |
| 97 | * |
| 98 | * @param WP_REST_Request $request The request object. |
| 99 | * |
| 100 | * @return WP_REST_Response|WP_Error The success or failure response. |
| 101 | */ |
| 102 | public function set_site_kit_configuration_permanent_dismissal( WP_REST_Request $request ) { |
| 103 | $is_dismissed = $request->get_param( 'is_dismissed' ); |
| 104 | |
| 105 | try { |
| 106 | $result = $this->permanently_dismissed_site_kit_configuration_repository->set_site_kit_configuration_dismissal( $is_dismissed ); |
| 107 | } catch ( Exception $exception ) { |
| 108 | return new WP_Error( |
| 109 | 'wpseo_set_site_kit_configuration_permanent_dismissal_error', |
| 110 | $exception->getMessage(), |
| 111 | (object) [], |
| 112 | ); |
| 113 | } |
| 114 | |
| 115 | return new WP_REST_Response( |
| 116 | [ |
| 117 | 'success' => $result, |
| 118 | ], |
| 119 | ( $result ) ? 200 : 400, |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Checks if the current user has the required capabilities. |
| 125 | * |
| 126 | * @return bool |
| 127 | */ |
| 128 | public function check_capabilities() { |
| 129 | return $this->capability_helper->current_user_can( 'wpseo_manage_options' ); |
| 130 | } |
| 131 | } |
| 132 |