Yoast\WP\SEO\Dashboard\User_Interface\Configuration
Site_Kit_Consent_Management_Route{} │ Yoast 1.0
Registers a route to set whether the Site Kit configuration is permanently dismissed.
Хуков нет.
Использование
$Site_Kit_Consent_Management_Route = new Site_Kit_Consent_Management_Route();
// use class methods
Методы
- public __construct(
- public check_capabilities()
- public static get_conditionals()
- public register_routes()
- public set_site_kit_consent( WP_REST_Request $request )
Код Site_Kit_Consent_Management_Route{} Site Kit Consent Management Route{}
Yoast 25.1
class Site_Kit_Consent_Management_Route implements Route_Interface {
/**
* The namespace for this route.
*
* @var string
*/
public const ROUTE_NAMESPACE = Main::API_V1_NAMESPACE;
/**
* The prefix for this route.
*
* @var string
*/
public const ROUTE_PREFIX = '/site_kit_manage_consent';
/**
* Holds the repository instance.
*
* @var Site_Kit_Consent_Repository_Interface
*/
private $site_kit_consent_repository;
/**
* Holds the capabilit helper instance.
*
* @var Capability_Helper
*/
private $capability_helper;
/**
* The needed conditionals.
*
* @return array<string>
*/
public static function get_conditionals() {
// This cannot have the Admin Conditional since it also needs to run in Rest requests.
return [ Google_Site_Kit_Feature_Conditional::class, Site_Kit_Conditional::class ];
}
/**
* Constructs the class.
*
* @param Site_Kit_Consent_Repository_Interface $site_kit_consent_repository The repository.
* @param Capability_Helper $capability_helper The capability helper.
*/
public function __construct(
Site_Kit_Consent_Repository_Interface $site_kit_consent_repository,
Capability_Helper $capability_helper
) {
$this->site_kit_consent_repository = $site_kit_consent_repository;
$this->capability_helper = $capability_helper;
}
/**
* Registers routes with WordPress.
*
* @return void
*/
public function register_routes() {
\register_rest_route(
self::ROUTE_NAMESPACE,
self::ROUTE_PREFIX,
[
[
'methods' => 'POST',
'callback' => [ $this, 'set_site_kit_consent' ],
'permission_callback' => [ $this, 'check_capabilities' ],
'args' => [
'consent' => [
'required' => true,
'type' => 'bool',
'sanitize_callback' => 'rest_sanitize_boolean',
],
],
],
]
);
}
/**
* Sets whether the Site Kit configuration is permanently dismissed.
*
* @param WP_REST_Request $request The request object.
*
* @return WP_REST_Response|WP_Error The success or failure response.
*/
public function set_site_kit_consent( WP_REST_Request $request ) {
$consent = $request->get_param( 'consent' );
try {
$result = $this->site_kit_consent_repository->set_site_kit_consent( $consent );
} catch ( Exception $exception ) {
return new WP_Error(
'wpseo_set_site_kit_consent_error',
$exception->getMessage(),
(object) []
);
}
return new WP_REST_Response(
[
'success' => $result,
],
( $result ) ? 200 : 400
);
}
/**
* Checks if the current user has the required capabilities.
*
* @return bool
*/
public function check_capabilities() {
return $this->capability_helper->current_user_can( 'wpseo_manage_options' );
}
}