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 / Import / Plugins_Import_REST_Controller.php

Plugins_Import_REST_Controller.php in Code Snippets 3.10.0, at php/REST_API/Import/Plugins_Import_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\Import;
4
5 use Code_Snippets\REST_API\Import\Plugins\Header_Footer_Code_Manager_Plugin_Importer;
6 use Code_Snippets\REST_API\Import\Plugins\Insert_Headers_And_Footers_Plugin_Importer;
7 use Code_Snippets\REST_API\Import\Plugins\Insert_PHP_Code_Snippet_Plugin_Importer;
8 use Code_Snippets\REST_API\Import\Plugins\Plugin_Importer;
9 use Code_Snippets\REST_API\REST_Controller;
10 use WP_REST_Request;
11 use WP_REST_Response;
12 use WP_REST_Server;
13 use function Code_Snippets\code_snippets;
14
15 /**
16 * REST API controller for plugin importers.
17 *
18 * Handles registering REST API routes and providing information about available plugin importers.
19 */
20 class Plugins_Import_REST_Controller extends REST_Controller {
21
22 /**
23 * Current API version.
24 */
25 public const VERSION = 1;
26
27 /**
28 * Base route for this controller.
29 */
30 public const BASE_ROUTE = 'import/plugins';
31
32 /**
33 * List of plugin importer instances.
34 *
35 * @var array Plugin_Importer[]
36 */
37 private array $plugin_importers;
38
39 /**
40 * Class constructor.
41 */
42 public function __construct() {
43 parent::__construct();
44
45 $this->plugin_importers = [
46 'insert-headers-and-footers' => new Insert_Headers_And_Footers_Plugin_Importer(),
47 'header-footer-code-manager' => new Header_Footer_Code_Manager_Plugin_Importer(),
48 'insert-php-code-snippet' => new Insert_PHP_Code_Snippet_Plugin_Importer(),
49 ];
50 }
51
52 /**
53 * Get the list of available plugin importers.
54 *
55 * @return WP_REST_Response List of importers with their name, title, and active status.
56 */
57 public function get_importer_details(): WP_REST_Response {
58 return rest_ensure_response(
59 array_map(
60 function ( $importer ) {
61 return [
62 'name' => $importer->get_name(),
63 'title' => $importer->get_title(),
64 'is_active' => $importer::is_active(),
65 ];
66 },
67 $this->plugin_importers
68 )
69 );
70 }
71
72 /**
73 * Register REST API route for fetching available importers.
74 *
75 * @return void
76 */
77 public function register_routes() {
78 register_rest_route(
79 $this->namespace,
80 self::BASE_ROUTE,
81 [
82 'methods' => WP_REST_Server::READABLE,
83 'callback' => [ $this, 'get_importer_details' ],
84 'permission_callback' => [ $this, 'permission_callback' ],
85 ]
86 );
87
88 foreach ( $this->plugin_importers as $importer ) {
89 $route = self::BASE_ROUTE . '/' . $importer->get_name();
90
91 register_rest_route(
92 $this->namespace,
93 $route,
94 [
95 'methods' => WP_REST_Server::READABLE,
96 'callback' => [ $importer, 'get_items' ],
97 'permission_callback' => [ $this, 'permission_callback' ],
98 ]
99 );
100
101 register_rest_route(
102 $this->namespace,
103 "$route/import",
104 [
105 'methods' => WP_REST_Server::CREATABLE,
106 'callback' => [ $importer, 'import' ],
107 'permission_callback' => [ $this, 'permission_callback' ],
108 'args' => Plugin_Importer::REST_ARGS,
109 ]
110 );
111 }
112 }
113
114 /**
115 * Determine whether the request has permission to import snippets.
116 *
117 * @param WP_REST_Request $request Incoming HTTP request.
118 *
119 * @return bool
120 */
121 public function permission_callback( WP_REST_Request $request ): bool {
122 return code_snippets()->current_user_can();
123 }
124 }
125