|
| 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 | +} |
0 commit comments