wordpress-seo
/
src
/
dashboard
/
user-interface
/
configuration
/
site-kit-consent-management-route.php
site-kit-consent-management-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-consent-management-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\Third_Party\Site_Kit_Conditional; |
| 11 | use Yoast\WP\SEO\Dashboard\Infrastructure\Configuration\Site_Kit_Consent_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_Consent_Management_Route implements Route_Interface { |
| 24 | |
| 25 | /** |
| 26 | * The namespace for this route. |
| 27 | * |
| 28 | * @var string |
| 29 | */ |
| 30 | public const ROUTE_NAMESPACE = Main::API_V1_NAMESPACE; |
| 31 | |
| 32 | /** |
| 33 | * The prefix for this route. |
| 34 | * |
| 35 | * @var string |
| 36 | */ |
| 37 | public const ROUTE_PREFIX = '/site_kit_manage_consent'; |
| 38 | |
| 39 | /** |
| 40 | * Holds the repository instance. |
| 41 | * |
| 42 | * @var Site_Kit_Consent_Repository_Interface |
| 43 | */ |
| 44 | private $site_kit_consent_repository; |
| 45 | |
| 46 | /** |
| 47 | * Holds the capabilit helper instance. |
| 48 | * |
| 49 | * @var Capability_Helper |
| 50 | */ |
| 51 | private $capability_helper; |
| 52 | |
| 53 | /** |
| 54 | * The needed conditionals. |
| 55 | * |
| 56 | * @return array<string> |
| 57 | */ |
| 58 | public static function get_conditionals() { |
| 59 | // This cannot have the Admin Conditional since it also needs to run in Rest requests. |
| 60 | return [ Site_Kit_Conditional::class ]; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Constructs the class. |
| 65 | * |
| 66 | * @param Site_Kit_Consent_Repository_Interface $site_kit_consent_repository The repository. |
| 67 | * @param Capability_Helper $capability_helper The capability helper. |
| 68 | */ |
| 69 | public function __construct( |
| 70 | Site_Kit_Consent_Repository_Interface $site_kit_consent_repository, |
| 71 | Capability_Helper $capability_helper |
| 72 | ) { |
| 73 | $this->site_kit_consent_repository = $site_kit_consent_repository; |
| 74 | $this->capability_helper = $capability_helper; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Registers routes with WordPress. |
| 79 | * |
| 80 | * @return void |
| 81 | */ |
| 82 | public function register_routes() { |
| 83 | \register_rest_route( |
| 84 | self::ROUTE_NAMESPACE, |
| 85 | self::ROUTE_PREFIX, |
| 86 | [ |
| 87 | [ |
| 88 | 'methods' => 'POST', |
| 89 | 'callback' => [ $this, 'set_site_kit_consent' ], |
| 90 | 'permission_callback' => [ $this, 'check_capabilities' ], |
| 91 | 'args' => [ |
| 92 | 'consent' => [ |
| 93 | 'required' => true, |
| 94 | 'type' => 'bool', |
| 95 | 'sanitize_callback' => 'rest_sanitize_boolean', |
| 96 | ], |
| 97 | ], |
| 98 | ], |
| 99 | ], |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Sets whether the Site Kit configuration is permanently dismissed. |
| 105 | * |
| 106 | * @param WP_REST_Request $request The request object. |
| 107 | * |
| 108 | * @return WP_REST_Response|WP_Error The success or failure response. |
| 109 | */ |
| 110 | public function set_site_kit_consent( WP_REST_Request $request ) { |
| 111 | $consent = $request->get_param( 'consent' ); |
| 112 | |
| 113 | try { |
| 114 | $result = $this->site_kit_consent_repository->set_site_kit_consent( $consent ); |
| 115 | } catch ( Exception $exception ) { |
| 116 | return new WP_Error( |
| 117 | 'wpseo_set_site_kit_consent_error', |
| 118 | $exception->getMessage(), |
| 119 | (object) [], |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | return new WP_REST_Response( |
| 124 | [ |
| 125 | 'success' => $result, |
| 126 | ], |
| 127 | ( $result ) ? 200 : 400, |
| 128 | ); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Checks if the current user has the required capabilities. |
| 133 | * |
| 134 | * @return bool |
| 135 | */ |
| 136 | public function check_capabilities() { |
| 137 | return $this->capability_helper->current_user_can( 'wpseo_manage_options' ); |
| 138 | } |
| 139 | } |
| 140 |