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