| 1 |
<?php |
| 2 |
/** |
| 3 |
* REST controller for the site-level Schema settings. |
| 4 |
* |
| 5 |
* Exposes {@see Schema_Settings} over a dedicated `jetpack/v4/seo/schema-settings` |
| 6 |
* route rather than `/jetpack/v4/settings`, which rejects the nested schema |
| 7 |
* container. GET returns the editing payload; the write method sanitizes, |
| 8 |
* persists, and returns the new payload. Gated on `manage_options`. |
| 9 |
* |
| 10 |
* @package automattic/jetpack-seo-package |
| 11 |
*/ |
| 12 |
|
| 13 |
namespace Automattic\Jetpack\SEO; |
| 14 |
|
| 15 |
use WP_REST_Request; |
| 16 |
use WP_REST_Server; |
| 17 |
|
| 18 |
/** |
| 19 |
* Registers and serves the Schema settings REST route. |
| 20 |
*/ |
| 21 |
class Schema_Settings_Controller { |
| 22 |
|
| 23 |
/** |
| 24 |
* REST namespace, shared with the package's other Jetpack routes. |
| 25 |
* |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
const REST_NAMESPACE = 'jetpack/v4'; |
| 29 |
|
| 30 |
/** |
| 31 |
* Route, relative to the namespace. |
| 32 |
* |
| 33 |
* @var string |
| 34 |
*/ |
| 35 |
const REST_BASE = '/seo/schema-settings'; |
| 36 |
|
| 37 |
/** |
| 38 |
* Register the GET (read) and write (sanitize + persist) route. |
| 39 |
* |
| 40 |
* @return void |
| 41 |
*/ |
| 42 |
public static function register_routes() { |
| 43 |
register_rest_route( |
| 44 |
self::REST_NAMESPACE, |
| 45 |
self::REST_BASE, |
| 46 |
array( |
| 47 |
array( |
| 48 |
'methods' => WP_REST_Server::READABLE, |
| 49 |
'callback' => array( __CLASS__, 'get_item' ), |
| 50 |
'permission_callback' => array( __CLASS__, 'permissions_check' ), |
| 51 |
), |
| 52 |
array( |
| 53 |
'methods' => WP_REST_Server::EDITABLE, |
| 54 |
'callback' => array( __CLASS__, 'update_item' ), |
| 55 |
'permission_callback' => array( __CLASS__, 'permissions_check' ), |
| 56 |
), |
| 57 |
) |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Only administrators can read or change the site's schema settings. |
| 63 |
* |
| 64 |
* @return bool |
| 65 |
*/ |
| 66 |
public static function permissions_check() { |
| 67 |
return current_user_can( 'manage_options' ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* GET: the editing payload (stored overrides plus placeholder defaults). |
| 72 |
* |
| 73 |
* @return \WP_REST_Response |
| 74 |
*/ |
| 75 |
public static function get_item() { |
| 76 |
return rest_ensure_response( Schema_Settings::get_editable() ); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* POST/PUT: sanitize and persist the submission, then return the new payload. |
| 81 |
* Unknown keys are ignored and omitted schema sections retain their stored values. |
| 82 |
* |
| 83 |
* @param WP_REST_Request $request The REST request. |
| 84 |
* @return \WP_REST_Response |
| 85 |
*/ |
| 86 |
public static function update_item( WP_REST_Request $request ) { |
| 87 |
$params = $request->get_json_params(); |
| 88 |
if ( ! is_array( $params ) ) { |
| 89 |
$params = $request->get_params(); |
| 90 |
} |
| 91 |
|
| 92 |
return rest_ensure_response( Schema_Settings::update( $params ) ); |
| 93 |
} |
| 94 |
} |
| 95 |
|