forked from Automattic/vip-go-mu-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvip-rest-api.php
117 lines (98 loc) · 2.66 KB
/
vip-rest-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
<?php
/*
Plugin Name: VIP REST API Endpoints
Plugin URI: https://vip.wordpress.com/
Description: Add custom REST API endpoints for VIP requests
Author: Erick Hitter, Automattic
Version: 0.1
*/
class WPCOM_VIP_REST_API_Endpoints {
/**
* SINGLETON
*/
private static $__instance = null;
public static function instance() {
if ( ! is_a( self::$__instance, __CLASS__ ) ) {
self::$__instance = new self;
}
return self::$__instance;
}
/**
* PLUGIN SETUP
*/
/**
* Class properties
*/
private $namespace = 'vip/v1';
private $cached_sites_list = 'wpcom-vip-sites-list';
/**
* Register hooks
*/
private function __construct() {
add_action( 'rest_api_init', array( $this, 'rest_api_init' ) );
}
/**
* Register API routes
*/
public function rest_api_init() {
register_rest_route( $this->namespace, '/sites/', array(
'methods' => 'GET',
'callback' => array( $this, 'list_sites' ),
) );
}
/**
* PLUGIN FUNCTIONALITY
*/
/**
* Build list of sites on a multisite network
*
* For consistency, will also return result on single-site
*/
public function list_sites() {
$sites = array();
if ( is_multisite() ) {
// `get_sites()` won't be in Core until at least 4.6
if ( function_exists( 'get_sites' ) ) {
$_sites = get_sites( array(
'public' => 1,
'archived' => 0,
'spam' => 0,
'deleted' => 0,
'fields' => 'ids',
) );
} else {
// Add support for 4.4 and 4.5, as `get_sites()` wasn't introduced until 4.6
$_sites = get_site_transient( $this->cached_sites_list );
if ( ! is_array( $_sites ) ) {
global $wpdb;
$_sites = $wpdb->get_col( "SELECT blog_id FROM {$wpdb->blogs} WHERE `public` = 1 AND `archived` = 0 AND `spam` = 0 AND `deleted` = 0 LIMIT 1000;" );
if ( is_array( $_sites ) ) {
set_site_transient( $this->cached_sites_list, $_sites, 30 * MINUTE_IN_SECONDS );
} else {
$_sites = false;
}
}
}
// Inflate raw list of site IDs, if available
if ( is_array( $_sites ) ) {
// Switch to the blog to ensure certain domain filtering is respected
foreach ( $_sites as $_site ) {
switch_to_blog( $_site );
$sites[] = array(
'domain_name' => parse_url( home_url(), PHP_URL_HOST ),
);
restore_current_blog();
}
} else {
$sites = new WP_Error( 'no-sites-found', 'Failed to retrieve any sites for this multisite network.' );
}
} else {
// Provided for consistency, even though this provides no insightful response
$sites[] = array(
'domain_name' => parse_url( home_url(), PHP_URL_HOST ),
);
}
return new WP_REST_Response( $sites );
}
}
WPCOM_VIP_REST_API_Endpoints::instance();