1
1
<?php
2
2
/*
3
- Plugin Name: VIP REST API Endpoints
4
- Plugin URI: https://wpvip.com
5
- Description: Add custom REST API endpoints for VIP requests; N.B. these endpoints are subject to change without notice, and should be considered "private".
6
- Author: Erick Hitter, Automattic
7
- Version: 0.1
8
- */
3
+ Plugin Name: VIP REST API Endpoints
4
+ Plugin URI: https://wpvip.com
5
+ Description: Add custom REST API endpoints for VIP requests; N.B. these endpoints are subject to change without notice, and should be considered "private".
6
+ Author: Erick Hitter, Automattic
7
+ Version: 0.1
8
+ */
9
9
10
10
class WPCOM_VIP_REST_API_Endpoints {
11
11
/**
12
12
* SINGLETON
13
13
*/
14
- private static $ __instance = null ;
14
+ private static $ instance = null ;
15
15
16
16
public static function instance () {
17
- if ( ! is_a ( self ::$ __instance , __CLASS__ ) ) {
18
- self ::$ __instance = new self ;
17
+ if ( ! is_a ( self ::$ instance , __CLASS__ ) ) {
18
+ self ::$ instance = new self () ;
19
19
}
20
20
21
- return self ::$ __instance ;
21
+ return self ::$ instance ;
22
22
}
23
23
24
24
/**
@@ -80,16 +80,19 @@ public function rest_api_init() {
80
80
public function force_authorized_access ( $ result ) {
81
81
global $ wp_rewrite ;
82
82
83
+ // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
84
+ $ request_uri = $ _SERVER ['REQUEST_URI ' ] ?? '' ;
85
+
83
86
if ( $ wp_rewrite ->using_permalinks () ) {
84
87
$ rest_prefix = rest_get_url_prefix ();
85
88
86
89
// Expected request.
87
90
$ expected_namespace = get_rest_url ( null , $ this ->namespace );
88
91
$ expected_namespace = trailingslashit ( $ expected_namespace );
89
- $ expected_namespace = parse_url ( $ expected_namespace , PHP_URL_PATH );
92
+ $ expected_namespace = wp_parse_url ( $ expected_namespace , PHP_URL_PATH );
90
93
91
94
// Actual request.
92
- $ request_parts = explode ( '/ ' , $ _SERVER [ ' REQUEST_URI ' ] );
95
+ $ request_parts = explode ( '/ ' , $ request_uri );
93
96
94
97
// Drop undesirable leading bits to rebuild namespace from request.
95
98
foreach ( $ request_parts as $ key => $ part ) {
@@ -111,14 +114,14 @@ public function force_authorized_access( $result ) {
111
114
return $ result ;
112
115
}
113
116
114
- $ slashed_request = trailingslashit ( $ _SERVER [ ' REQUEST_URI ' ] );
117
+ $ slashed_request = trailingslashit ( $ request_uri );
115
118
116
119
if ( 0 === strpos ( $ slashed_request , $ expected_namespace ) && wpcom_vip_go_rest_api_request_allowed ( $ this ->namespace ) ) {
117
120
return true ;
118
121
}
119
122
} else {
120
123
$ query_args = array ();
121
- $ query_string = parse_url ( $ _SERVER [ ' REQUEST_URI ' ] , PHP_URL_QUERY );
124
+ $ query_string = wp_parse_url ( $ request_uri , PHP_URL_QUERY );
122
125
wp_parse_str ( $ query_string , $ query_args );
123
126
124
127
if ( ! isset ( $ query_args ['rest_route ' ] ) ) {
@@ -159,7 +162,7 @@ public function list_sites() {
159
162
foreach ( $ _sites as $ _site ) {
160
163
switch_to_blog ( $ _site );
161
164
162
- $ url_parts = wp_parse_args ( parse_url ( home_url () ), array (
165
+ $ url_parts = wp_parse_args ( wp_parse_url ( home_url () ), array (
163
166
'host ' => '' ,
164
167
'path ' => '' ,
165
168
) );
@@ -173,7 +176,7 @@ public function list_sites() {
173
176
}
174
177
175
178
$ sites [] = array (
176
- 'ID ' => $ _site ,
179
+ 'ID ' => $ _site ,
177
180
'domain_name ' => $ url ,
178
181
);
179
182
@@ -187,8 +190,8 @@ public function list_sites() {
187
190
} else {
188
191
// Provided for consistency, even though this provides no insightful response
189
192
$ sites [] = array (
190
- 'ID ' => 1 ,
191
- 'domain_name ' => parse_url ( home_url (), PHP_URL_HOST ),
193
+ 'ID ' => 1 ,
194
+ 'domain_name ' => wp_parse_url ( home_url (), PHP_URL_HOST ),
192
195
);
193
196
}
194
197
@@ -209,7 +212,7 @@ public function list_plugins() {
209
212
*
210
213
* @return WP_REST_Response
211
214
*/
212
- public function list_jetpack_details ( $ request ): WP_REST_Response {
215
+ public function list_jetpack_details (): WP_REST_Response {
213
216
$ details = [];
214
217
215
218
if ( is_multisite () ) {
@@ -246,7 +249,7 @@ public function list_jetpack_details( $request ): WP_REST_Response {
246
249
*/
247
250
protected function get_jetpack_details_for_site (): array {
248
251
$ connection = new Automattic \Jetpack \Connection \Manager ();
249
- $ data = [
252
+ $ data = [
250
253
'site_id ' => get_current_blog_id (),
251
254
'cache_site_id ' => Jetpack::get_option ( 'id ' ),
252
255
'home_url ' => home_url (),
@@ -269,24 +272,24 @@ protected function get_all_plugins() {
269
272
270
273
// array of all standard plugins
271
274
$ standard_plugins = get_plugins ();
272
- $ tmp_plugins = array ();
275
+ $ tmp_plugins = array ();
273
276
foreach ( $ standard_plugins as $ key => $ plugin ) {
274
277
$ vip_plugin_slug = 'plugins/ ' . dirname ( $ key );
275
278
if ( is_plugin_active ( $ key ) ) {
276
279
$ tmp_plugins [ $ key ] = array (
277
- 'name ' => $ plugin ['Name ' ],
278
- 'version ' => $ plugin ['Version ' ],
280
+ 'name ' => $ plugin ['Name ' ],
281
+ 'version ' => $ plugin ['Version ' ],
279
282
'description ' => $ plugin ['Description ' ],
280
- 'type ' => 'standard ' ,
281
- 'active ' => true ,
283
+ 'type ' => 'standard ' ,
284
+ 'active ' => true ,
282
285
);
283
286
} elseif ( ! in_array ( $ vip_plugin_slug , $ vip_loaded_plugins , true ) ) {
284
287
$ tmp_plugins [ $ key ] = array (
285
- 'name ' => $ plugin ['Name ' ],
286
- 'version ' => $ plugin ['Version ' ],
288
+ 'name ' => $ plugin ['Name ' ],
289
+ 'version ' => $ plugin ['Version ' ],
287
290
'description ' => $ plugin ['Description ' ],
288
- 'type ' => 'standard ' ,
289
- 'active ' => false ,
291
+ 'type ' => 'standard ' ,
292
+ 'active ' => false ,
290
293
);
291
294
}
292
295
}
@@ -298,71 +301,72 @@ protected function get_all_plugins() {
298
301
$ vip_plugin_slug = 'plugins/ ' . dirname ( $ key );
299
302
if ( in_array ( $ vip_plugin_slug , $ vip_loaded_plugins , true ) ) {
300
303
$ tmp_plugins [ $ key ] = array (
301
- 'name ' => $ plugin ['Name ' ],
302
- 'version ' => $ plugin ['Version ' ],
304
+ 'name ' => $ plugin ['Name ' ],
305
+ 'version ' => $ plugin ['Version ' ],
303
306
'description ' => $ plugin ['Description ' ],
304
- 'type ' => 'standard-code ' ,
305
- 'active ' => true ,
307
+ 'type ' => 'standard-code ' ,
308
+ 'active ' => true ,
306
309
);
307
310
}
308
311
}
309
312
$ all_plugins ['standard-code ' ] = $ tmp_plugins ;
310
313
311
314
// array of all mu plugins
312
- $ mu_plugins = get_mu_plugins ();
315
+ $ mu_plugins = get_mu_plugins ();
313
316
$ tmp_plugins = array ();
314
317
foreach ( $ mu_plugins as $ key => $ plugin ) {
315
318
$ tmp_plugins [ $ key ] = array (
316
- 'name ' => $ plugin ['Name ' ],
317
- 'version ' => $ plugin ['Version ' ],
319
+ 'name ' => $ plugin ['Name ' ],
320
+ 'version ' => $ plugin ['Version ' ],
318
321
'description ' => $ plugin ['Description ' ],
319
- 'type ' => 'mu-plugin ' ,
320
- 'active ' => true ,
322
+ 'type ' => 'mu-plugin ' ,
323
+ 'active ' => true ,
321
324
);
322
325
}
323
326
$ all_plugins ['mu-plugin ' ] = $ tmp_plugins ;
324
327
325
328
// array of all client mu plugins
326
329
$ client_mu_plugins = wpcom_vip_get_client_mu_plugins_data ();
327
- $ tmp_plugins = array ();
330
+ $ tmp_plugins = array ();
328
331
foreach ( $ client_mu_plugins as $ key => $ plugin ) {
329
332
$ tmp_plugins [ $ key ] = array (
330
- 'name ' => $ plugin ['Name ' ],
331
- 'version ' => $ plugin ['Version ' ],
333
+ 'name ' => $ plugin ['Name ' ],
334
+ 'version ' => $ plugin ['Version ' ],
332
335
'description ' => $ plugin ['Description ' ],
333
- 'type ' => 'client-mu-plugin ' ,
334
- 'active ' => true ,
336
+ 'type ' => 'client-mu-plugin ' ,
337
+ 'active ' => true ,
335
338
);
336
339
}
337
340
$ all_plugins ['client-mu-plugin ' ] = $ tmp_plugins ;
338
341
339
342
// array of all shared plugins (activated via code and via UI)
340
343
// once the remaining shared plugins are retired we can remove this section
341
- $ tmp_ui_plugins = array ();
344
+ $ tmp_ui_plugins = array ();
342
345
$ tmp_code_plugins = array ();
343
346
foreach ( get_plugins ( '/../mu-plugins/shared-plugins ' ) as $ key => $ plugin ) {
344
- if ( $ active_plugin_type = $ this ->legacy_is_plugin_active ( basename ( dirname ( $ key ) ) ) ) {
347
+ $ active_plugin_type = $ this ->legacy_is_plugin_active ( basename ( dirname ( $ key ) ) );
348
+ if ( $ active_plugin_type ) {
345
349
if ( 'manual ' === $ active_plugin_type ) {
346
350
$ tmp_code_plugins [ $ key ] = array (
347
- 'name ' => $ plugin ['Name ' ],
348
- 'version ' => $ plugin ['Version ' ],
351
+ 'name ' => $ plugin ['Name ' ],
352
+ 'version ' => $ plugin ['Version ' ],
349
353
'description ' => $ plugin ['Description ' ],
350
- 'type ' => 'vip-shared-code ' ,
351
- 'active ' => true ,
354
+ 'type ' => 'vip-shared-code ' ,
355
+ 'active ' => true ,
352
356
);
353
357
} else {
354
358
$ tmp_ui_plugins [ $ key ] = array (
355
- 'name ' => $ plugin ['Name ' ],
356
- 'version ' => $ plugin ['Version ' ],
359
+ 'name ' => $ plugin ['Name ' ],
360
+ 'version ' => $ plugin ['Version ' ],
357
361
'description ' => $ plugin ['Description ' ],
358
- 'type ' => 'vip-shared-ui ' ,
359
- 'active ' => true ,
362
+ 'type ' => 'vip-shared-ui ' ,
363
+ 'active ' => true ,
360
364
);
361
365
}
362
366
}
363
367
}
364
368
$ all_plugins ['vip-shared-code ' ] = $ tmp_code_plugins ;
365
- $ all_plugins ['vip-shared-ui ' ] = $ tmp_ui_plugins ;
369
+ $ all_plugins ['vip-shared-ui ' ] = $ tmp_ui_plugins ;
366
370
367
371
// add constant to endpoint
368
372
$ all_plugins ['disable-shared-plugins ' ] = ( defined ( 'WPCOM_VIP_DISABLE_SHARED_PLUGINS ' ) && true === WPCOM_VIP_DISABLE_SHARED_PLUGINS ) ? true : false ;
0 commit comments