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