| 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 |
'monitors' => 'MainWP_Rest_Monitors_Controller', |
| 116 |
'rest_api_keys' => 'MainWP_Rest_API_Keys_Controller', |
| 117 |
'settings' => 'MainWP_Rest_Settings_Controller', |
| 118 |
'posts' => 'MainWP_Rest_Posts_Controller', |
| 119 |
'pages' => 'MainWP_Rest_Pages_Controller', |
| 120 |
'users' => 'MainWP_Rest_Users_Controller', |
| 121 |
); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Return the path to the package. |
| 126 |
* |
| 127 |
* @return string |
| 128 |
*/ |
| 129 |
public static function get_path() { |
| 130 |
return dirname( __DIR__ ); |
| 131 |
} |
| 132 |
} |
| 133 |
|