PluginProbe
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution / trunk
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution vtrunk
2.0.1 trunk 1.0.0 1.1.0 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.5.0 1.6.0 1.6.1 1.7.0 1.7.1 1.8.0 1.9.0 2.0.0
solid-performance / src / Performance / API / Provider.php

Provider.php in Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution trunk, at src/Performance/API/Provider.php

125 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The provider responsible for registering API classes with the container & WordPress.
4 *
5 * @since 0.1.0
6 *
7 * @package SolidWP\Performance
8 */
9
10 namespace SolidWP\Performance\API;
11
12 use SolidWP\Performance\API\Routes\Page_Cache\Cache_Count;
13 use SolidWP\Performance\API\Routes\Page_Cache\Cache_Delivery;
14 use SolidWP\Performance\API\Routes\Page_Cache\Clear;
15 use SolidWP\Performance\API\Routes\Page_Cache\Debug;
16 use SolidWP\Performance\API\Routes\Page_Cache\Nginx;
17 use SolidWP\Performance\API\Routes\Page_Cache\Off;
18 use SolidWP\Performance\API\Routes\Page_Cache\On;
19 use SolidWP\Performance\API\Routes\Page_Cache\Preload;
20 use SolidWP\Performance\API\Routes\Page_Cache\Htaccess;
21 use SolidWP\Performance\API\Routes\Page_Cache\Status;
22 use SolidWP\Performance\API\Routes\Page_Cache\Regenerate;
23 use SolidWP\Performance\Contracts\Service_Provider;
24
25 if ( ! defined( 'ABSPATH' ) ) {
26 exit;
27 }
28
29 /**
30 * The provider responsible for registering API classes with the container & WordPress.
31 *
32 * @since 0.1.0
33 *
34 * @package SolidWP\Performance
35 */
36 class Provider extends Service_Provider {
37
38 /**
39 * The namespace of the plugin's REST API routes.
40 *
41 * @since 0.1.0
42 *
43 * @var string
44 */
45 private string $namespace = 'solid-performance/v1';
46
47 /**
48 * All the endpoints that should be registered with the REST API.
49 *
50 * @since 0.1.0
51 *
52 * @var array<int,string>
53 */
54 private array $endpoints = [
55 Clear::class,
56 Debug::class,
57 Off::class,
58 On::class,
59 Regenerate::class,
60 Status::class,
61 Cache_Count::class,
62 Preload::class,
63 Htaccess::class,
64 Nginx::class,
65 Cache_Delivery::class,
66 ];
67
68 /**
69 * {@inheritdoc}
70 */
71 public function register(): void {
72 add_action( 'rest_api_init', [ $this, 'register_all_endpoints' ] );
73 }
74
75 /**
76 * Registers all endpoints of the REST API.
77 *
78 * @since 0.1.0
79 *
80 * @return void
81 */
82 public function register_all_endpoints(): void {
83 foreach ( $this->endpoints as $class ) {
84 /** @var Route $endpoint */
85 $endpoint = $this->container->get( $class );
86 $methods = $endpoint->get_methods();
87
88 if ( is_array( $methods ) ) {
89 foreach ( $methods as $method ) {
90 $args = $endpoint->get_arguments();
91 $args = $args[ $method ] ?? [];
92
93 register_rest_route(
94 $this->namespace,
95 $endpoint->get_path(),
96 [
97 [
98 'methods' => $method,
99 'callback' => [ $endpoint, 'callback' ],
100 'args' => $args,
101 'permission_callback' => [ $endpoint, 'permission_callback' ],
102 ],
103 'schema' => [ $endpoint, 'schema_callback' ],
104 ],
105 );
106 }
107 } else {
108 register_rest_route(
109 $this->namespace,
110 $endpoint->get_path(),
111 [
112 [
113 'methods' => $endpoint->get_methods(),
114 'callback' => [ $endpoint, 'callback' ],
115 'args' => $endpoint->get_arguments(),
116 'permission_callback' => [ $endpoint, 'permission_callback' ],
117 ],
118 'schema' => [ $endpoint, 'schema_callback' ],
119 ],
120 );
121 }
122 }
123 }
124 }
125