説明なし

class.wpcom-json-api-get-site-endpoint.php 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. <?php
  2. new WPCOM_JSON_API_GET_Site_Endpoint( array(
  3. 'description' => 'Get information about a site.',
  4. 'group' => 'sites',
  5. 'stat' => 'sites:X',
  6. 'allowed_if_flagged' => true,
  7. 'method' => 'GET',
  8. 'max_version' => '1.1',
  9. 'new_version' => '1.2',
  10. 'path' => '/sites/%s',
  11. 'path_labels' => array(
  12. '$site' => '(int|string) Site ID or domain',
  13. ),
  14. 'allow_jetpack_site_auth' => true,
  15. 'allow_fallback_to_jetpack_blog_token' => true,
  16. 'query_parameters' => array(
  17. 'context' => false,
  18. 'options' => '(string) Optional. Returns specified options only. Comma-separated list. Example: options=login_url,timezone',
  19. ),
  20. 'response_format' => WPCOM_JSON_API_GET_Site_Endpoint::$site_format,
  21. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/',
  22. ) );
  23. class WPCOM_JSON_API_GET_Site_Endpoint extends WPCOM_JSON_API_Endpoint {
  24. public static $site_format = array(
  25. 'ID' => '(int) Site ID',
  26. 'name' => '(string) Title of site',
  27. 'description' => '(string) Tagline or description of site',
  28. 'URL' => '(string) Full URL to the site',
  29. 'user_can_manage' => '(bool) The current user can manage this site', // deprecated.
  30. 'capabilities' => '(array) Array of capabilities for the current user on this site.',
  31. 'jetpack' => '(bool) Whether the site is a Jetpack site or not',
  32. 'jetpack_connection' => '(bool) Whether the site is connected to WP.com via `jetpack-connection`',
  33. 'is_multisite' => '(bool) Whether the site is a Multisite site or not. Always true for WP.com sites.',
  34. 'post_count' => '(int) The number of posts the site has',
  35. 'subscribers_count' => '(int) The number of subscribers the site has',
  36. 'lang' => '(string) Primary language code of the site',
  37. 'icon' => '(array) An array of icon formats for the site',
  38. 'logo' => '(array) The site logo, set in the Customizer',
  39. 'visible' => '(bool) If this site is visible in the user\'s site list',
  40. 'is_private' => '(bool) If the site is a private site or not',
  41. 'is_coming_soon' => '(bool) If the site is marked as "coming soon" or not',
  42. '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.',
  43. 'is_vip' => '(bool) If the site is a VIP site or not.',
  44. 'is_following' => '(bool) If the current user is subscribed to this site in the reader',
  45. 'organization_id' => '(int) P2 Organization identifier.',
  46. '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/',
  47. 'plan' => '(array) Details of the current plan for this site.',
  48. 'updates' => '(array) An array of available updates for plugins, themes, wordpress, and languages.',
  49. 'jetpack_modules' => '(array) A list of active Jetpack modules.',
  50. 'meta' => '(object) Meta data',
  51. 'quota' => '(array) An array describing how much space a user has left for uploads',
  52. 'launch_status' => '(string) A string describing the launch status of a site',
  53. 'site_migration' => '(array) Data about any migration into the site.',
  54. 'is_fse_active' => '(bool) If the site has Full Site Editing active or not.',
  55. 'is_fse_eligible' => '(bool) If the site is capable of Full Site Editing or not',
  56. 'is_core_site_editor_enabled' => '(bool) If the site has the core site editor enabled.',
  57. 'is_wpcom_atomic' => '(bool) If the site is a WP.com Atomic one.',
  58. );
  59. protected static $no_member_fields = array(
  60. 'ID',
  61. 'name',
  62. 'description',
  63. 'URL',
  64. 'jetpack',
  65. 'jetpack_connection',
  66. 'post_count',
  67. 'subscribers_count',
  68. 'lang',
  69. 'locale',
  70. 'icon',
  71. 'logo',
  72. 'visible',
  73. 'is_private',
  74. 'is_coming_soon',
  75. 'is_following',
  76. 'meta',
  77. 'launch_status',
  78. 'site_migration',
  79. 'is_fse_active',
  80. 'is_fse_eligible',
  81. 'is_core_site_editor_enabled',
  82. 'is_wpcom_atomic',
  83. );
  84. protected static $site_options_format = array(
  85. 'timezone',
  86. 'gmt_offset',
  87. 'blog_public',
  88. 'videopress_enabled',
  89. 'upgraded_filetypes_enabled',
  90. 'login_url',
  91. 'admin_url',
  92. 'is_mapped_domain',
  93. 'is_redirect',
  94. 'unmapped_url',
  95. 'featured_images_enabled',
  96. 'theme_slug',
  97. 'header_image',
  98. 'background_color',
  99. 'image_default_link_type',
  100. 'image_thumbnail_width',
  101. 'image_thumbnail_height',
  102. 'image_thumbnail_crop',
  103. 'image_medium_width',
  104. 'image_medium_height',
  105. 'image_large_width',
  106. 'image_large_height',
  107. 'permalink_structure',
  108. 'post_formats',
  109. 'default_post_format',
  110. 'default_category',
  111. 'allowed_file_types',
  112. 'show_on_front',
  113. /** This filter is documented in modules/likes.php */
  114. 'default_likes_enabled',
  115. 'default_sharing_status',
  116. 'default_comment_status',
  117. 'default_ping_status',
  118. 'software_version',
  119. 'created_at',
  120. 'wordads',
  121. 'publicize_permanently_disabled',
  122. 'frame_nonce',
  123. 'jetpack_frame_nonce',
  124. 'page_on_front',
  125. 'page_for_posts',
  126. 'headstart',
  127. 'headstart_is_fresh',
  128. 'ak_vp_bundle_enabled',
  129. Jetpack_SEO_Utils::FRONT_PAGE_META_OPTION,
  130. Jetpack_SEO_Titles::TITLE_FORMATS_OPTION,
  131. 'verification_services_codes',
  132. 'podcasting_archive',
  133. 'is_domain_only',
  134. 'is_automated_transfer',
  135. 'is_wpcom_atomic',
  136. 'is_wpcom_store',
  137. 'signup_is_store',
  138. 'has_pending_automated_transfer',
  139. 'woocommerce_is_active',
  140. 'design_type',
  141. 'site_goals',
  142. 'site_segment',
  143. 'import_engine',
  144. 'is_wpforteams_site',
  145. 'p2_hub_blog_id',
  146. 'site_creation_flow',
  147. 'is_cloud_eligible',
  148. 'selected_features',
  149. 'anchor_podcast',
  150. );
  151. protected static $jetpack_response_field_additions = array(
  152. 'subscribers_count',
  153. 'site_migration',
  154. );
  155. protected static $jetpack_response_field_member_additions = array(
  156. 'capabilities',
  157. 'plan',
  158. 'products',
  159. 'zendesk_site_meta',
  160. );
  161. protected static $jetpack_response_option_additions = array(
  162. 'publicize_permanently_disabled',
  163. 'ak_vp_bundle_enabled',
  164. 'is_automated_transfer',
  165. 'is_wpcom_atomic',
  166. 'is_wpcom_store',
  167. 'woocommerce_is_active',
  168. 'frame_nonce',
  169. 'jetpack_frame_nonce',
  170. 'design_type',
  171. 'wordads',
  172. // Use the site registered date from wpcom, since it is only available in a multisite context
  173. // and defaults to `0000-00-00T00:00:00+00:00` from the Jetpack site.
  174. // See https://github.com/Automattic/jetpack/blob/58638f46094b36f5df9cbc4570006544f0ad300c/sal/class.json-api-site-base.php#L387.
  175. 'created_at',
  176. );
  177. private $site;
  178. // protected $compact = null;
  179. protected $fields_to_include = '_all';
  180. protected $options_to_include = '_all';
  181. // /sites/mine
  182. // /sites/%s -> $blog_id
  183. function callback( $path = '', $blog_id = 0 ) {
  184. if ( 'mine' === $blog_id ) {
  185. $api = WPCOM_JSON_API::init();
  186. if ( ! $api->token_details || empty( $api->token_details['blog_id'] ) ) {
  187. return new WP_Error( 'authorization_required', 'An active access token must be used to query information about the current blog.', 403 );
  188. }
  189. $blog_id = $api->token_details['blog_id'];
  190. }
  191. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  192. if ( is_wp_error( $blog_id ) ) {
  193. return $blog_id;
  194. }
  195. $this->filter_fields_and_options();
  196. $response = $this->build_current_site_response();
  197. /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
  198. do_action( 'wpcom_json_api_objects', 'sites' );
  199. return $response;
  200. }
  201. public function filter_fields_and_options() {
  202. $query_args = $this->query_args();
  203. $this->fields_to_include = empty( $query_args['fields'] ) ? '_all' : array_map( 'trim', explode( ',', $query_args['fields'] ) );
  204. $this->options_to_include = empty( $query_args['options'] ) ? '_all' : array_map( 'trim', explode( ',', $query_args['options'] ) );
  205. }
  206. /**
  207. * Collects the necessary information to return for a site's response.
  208. *
  209. * @return array
  210. */
  211. public function build_current_site_response() {
  212. $blog_id = (int) $this->api->get_blog_id_for_output();
  213. $this->site = $this->get_platform()->get_site( $blog_id );
  214. /**
  215. * Filter the structure of information about the site to return.
  216. *
  217. * @module json-api
  218. *
  219. * @since 3.9.3
  220. *
  221. * @param array $site_format Data structure.
  222. */
  223. $default_fields = array_keys( apply_filters( 'sites_site_format', self::$site_format ) );
  224. $response_keys = is_array( $this->fields_to_include ) ?
  225. array_intersect( $default_fields, $this->fields_to_include ) :
  226. $default_fields;
  227. $has_blog_access = $this->has_blog_access( $this->api->token_details );
  228. $has_user_access = $this->has_user_access();
  229. if ( ! $has_user_access && ! $has_blog_access ) {
  230. // Public access without user or blog auth, only return `$no_member_fields`.
  231. $response_keys = array_intersect( $response_keys, self::$no_member_fields );
  232. } elseif ( $has_user_access && ! current_user_can( 'edit_posts' ) ) {
  233. // Subscriber level user, don't return site options.
  234. $response_keys = array_diff( $response_keys, array( 'options' ) );
  235. }
  236. return $this->render_response_keys( $response_keys );
  237. }
  238. /**
  239. * Checks that the current user has access to the current blog.
  240. *
  241. * @return bool Whether or not the current user can access the current blog.
  242. */
  243. private function has_user_access() {
  244. return is_user_member_of_blog( get_current_user_id(), get_current_blog_id() );
  245. }
  246. /**
  247. * Checks if the request has a valid blog token for the current blog.
  248. *
  249. * @param array $token_details Access token for the api request.
  250. * @return bool
  251. */
  252. private function has_blog_access( $token_details ) {
  253. $token_details = (array) $token_details;
  254. if ( ! isset( $token_details['access'], $token_details['auth'], $token_details['blog_id'] ) ) {
  255. return false;
  256. }
  257. return 'jetpack' === $token_details['auth'] &&
  258. 'blog' === $token_details['access'] &&
  259. get_current_blog_id() === $token_details['blog_id'];
  260. }
  261. private function render_response_keys( &$response_keys ) {
  262. $response = array();
  263. $is_user_logged_in = is_user_logged_in();
  264. $this->site->before_render();
  265. foreach ( $response_keys as $key ) {
  266. $this->render_response_key( $key, $response, $is_user_logged_in );
  267. }
  268. $this->site->after_render( $response );
  269. return $response;
  270. }
  271. protected function render_response_key( $key, &$response, $is_user_logged_in ) {
  272. do_action( 'pre_render_site_response_key', $key );
  273. switch ( $key ) {
  274. case 'ID' :
  275. $response[ $key ] = $this->site->blog_id;
  276. break;
  277. case 'name' :
  278. $response[ $key ] = $this->site->get_name();
  279. break;
  280. case 'description' :
  281. $response[ $key ] = $this->site->get_description();
  282. break;
  283. case 'URL' :
  284. $response[ $key ] = $this->site->get_url();
  285. break;
  286. case 'user_can_manage' :
  287. $response[ $key ] = $this->site->user_can_manage();
  288. case 'is_private' :
  289. $response[ $key ] = $this->site->is_private();
  290. break;
  291. case 'is_coming_soon' :
  292. // This option is stored on wp.com for both simple and atomic sites. @see mu-plugins/private-blog.php
  293. $response[ $key ] = $this->site->is_coming_soon();;
  294. break;
  295. case 'launch_status' :
  296. $response[ $key ] = $this->site->get_launch_status();
  297. break;
  298. case 'visible' :
  299. $response[ $key ] = $this->site->is_visible();
  300. break;
  301. case 'subscribers_count' :
  302. $response[ $key ] = $this->site->get_subscribers_count();
  303. break;
  304. case 'post_count' :
  305. if ( $is_user_logged_in ) {
  306. $response[ $key ] = $this->site->get_post_count();
  307. }
  308. break;
  309. case 'icon' :
  310. $icon = $this->site->get_icon();
  311. if ( ! is_null( $icon ) ) {
  312. $response[ $key ] = $icon;
  313. }
  314. break;
  315. case 'logo' :
  316. $response[ $key ] = $this->site->get_logo();
  317. break;
  318. case 'is_following':
  319. $response[ $key ] = $this->site->is_following();
  320. break;
  321. case 'options':
  322. // small optimisation - don't recalculate
  323. $all_options = apply_filters( 'sites_site_options_format', self::$site_options_format );
  324. $options_response_keys = is_array( $this->options_to_include ) ?
  325. array_intersect( $all_options, $this->options_to_include ) :
  326. $all_options;
  327. $options = $this->render_option_keys( $options_response_keys );
  328. $this->site->after_render_options( $options );
  329. $response[ $key ] = (object) $options;
  330. break;
  331. case 'meta':
  332. $this->build_meta_response( $response );
  333. break;
  334. case 'lang' :
  335. $response[ $key ] = $is_user_logged_in ? $this->site->get_locale() : false;
  336. break;
  337. case 'locale' :
  338. $response[ $key ] = $is_user_logged_in ? $this->site->get_locale() : false;
  339. break;
  340. case 'jetpack':
  341. $response[ $key ] = $this->site->is_jetpack();
  342. break;
  343. case 'jetpack_connection':
  344. $response[ $key ] = $this->site->is_jetpack_connection();
  345. break;
  346. case 'single_user_site' :
  347. $response[ $key ] = $this->site->is_single_user_site();
  348. break;
  349. case 'is_vip' :
  350. $response[ $key ] = $this->site->is_vip();
  351. break;
  352. case 'is_multisite' :
  353. $response[ $key ] = $this->site->is_multisite();
  354. break;
  355. case 'organization_id':
  356. $response[ $key ] = $this->site->get_p2_organization_id();
  357. break;
  358. case 'capabilities' :
  359. $response[ $key ] = $this->site->get_capabilities();
  360. break;
  361. case 'jetpack_modules':
  362. if ( is_user_member_of_blog() ) {
  363. $response[ $key ] = $this->site->get_jetpack_modules();
  364. }
  365. break;
  366. case 'plan' :
  367. $response[ $key ] = $this->site->get_plan();
  368. break;
  369. case 'products' :
  370. $response[ $key ] = $this->site->get_products();
  371. break;
  372. case 'zendesk_site_meta':
  373. $response[ $key ] = $this->site->get_zendesk_site_meta();
  374. break;
  375. case 'quota' :
  376. $response[ $key ] = $this->site->get_quota();
  377. break;
  378. case 'site_migration' :
  379. $response[ $key ] = $this->site->get_migration_meta();
  380. break;
  381. case 'is_fse_active':
  382. $response[ $key ] = $this->site->is_fse_active();
  383. break;
  384. case 'is_fse_eligible':
  385. $response[ $key ] = $this->site->is_fse_eligible();
  386. break;
  387. case 'is_core_site_editor_enabled':
  388. $response[ $key ] = $this->site->is_core_site_editor_enabled();
  389. break;
  390. case 'is_wpcom_atomic':
  391. $response[ $key ] = $this->site->is_wpcom_atomic();
  392. break;
  393. }
  394. do_action( 'post_render_site_response_key', $key );
  395. }
  396. protected function render_option_keys( &$options_response_keys ) {
  397. $options = array();
  398. $site = $this->site;
  399. $custom_front_page = $site->is_custom_front_page();
  400. foreach ( $options_response_keys as $key ) {
  401. switch ( $key ) {
  402. case 'timezone' :
  403. $options[ $key ] = $site->get_timezone();
  404. break;
  405. case 'gmt_offset' :
  406. $options[ $key ] = $site->get_gmt_offset();
  407. break;
  408. case 'videopress_enabled' :
  409. $options[ $key ] = $site->has_videopress();
  410. break;
  411. case 'upgraded_filetypes_enabled' :
  412. $options[ $key ] = $site->upgraded_filetypes_enabled();
  413. break;
  414. case 'login_url' :
  415. $options[ $key ] = $site->get_login_url();
  416. break;
  417. case 'admin_url' :
  418. $options[ $key ] = $site->get_admin_url();
  419. break;
  420. case 'is_mapped_domain' :
  421. $options[ $key ] = $site->is_mapped_domain();
  422. break;
  423. case 'is_redirect' :
  424. $options[ $key ] = $site->is_redirect();
  425. break;
  426. case 'unmapped_url' :
  427. $options[ $key ] = $site->get_unmapped_url();
  428. break;
  429. case 'featured_images_enabled' :
  430. $options[ $key ] = $site->featured_images_enabled();
  431. break;
  432. case 'theme_slug' :
  433. $options[ $key ] = $site->get_theme_slug();
  434. break;
  435. case 'header_image' :
  436. $options[ $key ] = $site->get_header_image();
  437. break;
  438. case 'background_color' :
  439. $options[ $key ] = $site->get_background_color();
  440. break;
  441. case 'image_default_link_type' :
  442. $options[ $key ] = $site->get_image_default_link_type();
  443. break;
  444. case 'image_thumbnail_width' :
  445. $options[ $key ] = $site->get_image_thumbnail_width();
  446. break;
  447. case 'image_thumbnail_height' :
  448. $options[ $key ] = $site->get_image_thumbnail_height();
  449. break;
  450. case 'image_thumbnail_crop' :
  451. $options[ $key ] = $site->get_image_thumbnail_crop();
  452. break;
  453. case 'image_medium_width' :
  454. $options[ $key ] = $site->get_image_medium_width();
  455. break;
  456. case 'image_medium_height' :
  457. $options[ $key ] = $site->get_image_medium_height();
  458. break;
  459. case 'image_large_width' :
  460. $options[ $key ] = $site->get_image_large_width();
  461. break;
  462. case 'image_large_height' :
  463. $options[ $key ] = $site->get_image_large_height();
  464. break;
  465. case 'permalink_structure' :
  466. $options[ $key ] = $site->get_permalink_structure();
  467. break;
  468. case 'post_formats' :
  469. $options[ $key ] = $site->get_post_formats();
  470. break;
  471. case 'default_post_format' :
  472. $options[ $key ] = $site->get_default_post_format();
  473. break;
  474. case 'default_category' :
  475. $options[ $key ] = $site->get_default_category();
  476. break;
  477. case 'allowed_file_types' :
  478. $options[ $key ] = $site->allowed_file_types();
  479. break;
  480. case 'show_on_front' :
  481. $options[ $key ] = $site->get_show_on_front();
  482. break;
  483. /** This filter is documented in modules/likes.php */
  484. case 'default_likes_enabled' :
  485. $options[ $key ] = $site->get_default_likes_enabled();
  486. break;
  487. case 'default_sharing_status' :
  488. $options[ $key ] = $site->get_default_sharing_status();
  489. break;
  490. case 'default_comment_status' :
  491. $options[ $key ] = $site->get_default_comment_status();
  492. break;
  493. case 'default_ping_status' :
  494. $options[ $key ] = $site->default_ping_status();
  495. break;
  496. case 'software_version' :
  497. $options[ $key ] = $site->get_wordpress_version();
  498. break;
  499. case 'created_at' :
  500. $options[ $key ] = $site->get_registered_date();
  501. break;
  502. case 'wordads' :
  503. $options[ $key ] = $site->has_wordads();
  504. break;
  505. case 'publicize_permanently_disabled' :
  506. $options[ $key ] = $site->is_publicize_permanently_disabled();
  507. break;
  508. case 'frame_nonce' :
  509. $options[ $key ] = $site->get_frame_nonce();
  510. break;
  511. case 'jetpack_frame_nonce' :
  512. $options[ $key ] = $site->get_jetpack_frame_nonce();
  513. break;
  514. case 'page_on_front' :
  515. if ( $custom_front_page ) {
  516. $options[ $key ] = $site->get_page_on_front();
  517. }
  518. break;
  519. case 'page_for_posts' :
  520. if ( $custom_front_page ) {
  521. $options[ $key ] = $site->get_page_for_posts();
  522. }
  523. break;
  524. case 'headstart' :
  525. $options[ $key ] = $site->is_headstart();
  526. break;
  527. case 'headstart_is_fresh' :
  528. $options[ $key ] = $site->is_headstart_fresh();
  529. break;
  530. case 'ak_vp_bundle_enabled' :
  531. $options[ $key ] = $site->get_ak_vp_bundle_enabled();
  532. break;
  533. case Jetpack_SEO_Utils::FRONT_PAGE_META_OPTION :
  534. $options[ $key ] = $site->get_jetpack_seo_front_page_description();
  535. break;
  536. case Jetpack_SEO_Titles::TITLE_FORMATS_OPTION :
  537. $options[ $key ] = $site->get_jetpack_seo_title_formats();
  538. break;
  539. case 'verification_services_codes' :
  540. $options[ $key ] = $site->get_verification_services_codes();
  541. break;
  542. case 'podcasting_archive':
  543. $options[ $key ] = $site->get_podcasting_archive();
  544. break;
  545. case 'is_domain_only':
  546. $options[ $key ] = $site->is_domain_only();
  547. break;
  548. case 'is_automated_transfer':
  549. $options[ $key ] = $site->is_automated_transfer();
  550. break;
  551. case 'blog_public':
  552. $options[ $key ] = $site->get_blog_public();
  553. break;
  554. case 'is_wpcom_atomic':
  555. $options[ $key ] = $site->is_wpcom_atomic();
  556. break;
  557. case 'is_wpcom_store':
  558. $options[ $key ] = $site->is_wpcom_store();
  559. break;
  560. case 'signup_is_store':
  561. $signup_is_store = $site->signup_is_store();
  562. if ( $signup_is_store ) {
  563. $options[ $key ] = $site->signup_is_store();
  564. }
  565. break;
  566. case 'has_pending_automated_transfer':
  567. $has_pending_automated_transfer = $site->has_pending_automated_transfer();
  568. if ( $has_pending_automated_transfer ) {
  569. $options[ $key ] = true;
  570. }
  571. break;
  572. case 'woocommerce_is_active':
  573. $options[ $key ] = $site->woocommerce_is_active();
  574. break;
  575. case 'design_type':
  576. $options[ $key ] = $site->get_design_type();
  577. break;
  578. case 'site_goals':
  579. $options[ $key ] = $site->get_site_goals();
  580. break;
  581. case 'site_segment':
  582. $options[ $key ] = $site->get_site_segment();
  583. break;
  584. case 'import_engine':
  585. $options[ $key ] = $site->get_import_engine();
  586. break;
  587. case 'is_wpforteams_site':
  588. $options[ $key ] = $site->is_wpforteams_site();
  589. break;
  590. case 'p2_hub_blog_id':
  591. $options[ $key ] = $site->get_p2_hub_blog_id();
  592. break;
  593. case 'site_creation_flow':
  594. $site_creation_flow = $site->get_site_creation_flow();
  595. if ( $site_creation_flow ) {
  596. $options[ $key ] = $site_creation_flow;
  597. }
  598. break;
  599. case 'is_cloud_eligible':
  600. $options[ $key ] = $site->is_cloud_eligible();
  601. break;
  602. case 'selected_features':
  603. $selected_features = $site->get_selected_features();
  604. if ( $selected_features ) {
  605. $options[ $key ] = $selected_features;
  606. }
  607. break;
  608. case 'anchor_podcast':
  609. $options[ $key ] = $site->get_anchor_podcast();
  610. break;
  611. }
  612. }
  613. return $options;
  614. }
  615. protected function build_meta_response( &$response ) {
  616. $links = array(
  617. 'self' => (string) $this->links->get_site_link( $this->site->blog_id ),
  618. 'help' => (string) $this->links->get_site_link( $this->site->blog_id, 'help' ),
  619. 'posts' => (string) $this->links->get_site_link( $this->site->blog_id, 'posts/' ),
  620. 'comments' => (string) $this->links->get_site_link( $this->site->blog_id, 'comments/' ),
  621. 'xmlrpc' => (string) $this->site->get_xmlrpc_url(),
  622. );
  623. $icon = $this->site->get_icon();
  624. if ( ! empty( $icon ) && ! empty( $icon['media_id'] ) ) {
  625. $links['site_icon'] = (string) $this->links->get_site_link( $this->site->blog_id, 'media/' . $icon['media_id'] );
  626. }
  627. $response['meta'] = (object) array(
  628. 'links' => (object) $links
  629. );
  630. }
  631. // apply any WPCOM-only response components to a Jetpack site response
  632. public function decorate_jetpack_response( &$response ) {
  633. $this->site = $this->get_platform()->get_site( $response->ID );
  634. switch_to_blog( $this->site->get_id() );
  635. $wpcom_response = $this->render_response_keys( self::$jetpack_response_field_additions );
  636. foreach( $wpcom_response as $key => $value ) {
  637. $response->{ $key } = $value;
  638. }
  639. if ( $this->has_user_access() || $this->has_blog_access( $this->api->token_details ) ) {
  640. $wpcom_member_response = $this->render_response_keys( self::$jetpack_response_field_member_additions );
  641. foreach( $wpcom_member_response as $key => $value ) {
  642. $response->{ $key } = $value;
  643. }
  644. } else {
  645. // ensure private data is not rendered for non members of the site
  646. unset( $response->options );
  647. unset( $response->is_vip );
  648. unset( $response->single_user_site );
  649. unset( $response->is_private );
  650. unset( $response->is_coming_soon );
  651. unset( $response->capabilities );
  652. unset( $response->lang );
  653. unset( $response->user_can_manage );
  654. unset( $response->is_multisite );
  655. unset( $response->plan );
  656. unset( $response->products );
  657. unset( $response->zendesk_site_meta );
  658. }
  659. // render additional options
  660. if ( $response->options ) {
  661. $wpcom_options_response = $this->render_option_keys( self::$jetpack_response_option_additions );
  662. foreach ( $wpcom_options_response as $key => $value ) {
  663. $response->options[ $key ] = $value;
  664. }
  665. }
  666. restore_current_blog();
  667. return $response; // possibly no need since it's modified in place
  668. }
  669. }
  670. new WPCOM_JSON_API_List_Post_Formats_Endpoint( array(
  671. 'description' => 'Get a list of post formats supported by a site.',
  672. 'group' => '__do_not_document',
  673. 'stat' => 'sites:X:post-formats',
  674. 'method' => 'GET',
  675. 'path' => '/sites/%s/post-formats',
  676. 'path_labels' => array(
  677. '$site' => '(int|string) Site ID or domain',
  678. ),
  679. 'query_parameters' => array(
  680. 'context' => false,
  681. ),
  682. 'allow_fallback_to_jetpack_blog_token' => true,
  683. 'response_format' => array(
  684. 'formats' => '(object) An object of supported post formats, each key a supported format slug mapped to its display string.',
  685. )
  686. ) );
  687. class WPCOM_JSON_API_List_Post_Formats_Endpoint extends WPCOM_JSON_API_Endpoint {
  688. // /sites/%s/post-formats -> $blog_id
  689. function callback( $path = '', $blog_id = 0 ) {
  690. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  691. if ( is_wp_error( $blog_id ) ) {
  692. return $blog_id;
  693. }
  694. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  695. $this->load_theme_functions();
  696. }
  697. // Get a list of supported post formats.
  698. $all_formats = get_post_format_strings();
  699. $supported = get_theme_support( 'post-formats' );
  700. $supported_formats = $response['formats'] = array();
  701. if ( isset( $supported[0] ) ) {
  702. foreach ( $supported[0] as $format ) {
  703. $supported_formats[ $format ] = $all_formats[ $format ];
  704. }
  705. }
  706. $response['formats'] = (object) $supported_formats;
  707. return $response;
  708. }
  709. }
  710. new WPCOM_JSON_API_List_Page_Templates_Endpoint( array(
  711. 'description' => 'Get a list of page templates supported by a site.',
  712. 'group' => 'sites',
  713. 'stat' => 'sites:X:post-templates',
  714. 'method' => 'GET',
  715. 'path' => '/sites/%s/page-templates',
  716. 'path_labels' => array(
  717. '$site' => '(int|string) Site ID or domain',
  718. ),
  719. 'query_parameters' => array(
  720. 'context' => false,
  721. ),
  722. 'response_format' => array(
  723. 'templates' => '(array) A list of supported page templates. Contains label and file.',
  724. ),
  725. 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/33534099/page-templates'
  726. ) );
  727. class WPCOM_JSON_API_List_Page_Templates_Endpoint extends WPCOM_JSON_API_Endpoint {
  728. // /sites/%s/page-templates -> $blog_id
  729. function callback( $path = '', $blog_id = 0 ) {
  730. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  731. if ( is_wp_error( $blog_id ) ) {
  732. return $blog_id;
  733. }
  734. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  735. $this->load_theme_functions();
  736. }
  737. $response = array();
  738. $page_templates = array();
  739. $templates = get_page_templates();
  740. ksort( $templates );
  741. foreach ( array_keys( $templates ) as $label ) {
  742. $page_templates[] = array(
  743. 'label' => $label,
  744. 'file' => $templates[ $label ]
  745. );
  746. }
  747. $response['templates'] = $page_templates;
  748. return $response;
  749. }
  750. }