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

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