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.


git-svn-id: https://develop.svn.wordpress.org/trunk@47763 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jake Spurlock 2020-05-05 21:15:49 +00:00
parent b19c56b736
commit 8338f44306
1 changed files with 89 additions and 31 deletions

View File

@ -146,16 +146,49 @@ final class WP_Customize_Nav_Menus {
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 ) {
// Add "Home" link. Treat as a page, but switch to custom on add.
$items[] = array(
'id' => 'home',
'title' => _x( 'Home', 'nav menu home label' ),
'type' => 'custom',
'type_label' => __( 'Custom Link' ),
'object' => '',
'url' => home_url(),
);
// 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.
$items[] = array(
'id' => 'home',
'title' => _x( 'Home', 'nav menu home label' ),
'type' => 'custom',
'type_label' => __( 'Custom Link' ),
'object' => '',
'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 ) {
// Add a post type archive link.
$items[] = array(
@ -179,17 +212,23 @@ final class WP_Customize_Nav_Menus {
}
}
$args = array(
'numberposts' => 10,
'offset' => 10 * $page,
'orderby' => 'date',
'order' => 'DESC',
'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,
get_posts(
array(
'numberposts' => 10,
'offset' => 10 * $page,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => $object,
)
)
$important_pages,
get_posts( $args )
);
foreach ( $posts as $post ) {
@ -198,11 +237,18 @@ final class WP_Customize_Nav_Menus {
/* translators: %d: ID of a post. */
$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(
'id' => "post-{$post->ID}",
'title' => html_entity_decode( $post_title, ENT_QUOTES, get_bloginfo( 'charset' ) ),
'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_id' => intval( $post->ID ),
'url' => get_permalink( intval( $post->ID ) ),
@ -224,6 +270,7 @@ final class WP_Customize_Nav_Menus {
'pad_counts' => false,
)
);
if ( is_wp_error( $terms ) ) {
return $terms;
}
@ -351,11 +398,18 @@ final class WP_Customize_Nav_Menus {
/* translators: %d: ID of a post. */
$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(
'id' => 'post-' . $post->ID,
'title' => html_entity_decode( $post_title, ENT_QUOTES, get_bloginfo( 'charset' ) ),
'type' => 'post_type',
'type_label' => $post_type_objects[ $post->post_type ]->labels->singular_name,
'type_label' => $post_type_label,
'object' => $post->post_type,
'object_id' => intval( $post->ID ),
'url' => get_permalink( intval( $post->ID ) ),
@ -391,17 +445,21 @@ final class WP_Customize_Nav_Menus {
// Add "Home" link if search term matches. Treat as a page, but switch to custom on add.
if ( isset( $args['s'] ) ) {
$title = _x( 'Home', 'nav menu home label' );
$matches = function_exists( 'mb_stripos' ) ? false !== mb_stripos( $title, $args['s'] ) : false !== stripos( $title, $args['s'] );
if ( $matches ) {
$items[] = array(
'id' => 'home',
'title' => $title,
'type' => 'custom',
'type_label' => __( 'Custom Link' ),
'object' => '',
'url' => home_url(),
);
// 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' );
$matches = function_exists( 'mb_stripos' ) ? false !== mb_stripos( $title, $args['s'] ) : false !== stripos( $title, $args['s'] );
if ( $matches ) {
$items[] = array(
'id' => 'home',
'title' => $title,
'type' => 'custom',
'type_label' => __( 'Custom Link' ),
'object' => '',
'url' => home_url(),
);
}
}
}