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

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

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