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