Make WordPress Core

Opened 10 hours ago

Closed 3 hours ago

#66327 closed enhancement (fixed)

REST API: Add action-set-homepage and action-set-posts-page links to pages

Reported by: jorgefilipecosta Owned by: jorgefilipecosta
Priority: normal Milestone: Awaiting Review
Component: General Version:
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses:

Description

Adds the https://api.w.org/action-set-homepage and https://api.w.org/action-set-posts-page links to pages in the edit context, like action-trash (#63670). Published pages get them when the current user can manage_options, except the current homepage and posts page.

The editor uses these links for its "Set as homepage" and "Set as posts page" actions, so it no longer has to read the site settings. And the actions can just rely on the current self contained item information without additional API changes or requests.

Gutenberg PR: ​https://github.com/WordPress/gutenberg/pull/84932

Change History (2)

This ticket was mentioned in ​PR #14347 on ​WordPress/wordpress-develop by ​@jorgefilipecosta.


10 hours ago
#1

  • Keywords has-patch has-unit-tests added

Backports the homepage and posts page action links from ​https://github.com/WordPress/gutenberg/pull/84932, part of ​https://github.com/WordPress/gutenberg/issues/83537.

Right now a client that wants to know whether a page can be set as the homepage or as the posts page needs the site settings on top of the page, and whether the user can read them. The editor reads them for its "Set as homepage" and "Set as posts page" actions. This PR lets the server say it with links, as action-publish, action-sticky and action-trash do for other actions:

  • Adds the https://api.w.org/action-set-homepage and https://api.w.org/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, like the settings route. The current homepage and posts page get neither.
  • Adds their link description objects to the schema of pages, like the other action links. The Gutenberg PR only adds the links.

Some notes:

  • The links reflect the settings when the page was requested, so a client that changes the homepage or the posts page needs to request the affected pages again. The Gutenberg PR does that after saving the settings.
  • Like the actions in the editor, the links don't check show_on_front, so the page stored in page_on_front gets neither while the homepage shows the latest posts.
  • The links don't check whether the theme has a front-page template, which makes the homepage setting have no effect. It's the same for every page, so the Gutenberg PR checks it once, when it registers the actions.

Testing Instructions

  1. Create three published pages, "Page A", "Page B" and "Page C", and set "Page A" as the homepage in Settings > Reading.
  2. Open wp-admin/post-new.php as an administrator. In the console, run ( await wp.apiFetch( { path: '/wp/v2/pages?context=edit' } ) ).map( ( page ) => [ page.title.raw, Object.keys( page._links ).filter( ( rel ) => rel.startsWith( 'wp:action-set' ) ) ] ) and verify "Page B" and "Page C" have both links and "Page A" has neither.
  3. Run the same request with context=view and verify no page has them.
  4. Log in as an editor, repeat step 2, and verify no page has them.
  5. Run npm run test:php -- --filter WP_Test_REST_Pages_Controller.

Gutenberg PR: ​https://github.com/WordPress/gutenberg/pull/84932

Use of AI Tools

AI assistance: Yes
Used for: porting the code and the tests from the Gutenberg PR, and drafting this description; reviewed and edited by me.

#2 @jorgefilipecosta
3 hours ago

  • Owner set to jorgefilipecosta
  • Resolution → fixed
  • Status new → closed

In 64433:

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, and their link description objects to the pages schema. Both change the site settings, so they require manage_options, and the current homepage and posts page get neither. The editor uses them for its "Set as homepage" and "Set as posts page" actions.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/14347 and ​https://github.com/WordPress/gutenberg/pull/84932.

Follow-up to [64343].

Props jorgefilipecosta, oandregal, chihsuan.
Fixes #66327.

Note: See TracTickets for help on using tickets.