From 0858a5dfd973818dc7069bf121724162481bbeab Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Fri, 16 Oct 2026 13:42:06 +0100 Subject: [PATCH 1/3] REST API: Add the homepage and posts page action links to pages. Adds the `action-set-homepage` and `action-set-posts-page` links to the published pages the current user can set as the homepage or as the posts page, in the `edit` context. Both change the site settings, so they require `manage_options`, and the current homepage and posts page get neither. --- .../class-wp-rest-posts-controller.php | 30 ++- .../tests/rest-api/rest-pages-controller.php | 192 ++++++++++++++++++ 2 files changed, 220 insertions(+), 2 deletions(-) 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..8ac8f9c4f8972 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,191 @@ public function test_get_item_schema() { $this->assertArrayHasKey( 'class_list', $properties ); } + /** + * @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 ); + $rels = wp_list_pluck( $schema['links'], 'rel' ); + + $this->assertContains( 'https://api.w.org/action-set-homepage', $rels, 'The action-set-homepage LDO should be on the schema.' ); + $this->assertContains( 'https://api.w.org/action-set-posts-page', $rels, 'The action-set-posts-page LDO should be on the schema.' ); + } + + /** + * @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 ); + $rels = wp_list_pluck( $schema['links'], 'rel' ); + + $this->assertNotContains( 'https://api.w.org/action-set-homepage', $rels, 'The action-set-homepage LDO should not be on the schema.' ); + $this->assertNotContains( 'https://api.w.org/action-set-posts-page', $rels, 'The action-set-posts-page LDO should not be on the schema.' ); + } + + /** + * @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 ); + $this->assertArrayHasKey( 'https://api.w.org/action-set-posts-page', $links ); + $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.' ); + } + + /** + * @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 ); + $this->assertArrayNotHasKey( 'https://api.w.org/action-set-posts-page', $links ); + } + + /** + * @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 ); + $this->assertArrayNotHasKey( 'https://api.w.org/action-set-posts-page', $links ); + } + + /** + * @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 ); + $this->assertArrayNotHasKey( 'https://api.w.org/action-set-posts-page', $links ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_unpublished_page_statuses() { + return array( + 'draft' => array( 'draft' ), + 'pending' => array( 'pending' ), + 'private' => array( 'private' ), + ); + } + + /** + * @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 ); + + $links = $this->get_response_links( "/wp/v2/pages/{$page_id}" ); + + $this->assertArrayNotHasKey( 'https://api.w.org/action-set-homepage', $links ); + $this->assertArrayNotHasKey( 'https://api.w.org/action-set-posts-page', $links ); + } + + /** + * @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 ); + + $links = $this->get_response_links( "/wp/v2/pages/{$page_id}" ); + + $this->assertArrayNotHasKey( 'https://api.w.org/action-set-homepage', $links ); + $this->assertArrayNotHasKey( 'https://api.w.org/action-set-posts-page', $links ); + } + + /** + * @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 ); + $this->assertArrayHasKey( 'https://api.w.org/action-set-posts-page', $links ); + } + + /** + * @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 ); + $this->assertArrayNotHasKey( 'https://api.w.org/action-set-posts-page', $links ); + } + + /** + * 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', From e89c7168a04d1e2acb2cf5ed510caef5d9e48151 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Fri, 16 Oct 2026 16:49:52 +0100 Subject: [PATCH 2/3] Tests: Read the homepage and posts page settings back from the database. `update_option()` leaves the integer in the options cache, so these tests read an integer from `get_option()`, while a request reads a string from the database. Clearing the cache makes them fail when the `(int)` casts in `get_available_actions()` are removed. --- tests/phpunit/tests/rest-api/rest-pages-controller.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/phpunit/tests/rest-api/rest-pages-controller.php b/tests/phpunit/tests/rest-api/rest-pages-controller.php index 8ac8f9c4f8972..af5d89507dce8 100644 --- a/tests/phpunit/tests/rest-api/rest-pages-controller.php +++ b/tests/phpunit/tests/rest-api/rest-pages-controller.php @@ -990,6 +990,9 @@ public function test_homepage_action_links_do_not_exist_for_homepage() { 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 ); @@ -1006,6 +1009,9 @@ public function test_homepage_action_links_do_not_exist_for_posts_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 ); From 1bf8ca0e5612b83614f95aaa678299ec4b1f43eb Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Fri, 16 Oct 2026 23:34:17 +0100 Subject: [PATCH 3/3] Tests: Add ticket references and failure messages to homepage link tests. Adds `@ticket 66327` to the new tests and a failure message to every assertion. The schema tests now count each LDO with `wp_list_filter()` and `assertCount()`, like the other LDO tests, so an LDO added twice fails them. --- .../tests/rest-api/rest-pages-controller.php | 73 ++++++++++++------- 1 file changed, 48 insertions(+), 25 deletions(-) diff --git a/tests/phpunit/tests/rest-api/rest-pages-controller.php b/tests/phpunit/tests/rest-api/rest-pages-controller.php index af5d89507dce8..3aabf5dfc4e4e 100644 --- a/tests/phpunit/tests/rest-api/rest-pages-controller.php +++ b/tests/phpunit/tests/rest-api/rest-pages-controller.php @@ -875,6 +875,8 @@ public function test_get_item_schema() { } /** + * @ticket 66327 + * * @covers WP_REST_Posts_Controller::get_schema_links */ public function test_homepage_action_ldos_registered() { @@ -882,14 +884,17 @@ public function test_homepage_action_ldos_registered() { $data = $response->get_data(); $schema = $data['schema']; - $this->assertArrayHasKey( 'links', $schema ); - $rels = wp_list_pluck( $schema['links'], 'rel' ); + $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->assertContains( 'https://api.w.org/action-set-homepage', $rels, 'The action-set-homepage LDO should be on the schema.' ); - $this->assertContains( 'https://api.w.org/action-set-posts-page', $rels, 'The action-set-posts-page LDO should be on the schema.' ); + $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() { @@ -897,14 +902,17 @@ public function test_homepage_action_ldos_not_registered_for_posts() { $data = $response->get_data(); $schema = $data['schema']; - $this->assertArrayHasKey( 'links', $schema ); - $rels = wp_list_pluck( $schema['links'], 'rel' ); + $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->assertNotContains( 'https://api.w.org/action-set-homepage', $rels, 'The action-set-homepage LDO should not be on the schema.' ); - $this->assertNotContains( 'https://api.w.org/action-set-posts-page', $rels, 'The action-set-posts-page LDO should not be on the schema.' ); + $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() { @@ -913,13 +921,15 @@ public function test_homepage_action_links_exist_for_admin() { $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 ); - $this->assertArrayHasKey( 'https://api.w.org/action-set-posts-page', $links ); + $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() { @@ -928,11 +938,13 @@ public function test_homepage_action_links_only_available_in_edit_context() { $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 ); - $this->assertArrayNotHasKey( 'https://api.w.org/action-set-posts-page', $links ); + $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() { @@ -941,11 +953,13 @@ public function test_homepage_action_links_do_not_exist_for_editor() { $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 ); - $this->assertArrayNotHasKey( 'https://api.w.org/action-set-posts-page', $links ); + $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 @@ -961,10 +975,11 @@ public function test_homepage_action_links_do_not_exist_for_unpublished_pages( $ 'post_status' => $status, ) ); - $links = $this->get_response_links( "/wp/v2/pages/{$page_id}" ); - $this->assertArrayNotHasKey( 'https://api.w.org/action-set-homepage', $links ); - $this->assertArrayNotHasKey( 'https://api.w.org/action-set-posts-page', $links ); + $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.' ); } /** @@ -981,6 +996,8 @@ public static function data_unpublished_page_statuses() { } /** + * @ticket 66327 + * * @covers WP_REST_Posts_Controller::get_available_actions */ public function test_homepage_action_links_do_not_exist_for_homepage() { @@ -995,11 +1012,13 @@ public function test_homepage_action_links_do_not_exist_for_homepage() { $links = $this->get_response_links( "/wp/v2/pages/{$page_id}" ); - $this->assertArrayNotHasKey( 'https://api.w.org/action-set-homepage', $links ); - $this->assertArrayNotHasKey( 'https://api.w.org/action-set-posts-page', $links ); + $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() { @@ -1014,11 +1033,13 @@ public function test_homepage_action_links_do_not_exist_for_posts_page() { $links = $this->get_response_links( "/wp/v2/pages/{$page_id}" ); - $this->assertArrayNotHasKey( 'https://api.w.org/action-set-homepage', $links ); - $this->assertArrayNotHasKey( 'https://api.w.org/action-set-posts-page', $links ); + $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() { @@ -1031,11 +1052,13 @@ public function test_homepage_action_links_exist_for_other_pages() { $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 ); - $this->assertArrayHasKey( 'https://api.w.org/action-set-posts-page', $links ); + $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() { @@ -1044,8 +1067,8 @@ public function test_homepage_action_links_do_not_exist_for_posts() { $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 ); - $this->assertArrayNotHasKey( 'https://api.w.org/action-set-posts-page', $links ); + $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.' ); } /**