Completed
Push — fix/videopress-flash-rss ( 410f56...198f69 )
by
unknown
19:49 queued 09:08
created

class.wpcom-json-api-get-site-endpoint.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
class WPCOM_JSON_API_GET_Site_Endpoint extends WPCOM_JSON_API_Endpoint {
3
4
	public static $site_format = array(
5
		'ID'                => '(int) Site ID',
6
		'name'              => '(string) Title of site',
7
		'description'       => '(string) Tagline or description of site',
8
		'URL'               => '(string) Full URL to the site',
9
		'user_can_manage'   => '(bool) The current user can manage this site', // deprecated
10
		'capabilities'      => '(array) Array of capabilities for the current user on this site.',
11
		'jetpack'           => '(bool)  Whether the site is a Jetpack site or not',
12
		'is_multisite'      => '(bool) Whether the site is a Multisite site or not. Always true for WP.com sites.',
13
		'post_count'        => '(int) The number of posts the site has',
14
		'subscribers_count' => '(int) The number of subscribers the site has',
15
		'lang'              => '(string) Primary language code of the site',
16
		'icon'              => '(array) An array of icon formats for the site',
17
		'logo'              => '(array) The site logo, set in the Customizer',
18
		'visible'           => '(bool) If this site is visible in the user\'s site list',
19
		'is_private'        => '(bool) If the site is a private site or not',
20
		'single_user_site'  => '(bool) Whether the site is single user. Only returned for WP.com sites and for Jetpack sites with version 3.4 or higher.',
21
		'is_vip'            => '(bool) If the site is a VIP site or not.',
22
		'is_following'      => '(bool) If the current user is subscribed to this site in the reader',
23
		'options'           => '(array) An array of options/settings for the blog. Only viewable by users with post editing rights to the site. Note: Post formats is deprecated, please see /sites/$id/post-formats/',
24
		'plan'              => '(array) Details of the current plan for this site.',
25
		'updates'           => '(array) An array of available updates for plugins, themes, wordpress, and languages.',
26
		'jetpack_modules'   => '(array) A list of active Jetpack modules.',
27
		'meta'              => '(object) Meta data',
28
	);
29
30
	protected static $no_member_fields = array(
31
		'ID',
32
		'name',
33
		'description',
34
		'URL',
35
		'jetpack',
36
		'post_count',
37
		'subscribers_count',
38
		'lang',
39
		'locale',
40
		'icon',
41
		'logo',
42
		'visible',
43
		'is_private',
44
		'is_following',
45
		'meta',
46
	);
47
48
	protected static $site_options_format = array(
49
		'timezone',
50
		'gmt_offset',
51
		'videopress_enabled',
52
		'upgraded_filetypes_enabled',
53
		'login_url',
54
		'admin_url',
55
		'is_mapped_domain',
56
		'is_redirect',
57
		'unmapped_url',
58
		'featured_images_enabled',
59
		'theme_slug',
60
		'header_image',
61
		'background_color',
62
		'image_default_link_type',
63
		'image_thumbnail_width',
64
		'image_thumbnail_height',
65
		'image_thumbnail_crop',
66
		'image_medium_width',
67
		'image_medium_height',
68
		'image_large_width',
69
		'image_large_height',
70
		'permalink_structure',
71
		'post_formats',
72
		'default_post_format',
73
		'default_category',
74
		'allowed_file_types',
75
		'show_on_front',
76
		/** This filter is documented in modules/likes.php */
77
		'default_likes_enabled',
78
		'default_sharing_status',
79
		'default_comment_status',
80
		'default_ping_status',
81
		'software_version',
82
		'created_at',
83
		'wordads',
84
		'publicize_permanently_disabled',
85
		'frame_nonce',
86
		'page_on_front',
87
		'page_for_posts',
88
		'headstart',
89
		'ak_vp_bundle_enabled'
90
	);
91
92
	protected static $jetpack_response_field_additions = array( 
93
		'subscribers_count',
94
	);
95
96
	protected static $jetpack_response_field_member_additions = array(
97
		'capabilities',
98
		'plan',
99
	);
100
101
	protected static $jetpack_response_option_additions = array( 
102
		'publicize_permanently_disabled',
103
		'ak_vp_bundle_enabled'
104
	);
105
106
	private $site;
107
108
	// protected $compact = null;
109
	protected $fields_to_include = '_all';
110
	protected $options_to_include = '_all';
111
112
	// /sites/mine
113
	// /sites/%s -> $blog_id
114
	function callback( $path = '', $blog_id = 0 ) {
115
		if ( 'mine' === $blog_id ) {
116
			$api = WPCOM_JSON_API::init();
117
			if ( ! $api->token_details || empty( $api->token_details['blog_id'] ) ) {
118
				return new WP_Error( 'authorization_required', 'An active access token must be used to query information about the current blog.', 403 );
119
			}
120
			$blog_id = $api->token_details['blog_id'];
121
		}
122
123
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
124
		if ( is_wp_error( $blog_id ) ) {
125
			return $blog_id;
126
		}
127
128
		// TODO: enable this when we can do so without being interfered with by 
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
129
		// other endpoints that might be wrapping this one.
130
		// Uncomment and see failing test: test_jetpack_site_should_have_true_jetpack_property_via_site_meta
131
		// $this->filter_fields_and_options();
132
133
		$response = $this->build_current_site_response();
134
135
		/** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
136
		do_action( 'wpcom_json_api_objects', 'sites' );
137
138
		return $response;
139
	}
140
141
	public function filter_fields_and_options() {
142
		$query_args = $this->query_args();
143
144
		$this->fields_to_include  = empty( $query_args['fields'] ) ? '_all' : array_map( 'trim', explode( ',', $query_args['fields'] ) );
145
		$this->options_to_include = empty( $query_args['options'] ) ? '_all' : array_map( 'trim', explode( ',', $query_args['options'] ) );
146
	}
147
148
	/**
149
	 * Collects the necessary information to return for a site's response.
150
	 *
151
	 * @return (array)
152
	 */
153
	public function build_current_site_response() {
154
155
		$blog_id = (int) $this->api->get_blog_id_for_output();
156
157
		$this->site = $this->get_platform()->get_site( $blog_id );
158
159
		/**
160
 		 * Filter the structure of information about the site to return.
161
 		 *
162
 		 * @module json-api
163
 		 *
164
 		 * @since 3.9.3
165
 		 *
166
 		 * @param array $site_format Data structure.
167
 		 */
168
		$default_fields = array_keys( apply_filters( 'sites_site_format', self::$site_format ) );
169
170
		$response_keys = is_array( $this->fields_to_include ) ?
171
			array_intersect( $default_fields, $this->fields_to_include ) :
172
			$default_fields;
173
174
		if ( ! is_user_member_of_blog( get_current_user(), $blog_id ) ) {
175
			$response_keys = array_intersect( $response_keys, self::$no_member_fields );
176
		}
177
178
		return $this->render_response_keys( $response_keys );
179
	}
180
181
	private function render_response_keys( &$response_keys ) {
182
		$response = array();
183
184
		$is_user_logged_in = is_user_logged_in();
185
186
		$this->site->before_render();
187
188
		foreach ( $response_keys as $key ) {
189
			$this->render_response_key( $key, $response, $is_user_logged_in );
190
		}
191
192
		$this->site->after_render( $response );
193
194
		return $response;
195
	}
196
197
	protected function render_response_key( $key, &$response, $is_user_logged_in ) {
198
		do_action( 'pre_render_site_response_key', $key );
199
200
		switch ( $key ) {
201
			case 'ID' :
202
				$response[ $key ] = $this->site->blog_id;
203
				break;
204
			case 'name' :
205
				$response[ $key ] = $this->site->get_name();
206
				break;
207
			case 'description' :
208
				$response[ $key ] = $this->site->get_description();
209
				break;
210
			case 'URL' :
211
				$response[ $key ] = $this->site->get_url();
212
				break;
213
			case 'user_can_manage' :
214
				$response[ $key ] = $this->site->user_can_manage();
215
			case 'is_private' :
216
				$response[ $key ] = $this->site->is_private();
217
				break;
218
			case 'visible' :
219
				$response[ $key ] = $this->site->is_visible();
220
				break;
221
			case 'subscribers_count' :
222
				$response[ $key ] = $this->site->get_subscribers_count();
223
				break;
224
			case 'post_count' :
225
				if ( $is_user_logged_in ) {
226
					$response[ $key ] = $this->site->get_post_count();
227
				}
228
				break;
229
			case 'icon' :
230
				$icon = $this->site->get_icon();
231
232
				if ( ! is_null( $icon ) ) {
233
					$response[ $key ] = $icon;
234
				}
235
				break;
236
			case 'logo' :
237
				$response[ $key ] = $this->site->get_logo();
238
				break;
239
			case 'is_following':
240
				$response[ $key ] = $this->site->is_following();
241
				break;
242
			case 'options':
243
				// small optimisation - don't recalculate 
244
				$all_options = apply_filters( 'sites_site_options_format', self::$site_options_format );
245
246
				$options_response_keys = is_array( $this->options_to_include ) ?
247
					array_intersect( $all_options, $this->options_to_include ) :
248
					$all_options;
249
250
				$options = $this->render_option_keys( $options_response_keys );
251
252
				$this->site->after_render_options( $options );
253
254
				$response[ $key ] = (object) $options;
255
				break;
256
			case 'meta':
257
				$this->build_meta_response( $response );
258
				break;
259
			case 'lang' :
260
				$response[ $key ] = $is_user_logged_in ? $this->site->get_locale() : false;
261
				break;
262
			case 'locale' :
263
				$response[ $key ] = $is_user_logged_in ? $this->site->get_locale() : false;
264
				break;
265
			case 'jetpack' :
266
				$response[ $key ] = $this->site->is_jetpack();
267
				break;
268
			case 'single_user_site' : 
269
				$response[ $key ] = $this->site->is_single_user_site();
270
				break;
271
			case 'is_vip' : 
272
				$response[ $key ] = $this->site->is_vip();
273
				break;
274
			case 'is_multisite' :
275
				$response[ $key ] = $this->site->is_multisite();
276
				break;
277
			case 'capabilities' : 
278
				$response[ $key ] = $this->site->get_capabilities();
279
				break;
280
			case 'jetpack_modules':
281
				$jetpack_modules = $this->site->get_jetpack_modules();
282
				if ( ! is_null( $jetpack_modules ) ) {
283
					$response[ $key ] = $jetpack_modules;
284
				}
285
				break;
286
			case 'plan' :
287
				$response[ $key ] = $this->site->get_plan();
288
				break;
289
		}
290
291
		do_action( 'post_render_site_response_key', $key );
292
	}
293
294
	protected function render_option_keys( &$options_response_keys ) {
295
		if ( ! current_user_can( 'edit_posts' ) ) {
296
			return array();
297
		}
298
299
		$options = array();
300
		$site = $this->site;
301
302
		$custom_front_page = $site->is_custom_front_page();
303
		
304
305
		foreach ( $options_response_keys as $key ) {
306
			switch ( $key ) {
307
				case 'timezone' :
308
					$options[ $key ] = $site->get_timezone();
309
					break;
310
				case 'gmt_offset' :
311
					$options[ $key ] = $site->get_gmt_offset();
312
					break;
313
				case 'videopress_enabled' :
314
					$options[ $key ] = $site->has_videopress();
315
					break;
316
				case 'upgraded_filetypes_enabled' :
317
					$options[ $key ] = $site->upgraded_filetypes_enabled();
318
					break;
319
				case 'login_url' :
320
					$options[ $key ] = $site->get_login_url();
321
					break;
322
				case 'admin_url' :
323
					$options[ $key ] = $site->get_admin_url();
324
					break;
325
				case 'is_mapped_domain' :
326
					$options[ $key ] = $site->is_mapped_domain();
327
					break;
328
				case 'is_redirect' :
329
					$options[ $key ] = $site->is_redirect();
330
					break;
331
				case 'unmapped_url' :
332
					$options[ $key ] = $site->get_unmapped_url();
333
					break;
334
				case 'featured_images_enabled' :
335
					$options[ $key ] = $site->featured_images_enabled();
336
					break;
337
				case 'theme_slug' :
338
					$options[ $key ] = $site->get_theme_slug();
339
					break;
340
				case 'header_image' :
341
					$options[ $key ] = $site->get_header_image();
342
					break;
343
				case 'background_color' :
344
					$options[ $key ] = $site->get_background_color();
345
					break;
346
				case 'image_default_link_type' :
347
					$options[ $key ] = $site->get_image_default_link_type();
348
					break;
349
				case 'image_thumbnail_width' :
350
					$options[ $key ] = $site->get_image_thumbnail_width();
351
					break;
352
				case 'image_thumbnail_height' :
353
					$options[ $key ] = $site->get_image_thumbnail_height();
354
					break;
355
				case 'image_thumbnail_crop' :
356
					$options[ $key ] = $site->get_image_thumbnail_crop();
357
					break;
358
				case 'image_medium_width' :
359
					$options[ $key ] = $site->get_image_medium_width();
360
					break;
361
				case 'image_medium_height' :
362
					$options[ $key ] = $site->get_image_medium_height();
363
					break;
364
				case 'image_large_width' :
365
					$options[ $key ] = $site->get_image_large_width();
366
					break;
367
				case 'image_large_height' :
368
					$options[ $key ] = $site->get_image_large_height(); 
369
					break;
370
				case 'permalink_structure' :
371
					$options[ $key ] = $site->get_permalink_structure();
372
					break;
373
				case 'post_formats' :
374
					$options[ $key ] = $site->get_post_formats();
375
					break;
376
				case 'default_post_format' :
377
					$options[ $key ] = $site->get_default_post_format();
378
					break;
379
				case 'default_category' :
380
					$options[ $key ] = $site->get_default_category();
381
					break;
382
				case 'allowed_file_types' :
383
					$options[ $key ] = $site->allowed_file_types();
384
					break;
385
				case 'show_on_front' :
386
					$options[ $key ] = $site->get_show_on_front();
387
					break;
388
				/** This filter is documented in modules/likes.php */
389
				case 'default_likes_enabled' :
390
					$options[ $key ] = $site->get_default_likes_enabled();
391
					break;
392
				case 'default_sharing_status' :
393
					$options[ $key ] = $site->get_default_sharing_status();
394
					break;
395
				case 'default_comment_status' :
396
					$options[ $key ] = $site->get_default_comment_status();
397
					break;
398
				case 'default_ping_status' :
399
					$options[ $key ] = $site->default_ping_status();
400
					break;
401
				case 'software_version' :
402
					$options[ $key ] = $site->get_wordpress_version();
403
					break;
404
				case 'created_at' :
405
					$options[ $key ] = $site->get_registered_date();
406
					break;
407
				case 'wordads' :
408
					$options[ $key ] = $site->has_wordads();
409
					break;
410
				case 'publicize_permanently_disabled' :
411
					$options[ $key ] = $site->is_publicize_permanently_disabled();
412
					break;
413
				case 'frame_nonce' :
414
					$options[ $key ] = $site->get_frame_nonce();
415
					break;
416
				case 'page_on_front' :
417
					if ( $custom_front_page ) {
418
						$options[ $key ] = $site->get_page_on_front();
419
					}
420
					break;
421
				case 'page_for_posts' :
422
					if ( $custom_front_page ) {
423
						$options[ $key ] = $site->get_page_for_posts();
424
					}
425
					break;
426
				case 'headstart' :
427
					$options[ $key ] = $site->is_headstart();
428
					break;
429
				case 'ak_vp_bundle_enabled' :
430
					$options[ $key ] = $site->get_ak_vp_bundle_enabled();
0 ignored issues
show
Are you sure the assignment to $options[$key] is correct as $site->get_ak_vp_bundle_enabled() (which targets Jetpack_Site::get_ak_vp_bundle_enabled()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
431
			}
432
		}
433
434
		return $options;
435
	}
436
437
	protected function build_meta_response( &$response ) {
438
		$response['meta'] = (object) array(
439
			'links' => (object) array(
440
				'self'     => (string) $this->links->get_site_link( $this->site->blog_id ),
441
				'help'     => (string) $this->links->get_site_link( $this->site->blog_id, 'help'      ),
442
				'posts'    => (string) $this->links->get_site_link( $this->site->blog_id, 'posts/'    ),
443
				'comments' => (string) $this->links->get_site_link( $this->site->blog_id, 'comments/' ),
444
				'xmlrpc'   => (string) $this->site->get_xmlrpc_url(),
445
			),
446
		);
447
	}
448
449
	// apply any WPCOM-only response components to a Jetpack site response
450
	public function decorate_jetpack_response( &$response ) {
451
		$this->site = $this->get_platform()->get_site( $response->ID );
452
453
		// ensure the response is marked as being from Jetpack
454
		$response->jetpack = true;
455
456
		$wpcom_response = $this->render_response_keys( self::$jetpack_response_field_additions );
457
458
		foreach( $wpcom_response as $key => $value ) {
459
			$response->{ $key } = $value;
460
		}
461
462
		if ( is_user_member_of_blog( get_current_user(), $response->ID ) ) {
463
			$wpcom_member_response = $this->render_response_keys( self::$jetpack_response_field_member_additions );
464
465
			foreach( $wpcom_member_response as $key => $value ) {
466
				$response->{ $key } = $value;
467
			}
468
		} else {
469
			// ensure private data is not rendered for non members of the site
470
			unset( $response->options );
471
			unset( $response->is_vip );
472
			unset( $response->single_user_site );
473
			unset( $response->is_private );
474
			unset( $response->capabilities );
475
			unset( $response->lang );
476
			unset( $response->user_can_manage );
477
			unset( $response->is_multisite );
478
			unset( $response->plan );
479
		}
480
481
		// render additional options
482
		if ( $response->options ) {
483
			$wpcom_options_response = $this->render_option_keys( self::$jetpack_response_option_additions );
484
485
			foreach ( $wpcom_options_response as $key => $value ) {
486
				$response->options[ $key ] = $value;
487
			}
488
		}
489
490
		return $response; // possibly no need since it's modified in place
491
	}
492
}
493
494
class WPCOM_JSON_API_List_Post_Formats_Endpoint extends WPCOM_JSON_API_Endpoint {
495
	// /sites/%s/post-formats -> $blog_id
496
	function callback( $path = '', $blog_id = 0 ) {
497
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
498
		if ( is_wp_error( $blog_id ) ) {
499
			return $blog_id;
500
		}
501
502
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
503
			$this->load_theme_functions();
504
		}
505
506
		// Get a list of supported post formats.
507
		$all_formats = get_post_format_strings();
508
		$supported   = get_theme_support( 'post-formats' );
509
510
		$supported_formats = $response['formats'] = array();
511
512 View Code Duplication
		if ( isset( $supported[0] ) ) {
513
			foreach ( $supported[0] as $format ) {
514
				$supported_formats[ $format ] = $all_formats[ $format ];
515
			}
516
		}
517
518
		$response['formats'] = (object) $supported_formats;
519
520
		return $response;
521
	}
522
}
523
524
class WPCOM_JSON_API_List_Page_Templates_Endpoint extends WPCOM_JSON_API_Endpoint {
525
	// /sites/%s/page-templates -> $blog_id
526
	function callback( $path = '', $blog_id = 0 ) {
527
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
528
		if ( is_wp_error( $blog_id ) ) {
529
			return $blog_id;
530
		}
531
532
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
533
			$this->load_theme_functions();
534
		}
535
536
		$response = array();
537
		$page_templates = array();
538
539
		$templates = get_page_templates();
540
		ksort( $templates );
541
542
		foreach ( array_keys( $templates ) as $label ) {
543
			$page_templates[] = array(
544
				'label' => $label,
545
				'file'  => $templates[ $label ]
546
			);
547
		}
548
549
		$response['templates'] = $page_templates;
550
551
		return $response;
552
	}
553
}
554