diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php index e276afe5a7d3d..1ba360419082a 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php @@ -2333,7 +2333,7 @@ protected function prepare_links( $post ) { * Gets the link relations available for the post and current user. * * @since 4.9.8 - * @since 7.2.0 Added the `action-trash` link relation. + * @since 7.2.0 Added the `action-trash`, `action-set-homepage`, and `action-set-posts-page` link relations. * * @param WP_Post $post Post object. * @param WP_REST_Request $request Request object. @@ -2382,6 +2382,18 @@ protected function get_available_actions( $post, $request ) { } } + /* + * Setting a page as the homepage or as the posts page changes the site settings, + * so it requires the same capability as the settings route. The current homepage + * and posts page get neither link. + */ + if ( 'page' === $post_type->name && 'publish' === $post->post_status && current_user_can( 'manage_options' ) ) { + if ( (int) get_option( 'page_on_front' ) !== $post->ID && (int) get_option( 'page_for_posts' ) !== $post->ID ) { + $rels[] = 'https://api.w.org/action-set-homepage'; + $rels[] = 'https://api.w.org/action-set-posts-page'; + } + } + $taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) ); foreach ( $taxonomies as $tax ) { @@ -2853,7 +2865,7 @@ public function get_item_schema() { * Retrieves Link Description Objects that should be added to the Schema for the posts collection. * * @since 4.9.8 - * @since 7.2.0 Added the `action-trash` link relation. + * @since 7.2.0 Added the `action-trash`, `action-set-homepage`, and `action-set-posts-page` link relations. * * @return array */ @@ -2934,6 +2946,20 @@ protected function get_schema_links() { 'href' => $href, ); + if ( 'page' === $this->post_type ) { + $links[] = array( + 'rel' => 'https://api.w.org/action-set-homepage', + 'title' => __( 'The current user can set this page as the homepage.' ), + 'href' => $href, + ); + + $links[] = array( + 'rel' => 'https://api.w.org/action-set-posts-page', + 'title' => __( 'The current user can set this page as the posts page.' ), + 'href' => $href, + ); + } + $taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) ); foreach ( $taxonomies as $tax ) { diff --git a/tests/phpunit/tests/rest-api/rest-pages-controller.php b/tests/phpunit/tests/rest-api/rest-pages-controller.php index df3d000c1d433..3aabf5dfc4e4e 100644 --- a/tests/phpunit/tests/rest-api/rest-pages-controller.php +++ b/tests/phpunit/tests/rest-api/rest-pages-controller.php @@ -9,9 +9,15 @@ * @group restapi */ class WP_Test_REST_Pages_Controller extends WP_Test_REST_Post_Type_Controller_Testcase { + protected static $admin_id; protected static $editor_id; public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { + self::$admin_id = $factory->user->create( + array( + 'role' => 'administrator', + ) + ); self::$editor_id = $factory->user->create( array( 'role' => 'editor', @@ -20,6 +26,7 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { } public static function wpTearDownAfterClass() { + self::delete_user( self::$admin_id ); self::delete_user( self::$editor_id ); } @@ -867,6 +874,220 @@ public function test_get_item_schema() { $this->assertArrayHasKey( 'class_list', $properties ); } + /** + * @ticket 66327 + * + * @covers WP_REST_Posts_Controller::get_schema_links + */ + public function test_homepage_action_ldos_registered() { + $response = rest_get_server()->dispatch( new WP_REST_Request( 'OPTIONS', '/wp/v2/pages' ) ); + $data = $response->get_data(); + $schema = $data['schema']; + + $this->assertArrayHasKey( 'links', $schema, 'The schema should have links.' ); + $set_homepage = wp_list_filter( $schema['links'], array( 'rel' => 'https://api.w.org/action-set-homepage' ) ); + $set_posts_page = wp_list_filter( $schema['links'], array( 'rel' => 'https://api.w.org/action-set-posts-page' ) ); + + $this->assertCount( 1, $set_homepage, 'The action-set-homepage LDO should be on the schema once.' ); + $this->assertCount( 1, $set_posts_page, 'The action-set-posts-page LDO should be on the schema once.' ); + } + + /** + * @ticket 66327 + * + * @covers WP_REST_Posts_Controller::get_schema_links + */ + public function test_homepage_action_ldos_not_registered_for_posts() { + $response = rest_get_server()->dispatch( new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' ) ); + $data = $response->get_data(); + $schema = $data['schema']; + + $this->assertArrayHasKey( 'links', $schema, 'The schema should have links.' ); + $set_homepage = wp_list_filter( $schema['links'], array( 'rel' => 'https://api.w.org/action-set-homepage' ) ); + $set_posts_page = wp_list_filter( $schema['links'], array( 'rel' => 'https://api.w.org/action-set-posts-page' ) ); + + $this->assertCount( 0, $set_homepage, 'The action-set-homepage LDO should not be on the schema.' ); + $this->assertCount( 0, $set_posts_page, 'The action-set-posts-page LDO should not be on the schema.' ); + } + + /** + * @ticket 66327 + * + * @covers WP_REST_Posts_Controller::get_available_actions + */ + public function test_homepage_action_links_exist_for_admin() { + wp_set_current_user( self::$admin_id ); + + $page_id = self::factory()->post->create( array( 'post_type' => 'page' ) ); + $links = $this->get_response_links( "/wp/v2/pages/{$page_id}" ); + + $this->assertArrayHasKey( 'https://api.w.org/action-set-homepage', $links, 'The action-set-homepage link should exist.' ); + $this->assertArrayHasKey( 'https://api.w.org/action-set-posts-page', $links, 'The action-set-posts-page link should exist.' ); + $this->assertSame( $links['self'][0]['href'], $links['https://api.w.org/action-set-homepage'][0]['href'], 'The action-set-homepage link should target the page.' ); + $this->assertSame( $links['self'][0]['href'], $links['https://api.w.org/action-set-posts-page'][0]['href'], 'The action-set-posts-page link should target the page.' ); + } + + /** + * @ticket 66327 + * + * @covers WP_REST_Posts_Controller::get_available_actions + */ + public function test_homepage_action_links_only_available_in_edit_context() { + wp_set_current_user( self::$admin_id ); + + $page_id = self::factory()->post->create( array( 'post_type' => 'page' ) ); + $links = $this->get_response_links( "/wp/v2/pages/{$page_id}", 'view' ); + + $this->assertArrayNotHasKey( 'https://api.w.org/action-set-homepage', $links, 'The action-set-homepage link should not exist.' ); + $this->assertArrayNotHasKey( 'https://api.w.org/action-set-posts-page', $links, 'The action-set-posts-page link should not exist.' ); + } + + /** + * @ticket 66327 + * + * @covers WP_REST_Posts_Controller::get_available_actions + */ + public function test_homepage_action_links_do_not_exist_for_editor() { + wp_set_current_user( self::$editor_id ); + + $page_id = self::factory()->post->create( array( 'post_type' => 'page' ) ); + $links = $this->get_response_links( "/wp/v2/pages/{$page_id}" ); + + $this->assertArrayNotHasKey( 'https://api.w.org/action-set-homepage', $links, 'The action-set-homepage link should not exist.' ); + $this->assertArrayNotHasKey( 'https://api.w.org/action-set-posts-page', $links, 'The action-set-posts-page link should not exist.' ); + } + + /** + * @ticket 66327 + * + * @dataProvider data_unpublished_page_statuses + * + * @covers WP_REST_Posts_Controller::get_available_actions + * + * @param string $status Post status. + */ + public function test_homepage_action_links_do_not_exist_for_unpublished_pages( $status ) { + wp_set_current_user( self::$admin_id ); + + $page_id = self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_status' => $status, + ) + ); + + $links = $this->get_response_links( "/wp/v2/pages/{$page_id}" ); + + $this->assertArrayNotHasKey( 'https://api.w.org/action-set-homepage', $links, 'The action-set-homepage link should not exist.' ); + $this->assertArrayNotHasKey( 'https://api.w.org/action-set-posts-page', $links, 'The action-set-posts-page link should not exist.' ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_unpublished_page_statuses() { + return array( + 'draft' => array( 'draft' ), + 'pending' => array( 'pending' ), + 'private' => array( 'private' ), + ); + } + + /** + * @ticket 66327 + * + * @covers WP_REST_Posts_Controller::get_available_actions + */ + public function test_homepage_action_links_do_not_exist_for_homepage() { + wp_set_current_user( self::$admin_id ); + + $page_id = self::factory()->post->create( array( 'post_type' => 'page' ) ); + update_option( 'show_on_front', 'page' ); + update_option( 'page_on_front', $page_id ); + + // Reload the options from the database, so get_option() returns strings, as on a real request. + wp_cache_delete( 'alloptions', 'options' ); + + $links = $this->get_response_links( "/wp/v2/pages/{$page_id}" ); + + $this->assertArrayNotHasKey( 'https://api.w.org/action-set-homepage', $links, 'The action-set-homepage link should not exist.' ); + $this->assertArrayNotHasKey( 'https://api.w.org/action-set-posts-page', $links, 'The action-set-posts-page link should not exist.' ); + } + + /** + * @ticket 66327 + * + * @covers WP_REST_Posts_Controller::get_available_actions + */ + public function test_homepage_action_links_do_not_exist_for_posts_page() { + wp_set_current_user( self::$admin_id ); + + $page_id = self::factory()->post->create( array( 'post_type' => 'page' ) ); + update_option( 'show_on_front', 'page' ); + update_option( 'page_for_posts', $page_id ); + + // Reload the options from the database, so get_option() returns strings, as on a real request. + wp_cache_delete( 'alloptions', 'options' ); + + $links = $this->get_response_links( "/wp/v2/pages/{$page_id}" ); + + $this->assertArrayNotHasKey( 'https://api.w.org/action-set-homepage', $links, 'The action-set-homepage link should not exist.' ); + $this->assertArrayNotHasKey( 'https://api.w.org/action-set-posts-page', $links, 'The action-set-posts-page link should not exist.' ); + } + + /** + * @ticket 66327 + * + * @covers WP_REST_Posts_Controller::get_available_actions + */ + public function test_homepage_action_links_exist_for_other_pages() { + wp_set_current_user( self::$admin_id ); + + update_option( 'show_on_front', 'page' ); + update_option( 'page_on_front', self::factory()->post->create( array( 'post_type' => 'page' ) ) ); + update_option( 'page_for_posts', self::factory()->post->create( array( 'post_type' => 'page' ) ) ); + + $page_id = self::factory()->post->create( array( 'post_type' => 'page' ) ); + $links = $this->get_response_links( "/wp/v2/pages/{$page_id}" ); + + $this->assertArrayHasKey( 'https://api.w.org/action-set-homepage', $links, 'The action-set-homepage link should exist.' ); + $this->assertArrayHasKey( 'https://api.w.org/action-set-posts-page', $links, 'The action-set-posts-page link should exist.' ); + } + + /** + * @ticket 66327 + * + * @covers WP_REST_Posts_Controller::get_available_actions + */ + public function test_homepage_action_links_do_not_exist_for_posts() { + wp_set_current_user( self::$admin_id ); + + $post_id = self::factory()->post->create(); + $links = $this->get_response_links( "/wp/v2/posts/{$post_id}" ); + + $this->assertArrayNotHasKey( 'https://api.w.org/action-set-homepage', $links, 'The action-set-homepage link should not exist.' ); + $this->assertArrayNotHasKey( 'https://api.w.org/action-set-posts-page', $links, 'The action-set-posts-page link should not exist.' ); + } + + /** + * Requests a post and returns the links of the response. + * + * @param string $route Route of the post. + * @param string $context Optional. Request context. Default 'edit'. + * @return array Links of the response. + */ + protected function get_response_links( $route, $context = 'edit' ) { + $request = new WP_REST_Request( 'GET', $route ); + $request->set_query_params( array( 'context' => $context ) ); + + $response = rest_get_server()->dispatch( $request ); + $this->assertSame( 200, $response->get_status(), 'The post should be found.' ); + + return $response->get_links(); + } + public function filter_theme_page_templates( $page_templates ) { return array( 'page-my-test-template.php' => 'My Test Template',