Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
c633052
Navigation: Add an "Auto add pages" menu setting and drop the Page Li…
jeryj Oct 10, 2026
a578102
Block Library: Link the changelog entry to the pull request
jeryj Oct 10, 2026
8130743
Core Data: Refresh loaded Navigation Menus when a page is published
jeryj Oct 10, 2026
1502b6b
Extensible Site Editor: Save the "Auto add pages" setting from the Sa…
jeryj Oct 10, 2026
ee3b21c
Navigation: Save the "Auto add pages" setting as soon as it changes
jeryj Oct 10, 2026
72ca92c
Block Editor: Keep controlled inner blocks references stable across a…
jeryj Oct 14, 2026
af50ef0
Navigation: Save the "Auto add pages" setting through the editor's sa…
jeryj Oct 14, 2026
b3a1096
Navigation: Nest an auto-added child page under its parent's link
jeryj Oct 20, 2026
72d4076
Navigation: Test that a page created from the appender is added once
jeryj Oct 21, 2026
717fc45
Navigation: Test that auto-add keeps a page title intact with no curr…
jeryj Oct 26, 2026
567ddae
Navigation: Document how a cron save re-encodes existing link labels
jeryj Oct 26, 2026
0134ed2
Navigation: Assert existing links survive a no-user auto-add save unc…
jeryj Oct 26, 2026
4ff671e
Navigation: Show the "Auto add pages" setting below the menu's list o…
jeryj Oct 27, 2026
ffd4580
Navigation: Shorten the "Auto add pages" description and set the cont…
jeryj Oct 27, 2026
4760cf0
Navigation: Remove bound links to a deleted post or term from menus
jeryj Oct 26, 2026
fd13bb0
Navigation: Keep wrapper markup and match spaced IDs when removing links
jeryj Oct 26, 2026
b2f5401
Navigation: Assert untouched links survive a no-user removal unchanged
jeryj Oct 26, 2026
f2f184f
Merge remote-tracking branch 'origin/trunk' into try/auto-add-pages
jeryj Oct 27, 2026
870235d
Merge branch 'try/auto-add-pages' into add/navigation-remove-links-on…
jeryj Oct 27, 2026
01e816e
Core Data: Move the changelog entry under Unreleased
jeryj Oct 27, 2026
d029b41
Merge branch 'try/auto-add-pages' into add/navigation-remove-links-on…
jeryj Oct 27, 2026
f6bc656
Navigation: Rename the setting to "Auto add new pages"
jeryj Oct 28, 2026
748ff6d
Merge branch 'try/auto-add-pages' into add/navigation-remove-links-on…
jeryj Oct 28, 2026
dceab89
Navigation: Add new pages in page order and respect fallback opt-outs
jeryj Oct 29, 2026
2a79b52
Merge branch 'try/auto-add-pages' into add/navigation-remove-links-on…
jeryj Oct 29, 2026
3548485
Navigation: Keep the Page List as the default menu
jeryj Nov 24, 2026
8751cce
Merge branch 'try/auto-add-pages' into add/navigation-remove-links-on…
jeryj Nov 24, 2026
22441b7
Navigation: Don't say a new top-level page is appended
jeryj Nov 24, 2026
fb5f1e4
Merge branch 'try/auto-add-pages' into add/navigation-remove-links-on…
jeryj Nov 24, 2026
f2c120b
Navigation: Keep "Auto add new pages" when duplicating a menu
jeryj Nov 24, 2026
3b68e16
Merge remote-tracking branch 'origin/trunk' into try/auto-add-pages
jeryj Nov 24, 2026
06e7584
Merge branch 'try/auto-add-pages' into add/navigation-remove-links-on…
jeryj Nov 24, 2026
3aabb12
Navigation: Write the "Auto add new pages" e2e tests in TypeScript
jeryj Nov 24, 2026
42fcccb
Merge branch 'try/auto-add-pages' into add/navigation-remove-links-on…
jeryj Nov 24, 2026
86ea2d9
Navigation: Write the deleted links e2e test in TypeScript
jeryj Nov 24, 2026
4d10947
Merge remote-tracking branch 'origin/trunk' into try/auto-add-pages
jeryj Dec 1, 2026
68345dd
Navigation: Move the "Auto add new pages" PHP to the WordPress 7.3 co…
jeryj Dec 1, 2026
0848e76
Merge branch 'try/auto-add-pages' into add/navigation-remove-links-on…
jeryj Dec 1, 2026
807fdf9
Navigation: Move the deleted links PHP to the WordPress 7.3 compat fo…
jeryj Dec 1, 2026
65dbee5
Core Data: Put the changelog's Enhancements before Deprecations
jeryj Dec 1, 2026
a7bb3c6
Navigation: Type the "Auto add new pages" PHP for strict PHPStan
jeryj Dec 1, 2026
84329a5
Merge branch 'try/auto-add-pages' into add/navigation-remove-links-on…
jeryj Dec 1, 2026
73063dd
Navigation: Type the deleted links PHP for strict PHPStan
jeryj Dec 1, 2026
28aaedd
Navigation: Pin the "Auto add new pages" setting to the bottom of the…
jeryj Dec 1, 2026
edfa84d
Merge branch 'try/auto-add-pages' into add/navigation-remove-links-on…
jeryj Dec 1, 2026
9b33199
Merge remote-tracking branch 'origin/trunk' into try/auto-add-pages
jeryj Dec 2, 2026
bf3e403
Merge branch 'try/auto-add-pages' into add/navigation-remove-links-on…
jeryj Dec 2, 2026
bb640a0
Merge remote-tracking branch 'origin/trunk' into try/auto-add-pages
jeryj Dec 2, 2026
2bc2cc8
Merge branch 'try/auto-add-pages' into add/navigation-remove-links-on…
jeryj Dec 2, 2026
abb3b6b
Merge remote-tracking branch 'origin/trunk' into try/auto-add-pages
jeryj Dec 2, 2026
11fad34
Merge branch 'try/auto-add-pages' into add/navigation-remove-links-on…
jeryj Dec 2, 2026
70ae531
Navigation: Keep auto-added links in edited menus, with markup, and b…
jeryj Dec 2, 2026
030f404
Merge branch 'try/auto-add-pages' into add/navigation-remove-links-on…
jeryj Dec 2, 2026
10af3d9
Navigation: Address review round 1 on auto add new pages
jeryj Dec 2, 2026
d39b39b
Navigation: Address review round 2 on auto add new pages
jeryj Dec 2, 2026
6b70b57
Navigation: Address review round 3 on auto add new pages
jeryj Dec 2, 2026
c6d5d67
Navigation: Address review round 4 on auto add new pages
jeryj Dec 2, 2026
d71719c
Merge branch 'try/auto-add-pages' into add/navigation-remove-links-on…
jeryj Dec 2, 2026
04eca01
Merge remote-tracking branch 'origin/trunk' into try/auto-add-pages
jeryj Dec 3, 2026
b4c77cf
Merge branch 'try/auto-add-pages' into add/navigation-remove-links-on…
jeryj Dec 3, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
358 changes: 358 additions & 0 deletions lib/compat/wordpress-7.3/navigation-auto-add-pages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,358 @@
<?php
/**
* Navigation menus: the "Auto add new pages" setting.
*
* While the setting is on, publishing a new page adds a link for it to the
* menu, the way classic menus do with their "Automatically add new top-level
* pages" option. A top-level page is added to the menu; a child page is
* nested under its parent's link when the menu has one, which turns that link
* into a submenu. Existing items are never reordered, renamed or removed.
*
* @package gutenberg
*/

/**
* Registers the `wp_navigation_auto_add_pages` meta on Navigation Menus.
*
* The posts REST controller only exposes `meta` for post types that support
* custom fields, so that support is added to `wp_navigation` as well.
*
* @return void
*/
function gutenberg_register_navigation_auto_add_pages_meta() {
if ( registered_meta_key_exists( 'post', 'wp_navigation_auto_add_pages', 'wp_navigation' ) ) {
return;
}

add_post_type_support( 'wp_navigation', 'custom-fields' );

register_post_meta(
'wp_navigation',
'wp_navigation_auto_add_pages',
array(
'type' => 'boolean',
'description' => __( 'Whether newly published pages are added to the menu.', 'gutenberg' ),
'single' => true,
'default' => false,
'sanitize_callback' => 'rest_sanitize_boolean',
'show_in_rest' => true,
)
);
}
add_action( 'init', 'gutenberg_register_navigation_auto_add_pages_meta' );

/**
* Builds a Navigation Link block (or a Navigation Submenu block, when it has
* inner blocks) pointing at a page.
*
* Matches the blocks the Page List block creates when it is detached into
* links, including the URL binding that keeps the link in step with the
* page's permalink.
*
* @param WP_Post $page The page to link to.
* @param array<mixed> $inner_blocks Optional. Parsed blocks nested under the link.
* @param bool $filter_label Optional. Whether to filter the label with
* kses, for a user who can't post unfiltered
* HTML. Default false.
* @return array<string, mixed> A parsed block.
*/
function gutenberg_navigation_build_page_link_block( $page, $inner_blocks = array(), $filter_label = false ) {
$label = '' !== $page->post_title ? $page->post_title : __( '(no title)', 'gutenberg' );
return array(
'blockName' => empty( $inner_blocks ) ? 'core/navigation-link' : 'core/navigation-submenu',
'attrs' => array(
'label' => $filter_label ? wp_kses_post( $label ) : $label,
'type' => 'page',
'id' => (int) $page->ID,
'url' => get_permalink( $page ),
'kind' => 'post-type',
'metadata' => array(
'bindings' => array(
'url' => array(
'source' => 'core/post-data',
'args' => array( 'field' => 'link' ),
),
),
),
),
'innerBlocks' => $inner_blocks,
'innerHTML' => '',
'innerContent' => array_fill( 0, count( $inner_blocks ), null ),
);
}

/**
* Checks whether a block is a Navigation Link or Submenu pointing at a page.
*
* @param array<mixed> $block Parsed block.
* @param int $page_id Page ID.
* @return bool Whether the block links to the page.
*/
function gutenberg_navigation_block_links_to_page( $block, $page_id ) {
if ( ! in_array( $block['blockName'] ?? null, array( 'core/navigation-link', 'core/navigation-submenu' ), true ) ) {
return false;
}
$attrs = isset( $block['attrs'] ) && is_array( $block['attrs'] ) ? $block['attrs'] : array();
if ( ! isset( $attrs['id'] ) || ! is_numeric( $attrs['id'] ) || (int) $attrs['id'] !== $page_id ) {
return false;
}
$kind = isset( $attrs['kind'] ) ? $attrs['kind'] : '';
$type = isset( $attrs['type'] ) ? $attrs['type'] : '';
return 'post-type' === $kind || 'page' === $type;
}

/**
* Checks whether any block in a tree links to the given page.
*
* @param array<mixed> $blocks Parsed blocks.
* @param int $page_id Page ID.
* @return bool Whether a Navigation Link or Submenu for the page exists.
*/
function gutenberg_navigation_blocks_link_to_page( $blocks, $page_id ) {
foreach ( $blocks as $block ) {
if ( ! is_array( $block ) ) {
continue;
}
if ( gutenberg_navigation_block_links_to_page( $block, $page_id ) ) {
return true;
}
if ( ! empty( $block['innerBlocks'] ) && is_array( $block['innerBlocks'] ) && gutenberg_navigation_blocks_link_to_page( $block['innerBlocks'], $page_id ) ) {
return true;
}
}

return false;
}

/**
* Checks whether a Page List block lists a page.
*
* Like the Page List block, it lists published pages under published parent
* pages: every page or, with a parent page set, the pages under that page.
*
* @param int $parent_page_id The Page List's parent page, or 0 for all pages.
* @param WP_Post $page The page.
* @return bool Whether the Page List lists the page.
*/
function gutenberg_navigation_page_list_lists_page( $parent_page_id, $page ) {
foreach ( get_post_ancestors( $page ) as $ancestor_id ) {
if ( $parent_page_id === (int) $ancestor_id ) {
return true;
}
if ( 'publish' !== get_post_status( $ancestor_id ) ) {
return false;
}
}

return 0 === $parent_page_id;
}

/**
* Checks whether a Page List block in a tree shows a page.
*
* A Page List shows every page or, with `parentPageID` set, the pages under
* that page.
*
* @param array<mixed> $blocks Parsed blocks.
* @param WP_Post $page The page.
* @return bool Whether a Page List block in the tree shows the page.
*/
function gutenberg_navigation_page_list_shows_page( $blocks, $page ) {
foreach ( $blocks as $block ) {
if ( ! is_array( $block ) ) {
continue;
}
if ( 'core/page-list' === ( $block['blockName'] ?? null ) ) {
$attrs = isset( $block['attrs'] ) && is_array( $block['attrs'] ) ? $block['attrs'] : array();
// A value that isn't an integer fails the block's attribute
// validation, which falls back to the default, 0.
$parent_page_id = isset( $attrs['parentPageID'] ) && is_numeric( $attrs['parentPageID'] ) && rest_is_integer( $attrs['parentPageID'] ) ? (int) $attrs['parentPageID'] : 0;
if ( gutenberg_navigation_page_list_lists_page( $parent_page_id, $page ) ) {
return true;
}
}
if ( ! empty( $block['innerBlocks'] ) && is_array( $block['innerBlocks'] ) && gutenberg_navigation_page_list_shows_page( $block['innerBlocks'], $page ) ) {
return true;
}
}

return false;
}

/**
* Inserts a link to a page among its sibling blocks in page order.
*
* The link goes before the first link to a page that sorts after it by menu
* order and then title, as the Page List block sorts pages. When no linked
* page sorts after it, it goes after the last link to a page, or at the end
* when there is none. Other blocks keep their places.
*
* @param array<mixed> $blocks Parsed sibling blocks, modified in place.
* @param WP_Post $page The page the link points at.
* @param array<string, mixed> $link Parsed block linking to the page.
* @return void
*/
function gutenberg_navigation_insert_page_link( &$blocks, $page, $link ) {
$before = null;
$after = null;
foreach ( array_values( $blocks ) as $index => $block ) {
if ( ! is_array( $block ) ) {
continue;
}
$attrs = isset( $block['attrs'] ) && is_array( $block['attrs'] ) ? $block['attrs'] : array();
$sibling_id = isset( $attrs['id'] ) && is_numeric( $attrs['id'] ) ? (int) $attrs['id'] : 0;
$sibling = $sibling_id && gutenberg_navigation_block_links_to_page( $block, $sibling_id )
? get_post( $sibling_id )
: null;
if ( ! $sibling || 'page' !== $sibling->post_type ) {
continue;
}
$order = $sibling->menu_order <=> $page->menu_order;
if ( 0 === $order ) {
// Close to the database's collation, by which the Page List sorts
// titles: case and accents are ignored.
$order = strcasecmp( remove_accents( $sibling->post_title ), remove_accents( $page->post_title ) );
}
if ( $order > 0 ) {
$before = $index;
break;
}
$after = $index + 1;
}

array_splice( $blocks, $before ?? $after ?? count( $blocks ), 0, array( $link ) );
}

/**
* Nests a link to a page under the first block in a tree that links to the
* page's parent, in page order among the parent's other links.
*
* A Navigation Link for the page becomes a Navigation Submenu with the same
* attributes. With submenus set to open on click, the parent's item then
* opens the submenu instead of the page, as a parent page does in a Page
* List.
*
* @param array<mixed> $blocks Parsed blocks, modified in place.
* @param WP_Post $page The child page.
* @param array<string, mixed> $link Parsed block linking to the page.
* @return bool Whether a parent block was found.
*/
function gutenberg_navigation_nest_block_under_page( &$blocks, $page, $link ) {
foreach ( $blocks as &$block ) {
if ( ! is_array( $block ) ) {
continue;
}
$inner_blocks = isset( $block['innerBlocks'] ) && is_array( $block['innerBlocks'] ) ? $block['innerBlocks'] : array();
if ( gutenberg_navigation_block_links_to_page( $block, $page->post_parent ) ) {
gutenberg_navigation_insert_page_link( $inner_blocks, $page, $link );
$block['blockName'] = 'core/navigation-submenu';
$block['innerBlocks'] = $inner_blocks;
$block['innerHTML'] = '';
$block['innerContent'] = array_fill( 0, count( $inner_blocks ), null );
return true;
}
if ( ! empty( $inner_blocks ) && gutenberg_navigation_nest_block_under_page( $inner_blocks, $page, $link ) ) {
$block['innerBlocks'] = $inner_blocks;
return true;
}
}

return false;
}

/**
* Adds a link to a newly published page to every Navigation Menu that has
* "Auto add new pages" switched on.
*
* A top-level page is added to the menu in page order. A child page is nested
* under the link to its parent, which becomes a submenu; a menu with no link
* to the parent is left alone, and so is a menu with a Page List block that
* already shows the page. Like `_wp_auto_add_pages_to_menu()` for classic
* menus, only the transition into `publish` counts, and a page the menu
* already links to is not added again.
*
* @param string $new_status New post status.
* @param string $old_status Old post status.
* @param WP_Post $post The post being transitioned.
* @return void
*/
function gutenberg_navigation_auto_add_page_on_publish( $new_status, $old_status, $post ) {
if ( 'publish' !== $new_status || 'publish' === $old_status || 'page' !== $post->post_type ) {
return;
}

$menus = get_posts(
array(
'post_type' => 'wp_navigation',
'post_status' => 'any',
'posts_per_page' => -1,
'meta_key' => 'wp_navigation_auto_add_pages',
'meta_value' => '1',
'no_found_rows' => true,
'update_post_term_cache' => false,
)
);

// The kses filters on the post fields the current user saves, with their
// priorities. They're there when the user can't post unfiltered HTML.
$kses_filters = array();
foreach (
array(
'title_save_pre' => 'wp_filter_kses',
'content_save_pre' => 'wp_filter_post_kses',
'excerpt_save_pre' => 'wp_filter_post_kses',
'content_filtered_save_pre' => 'wp_filter_post_kses',
) as $hook => $callback
) {
$priority = has_filter( $hook, $callback );
if ( false !== $priority ) {
$kses_filters[] = array( $hook, $callback, $priority );
}
}
$filters_content = false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' );

foreach ( $menus as $menu ) {
$blocks = parse_blocks( $menu->post_content );
if ( gutenberg_navigation_page_list_shows_page( $blocks, $post ) || gutenberg_navigation_blocks_link_to_page( $blocks, $post->ID ) ) {
continue;
}

$link = gutenberg_navigation_build_page_link_block( $post, array(), $filters_content );
if ( empty( $post->post_parent ) ) {
gutenberg_navigation_insert_page_link( $blocks, $post, $link );
} elseif ( ! gutenberg_navigation_nest_block_under_page( $blocks, $post, $link ) ) {
continue;
}

/**
* The parsed blocks, with the link added.
*
* @var array<int|string, array{blockName: string|null, attrs: array<mixed>, innerBlocks: array<array<mixed>>, innerHTML: string, innerContent: array<mixed>}> $blocks
*/
$content = serialize_blocks( $blocks );

// The menu's items were filtered when they were saved, by a user
// allowed to save them. Filtering them again here, as the user who
// publishes the page, would strip what that user can't post, such as
// an icon in a link's label. Only the new link's label comes from this
// user, so only it is filtered (above), and the kses filters are
// lifted for this save alone, which also writes back the menu's
// title.
foreach ( $kses_filters as list( $hook, $callback, $priority ) ) {
remove_filter( $hook, $callback, $priority );
}
try {
wp_update_post(
array(
'ID' => $menu->ID,
'post_content' => wp_slash( $content ),
)
);
} finally {
foreach ( $kses_filters as list( $hook, $callback, $priority ) ) {
add_filter( $hook, $callback, $priority );
}
}
}
}
add_action( 'transition_post_status', 'gutenberg_navigation_auto_add_page_on_publish', 10, 3 );
Loading
Loading