PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.3
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.3
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 All 506 releases
jetpack / json-endpoints / class.wpcom-json-api-get-site-endpoint.php

class.wpcom-json-api-get-site-endpoint.php in Jetpack – WP Security, Backup, Speed, & Growth 16.3-a.3, at json-endpoints/class.wpcom-json-api-get-site-endpoint.php

1,317 lines 42.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit( 0 );
5 }
6
7 new WPCOM_JSON_API_GET_Site_Endpoint(
8 array(
9 'description' => 'Get information about a site.',
10 'group' => 'sites',
11 'stat' => 'sites:X',
12 'allowed_if_flagged' => true,
13 'method' => 'GET',
14 'max_version' => '1.1',
15 'new_version' => '1.2',
16 'path' => '/sites/%s',
17 'path_labels' => array(
18 '$site' => '(int|string) Site ID or domain',
19 ),
20 'rest_route' => '/site',
21 'rest_min_jp_version' => '15.9',
22 'allow_jetpack_site_auth' => true,
23
24 'allow_fallback_to_jetpack_blog_token' => true,
25
26 'query_parameters' => array(
27 'context' => false,
28 'options' => '(string) Optional. Returns specified options only. Comma-separated list. Example: options=login_url,timezone',
29 ),
30
31 'response_format' => WPCOM_JSON_API_GET_Site_Endpoint::$site_format,
32
33 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/',
34 )
35 );
36
37 /**
38 * GET Site endpoint class.
39 *
40 * @phan-constructor-used-for-side-effects
41 */
42 class WPCOM_JSON_API_GET_Site_Endpoint extends WPCOM_JSON_API_Endpoint {
43
44 /**
45 * Site meta data.
46 *
47 * @var array $site_format
48 */
49 public static $site_format = array(
50 'ID' => '(int) Site ID',
51 'slug' => '(string) Slug of site',
52 'name' => '(string) Title of site',
53 'description' => '(string) Tagline or description of site',
54 'URL' => '(string) Full URL to the site',
55 'user_can_manage' => '(bool) The current user can manage this site', // deprecated.
56 'capabilities' => '(array) Array of capabilities for the current user on this site.',
57 'jetpack' => '(bool) Whether the site is a Jetpack site or not',
58 'jetpack_connection' => '(bool) Whether the site is connected to WP.com via `jetpack-connection`',
59 'is_multisite' => '(bool) Whether the site is a Multisite site or not. Always true for WP.com sites.',
60 'site_owner' => '(int) User ID of the site owner',
61 'post_count' => '(int) The number of posts the site has',
62 'subscribers_count' => '(int) The number of subscribers the site has',
63 'lang' => '(string) Primary language code of the site',
64 'icon' => '(array) An array of icon formats for the site',
65 'logo' => '(array) The site logo, set in the Customizer',
66 'visible' => '(bool) If this site is visible in the user\'s site list',
67 'is_private' => '(bool) If the site is a private site or not',
68 'is_coming_soon' => '(bool) If the site is marked as "coming soon" or not',
69 '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.',
70 'is_vip' => '(bool) If the site is a VIP site or not.',
71 'is_following' => '(bool) If the current user is subscribed to this site in the reader',
72 'organization_id' => '(int) P2 Organization identifier.',
73 '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/',
74 'p2_thumbnail_elements' => '(array) Details used to render a thumbnail of the site. P2020 themed sites only.',
75 'plan' => '(array) Details of the current plan for this site.',
76 'products' => '(array) Details of the current products for this site.',
77 'zendesk_site_meta' => '(array) Site meta data for Zendesk.',
78 'updates' => '(array) An array of available updates for plugins, themes, wordpress, and languages.',
79 'jetpack_modules' => '(array) A list of active Jetpack modules.',
80 'meta' => '(object) Meta data',
81 'quota' => '(array) An array describing how much space a user has left for uploads',
82 'launch_status' => '(string) A string describing the launch status of a site',
83 'site_migration' => '(array) Data about any migration into the site.',
84 'is_fse_active' => '(bool) If the site has Full Site Editing active or not.',
85 'is_fse_eligible' => '(bool) If the site is capable of Full Site Editing or not',
86 'is_core_site_editor_enabled' => '(bool) If the site has the core site editor enabled.',
87 'is_wpcom_atomic' => '(bool) If the site is a WP.com Atomic one.',
88 'is_wpcom_staging_site' => '(bool) If the site is a WP.com staging site.',
89 'user_interactions' => '(array) An array of user interactions with a site.',
90 'was_ecommerce_trial' => '(bool) If the site ever used an eCommerce trial.',
91 'was_upgraded_from_trial' => '(bool) If the site ever upgraded to a paid plan from a trial.',
92 'was_migration_trial' => '(bool) If the site ever used a migration trial.',
93 'was_hosting_trial' => '(bool) If the site ever used a hosting trial.',
94 'wpcom_site_setup' => '(string) The WP.com site setup identifier.',
95 'is_deleted' => '(bool) If the site flagged as deleted.',
96 'is_a4a_client' => '(bool) If the site is an A4A client site.',
97 'is_a4a_dev_site' => '(bool) If the site is an A4A dev site.',
98 'is_garden' => '(bool) If the site is a Garden site.',
99 'garden_name' => '(string) The name of the Garden site.',
100 'garden_partner' => '(string) The partner of the Garden site.',
101 'garden_is_provisioned' => '(bool) If the Garden site is provisioned.',
102 'is_wpcom_flex' => '(bool) If the site is a Flex site',
103 'big_sky_enabled' => '(bool) Whether the Big Sky AI assistant is enabled for this site.',
104 'atomic_email_block' => '(object) State of the block on the site\'s outgoing email, with `status` (`blocked` while a block is active, `at_risk` once it has expired), `reason` and `expires_on` keys, or null when the site has no block history. WordPress.com platform only.',
105 'hosting_provider_guess' => '(string) Guess of the hosting provider. WordPress.com platform only; only returned when explicitly requested via the fields parameter.',
106 'environment_type' => '(string) The WP_ENVIRONMENT_TYPE of the site as synced by Jetpack. WordPress.com platform only; only returned when explicitly requested via the fields parameter.',
107 );
108
109 /**
110 * No member fields.
111 *
112 * @var array $no_member_fields
113 */
114 protected static $no_member_fields = array(
115 'ID',
116 'name',
117 'description',
118 'URL',
119 'jetpack',
120 'jetpack_connection',
121 'post_count',
122 'subscribers_count',
123 'lang',
124 'locale',
125 'icon',
126 'logo',
127 'visible',
128 'is_private',
129 'is_coming_soon',
130 'is_following',
131 'organization_id',
132 'meta',
133 'launch_status',
134 'site_migration',
135 'is_fse_active',
136 'is_fse_eligible',
137 'is_core_site_editor_enabled',
138 'is_wpcom_atomic',
139 'is_wpcom_staging_site',
140 'is_deleted',
141 'is_wpcom_flex',
142 'is_a4a_client',
143 'is_a4a_dev_site',
144 'is_garden',
145 'big_sky_enabled',
146 'garden_name',
147 );
148
149 /**
150 * Site options.
151 *
152 * @var array $site_options_format
153 */
154 protected static $site_options_format = array(
155 'timezone',
156 'gmt_offset',
157 'blog_public',
158 'videopress_enabled',
159 'upgraded_filetypes_enabled',
160 'login_url',
161 'admin_url',
162 'is_mapped_domain',
163 'is_redirect',
164 'unmapped_url',
165 'featured_images_enabled',
166 'theme_slug',
167 'theme_errors',
168 'header_image',
169 'background_color',
170 'image_default_link_type',
171 'image_thumbnail_width',
172 'image_thumbnail_height',
173 'image_thumbnail_crop',
174 'image_medium_width',
175 'image_medium_height',
176 'image_large_width',
177 'image_large_height',
178 'permalink_structure',
179 'post_formats',
180 'default_post_format',
181 'default_category',
182 'allowed_file_types',
183 'show_on_front',
184 /** This filter is documented in modules/likes.php */
185 'default_likes_enabled',
186 'default_sharing_status',
187 'default_comment_status',
188 'default_ping_status',
189 'software_version',
190 'created_at',
191 'updated_at',
192 'wordads',
193 'publicize_permanently_disabled',
194 'frame_nonce',
195 'jetpack_frame_nonce',
196 'page_on_front',
197 'page_for_posts',
198 'headstart',
199 'headstart_is_fresh',
200 'ak_vp_bundle_enabled',
201 Jetpack_SEO_Utils::FRONT_PAGE_META_OPTION,
202 Jetpack_SEO_Titles::TITLE_FORMATS_OPTION,
203 'verification_services_codes',
204 'podcasting_archive',
205 'is_domain_only',
206 'is_automated_transfer',
207 'is_wpcom_atomic',
208 'is_wpcom_store',
209 'signup_is_store',
210 'has_pending_automated_transfer',
211 'woocommerce_is_active',
212 'editing_toolkit_is_active',
213 'design_type',
214 'site_goals',
215 'site_segment',
216 'site_source_slug',
217 'import_engine',
218 'is_pending_plan',
219 'is_wpforteams_site',
220 'p2_hub_blog_id',
221 'site_creation_flow',
222 'is_cloud_eligible',
223 'selected_features',
224 'anchor_podcast',
225 'was_created_with_blank_canvas_design',
226 'videopress_storage_used',
227 'is_difm_lite_in_progress',
228 'difm_lite_site_options',
229 'is_gating_business_q1',
230 'is_legacy_gating_site',
231 'site_intent',
232 'site_partner_bundle',
233 'onboarding_segment',
234 'site_vertical_id',
235 'blogging_prompts_settings',
236 'launchpad_screen',
237 'launchpad_checklist_tasks_statuses',
238 'migration_source_site_domain',
239 'wpcom_production_blog_id',
240 'wpcom_staging_blog_ids',
241 'can_blaze',
242 'wpcom_site_setup',
243 'is_commercial',
244 'is_commercial_reasons',
245 'wpcom_admin_interface',
246 'wpcom_classic_early_release',
247 'jetpack_recovery_mode_status',
248 'apm_enabled',
249 'wpcom_ai_launchpad_enabled',
250 'wpcom_ai_launchpad_dismissed',
251 'wpcom_ai_launchpad_completed',
252 );
253
254 /**
255 * Jetpack response fields.
256 *
257 * @var array $jetpack_response_field_additions
258 */
259 protected static $jetpack_response_field_additions = array(
260 'slug',
261 'subscribers_count',
262 'site_migration',
263 'site_owner',
264 'is_wpcom_staging_site',
265 'was_ecommerce_trial',
266 'was_migration_trial',
267 'was_hosting_trial',
268 'was_upgraded_from_trial',
269 'is_a4a_dev_site',
270 'is_garden',
271 'garden_name',
272 'garden_partner',
273 'garden_is_provisioned',
274 'is_wpcom_flex',
275 'big_sky_enabled',
276 'atomic_email_block',
277 );
278
279 /**
280 * Jetpack response field member additions.
281 *
282 * @var array $jetpack_response_field_member_additions
283 */
284 protected static $jetpack_response_field_member_additions = array(
285 'capabilities',
286 'plan',
287 'products',
288 'zendesk_site_meta',
289 );
290
291 /**
292 * Jetpack response option additions.
293 *
294 * @var array $jetpack_response_option_additions
295 */
296 protected static $jetpack_response_option_additions = array(
297 'publicize_permanently_disabled',
298 'ak_vp_bundle_enabled',
299 'is_automated_transfer',
300 'is_wpcom_atomic',
301 'is_wpcom_store',
302 'woocommerce_is_active',
303 'editing_toolkit_is_active',
304 'frame_nonce',
305 'jetpack_frame_nonce',
306 'design_type',
307 'wordads',
308 // Use the site registered date from wpcom, since it is only available in a multisite context
309 // and defaults to `0000-00-00T00:00:00+00:00` from the Jetpack site.
310 // See https://github.com/Automattic/jetpack/blob/58638f46094b36f5df9cbc4570006544f0ad300c/sal/class.json-api-site-base.php#L387.
311 'created_at',
312 'updated_at',
313 'is_pending_plan',
314 'is_cloud_eligible',
315 'videopress_storage_used',
316 'blogging_prompts_settings',
317 'wpcom_production_blog_id',
318 'wpcom_staging_blog_ids',
319 'is_commercial',
320 'is_commercial_reasons',
321 'wpcom_admin_interface',
322 'wpcom_classic_early_release',
323 'jetpack_recovery_mode_status',
324 'apm_enabled',
325 );
326
327 /**
328 * Current enabled trials.
329 *
330 * @var array $jetpack_enabled_trials
331 */
332 public static $jetpack_enabled_trials = array(
333 'was_ecommerce_trial' => 'ecommerce',
334 'was_migration_trial' => 'migration',
335 'was_hosting_trial' => 'hosting',
336 );
337
338 /**
339 * Site.
340 *
341 * @var SAL_Site $site.
342 */
343 private $site;
344
345 /**
346 * Fields to include.
347 *
348 * @var $fields_to_include
349 */
350 protected $fields_to_include = '_all';
351
352 /**
353 * Options to include.
354 *
355 * @var $options_to_include
356 */
357 protected $options_to_include = '_all';
358
359 /**
360 *
361 * API callback.
362 *
363 * /sites/mine
364 * /sites/%s -> $blog_id\
365 *
366 * @param string $path - the path.
367 * @param int|string $blog_id - the blog ID or the string 'mine'.
368 *
369 * @return array|\WP_Error Site response array on success, or WP_Error on failure.
370 */
371 public function callback( $path = '', $blog_id = 0 ) {
372 if ( 'mine' === $blog_id ) {
373 $api = WPCOM_JSON_API::init();
374 if ( ! $api->token_details || empty( $api->token_details['blog_id'] ) ) {
375 return new WP_Error( 'authorization_required', 'An active access token must be used to query information about the current blog.', 403 );
376 }
377 $blog_id = $api->token_details['blog_id'];
378 }
379
380 add_filter( 'wpcom_allow_jetpack_blog_token', '__return_true' );
381 $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
382 if ( is_wp_error( $blog_id ) ) {
383 return $blog_id;
384 }
385
386 $this->filter_fields_and_options();
387
388 $response = $this->build_current_site_response();
389
390 /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
391 do_action( 'wpcom_json_api_objects', 'sites' );
392
393 return $response;
394 }
395
396 /**
397 * Filter fields and options.
398 */
399 public function filter_fields_and_options() {
400 $query_args = $this->query_args();
401
402 $this->fields_to_include = empty( $query_args['fields'] ) ? '_all' : array_map( 'trim', explode( ',', $query_args['fields'] ) );
403 $this->options_to_include = empty( $query_args['options'] ) ? '_all' : array_map( 'trim', explode( ',', $query_args['options'] ) );
404 }
405
406 /**
407 * Collects the necessary information to return for a site's response.
408 *
409 * @return array
410 */
411 public function build_current_site_response() {
412
413 $blog_id = (int) $this->api->get_blog_id_for_output();
414
415 $this->site = $this->get_platform()->get_site( $blog_id );
416
417 /**
418 * Filter the structure of information about the site to return.
419 *
420 * @module json-api
421 *
422 * @since 3.9.3
423 *
424 * @param array $site_format Data structure.
425 */
426 $default_fields = array_keys( apply_filters( 'sites_site_format', self::$site_format ) );
427
428 $response_keys = is_array( $this->fields_to_include ) ?
429 array_intersect( $default_fields, $this->fields_to_include ) :
430 $default_fields;
431
432 $has_blog_access = $this->has_blog_access( $this->api->token_details );
433 $has_user_access = $this->has_user_access();
434
435 if ( ! $has_user_access && ! $has_blog_access ) {
436 // Public access without user or blog auth, only return `$no_member_fields`.
437 $response_keys = array_intersect( $response_keys, self::$no_member_fields );
438 } elseif ( $has_user_access && ! current_user_can( 'edit_posts' ) ) {
439 // Subscriber level user, don't return site options.
440 $response_keys = array_diff( $response_keys, array( 'options' ) );
441 }
442
443 return $this->render_response_keys( $response_keys );
444 }
445
446 /**
447 * Checks that the current user has access to the current blog.
448 *
449 * @return bool Whether or not the current user can access the current blog.
450 */
451 private function has_user_access() {
452 return is_user_member_of_blog( get_current_user_id(), get_current_blog_id() );
453 }
454
455 /**
456 * Checks if the request has a valid blog token for the current blog.
457 *
458 * @param array $token_details Access token for the api request.
459 * @return bool
460 */
461 private function has_blog_access( $token_details ) {
462 $token_details = (array) $token_details;
463 if ( ! isset( $token_details['access'] ) || ! isset( $token_details['auth'] ) || ! isset( $token_details['blog_id'] ) ) {
464 return false;
465 }
466
467 return 'jetpack' === $token_details['auth'] &&
468 'blog' === $token_details['access'] &&
469 get_current_blog_id() === $token_details['blog_id'];
470 }
471
472 /**
473 * Render response keys.
474 *
475 * @param array $response_keys - the response keys.
476 */
477 private function render_response_keys( &$response_keys ) {
478 $response = array();
479
480 $is_user_logged_in = is_user_logged_in();
481
482 $this->site->before_render();
483
484 foreach ( $response_keys as $key ) {
485 $this->render_response_key( $key, $response, $is_user_logged_in );
486 }
487
488 $this->site->after_render( $response );
489
490 return $response;
491 }
492
493 /**
494 * Render response key.
495 *
496 * @param string $key - the key.
497 * @param array $response - the response.
498 * @param boolean $is_user_logged_in - if the user is logged in.
499 */
500 protected function render_response_key( $key, &$response, $is_user_logged_in ) {
501 do_action( 'pre_render_site_response_key', $key );
502
503 switch ( $key ) {
504 case 'ID':
505 $response[ $key ] = $this->site->blog_id;
506 break;
507 case 'slug':
508 $response[ $key ] = $this->site->get_slug();
509 break;
510 case 'name':
511 $response[ $key ] = $this->site->get_name();
512 break;
513 case 'description':
514 $response[ $key ] = $this->site->get_description();
515 break;
516 case 'URL':
517 $response[ $key ] = $this->site->get_url();
518 break;
519 case 'user_can_manage':
520 $response[ $key ] = $this->site->user_can_manage();
521 // fall through is intentional.
522 case 'is_private':
523 $response[ $key ] = $this->site->is_private();
524 break;
525 case 'is_coming_soon':
526 // This option is stored on wp.com for both simple and atomic sites. @see mu-plugins/private-blog.php.
527 $response[ $key ] = $this->site->is_coming_soon();
528
529 break;
530 case 'launch_status':
531 $response[ $key ] = $this->site->get_launch_status();
532 break;
533 case 'visible':
534 $response[ $key ] = $this->site->is_visible();
535 break;
536 case 'subscribers_count':
537 $response[ $key ] = $this->site->get_subscribers_count();
538 break;
539 case 'post_count':
540 if ( $is_user_logged_in ) {
541 $response[ $key ] = $this->site->get_post_count();
542 }
543 break;
544 case 'icon':
545 $icon = $this->site->get_icon();
546
547 if ( $icon !== null ) {
548 $response[ $key ] = $icon;
549 }
550 break;
551 case 'logo':
552 $response[ $key ] = $this->site->get_logo();
553 break;
554 case 'is_following':
555 $response[ $key ] = $this->site->is_following();
556 break;
557 case 'options':
558 // small optimisation - don't recalculate.
559 $all_options = apply_filters( 'sites_site_options_format', self::$site_options_format );
560
561 $options_response_keys = is_array( $this->options_to_include ) ?
562 array_intersect( $all_options, $this->options_to_include ) :
563 $all_options;
564
565 $options = $this->render_option_keys( $options_response_keys );
566
567 $this->site->after_render_options( $options );
568
569 $response[ $key ] = (object) $options;
570 break;
571 case 'meta':
572 $this->build_meta_response( $response );
573 break;
574 case 'lang':
575 $response[ $key ] = $is_user_logged_in ? $this->site->get_locale() : false;
576 break;
577 case 'locale':
578 $response[ $key ] = $is_user_logged_in ? $this->site->get_locale() : false;
579 break;
580 case 'jetpack':
581 $response[ $key ] = $this->site->is_jetpack();
582 break;
583 case 'jetpack_connection':
584 $response[ $key ] = $this->site->is_jetpack_connection();
585 break;
586 case 'single_user_site':
587 $response[ $key ] = $this->site->is_single_user_site();
588 break;
589 case 'is_vip':
590 $response[ $key ] = $this->site->is_vip();
591 break;
592 case 'is_multisite':
593 $response[ $key ] = $this->site->is_multisite();
594 break;
595 case 'site_owner':
596 $response[ $key ] = $this->site->get_site_owner();
597 break;
598 case 'organization_id':
599 $response[ $key ] = $this->site->get_p2_organization_id();
600 break;
601
602 case 'capabilities':
603 $response[ $key ] = $this->site->get_capabilities();
604 break;
605 case 'jetpack_modules':
606 if ( is_user_member_of_blog() ) {
607 $response[ $key ] = $this->site->get_jetpack_modules();
608 }
609 break;
610 case 'plan':
611 $response[ $key ] = $this->site->get_plan();
612 break;
613 case 'products':
614 $response[ $key ] = $this->site->get_products();
615 break;
616 case 'zendesk_site_meta':
617 $response[ $key ] = $this->site->get_zendesk_site_meta();
618 break;
619 case 'quota':
620 $response[ $key ] = $this->site->get_quota();
621 break;
622 case 'site_migration':
623 $response[ $key ] = $this->site->get_migration_meta();
624 break;
625 case 'is_fse_active':
626 $response[ $key ] = $this->site->is_fse_active();
627 break;
628 case 'is_fse_eligible':
629 $response[ $key ] = $this->site->is_fse_eligible();
630 break;
631 case 'is_core_site_editor_enabled':
632 $response[ $key ] = $this->site->is_core_site_editor_enabled();
633 break;
634 case 'is_wpcom_atomic':
635 $response[ $key ] = $this->site->is_wpcom_atomic();
636 break;
637 case 'is_wpcom_staging_site':
638 $response[ $key ] = $this->site->is_wpcom_staging_site();
639 break;
640 case 'user_interactions':
641 $response[ $key ] = $this->site->get_user_interactions();
642 break;
643 case 'p2_thumbnail_elements':
644 $response[ $key ] = $this->site->get_p2_thumbnail_elements();
645 break;
646 case 'was_ecommerce_trial':
647 $response[ $key ] = $this->site->was_trial( self::$jetpack_enabled_trials['was_ecommerce_trial'] );
648 break;
649 case 'was_migration_trial':
650 $response[ $key ] = $this->site->was_trial( self::$jetpack_enabled_trials['was_migration_trial'] );
651 break;
652 case 'was_hosting_trial':
653 $response[ $key ] = $this->site->was_trial( self::$jetpack_enabled_trials['was_hosting_trial'] );
654 break;
655 case 'was_upgraded_from_trial':
656 $response[ $key ] = $this->site->was_upgraded_from_trial();
657 break;
658 case 'is_deleted':
659 $response[ $key ] = $this->site->is_deleted();
660 break;
661 case 'is_a4a_client':
662 $response[ $key ] = $this->site->is_a4a_client();
663 break;
664 case 'is_a4a_dev_site':
665 $response[ $key ] = $this->site->is_a4a_dev_site();
666 break;
667 case 'is_garden':
668 $response[ $key ] = $this->site->is_garden();
669 break;
670 case 'garden_name':
671 $response[ $key ] = $this->site->garden_name();
672 break;
673 case 'garden_partner':
674 $response[ $key ] = $this->site->garden_partner();
675 break;
676 case 'garden_is_provisioned':
677 $response[ $key ] = $this->site->garden_is_provisioned();
678 break;
679 case 'is_wpcom_flex':
680 $response[ $key ] = $this->site->is_wpcom_flex();
681 break;
682 case 'big_sky_enabled':
683 $response[ $key ] = $this->site->is_big_sky_enabled();
684 break;
685 case 'atomic_email_block':
686 $response[ $key ] = $this->site->get_atomic_email_block();
687 break;
688 case 'hosting_provider_guess':
689 // WordPress.com platform decoration, computed only when explicitly requested
690 // via `fields` so default `_all` responses are unchanged.
691 if ( $this->is_wpcom()
692 && function_exists( 'get_jetpack_hosting_provider' )
693 && is_array( $this->fields_to_include ) ) {
694 $response[ $key ] = get_jetpack_hosting_provider( get_current_blog_id() );
695 }
696 break;
697 case 'environment_type':
698 // WordPress.com platform decoration, computed only when explicitly requested
699 // via `fields` so default `_all` responses are unchanged.
700 if ( $this->is_wpcom() && is_array( $this->fields_to_include ) ) {
701 $response[ $key ] = $this->get_site_environment_type();
702 }
703 break;
704 }
705
706 do_action( 'post_render_site_response_key', $key );
707 }
708
709 /**
710 * Render option keys.
711 *
712 * @param array $options_response_keys - the response keys.
713 */
714 protected function render_option_keys( &$options_response_keys ) {
715 $options = array();
716 $site = $this->site;
717
718 $custom_front_page = $site->is_custom_front_page();
719
720 foreach ( $options_response_keys as $key ) {
721 switch ( $key ) {
722 case 'timezone':
723 $options[ $key ] = $site->get_timezone();
724 break;
725 case 'gmt_offset':
726 $options[ $key ] = $site->get_gmt_offset();
727 break;
728 case 'videopress_enabled':
729 $options[ $key ] = $site->has_videopress();
730 break;
731 case 'upgraded_filetypes_enabled':
732 $options[ $key ] = $site->upgraded_filetypes_enabled();
733 break;
734 case 'login_url':
735 $options[ $key ] = $site->get_login_url();
736 break;
737 case 'admin_url':
738 $options[ $key ] = $site->get_admin_url();
739 break;
740 case 'is_mapped_domain':
741 $options[ $key ] = $site->is_mapped_domain();
742 break;
743 case 'is_redirect':
744 $options[ $key ] = $site->is_redirect();
745 break;
746 case 'unmapped_url':
747 $options[ $key ] = $site->get_unmapped_url();
748 break;
749 case 'featured_images_enabled':
750 $options[ $key ] = $site->featured_images_enabled();
751 break;
752 case 'theme_slug':
753 $options[ $key ] = $site->get_theme_slug();
754 break;
755 case 'theme_errors':
756 $options[ $key ] = $site->get_theme_errors();
757 break;
758 case 'header_image':
759 $options[ $key ] = $site->get_header_image();
760 break;
761 case 'background_color':
762 $options[ $key ] = $site->get_background_color();
763 break;
764 case 'image_default_link_type':
765 $options[ $key ] = $site->get_image_default_link_type();
766 break;
767 case 'image_thumbnail_width':
768 $options[ $key ] = $site->get_image_thumbnail_width();
769 break;
770 case 'image_thumbnail_height':
771 $options[ $key ] = $site->get_image_thumbnail_height();
772 break;
773 case 'image_thumbnail_crop':
774 $options[ $key ] = $site->get_image_thumbnail_crop();
775 break;
776 case 'image_medium_width':
777 $options[ $key ] = $site->get_image_medium_width();
778 break;
779 case 'image_medium_height':
780 $options[ $key ] = $site->get_image_medium_height();
781 break;
782 case 'image_large_width':
783 $options[ $key ] = $site->get_image_large_width();
784 break;
785 case 'image_large_height':
786 $options[ $key ] = $site->get_image_large_height();
787 break;
788 case 'permalink_structure':
789 $options[ $key ] = $site->get_permalink_structure();
790 break;
791 case 'post_formats':
792 $options[ $key ] = $site->get_post_formats();
793 break;
794 case 'default_post_format':
795 $options[ $key ] = $site->get_default_post_format();
796 break;
797 case 'default_category':
798 $options[ $key ] = $site->get_default_category();
799 break;
800 case 'allowed_file_types':
801 $options[ $key ] = $site->allowed_file_types();
802 break;
803 case 'show_on_front':
804 $options[ $key ] = $site->get_show_on_front();
805 break;
806 /** This filter is documented in modules/likes.php */
807 case 'default_likes_enabled':
808 $options[ $key ] = $site->get_default_likes_enabled();
809 break;
810 case 'default_sharing_status':
811 $options[ $key ] = $site->get_default_sharing_status();
812 break;
813 case 'default_comment_status':
814 $options[ $key ] = $site->get_default_comment_status();
815 break;
816 case 'default_ping_status':
817 $options[ $key ] = $site->default_ping_status();
818 break;
819 case 'software_version':
820 $options[ $key ] = $site->get_wordpress_version();
821 break;
822 case 'created_at':
823 $options[ $key ] = $site->get_registered_date();
824 break;
825 case 'updated_at':
826 $options[ $key ] = $site->get_last_update_date();
827 break;
828 case 'wordads':
829 $options[ $key ] = $site->has_wordads();
830 break;
831 case 'publicize_permanently_disabled':
832 $options[ $key ] = $site->is_publicize_permanently_disabled();
833 break;
834 case 'frame_nonce':
835 $options[ $key ] = $site->get_frame_nonce();
836 break;
837 case 'jetpack_frame_nonce':
838 $options[ $key ] = $site->get_jetpack_frame_nonce();
839 break;
840 case 'page_on_front':
841 if ( $custom_front_page ) {
842 $options[ $key ] = $site->get_page_on_front();
843 }
844 break;
845 case 'page_for_posts':
846 if ( $custom_front_page ) {
847 $options[ $key ] = $site->get_page_for_posts();
848 }
849 break;
850 case 'headstart':
851 $options[ $key ] = $site->is_headstart();
852 break;
853 case 'headstart_is_fresh':
854 $options[ $key ] = $site->is_headstart_fresh();
855 break;
856 case 'ak_vp_bundle_enabled':
857 $options[ $key ] = $site->get_ak_vp_bundle_enabled();
858 break;
859 case Jetpack_SEO_Utils::FRONT_PAGE_META_OPTION:
860 $options[ $key ] = $site->get_jetpack_seo_front_page_description();
861 break;
862 case Jetpack_SEO_Titles::TITLE_FORMATS_OPTION:
863 $options[ $key ] = $site->get_jetpack_seo_title_formats();
864 break;
865 case 'verification_services_codes':
866 $options[ $key ] = $site->get_verification_services_codes();
867 break;
868 case 'podcasting_archive':
869 $options[ $key ] = $site->get_podcasting_archive();
870 break;
871 case 'is_domain_only':
872 $options[ $key ] = $site->is_domain_only();
873 break;
874 case 'is_automated_transfer':
875 $options[ $key ] = $site->is_automated_transfer();
876 break;
877 case 'blog_public':
878 $options[ $key ] = $site->get_blog_public();
879 break;
880 case 'is_wpcom_atomic':
881 $options[ $key ] = $site->is_wpcom_atomic();
882 break;
883 case 'is_wpcom_store':
884 $options[ $key ] = $site->is_wpcom_store();
885 break;
886 case 'signup_is_store':
887 $signup_is_store = $site->signup_is_store();
888
889 if ( $signup_is_store ) {
890 $options[ $key ] = $site->signup_is_store();
891 }
892
893 break;
894 case 'has_pending_automated_transfer':
895 $has_pending_automated_transfer = $site->has_pending_automated_transfer();
896
897 if ( $has_pending_automated_transfer ) {
898 $options[ $key ] = true;
899 }
900
901 break;
902 case 'woocommerce_is_active':
903 $options[ $key ] = $site->woocommerce_is_active();
904 break;
905 case 'editing_toolkit_is_active':
906 $options[ $key ] = $site->editing_toolkit_is_active();
907 break;
908 case 'design_type':
909 $options[ $key ] = $site->get_design_type();
910 break;
911 case 'site_segment':
912 $options[ $key ] = $site->get_site_segment();
913 break;
914 case 'import_engine':
915 $options[ $key ] = $site->get_import_engine();
916 break;
917 case 'is_pending_plan':
918 $options[ $key ] = $site->is_pending_plan();
919 break;
920
921 case 'is_wpforteams_site':
922 $options[ $key ] = $site->is_wpforteams_site();
923 break;
924 case 'p2_hub_blog_id':
925 $options[ $key ] = $site->get_p2_hub_blog_id();
926 break;
927
928 case 'site_creation_flow':
929 $site_creation_flow = $site->get_site_creation_flow();
930 if ( $site_creation_flow ) {
931 $options[ $key ] = $site_creation_flow;
932 }
933 break;
934 case 'site_source_slug':
935 $site_source_slug = $site->get_site_source_slug();
936 if ( $site_source_slug ) {
937 $options[ $key ] = $site_source_slug;
938 }
939 break;
940 case 'is_cloud_eligible':
941 $options[ $key ] = $site->is_cloud_eligible();
942 break;
943 case 'selected_features':
944 $selected_features = $site->get_selected_features();
945 if ( $selected_features ) {
946 $options[ $key ] = $selected_features;
947 }
948 break;
949 case 'anchor_podcast':
950 $options[ $key ] = $site->get_anchor_podcast();
951 break;
952 case 'was_created_with_blank_canvas_design':
953 $options[ $key ] = $site->was_created_with_blank_canvas_design();
954 break;
955 case 'videopress_storage_used':
956 $options[ $key ] = $this->site->get_videopress_storage_used();
957 break;
958 case 'is_difm_lite_in_progress':
959 $options[ $key ] = $site->is_difm_lite_in_progress();
960 break;
961 case 'difm_lite_site_options':
962 $difm_lite_site_options = $site->get_difm_lite_site_options();
963 if ( null !== $difm_lite_site_options ) {
964 $options[ $key ] = (object) $difm_lite_site_options;
965 }
966 break;
967 case 'is_gating_business_q1':
968 $options[ $key ] = $site->is_gating_business_q1();
969 break;
970 case 'is_legacy_gating_site':
971 $options[ $key ] = $site->is_legacy_gating_site();
972 break;
973 case 'site_intent':
974 $options[ $key ] = $site->get_site_intent();
975 break;
976 case 'site_partner_bundle':
977 $options[ $key ] = $site->get_site_partner_bundle();
978 break;
979 case 'site_goals':
980 $options[ $key ] = $site->get_site_goals();
981 break;
982 case 'onboarding_segment':
983 $options[ $key ] = $site->get_onboarding_segment();
984 break;
985 case 'site_vertical_id':
986 $options[ $key ] = $site->get_site_vertical_id();
987 break;
988 case 'blogging_prompts_settings':
989 if ( current_user_can( 'edit_posts' ) ) {
990 $options[ $key ] = $site->get_blogging_prompts_settings( get_current_user_id(), $site->blog_id );
991 }
992 break;
993 case 'launchpad_screen':
994 $options[ $key ] = $site->get_launchpad_screen();
995 break;
996 case 'launchpad_checklist_tasks_statuses':
997 $options[ $key ] = $site->get_launchpad_checklist_tasks_statuses();
998 break;
999 case 'migration_source_site_domain':
1000 $options[ $key ] = $site->get_migration_source_site_domain();
1001 break;
1002 case 'wpcom_production_blog_id':
1003 $options[ $key ] = $site->get_wpcom_production_blog_id();
1004 break;
1005 case 'wpcom_staging_blog_ids':
1006 $options[ $key ] = $site->get_wpcom_staging_blog_ids();
1007 break;
1008 case 'can_blaze':
1009 $options[ $key ] = $site->can_blaze();
1010 break;
1011 case 'wpcom_site_setup':
1012 $options[ $key ] = $site->get_wpcom_site_setup();
1013 break;
1014 case 'is_commercial':
1015 $options[ $key ] = $site->is_commercial();
1016 break;
1017 case 'is_commercial_reasons':
1018 $options[ $key ] = $site->get_is_commercial_reasons();
1019 break;
1020 case 'wpcom_admin_interface':
1021 $options[ $key ] = $site->get_wpcom_admin_interface();
1022 break;
1023 case 'wpcom_classic_early_release':
1024 $options[ $key ] = $site->get_wpcom_classic_early_release();
1025 break;
1026 case 'jetpack_recovery_mode_status':
1027 $options[ $key ] = $site->get_jetpack_recovery_mode_status();
1028 break;
1029 case 'apm_enabled':
1030 $options[ $key ] = $site->get_apm_enabled();
1031 break;
1032 case 'wpcom_ai_launchpad_enabled':
1033 $options[ $key ] = $site->is_ai_launchpad_enabled();
1034 break;
1035 case 'wpcom_ai_launchpad_dismissed':
1036 $options[ $key ] = $site->is_ai_launchpad_dismissed();
1037 break;
1038 case 'wpcom_ai_launchpad_completed':
1039 $options[ $key ] = $site->is_ai_launchpad_completed();
1040 break;
1041 }
1042 }
1043
1044 return $options;
1045 }
1046
1047 /**
1048 * Build meta response.
1049 *
1050 * @param array $response - the response.
1051 */
1052 protected function build_meta_response( &$response ) {
1053 $links = array(
1054 'self' => (string) $this->links->get_site_link( $this->site->blog_id ),
1055 'help' => (string) $this->links->get_site_link( $this->site->blog_id, 'help' ),
1056 'posts' => (string) $this->links->get_site_link( $this->site->blog_id, 'posts/' ),
1057 'comments' => (string) $this->links->get_site_link( $this->site->blog_id, 'comments/' ),
1058 'xmlrpc' => (string) $this->site->get_xmlrpc_url(),
1059 );
1060
1061 $icon = $this->site->get_icon();
1062 if ( ! empty( $icon ) && ! empty( $icon['media_id'] ) ) {
1063 $links['site_icon'] = (string) $this->links->get_site_link( $this->site->blog_id, 'media/' . $icon['media_id'] );
1064 }
1065
1066 $response['meta'] = (object) array(
1067 'links' => (object) $links,
1068 );
1069 }
1070
1071 /**
1072 * Apply any WPCOM-only response components to a Jetpack site response.
1073 *
1074 * @param object $response - the response.
1075 */
1076 public function decorate_jetpack_response( &$response ) {
1077 $this->filter_fields_and_options();
1078 $this->site = $this->get_platform()->get_site( $response->ID );
1079 switch_to_blog( $this->site->get_id() );
1080
1081 // Allow the SAL site to apply its own overrides to the proxied response.
1082 if ( method_exists( $this->site, 'decorate_jetpack_response' ) ) {
1083 $this->site->decorate_jetpack_response( $response ); // @phan-suppress-current-line PhanUndeclaredMethod -- checked via method_exists().
1084 }
1085
1086 $wpcom_response = $this->render_response_keys( self::$jetpack_response_field_additions );
1087
1088 foreach ( $wpcom_response as $key => $value ) {
1089 $response->{ $key } = $value;
1090 }
1091
1092 $has_site_access = $this->has_user_access() || $this->has_blog_access( $this->api->token_details );
1093
1094 if ( $has_site_access ) {
1095 $wpcom_member_response = $this->render_response_keys( self::$jetpack_response_field_member_additions );
1096
1097 foreach ( $wpcom_member_response as $key => $value ) {
1098 $response->{ $key } = $value;
1099 }
1100 } else {
1101 // ensure private data is not rendered for non members of the site.
1102 unset( $response->options );
1103 unset( $response->is_vip );
1104 unset( $response->single_user_site );
1105 unset( $response->is_private );
1106 unset( $response->is_coming_soon );
1107 unset( $response->capabilities );
1108 unset( $response->lang );
1109 unset( $response->user_can_manage );
1110 unset( $response->is_multisite );
1111 unset( $response->site_owner );
1112 unset( $response->plan );
1113 unset( $response->products );
1114 unset( $response->zendesk_site_meta );
1115 unset( $response->atomic_email_block );
1116 }
1117
1118 // render additional options.
1119 if ( isset( $response->options ) && $response->options ) {
1120 $wpcom_options_response = $this->render_option_keys( self::$jetpack_response_option_additions );
1121
1122 // Remove heic from jetpack (and atomic) sites so that the iOS app know to convert the file format into a JPEG.
1123 // heic fromat is currently not supported by for uploading.
1124 // See https://jetpackp2.wordpress.com/2020/08/19/image-uploads-in-the-wp-ios-app-broken
1125 if (
1126 $this->site->is_jetpack()
1127 && isset( $response->options['allowed_file_types'] )
1128 && is_array( $response->options['allowed_file_types'] )
1129 ) {
1130 $allowed_file_types = (array) $response->options['allowed_file_types'];
1131 $remove_file_types = array(
1132 'heic',
1133 );
1134 $response->options['allowed_file_types'] = array_values( array_diff( $allowed_file_types, $remove_file_types ) );
1135 }
1136
1137 foreach ( $wpcom_options_response as $key => $value ) {
1138 $response->options[ $key ] = $value;
1139 }
1140 }
1141
1142 // WordPress.com platform fields are not part of proxied Jetpack responses;
1143 // add them only when explicitly requested.
1144 if ( $this->is_wpcom() && $has_site_access && is_array( $this->fields_to_include ) ) {
1145 if ( function_exists( 'get_jetpack_hosting_provider' ) && in_array( 'hosting_provider_guess', $this->fields_to_include, true ) ) {
1146 $response->hosting_provider_guess = get_jetpack_hosting_provider( get_current_blog_id() );
1147 }
1148 if ( in_array( 'environment_type', $this->fields_to_include, true ) ) {
1149 $response->environment_type = $this->get_site_environment_type();
1150 }
1151 }
1152
1153 restore_current_blog();
1154 return $response; // possibly no need since it's modified in place.
1155 }
1156
1157 /**
1158 * Whether this request is running on the WordPress.com platform.
1159 *
1160 * @return bool
1161 */
1162 protected function is_wpcom() {
1163 return ( new \Automattic\Jetpack\Status\Host() )->is_wpcom_platform();
1164 }
1165
1166 /**
1167 * Get the site's synced WP_ENVIRONMENT_TYPE.
1168 *
1169 * @return string|null
1170 */
1171 protected function get_site_environment_type() {
1172 if ( function_exists( 'get_blog_option' ) ) {
1173 return get_blog_option( get_current_blog_id(), 'jetpack_callable_wp_get_environment_type', null );
1174 }
1175
1176 return get_option( 'jetpack_callable_wp_get_environment_type', null );
1177 }
1178 }
1179
1180 new WPCOM_JSON_API_List_Post_Formats_Endpoint(
1181 array(
1182 'description' => 'Get a list of post formats supported by a site.',
1183 'group' => '__do_not_document',
1184 'stat' => 'sites:X:post-formats',
1185
1186 'method' => 'GET',
1187 'path' => '/sites/%s/post-formats',
1188 'path_labels' => array(
1189 '$site' => '(int|string) Site ID or domain',
1190 ),
1191
1192 'query_parameters' => array(
1193 'context' => false,
1194 ),
1195
1196 'allow_fallback_to_jetpack_blog_token' => true,
1197
1198 'response_format' => array(
1199 'formats' => '(object) An object of supported post formats, each key a supported format slug mapped to its display string.',
1200 ),
1201 )
1202 );
1203
1204 /**
1205 * List Post Formats endpoint class.
1206 *
1207 * @phan-constructor-used-for-side-effects
1208 */
1209 class WPCOM_JSON_API_List_Post_Formats_Endpoint extends WPCOM_JSON_API_Endpoint { // phpcs:ignore
1210 /**
1211 *
1212 * API callback.
1213 *
1214 * /sites/%s/post-formats -> $blog_id
1215 *
1216 * @param string $path - the path.
1217 * @param int $blog_id - the blog ID.
1218 *
1219 * @return array|\WP_Error Array with 'formats' on success, or WP_Error on failure.
1220 */
1221 public function callback( $path = '', $blog_id = 0 ) {
1222 $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
1223 if ( is_wp_error( $blog_id ) ) {
1224 return $blog_id;
1225 }
1226
1227 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
1228 $this->load_theme_functions();
1229 }
1230
1231 // Get a list of supported post formats.
1232 $all_formats = get_post_format_strings();
1233 $supported = get_theme_support( 'post-formats' );
1234
1235 $response = array(
1236 'formats' => array(),
1237 );
1238 $supported_formats = $response['formats'];
1239
1240 if ( isset( $supported[0] ) ) {
1241 foreach ( $supported[0] as $format ) {
1242 $supported_formats[ $format ] = $all_formats[ $format ];
1243 }
1244 }
1245
1246 $response['formats'] = (object) $supported_formats;
1247
1248 return $response;
1249 }
1250 }
1251
1252 new WPCOM_JSON_API_List_Page_Templates_Endpoint(
1253 array(
1254 'description' => 'Get a list of page templates supported by a site.',
1255 'group' => 'sites',
1256 'stat' => 'sites:X:post-templates',
1257
1258 'method' => 'GET',
1259 'path' => '/sites/%s/page-templates',
1260 'path_labels' => array(
1261 '$site' => '(int|string) Site ID or domain',
1262 ),
1263 'query_parameters' => array(
1264 'context' => false,
1265 ),
1266 'response_format' => array(
1267 'templates' => '(array) A list of supported page templates. Contains label and file.',
1268 ),
1269 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/33534099/page-templates',
1270 )
1271 );
1272
1273 /**
1274 * List page templates endpoint class.
1275 *
1276 * @phan-constructor-used-for-side-effects
1277 */
1278 class WPCOM_JSON_API_List_Page_Templates_Endpoint extends WPCOM_JSON_API_Endpoint { // phpcs:ignore
1279 /**
1280 *
1281 * API callback.
1282 * /sites/%s/page-templates -> $blog_id
1283 *
1284 * @param string $path - the path.
1285 * @param int $blog_id - the blog ID.
1286 *
1287 * @return array|\WP_Error Array with 'templates' on success, or WP_Error on failure.
1288 */
1289 public function callback( $path = '', $blog_id = 0 ) {
1290 $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
1291 if ( is_wp_error( $blog_id ) ) {
1292 return $blog_id;
1293 }
1294
1295 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
1296 $this->load_theme_functions();
1297 }
1298
1299 $response = array();
1300 $page_templates = array();
1301
1302 $templates = get_page_templates();
1303 ksort( $templates );
1304
1305 foreach ( array_keys( $templates ) as $label ) {
1306 $page_templates[] = array(
1307 'label' => $label,
1308 'file' => $templates[ $label ],
1309 );
1310 }
1311
1312 $response['templates'] = $page_templates;
1313
1314 return $response;
1315 }
1316 }
1317