-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-multisite-api.php
123 lines (109 loc) · 4.04 KB
/
class-multisite-api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/**
* Plugin Name: Ici On Drive - Multisite API
* Description: Create endpoint to the WordPress REST API for multisite. Plugin developed for the "Ici On Drive" platform.
* Plugin URI: https://github.com/iciondrive/multisite-api
* Author: Thomas Navarro
* Version: 1.0.0
* Network: true
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Author URI: http://github.com/thomasnavarro.
*/
namespace IciOnDrive;
if (!defined('ABSPATH')) {
exit;
}
if (!class_exists('Multisite_API')) {
class Multisite_API
{
/**
* @var string namespace
*/
private const NAMESPACE = 'iciondrive/v1';
/**
* @var string rest_base
*/
private $rest_base = 'sites';
public function __construct()
{
// WP hooks
add_action('init', [$this, 'init_hook']);
$this->get_sites = new Get_Sites();
$this->get_site = new Get_Site();
}
public function init_hook()
{
add_action('rest_api_init', [$this, 'register_rest_route']);
}
public function register_rest_route()
{
// Get all sites
register_rest_route(
self::NAMESPACE, '/'.$this->rest_base,
[
'methods' => \WP_REST_Server::READABLE,
'callback' => [$this->get_sites, 'callback'],
'permission_callback' => '__return_true',
'args' => [
'page' => [
'description' => __('Current page of the result.'),
'type' => 'integer',
'default' => 1,
],
'per_page' => [
'description' => __('Maximum number of items to be returned in result set.'),
'type' => 'integer',
'default' => 10,
],
'order' => [
'description' => __('How to order retrieved sites. Accepts "ASC", "DESC".'),
'type' => 'string',
'default' => 'ASC',
],
'fields' => [
'default' => true,
'type' => 'boolean',
'description' => __('Retrieves the ACF fields from the site.'),
],
],
'schema' => [$this, 'get_public_item_schema'],
]
);
// Get site by blog ID
register_rest_route(
self::NAMESPACE, '/'.$this->rest_base.'/(?P<id>[\d]+)',
[
'methods' => \WP_REST_Server::READABLE,
'callback' => [$this->get_site, 'callback'],
'permission_callback' => '__return_true',
'args' => [
'id' => [
'description' => __('Unique identifier for the object.'),
'type' => 'integer',
],
'fields' => [
'default' => true,
'type' => 'boolean',
'description' => __('Retrieves the ACF fields from the site.'),
],
],
'schema' => [$this, 'get_public_item_schema'],
]
);
}
public function get_item_permissions_check()
{
global $wp_version;
if (version_compare($wp_version, '4.8', '>=')) {
return current_user_can('setup_network');
}
return is_admin();
}
}
// Includes
require_once dirname(__FILE__).'/class-multisite-get-sites.php';
require_once dirname(__FILE__).'/class-multisite-get-site.php';
// Instantiate
new Multisite_API();
}