Menus: Denote the special pages in the Customizer menu editor.

The special pages here are the pages that are used for the Privacy Policy, Home, and the Posts page.

Fixes #46829.
Props garrett-eclipse, audrasjb.

Built from https://develop.svn.wordpress.org/trunk@47763


git-svn-id: http://core.svn.wordpress.org/trunk@47539 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
whyisjake 2020-05-05 21:17:06 +00:00
parent 9782eb82ad
commit 7a48040e13
2 changed files with 90 additions and 32 deletions

View File

@ -146,7 +146,20 @@ final class WP_Customize_Nav_Menus {
return new WP_Error( 'nav_menus_invalid_post_type' ); return new WP_Error( 'nav_menus_invalid_post_type' );
} }
/*
* If we're dealing with pages, let's prioritize the Front Page,
* Posts Page and Privacy Policy Page at the top of the list.
*/
$important_pages = array();
$suppress_page_ids = array();
if ( 0 === $page && 'page' === $object ) { if ( 0 === $page && 'page' === $object ) {
// Insert Front Page or custom "Home" link.
$front_page = 'page' == get_option( 'show_on_front' ) ? (int) get_option( 'page_on_front' ) : 0;
if ( ! empty( $front_page ) ) {
$front_page_obj = get_post( $front_page );
$important_pages[] = $front_page_obj;
$suppress_page_ids[] = $front_page_obj->ID;
} else {
// Add "Home" link. Treat as a page, but switch to custom on add. // Add "Home" link. Treat as a page, but switch to custom on add.
$items[] = array( $items[] = array(
'id' => 'home', 'id' => 'home',
@ -156,6 +169,26 @@ final class WP_Customize_Nav_Menus {
'object' => '', 'object' => '',
'url' => home_url(), 'url' => home_url(),
); );
}
// Insert Posts Page.
$posts_page = 'page' == get_option( 'show_on_front' ) ? (int) get_option( 'page_for_posts' ) : 0;
if ( ! empty( $posts_page ) ) {
$posts_page_obj = get_post( $posts_page );
$important_pages[] = $posts_page_obj;
$suppress_page_ids[] = $posts_page_obj->ID;
}
// Insert Privacy Policy Page.
$privacy_policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' );
if ( ! empty( $privacy_policy_page_id ) ) {
$privacy_policy_page = get_post( $privacy_policy_page_id );
if ( $privacy_policy_page instanceof WP_Post && 'publish' === $privacy_policy_page->post_status ) {
$important_pages[] = $privacy_policy_page;
$suppress_page_ids[] = $privacy_policy_page->ID;
}
}
} elseif ( 'post' !== $object && 0 === $page && $post_type->has_archive ) { } elseif ( 'post' !== $object && 0 === $page && $post_type->has_archive ) {
// Add a post type archive link. // Add a post type archive link.
$items[] = array( $items[] = array(
@ -179,17 +212,23 @@ final class WP_Customize_Nav_Menus {
} }
} }
$posts = array_merge( $args = array(
$posts,
get_posts(
array(
'numberposts' => 10, 'numberposts' => 10,
'offset' => 10 * $page, 'offset' => 10 * $page,
'orderby' => 'date', 'orderby' => 'date',
'order' => 'DESC', 'order' => 'DESC',
'post_type' => $object, 'post_type' => $object,
) );
)
// Add suppression array to arguments for get_posts.
if ( ! empty( $suppress_page_ids ) ) {
$args['post__not_in'] = $suppress_page_ids;
}
$posts = array_merge(
$posts,
$important_pages,
get_posts( $args )
); );
foreach ( $posts as $post ) { foreach ( $posts as $post ) {
@ -198,11 +237,18 @@ final class WP_Customize_Nav_Menus {
/* translators: %d: ID of a post. */ /* translators: %d: ID of a post. */
$post_title = sprintf( __( '#%d (no title)' ), $post->ID ); $post_title = sprintf( __( '#%d (no title)' ), $post->ID );
} }
$post_type_label = get_post_type_object( $post->post_type )->labels->singular_name;
$post_states = get_post_states( $post );
if ( ! empty( $post_states ) ) {
$post_type_label = implode( ',', $post_states );
}
$items[] = array( $items[] = array(
'id' => "post-{$post->ID}", 'id' => "post-{$post->ID}",
'title' => html_entity_decode( $post_title, ENT_QUOTES, get_bloginfo( 'charset' ) ), 'title' => html_entity_decode( $post_title, ENT_QUOTES, get_bloginfo( 'charset' ) ),
'type' => 'post_type', 'type' => 'post_type',
'type_label' => get_post_type_object( $post->post_type )->labels->singular_name, 'type_label' => $post_type_label,
'object' => $post->post_type, 'object' => $post->post_type,
'object_id' => intval( $post->ID ), 'object_id' => intval( $post->ID ),
'url' => get_permalink( intval( $post->ID ) ), 'url' => get_permalink( intval( $post->ID ) ),
@ -224,6 +270,7 @@ final class WP_Customize_Nav_Menus {
'pad_counts' => false, 'pad_counts' => false,
) )
); );
if ( is_wp_error( $terms ) ) { if ( is_wp_error( $terms ) ) {
return $terms; return $terms;
} }
@ -351,11 +398,18 @@ final class WP_Customize_Nav_Menus {
/* translators: %d: ID of a post. */ /* translators: %d: ID of a post. */
$post_title = sprintf( __( '#%d (no title)' ), $post->ID ); $post_title = sprintf( __( '#%d (no title)' ), $post->ID );
} }
$post_type_label = $post_type_objects[ $post->post_type ]->labels->singular_name;
$post_states = get_post_states( $post );
if ( ! empty( $post_states ) ) {
$post_type_label = implode( ',', $post_states );
}
$items[] = array( $items[] = array(
'id' => 'post-' . $post->ID, 'id' => 'post-' . $post->ID,
'title' => html_entity_decode( $post_title, ENT_QUOTES, get_bloginfo( 'charset' ) ), 'title' => html_entity_decode( $post_title, ENT_QUOTES, get_bloginfo( 'charset' ) ),
'type' => 'post_type', 'type' => 'post_type',
'type_label' => $post_type_objects[ $post->post_type ]->labels->singular_name, 'type_label' => $post_type_label,
'object' => $post->post_type, 'object' => $post->post_type,
'object_id' => intval( $post->ID ), 'object_id' => intval( $post->ID ),
'url' => get_permalink( intval( $post->ID ) ), 'url' => get_permalink( intval( $post->ID ) ),
@ -391,6 +445,9 @@ final class WP_Customize_Nav_Menus {
// Add "Home" link if search term matches. Treat as a page, but switch to custom on add. // Add "Home" link if search term matches. Treat as a page, but switch to custom on add.
if ( isset( $args['s'] ) ) { if ( isset( $args['s'] ) ) {
// Only insert custom "Home" link if there's no Front Page
$front_page = 'page' == get_option( 'show_on_front' ) ? (int) get_option( 'page_on_front' ) : 0;
if ( empty( $front_page ) ) {
$title = _x( 'Home', 'nav menu home label' ); $title = _x( 'Home', 'nav menu home label' );
$matches = function_exists( 'mb_stripos' ) ? false !== mb_stripos( $title, $args['s'] ) : false !== stripos( $title, $args['s'] ); $matches = function_exists( 'mb_stripos' ) ? false !== mb_stripos( $title, $args['s'] ) : false !== stripos( $title, $args['s'] );
if ( $matches ) { if ( $matches ) {
@ -404,6 +461,7 @@ final class WP_Customize_Nav_Menus {
); );
} }
} }
}
/** /**
* Filters the available menu items during a search request. * Filters the available menu items during a search request.

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.5-alpha-47762'; $wp_version = '5.5-alpha-47763';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.