class.wpcom-json-api-update-site-homepage-endpoint.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. new WPCOM_JSON_API_Update_Site_Homepage_Endpoint( array (
  3. 'description' => 'Set site homepage settings',
  4. 'group' => '__do_not_document',
  5. 'stat' => 'sites:1:homepage',
  6. 'method' => 'POST',
  7. 'min_version' => '1.1',
  8. 'path' => '/sites/%s/homepage',
  9. 'path_labels' => array(
  10. '$site' => '(string) Site ID or domain.',
  11. ),
  12. 'request_format' => array(
  13. 'is_page_on_front' => '(bool) True if we will use a page as the homepage; false to use a blog page as the homepage.',
  14. 'page_on_front_id' => '(int) Optional. The ID of the page to use as the homepage if is_page_on_front is true.',
  15. 'page_for_posts_id' => '(int) Optional. The ID of the page to use as the blog page if is_page_on_front is true.',
  16. ),
  17. 'response_format' => array(
  18. 'is_page_on_front' => '(bool) True if we will use a page as the homepage; false to use a blog page as the homepage.',
  19. 'page_on_front_id' => '(int) The ID of the page to use as the homepage if is_page_on_front is true.',
  20. 'page_for_posts_id' => '(int) The ID of the page to use as the blog page if is_page_on_front is true.',
  21. ),
  22. 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/homepage',
  23. 'example_request_data' => array(
  24. 'headers' => array( 'authorization' => 'Bearer YOUR_API_TOKEN' ),
  25. 'body' => array(
  26. 'is_page_on_front' => true,
  27. 'page_on_front_id' => 1,
  28. 'page_for_posts_id' => 0,
  29. ),
  30. ),
  31. 'example_response' => '{"is_page_on_front":true,"page_on_front_id":1,"page_for_posts_id":0}'
  32. ) );
  33. class WPCOM_JSON_API_Update_Site_Homepage_Endpoint extends WPCOM_JSON_API_Endpoint {
  34. function callback( $path = '', $site_id = 0 ) {
  35. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $site_id ) );
  36. if ( is_wp_error( $blog_id ) ) {
  37. return $blog_id;
  38. }
  39. if ( ! current_user_can( 'edit_theme_options' ) ) {
  40. return new WP_Error( 'unauthorized', 'User is not authorized to access homepage settings', 403 );
  41. }
  42. $args = $this->input();
  43. if ( empty( $args ) || ! is_array( $args ) ) {
  44. return $this->get_current_settings();
  45. }
  46. if ( isset( $args['is_page_on_front'] ) ) {
  47. $show_on_front = $args['is_page_on_front'] ? 'page' : 'posts';
  48. update_option( 'show_on_front', $show_on_front );
  49. }
  50. if ( isset( $args['page_on_front_id'] ) ) {
  51. update_option( 'page_on_front', $args['page_on_front_id'] );
  52. }
  53. if ( isset( $args['page_for_posts_id'] ) ) {
  54. update_option( 'page_for_posts', $args['page_for_posts_id'] );
  55. }
  56. return $this->get_current_settings();
  57. }
  58. function get_current_settings() {
  59. $is_page_on_front = ( get_option( 'show_on_front' ) === 'page' );
  60. $page_on_front_id = get_option( 'page_on_front' );
  61. $page_for_posts_id = get_option( 'page_for_posts' );
  62. return array(
  63. 'is_page_on_front' => $is_page_on_front,
  64. 'page_on_front_id' => $page_on_front_id,
  65. 'page_for_posts_id' => $page_for_posts_id,
  66. );
  67. }
  68. }