← All changes
|
json-endpoints/class.wpcom-json-api-update-site-homepage-endpoint.php
+103
-0
16.2-beta
→
16.3
View file →
| @@ -1,0 +1,103 @@ | ||
| 1 | +<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName | |
| 2 | +/** | |
| 3 | + * Set site homepage settings API endpoint. | |
| 4 | + * | |
| 5 | + * Endpoint: /sites/%s/homepage | |
| 6 | + */ | |
| 7 | + | |
| 8 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 9 | + exit( 0 ); | |
| 10 | +} | |
| 11 | + | |
| 12 | +new WPCOM_JSON_API_Update_Site_Homepage_Endpoint( | |
| 13 | + array( | |
| 14 | + 'description' => 'Set site homepage settings', | |
| 15 | + 'group' => '__do_not_document', | |
| 16 | + 'stat' => 'sites:1:homepage', | |
| 17 | + 'method' => 'POST', | |
| 18 | + 'min_version' => '1.1', | |
| 19 | + 'path' => '/sites/%s/homepage', | |
| 20 | + 'path_labels' => array( | |
| 21 | + '$site' => '(string) Site ID or domain.', | |
| 22 | + ), | |
| 23 | + 'request_format' => array( | |
| 24 | + 'is_page_on_front' => '(bool) True if we will use a page as the homepage; false to use a blog page as the homepage.', | |
| 25 | + 'page_on_front_id' => '(int) Optional. The ID of the page to use as the homepage if is_page_on_front is true.', | |
| 26 | + 'page_for_posts_id' => '(int) Optional. The ID of the page to use as the blog page if is_page_on_front is true.', | |
| 27 | + ), | |
| 28 | + 'response_format' => array( | |
| 29 | + 'is_page_on_front' => '(bool) True if we will use a page as the homepage; false to use a blog page as the homepage.', | |
| 30 | + 'page_on_front_id' => '(int) The ID of the page to use as the homepage if is_page_on_front is true.', | |
| 31 | + 'page_for_posts_id' => '(int) The ID of the page to use as the blog page if is_page_on_front is true.', | |
| 32 | + ), | |
| 33 | + 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/homepage', | |
| 34 | + 'example_request_data' => array( | |
| 35 | + 'headers' => array( 'authorization' => 'Bearer YOUR_API_TOKEN' ), | |
| 36 | + 'body' => array( | |
| 37 | + 'is_page_on_front' => true, | |
| 38 | + 'page_on_front_id' => 1, | |
| 39 | + 'page_for_posts_id' => 0, | |
| 40 | + ), | |
| 41 | + ), | |
| 42 | + 'example_response' => '{"is_page_on_front":true,"page_on_front_id":1,"page_for_posts_id":0}', | |
| 43 | + ) | |
| 44 | +); | |
| 45 | + | |
| 46 | +/** | |
| 47 | + * Site homepage setting endpoint class. | |
| 48 | + * | |
| 49 | + * @phan-constructor-used-for-side-effects | |
| 50 | + */ | |
| 51 | +class WPCOM_JSON_API_Update_Site_Homepage_Endpoint extends WPCOM_JSON_API_Endpoint { | |
| 52 | + /** | |
| 53 | + * Set site homepage setting API callback. | |
| 54 | + * | |
| 55 | + * @param string $path API path. | |
| 56 | + * @param int $site_id Blog ID. | |
| 57 | + */ | |
| 58 | + public function callback( $path = '', $site_id = 0 ) { | |
| 59 | + $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $site_id ) ); | |
| 60 | + if ( is_wp_error( $blog_id ) ) { | |
| 61 | + return $blog_id; | |
| 62 | + } | |
| 63 | + | |
| 64 | + if ( ! current_user_can( 'edit_theme_options' ) ) { | |
| 65 | + return new WP_Error( 'unauthorized', 'User is not authorized to access homepage settings', 403 ); | |
| 66 | + } | |
| 67 | + | |
| 68 | + $args = $this->input(); | |
| 69 | + if ( empty( $args ) || ! is_array( $args ) ) { | |
| 70 | + return $this->get_current_settings(); | |
| 71 | + } | |
| 72 | + | |
| 73 | + if ( isset( $args['is_page_on_front'] ) ) { | |
| 74 | + $show_on_front = $args['is_page_on_front'] ? 'page' : 'posts'; | |
| 75 | + update_option( 'show_on_front', $show_on_front ); | |
| 76 | + } | |
| 77 | + if ( isset( $args['page_on_front_id'] ) ) { | |
| 78 | + update_option( 'page_on_front', $args['page_on_front_id'] ); | |
| 79 | + } | |
| 80 | + if ( isset( $args['page_for_posts_id'] ) ) { | |
| 81 | + update_option( 'page_for_posts', $args['page_for_posts_id'] ); | |
| 82 | + } | |
| 83 | + | |
| 84 | + return $this->get_current_settings(); | |
| 85 | + } | |
| 86 | + | |
| 87 | + /** | |
| 88 | + * Get current site homepage settings. | |
| 89 | + * | |
| 90 | + * @return array | |
| 91 | + */ | |
| 92 | + public function get_current_settings() { | |
| 93 | + $is_page_on_front = ( get_option( 'show_on_front' ) === 'page' ); | |
| 94 | + $page_on_front_id = get_option( 'page_on_front' ); | |
| 95 | + $page_for_posts_id = get_option( 'page_for_posts' ); | |
| 96 | + | |
| 97 | + return array( | |
| 98 | + 'is_page_on_front' => $is_page_on_front, | |
| 99 | + 'page_on_front_id' => $page_on_front_id, | |
| 100 | + 'page_for_posts_id' => $page_for_posts_id, | |
| 101 | + ); | |
| 102 | + } | |
| 103 | +} | |