PluginProbe
Code Snippets / 4.0.0-beta.1
Code Snippets v4.0.0-beta.1
4.0.0-beta.2 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 All 65 releases
code-snippets / php / REST_API / Snippets / Recently_Active_REST_Controller.php

Recently_Active_REST_Controller.php in Code Snippets 4.0.0-beta.1, at php/REST_API/Snippets/Recently_Active_REST_Controller.php

111 lines 3.1 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\Utils\delete_self_option;
10 use function Code_Snippets\Utils\get_self_option;
11
12 /**
13 * Controller for fetching and clearing list of recently active snippets.
14 *
15 * @package Code_Snippets
16 */
17 final class Recently_Active_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 = 'recently-active';
28
29 /**
30 * The name of the option used to store the recently active snippets list.
31 */
32 private const OPTION_NAME = 'recently_active_snippets';
33
34 /**
35 * Register REST routes.
36 */
37 public function register_routes() {
38 register_rest_route(
39 $this->namespace,
40 self::BASE_ROUTE,
41 [
42 [
43 'methods' => WP_REST_Server::READABLE,
44 'callback' => [ $this, 'get_recent_list_callback' ],
45 'permission_callback' => [ $this, 'permission_callback' ],
46 'args' => [
47 'network' => [
48 'description' => esc_html__( 'Fetch the recent list for network-wide snippets instead of site-wide.', 'code-snippets' ),
49 'type' => 'boolean',
50 'default' => false,
51 ],
52 ],
53 ],
54 ]
55 );
56
57 register_rest_route(
58 $this->namespace,
59 self::BASE_ROUTE,
60 [
61 [
62 'methods' => WP_REST_Server::DELETABLE,
63 'callback' => [ $this, 'clear_recent_list_callback' ],
64 'permission_callback' => [ $this, 'permission_callback' ],
65 'args' => [
66 'network' => [
67 'description' => esc_html__( 'Clear the recent list for network-wide snippets instead of site-wide.', 'code-snippets' ),
68 'type' => 'boolean',
69 'default' => false,
70 ],
71 ],
72 ],
73 ]
74 );
75 }
76
77 /**
78 * Callback for retrieving the recently active snippets list.
79 *
80 * This will return the list of recently active snippets, either site-wide or network-wide,
81 * depending on the 'network' parameter.
82 *
83 * @param WP_REST_Request $request The REST request object.
84 *
85 * @return WP_REST_Response The recently active snippets list.
86 */
87 public function get_recent_list_callback( WP_REST_Request $request ): WP_REST_Response {
88 $network = boolval( $request->get_param( 'network' ) );
89 return rest_ensure_response( get_self_option( $network, self::OPTION_NAME, [] ) );
90 }
91
92 /**
93 * Callback for clearing the recently active snippets list.
94 *
95 * This will clear the list of recently active snippets, either site-wide or network-wide,
96 * depending on the 'network' parameter.
97 *
98 * @param WP_REST_Request $request The REST request object.
99 *
100 * @return WP_REST_Response The recently active snippets list prior to clearing it.
101 */
102 public function clear_recent_list_callback( WP_REST_Request $request ): WP_REST_Response {
103 $network = boolval( $request->get_param( 'network' ) );
104
105 $current = get_self_option( $network, self::OPTION_NAME, [] );
106 delete_self_option( $network, self::OPTION_NAME );
107
108 return rest_ensure_response( $current );
109 }
110 }
111