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