| 1 |
<?php |
| 2 |
|
| 3 |
namespace Code_Snippets\REST_API\Snippets; |
| 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 |
|
| 11 |
/** |
| 12 |
* Controller for reading and updating plugin-wide interface preferences, |
| 13 |
* such as whether snippet lists display as a table or a grid of cards. |
| 14 |
* |
| 15 |
* @package Code_Snippets |
| 16 |
*/ |
| 17 |
final class Preferences_REST_Controller extends REST_Controller { |
| 18 |
|
| 19 |
/** |
| 20 |
* Current API version. |
| 21 |
*/ |
| 22 |
public const VERSION = 1; |
| 23 |
|
| 24 |
/** |
| 25 |
* The base of this controller's route. |
| 26 |
*/ |
| 27 |
public const BASE_ROUTE = 'preferences'; |
| 28 |
|
| 29 |
/** |
| 30 |
* The name of the option used to store the snippet view preference. |
| 31 |
*/ |
| 32 |
public const SNIPPET_VIEW_OPTION = 'code_snippets_snippet_view'; |
| 33 |
|
| 34 |
/** |
| 35 |
* Valid snippet view values. |
| 36 |
*/ |
| 37 |
public const SNIPPET_VIEWS = [ 'card', 'table' ]; |
| 38 |
|
| 39 |
/** |
| 40 |
* The snippet view shown when no preference has been saved. |
| 41 |
*/ |
| 42 |
public const DEFAULT_SNIPPET_VIEW = 'table'; |
| 43 |
|
| 44 |
/** |
| 45 |
* Retrieve the current snippet view preference, falling back to the |
| 46 |
* default when the stored value is missing or invalid. |
| 47 |
* |
| 48 |
* @return string Either 'card' or 'table'. |
| 49 |
*/ |
| 50 |
public static function get_snippet_view(): string { |
| 51 |
$view = get_option( self::SNIPPET_VIEW_OPTION, self::DEFAULT_SNIPPET_VIEW ); |
| 52 |
|
| 53 |
return in_array( $view, self::SNIPPET_VIEWS, true ) |
| 54 |
? $view |
| 55 |
: self::DEFAULT_SNIPPET_VIEW; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Register REST routes. |
| 60 |
*/ |
| 61 |
public function register_routes() { |
| 62 |
register_rest_route( |
| 63 |
$this->namespace, |
| 64 |
self::BASE_ROUTE . '/snippet-view', |
| 65 |
[ |
| 66 |
[ |
| 67 |
'methods' => WP_REST_Server::READABLE, |
| 68 |
'callback' => [ $this, 'get_snippet_view_callback' ], |
| 69 |
'permission_callback' => [ $this, 'permission_callback' ], |
| 70 |
], |
| 71 |
[ |
| 72 |
'methods' => WP_REST_Server::EDITABLE, |
| 73 |
'callback' => [ $this, 'update_snippet_view_callback' ], |
| 74 |
'permission_callback' => [ $this, 'permission_callback' ], |
| 75 |
'args' => [ |
| 76 |
'view' => [ |
| 77 |
'description' => esc_html__( 'Whether snippet lists display as a grid of cards or a table.', 'code-snippets' ), |
| 78 |
'type' => 'string', |
| 79 |
'enum' => self::SNIPPET_VIEWS, |
| 80 |
'required' => true, |
| 81 |
], |
| 82 |
], |
| 83 |
], |
| 84 |
] |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Determine whether the request has permission to manage preferences. |
| 90 |
* |
| 91 |
* @param WP_REST_Request $request Full data about the request. |
| 92 |
* |
| 93 |
* @return bool |
| 94 |
*/ |
| 95 |
public function permission_callback( WP_REST_Request $request ): bool { |
| 96 |
return code_snippets()->current_user_can(); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Retrieve the current snippet view preference. |
| 101 |
* |
| 102 |
* @param WP_REST_Request $request Full data about the request. |
| 103 |
* |
| 104 |
* @return WP_REST_Response |
| 105 |
*/ |
| 106 |
public function get_snippet_view_callback( WP_REST_Request $request ): WP_REST_Response { |
| 107 |
return new WP_REST_Response( [ 'view' => self::get_snippet_view() ] ); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Update the snippet view preference. |
| 112 |
* |
| 113 |
* @param WP_REST_Request $request Full data about the request. |
| 114 |
* |
| 115 |
* @return WP_REST_Response |
| 116 |
*/ |
| 117 |
public function update_snippet_view_callback( WP_REST_Request $request ): WP_REST_Response { |
| 118 |
$view = $request->get_param( 'view' ); |
| 119 |
|
| 120 |
update_option( self::SNIPPET_VIEW_OPTION, $view ); |
| 121 |
|
| 122 |
return new WP_REST_Response( [ 'view' => $view ] ); |
| 123 |
} |
| 124 |
} |
| 125 |
|