| 1 |
<?php |
| 2 |
|
| 3 |
namespace Code_Snippets\REST_API\Preferences; |
| 4 |
|
| 5 |
use Code_Snippets\REST_API\REST_Controller; |
| 6 |
use WP_REST_Request; |
| 7 |
use WP_REST_Response; |
| 8 |
use WP_REST_Server; |
| 9 |
use function Code_Snippets\code_snippets; |
| 10 |
use const Code_Snippets\REST_API_NAMESPACE; |
| 11 |
|
| 12 |
/** |
| 13 |
* Controller for reading and updating a plugin-wide interface preference. |
| 14 |
* |
| 15 |
* @package Code_Snippets |
| 16 |
*/ |
| 17 |
abstract class Preference_REST_Controller extends REST_Controller { |
| 18 |
|
| 19 |
/** |
| 20 |
* Current API version. |
| 21 |
*/ |
| 22 |
public const VERSION = 0; |
| 23 |
|
| 24 |
public const BASE_ROUTE_PREFIX = 'preferences/'; |
| 25 |
|
| 26 |
/** |
| 27 |
* The key used to identify this preference in the REST API. |
| 28 |
*/ |
| 29 |
protected const PREFERENCE_KEY = ''; |
| 30 |
|
| 31 |
/** |
| 32 |
* The name of the option used to store the preference. |
| 33 |
*/ |
| 34 |
protected const OPTION_NAME = ''; |
| 35 |
|
| 36 |
/** |
| 37 |
* Class constructor. |
| 38 |
*/ |
| 39 |
public function __construct() { |
| 40 |
parent::__construct(); |
| 41 |
assert( ! empty( static::PREFERENCE_KEY ), get_class( $this ) . '::PREFERENCE_KEY must be set' ); |
| 42 |
assert( ! empty( static::OPTION_NAME ), get_class( $this ) . '::OPTION_NAME must be set' ); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
/** |
| 47 |
* Retrieve this controller's REST API base path, including namespace. |
| 48 |
* |
| 49 |
* @return string |
| 50 |
*/ |
| 51 |
public static function get_base_route(): string { |
| 52 |
return REST_API_NAMESPACE . static::VERSION . '/' . self::BASE_ROUTE_PREFIX . static::BASE_ROUTE; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Register REST routes. |
| 57 |
*/ |
| 58 |
public function register_routes() { |
| 59 |
register_rest_route( |
| 60 |
$this->namespace, |
| 61 |
self::BASE_ROUTE_PREFIX . static::BASE_ROUTE, |
| 62 |
[ |
| 63 |
[ |
| 64 |
'methods' => WP_REST_Server::READABLE, |
| 65 |
'callback' => [ $this, 'get_option_value_callback' ], |
| 66 |
'permission_callback' => [ $this, 'permission_callback' ], |
| 67 |
], |
| 68 |
[ |
| 69 |
'methods' => WP_REST_Server::EDITABLE, |
| 70 |
'callback' => [ $this, 'update_option_value_callback' ], |
| 71 |
'permission_callback' => [ $this, 'permission_callback' ], |
| 72 |
'args' => [ |
| 73 |
static::PREFERENCE_KEY => $this->get_update_request_schema(), |
| 74 |
], |
| 75 |
], |
| 76 |
] |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Retrieve the schema for the preference argument, to be used in the REST API documentation. |
| 82 |
* |
| 83 |
* @return array |
| 84 |
*/ |
| 85 |
abstract protected function get_update_request_schema(): array; |
| 86 |
|
| 87 |
/** |
| 88 |
* Determine whether the request has permission to manage preferences. |
| 89 |
* |
| 90 |
* @param WP_REST_Request $request Full data about the request. |
| 91 |
* |
| 92 |
* @return bool |
| 93 |
*/ |
| 94 |
public function permission_callback( WP_REST_Request $request ): bool { |
| 95 |
return code_snippets()->current_user_can(); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Retrieve the stored preference value, falling back to the default when the |
| 100 |
* stored value is missing or invalid. |
| 101 |
* |
| 102 |
* @return mixed. |
| 103 |
*/ |
| 104 |
abstract protected function get_option_value(); |
| 105 |
|
| 106 |
/** |
| 107 |
* Retrieve the stored preference value as a REST response. |
| 108 |
* |
| 109 |
* @return WP_REST_Response |
| 110 |
*/ |
| 111 |
public function get_option_value_callback(): WP_REST_Response { |
| 112 |
return new WP_REST_Response( [ static::PREFERENCE_KEY => static::get_option_value() ] ); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Update the preference value. |
| 117 |
* |
| 118 |
* @param WP_REST_Request $request Full data about the request. |
| 119 |
* |
| 120 |
* @return WP_REST_Response |
| 121 |
*/ |
| 122 |
public function update_option_value_callback( WP_REST_Request $request ): WP_REST_Response { |
| 123 |
$value = $request->get_param( static::PREFERENCE_KEY ); |
| 124 |
|
| 125 |
update_option( static::OPTION_NAME, $value ); |
| 126 |
|
| 127 |
return new WP_REST_Response( [ static::PREFERENCE_KEY => $value ] ); |
| 128 |
} |
| 129 |
} |
| 130 |
|