Skip to content

Commit 1488303

Browse files
committed
wip: initial commit
0 parents  commit 1488303

6 files changed

+215
-0
lines changed

.editorconfig

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# For more information about the properties used in
2+
# this file, please see the EditorConfig documentation:
3+
# http://editorconfig.org/
4+
5+
root = true
6+
7+
[*]
8+
charset = utf-8
9+
end_of_line = lf
10+
insert_final_newline = true
11+
trim_trailing_whitespace = true
12+
indent_size = 4
13+
indent_style = space
14+
15+
[*.{php,js,html,css,scss,xml,phtml,twig,json}]
16+
indent_size = 4
17+
18+
[*.md]
19+
trim_trailing_whitespace = false
20+
21+
[*.bat]
22+
end_of_line = crlf

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Ici On Drive - Multisite API
2+
3+
Create endpoint to the WordPress REST API for multisite. Plugin developed for the "Ici On Drive" platform.
4+
5+
## Requirements
6+
7+
This plugin requires [API Rest enabled](https://developer.wordpress.org/rest-api/), [Multisite WordPress enabled](https://codex.wordpress.org/fr:Cr%C3%A9er_un_r%C3%A9seau)
8+
9+
## Included
10+
11+
- Create endpoint: get all sites `http://example.com/wp-json/sites`
12+
- Create endpoint: get site by blog_id `http://example.com/wp-json/sites/<id>`
13+
- Added some filters to customize the data of your request.

class-multisite-api.php

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
/**
3+
* Plugin Name: Ici On Drive - Multisite API
4+
* Description: Create endpoint to the WordPress REST API for multisite. Plugin developed for the "Ici On Drive" platform.
5+
* Plugin URI: https://github.com/iciondrive/multisite-api
6+
* Author: Thomas Navarro
7+
* Version: 1.0.0
8+
* Network: true
9+
* License: GPL v2 or later
10+
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
11+
* Author URI: http://github.com/thomasnavarro.
12+
*/
13+
if (!defined('ABSPATH')) {
14+
exit;
15+
}
16+
17+
if (!class_exists('IOD_Multisite_API')) {
18+
class IOD_Multisite_API
19+
{
20+
/**
21+
* @var string namespace
22+
*/
23+
private const NAMESPACE = 'iciondrive/v1';
24+
25+
/**
26+
* @var string rest_base
27+
*/
28+
private $rest_base = 'sites';
29+
30+
public function __construct()
31+
{
32+
// WP hooks
33+
add_action('init', [$this, 'init_hook']);
34+
35+
$this->get_sites = new IOD_Multisite_API_Get_Sites();
36+
$this->get_site = new IOD_Multisite_API_Get_Site();
37+
}
38+
39+
public function init_hook()
40+
{
41+
add_action('rest_api_init', [$this, 'register_rest_route']);
42+
}
43+
44+
public function register_rest_route()
45+
{
46+
// Get all sites
47+
register_rest_route(
48+
self::NAMESPACE, '/sites',
49+
[
50+
'methods' => WP_REST_Server::READABLE,
51+
'callback' => [$this->get_sites, 'callback'],
52+
// 'permission_callback' => [$this, 'get_item_permissions_check'],
53+
'args' => [],
54+
]);
55+
56+
// Get site by blog ID
57+
register_rest_route(
58+
self::NAMESPACE, '/'.$this->rest_base.'/(?P<id>[\d]+)',
59+
[
60+
'args' => [
61+
'id' => [
62+
'description' => __('Unique identifier for the object.'),
63+
'type' => 'integer',
64+
],
65+
],
66+
[
67+
'methods' => WP_REST_Server::READABLE,
68+
'callback' => [$this->get_site, 'callback'],
69+
// 'permission_callback' => [$this, 'get_item_permissions_check'],
70+
'args' => [],
71+
],
72+
'schema' => [$this, 'get_public_item_schema'],
73+
]
74+
);
75+
}
76+
77+
private function get_item_permissions_check()
78+
{
79+
global $wp_version;
80+
81+
if (version_compare($wp_version, '4.8', '>=')) {
82+
return current_user_can('setup_network');
83+
}
84+
85+
return is_super_admin();
86+
}
87+
}
88+
89+
// Includes
90+
require_once dirname(__FILE__).'/class-multisite-get-sites.php';
91+
require_once dirname(__FILE__).'/class-multisite-get-site.php';
92+
93+
// Instantiate
94+
new IOD_Multisite_API();
95+
}

class-multisite-get-site.php

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
class IOD_Multisite_API_Get_Site extends WP_REST_Controller
4+
{
5+
/**
6+
* Get a collection of items.
7+
*
8+
* @param WP_REST_Request $request full data about the request
9+
*
10+
* @return WP_Error|WP_REST_Response
11+
*/
12+
public function callback($request)
13+
{
14+
$site = apply_filters('multisite_api/get_site', $this->get_site($request['id']));
15+
16+
if (is_wp_error($site)) {
17+
return $site;
18+
}
19+
20+
return $site;
21+
}
22+
23+
protected function get_site($id)
24+
{
25+
$error = new WP_Error(
26+
'rest_site_invalid_id',
27+
__('Invalid site ID.'),
28+
['status' => 404]
29+
);
30+
31+
if ((int) $id <= 0) {
32+
return $error;
33+
}
34+
35+
$site = get_site((int) $id);
36+
37+
if (empty($site) || empty($site->blog_id)) {
38+
return $error;
39+
}
40+
41+
return $site;
42+
}
43+
}

class-multisite-get-sites.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
class IOD_Multisite_API_Get_Sites extends WP_REST_Controller
4+
{
5+
/**
6+
* Get a collection of items.
7+
*
8+
* @param WP_REST_Request $request full data about the request
9+
*
10+
* @return WP_Error|WP_REST_Response
11+
*/
12+
public function callback($request)
13+
{
14+
$args = [
15+
'public' => 1,
16+
'archived' => 0,
17+
'mature' => 0,
18+
'spam' => 0,
19+
'deleted' => 0,
20+
'site__not_in' => [1],
21+
];
22+
23+
$args = apply_filters('multisite_api/get_sites/args', $args);
24+
$sites = apply_filters('multisite_api/get_sites', get_sites($args));
25+
26+
return rest_ensure_response($sites);
27+
}
28+
}

composer.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "iciondrive/multisite-api",
3+
"description": "Create endpoint to the WordPress REST API for multisite. Plugin developed for the \"Ici On Drive\" platform.",
4+
"type": "wordpress-plugin",
5+
"version": "1.0.0",
6+
"license": "GPLv2 or later",
7+
"authors": [
8+
{
9+
"name": "Thomas Navarro",
10+
"email": "[email protected]"
11+
}
12+
],
13+
"require": {}
14+
}

0 commit comments

Comments
 (0)