PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.4
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.4
6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 5.2.2 5.3 All 153 releases
mainwp / includes / rest-api / class-mainwp-rest-server.php

class-mainwp-rest-server.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 5.4, at includes/rest-api/class-mainwp-rest-server.php

127 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Initialize this version of the REST API.
4 *
5 * @package MainWP\Dashboard
6 */
7
8 defined( 'ABSPATH' ) || exit;
9
10 /**
11 * Class responsible for loading the REST API and all REST API namespaces.
12 */
13 class MainWP_Rest_Server {
14
15
16 /**
17 * Protected static variable to hold the single instance of the class.
18 *
19 * @var mixed Default null
20 */
21 private static $instance = null;
22
23
24 /**
25 * REST API namespaces and endpoints.
26 *
27 * @var array
28 */
29 protected $controllers = array();
30
31
32 /**
33 * Method instance()
34 *
35 * Create public static instance.
36 *
37 * @static
38 * @return static::$instance
39 */
40 public static function instance() {
41 if ( null === static::$instance ) {
42 static::$instance = new self();
43 }
44 return static::$instance;
45 }
46
47 /**
48 * Hook into WordPress ready to init the REST API as needed.
49 */
50 public function init() {
51 if ( apply_filters( 'mainwp_rest_api_v2_enabled', false ) ) {
52 add_action( 'rest_api_init', array( $this, 'register_rest_routes' ), 10 );
53 }
54 }
55
56 /**
57 * Register REST API routes.
58 */
59 public function register_rest_routes() {
60 foreach ( $this->get_rest_namespaces() as $namespace => $controllers ) {
61 foreach ( $controllers as $controller_name => $controller_class ) {
62 if ( ! isset( $this->controllers[ $namespace ][ $controller_name ] ) ) {
63 if ( class_exists( $controller_class ) ) {
64 $this->controllers[ $namespace ][ $controller_name ] = new $controller_class();
65 $this->controllers[ $namespace ][ $controller_name ]->register_routes();
66 } else {
67 $this->controllers[ $namespace ][ $controller_name ] = false;
68 }
69 }
70 }
71 }
72 }
73
74 /**
75 * Get registered routes controller.
76 *
77 * @param string $nspace namespace.
78 * @param string $name name.
79 *
80 * @return mixed result.
81 */
82 public function get_rest_controller( $nspace, $name ) {
83 if ( is_array( $this->controllers ) && isset( $this->controllers[ $nspace ][ $name ] ) && isset( $this->controllers[ $nspace ][ $name ] ) ) {
84 return $this->controllers[ $nspace ][ $name ];
85 }
86 return false;
87 }
88
89 /**
90 * Get API namespaces - new namespaces should be registered here.
91 *
92 * @return array List of Namespaces and Main controller classes.
93 */
94 protected function get_rest_namespaces() {
95 return apply_filters(
96 'mainwp_rest_api_get_rest_namespaces',
97 array(
98 'mainwp/v2' => $this->get_v2_controllers(),
99 )
100 );
101 }
102
103 /**
104 * List of controllers in the wc/v2 namespace.
105 *
106 * @return array
107 */
108 protected function get_v2_controllers() {
109 return array(
110 'sites' => 'MainWP_Rest_Sites_Controller',
111 'clients' => 'MainWP_Rest_Clients_Controller',
112 'tags' => 'MainWP_Rest_Tags_Controller',
113 'updates' => 'MainWP_Rest_Updates_Controller',
114 'batch' => 'MainWP_Rest_Global_Batch_Controller',
115 );
116 }
117
118 /**
119 * Return the path to the package.
120 *
121 * @return string
122 */
123 public static function get_path() {
124 return dirname( __DIR__ );
125 }
126 }
127