PluginProbe
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution / 1.5.0
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution v1.5.0
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 1.5.0, at src/Performance/API/Provider.php

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