PluginProbe
Code Snippets / 3.10.0
Code Snippets v3.10.0
3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 3.0.1 All 64 releases
code-snippets / php / REST_API / Snippets / Preferences_REST_Controller.php

Preferences_REST_Controller.php in Code Snippets 3.10.0, at php/REST_API/Snippets/Preferences_REST_Controller.php

125 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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