diff -r 3d4e9c994f10 -r a86126ab1dd4 wp/wp-admin/includes/nav-menu.php --- a/wp/wp-admin/includes/nav-menu.php Tue Oct 22 16:11:46 2019 +0200 +++ b/wp/wp-admin/includes/nav-menu.php Tue Dec 15 13:49:49 2020 +0100 @@ -8,10 +8,10 @@ */ /** Walker_Nav_Menu_Edit class */ -require_once( ABSPATH . 'wp-admin/includes/class-walker-nav-menu-edit.php' ); +require_once ABSPATH . 'wp-admin/includes/class-walker-nav-menu-edit.php'; /** Walker_Nav_Menu_Checklist class */ -require_once( ABSPATH . 'wp-admin/includes/class-walker-nav-menu-checklist.php' ); +require_once ABSPATH . 'wp-admin/includes/class-walker-nav-menu-checklist.php'; /** * Prints the appropriate response to a menu quick search. @@ -25,19 +25,23 @@ $type = isset( $request['type'] ) ? $request['type'] : ''; $object_type = isset( $request['object_type'] ) ? $request['object_type'] : ''; $query = isset( $request['q'] ) ? $request['q'] : ''; - $response_format = isset( $request['response-format'] ) && in_array( $request['response-format'], array( 'json', 'markup' ) ) ? $request['response-format'] : 'json'; + $response_format = isset( $request['response-format'] ) ? $request['response-format'] : ''; - if ( 'markup' == $response_format ) { + if ( ! $response_format || ! in_array( $response_format, array( 'json', 'markup' ), true ) ) { + $response_format = 'json'; + } + + if ( 'markup' === $response_format ) { $args['walker'] = new Walker_Nav_Menu_Checklist; } - if ( 'get-post-item' == $type ) { + if ( 'get-post-item' === $type ) { if ( post_type_exists( $object_type ) ) { if ( isset( $request['ID'] ) ) { $object_id = (int) $request['ID']; - if ( 'markup' == $response_format ) { + if ( 'markup' === $response_format ) { echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', array( get_post( $object_id ) ) ), 0, (object) $args ); - } elseif ( 'json' == $response_format ) { + } elseif ( 'json' === $response_format ) { echo wp_json_encode( array( 'ID' => $object_id, @@ -51,9 +55,9 @@ } elseif ( taxonomy_exists( $object_type ) ) { if ( isset( $request['ID'] ) ) { $object_id = (int) $request['ID']; - if ( 'markup' == $response_format ) { + if ( 'markup' === $response_format ) { echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', array( get_term( $object_id, $object_type ) ) ), 0, (object) $args ); - } elseif ( 'json' == $response_format ) { + } elseif ( 'json' === $response_format ) { $post_obj = get_term( $object_id, $object_type ); echo wp_json_encode( array( @@ -67,7 +71,7 @@ } } } elseif ( preg_match( '/quick-search-(posttype|taxonomy)-([a-zA-Z_-]*\b)/', $type, $matches ) ) { - if ( 'posttype' == $matches[1] && get_post_type_object( $matches[2] ) ) { + if ( 'posttype' === $matches[1] && get_post_type_object( $matches[2] ) ) { $post_type_obj = _wp_nav_menu_meta_box_object( get_post_type_object( $matches[2] ) ); $args = array_merge( $args, @@ -89,10 +93,10 @@ } while ( $search_results_query->have_posts() ) { $post = $search_results_query->next_post(); - if ( 'markup' == $response_format ) { + if ( 'markup' === $response_format ) { $var_by_ref = $post->ID; echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', array( get_post( $var_by_ref ) ) ), 0, (object) $args ); - } elseif ( 'json' == $response_format ) { + } elseif ( 'json' === $response_format ) { echo wp_json_encode( array( 'ID' => $post->ID, @@ -103,21 +107,22 @@ echo "\n"; } } - } elseif ( 'taxonomy' == $matches[1] ) { + } elseif ( 'taxonomy' === $matches[1] ) { $terms = get_terms( array( 'taxonomy' => $matches[2], 'name__like' => $query, 'number' => 10, + 'hide_empty' => false, ) ); if ( empty( $terms ) || is_wp_error( $terms ) ) { return; } foreach ( (array) $terms as $term ) { - if ( 'markup' == $response_format ) { + if ( 'markup' === $response_format ) { echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', array( $term ) ), 0, (object) $args ); - } elseif ( 'json' == $response_format ) { + } elseif ( 'json' === $response_format ) { echo wp_json_encode( array( 'ID' => $term->term_id, @@ -138,12 +143,12 @@ * @since 3.0.0 */ function wp_nav_menu_setup() { - // Register meta boxes + // Register meta boxes. wp_nav_menu_post_type_meta_boxes(); add_meta_box( 'add-custom-links', __( 'Custom Links' ), 'wp_nav_menu_item_link_meta_box', 'nav-menus', 'side', 'default' ); wp_nav_menu_taxonomy_meta_boxes(); - // Register advanced menu items (columns) + // Register advanced menu items (columns). add_filter( 'manage_nav-menus_columns', 'wp_nav_menu_manage_columns' ); // If first time editing, disable advanced items by default. @@ -184,7 +189,7 @@ foreach ( array_keys( $wp_meta_boxes['nav-menus'] ) as $context ) { foreach ( array_keys( $wp_meta_boxes['nav-menus'][ $context ] ) as $priority ) { foreach ( $wp_meta_boxes['nav-menus'][ $context ][ $priority ] as $box ) { - if ( in_array( $box['id'], $initial_meta_boxes ) ) { + if ( in_array( $box['id'], $initial_meta_boxes, true ) ) { unset( $box['id'] ); } else { $hidden_meta_boxes[] = $box['id']; @@ -219,14 +224,14 @@ * * @since 3.0.0 * - * @param object $meta_box_object The current object to add a menu items - * meta box for. + * @param WP_Post_Type|false $post_type The current object to add a menu items + * meta box for. */ $post_type = apply_filters( 'nav_menu_meta_box_object', $post_type ); if ( $post_type ) { $id = $post_type->name; // Give pages a higher priority. - $priority = ( 'page' == $post_type->name ? 'core' : 'default' ); + $priority = ( 'page' === $post_type->name ? 'core' : 'default' ); add_meta_box( "add-post-type-{$id}", $post_type->labels->name, 'wp_nav_menu_item_post_type_meta_box', 'nav-menus', 'side', $priority, $post_type ); } } @@ -255,23 +260,25 @@ } /** - * Check whether to disable the Menu Locations meta box submit button + * Check whether to disable the Menu Locations meta box submit button and inputs. * * @since 3.6.0 + * @since 5.3.1 The `$echo` parameter was added. * * @global bool $one_theme_location_no_menus to determine if no menus exist * - * @param int|string $nav_menu_selected_id (id, name or slug) of the currently-selected menu - * @return string Disabled attribute if at least one menu exists, false if not + * @param int|string $nav_menu_selected_id ID, name, or slug of the currently selected menu. + * @param bool $echo Whether to echo or just return the string. + * @return string|false Disabled attribute if at least one menu exists, false if not. */ -function wp_nav_menu_disabled_check( $nav_menu_selected_id ) { +function wp_nav_menu_disabled_check( $nav_menu_selected_id, $echo = true ) { global $one_theme_location_no_menus; if ( $one_theme_location_no_menus ) { return false; } - return disabled( $nav_menu_selected_id, 0 ); + return disabled( $nav_menu_selected_id, 0, $echo ); } /** @@ -292,12 +299,12 @@

@@ -325,7 +332,7 @@ * * @type string $id Meta box 'id' attribute. * @type string $title Meta box title. - * @type string $callback Meta box display callback. + * @type callable $callback Meta box display callback. * @type WP_Post_Type $args Extra meta box arguments (the post type object for this meta box). * } */ @@ -333,10 +340,12 @@ global $_nav_menu_placeholder, $nav_menu_selected_id; $post_type_name = $box['args']->name; + $post_type = get_post_type_object( $post_type_name ); + $tab_name = $post_type_name . '-tab'; // Paginate browsing for large numbers of post objects. $per_page = 50; - $pagenum = isset( $_REQUEST[ $post_type_name . '-tab' ] ) && isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 1; + $pagenum = isset( $_REQUEST[ $tab_name ] ) && isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 1; $offset = 0 < $pagenum ? $per_page * ( $pagenum - 1 ) : 0; $args = array( @@ -354,12 +363,88 @@ $args = array_merge( $args, (array) $box['args']->_default_query ); } - // @todo transient caching of these results with proper invalidation on updating of a post of this 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(); + if ( 'page' === $post_type_name ) { + $suppress_page_ids = array(); + + // Insert Front Page or custom Home link. + $front_page = 'page' === get_option( 'show_on_front' ) ? (int) get_option( 'page_on_front' ) : 0; + + $front_page_obj = null; + if ( ! empty( $front_page ) ) { + $front_page_obj = get_post( $front_page ); + $front_page_obj->front_or_home = true; + + $important_pages[] = $front_page_obj; + $suppress_page_ids[] = $front_page_obj->ID; + } else { + $_nav_menu_placeholder = ( 0 > $_nav_menu_placeholder ) ? intval( $_nav_menu_placeholder ) - 1 : -1; + $front_page_obj = (object) array( + 'front_or_home' => true, + 'ID' => 0, + 'object_id' => $_nav_menu_placeholder, + 'post_content' => '', + 'post_excerpt' => '', + 'post_parent' => '', + 'post_title' => _x( 'Home', 'nav menu home label' ), + 'post_type' => 'nav_menu_item', + 'type' => 'custom', + 'url' => home_url( '/' ), + ); + + $important_pages[] = $front_page_obj; + } + + // 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 ); + $posts_page_obj->posts_page = true; + + $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 ) { + $privacy_policy_page->privacy_policy_page = true; + + $important_pages[] = $privacy_policy_page; + $suppress_page_ids[] = $privacy_policy_page->ID; + } + } + + // Add suppression array to arguments for WP_Query. + if ( ! empty( $suppress_page_ids ) ) { + $args['post__not_in'] = $suppress_page_ids; + } + } + + // @todo Transient caching of these results with proper invalidation on updating of a post of this type. $get_posts = new WP_Query; $posts = $get_posts->query( $args ); + + // Only suppress and insert when more than just suppression pages available. if ( ! $get_posts->post_count ) { - echo '

' . __( 'No items.' ) . '

'; - return; + if ( ! empty( $suppress_page_ids ) ) { + unset( $args['post__not_in'] ); + $get_posts = new WP_Query; + $posts = $get_posts->query( $args ); + } else { + echo '

' . __( 'No items.' ) . '

'; + return; + } + } elseif ( ! empty( $important_pages ) ) { + $posts = array_merge( $important_pages, $posts ); } $num_pages = $get_posts->max_num_pages; @@ -368,10 +453,10 @@ array( 'base' => add_query_arg( array( - $post_type_name . '-tab' => 'all', - 'paged' => '%#%', - 'item-type' => 'post_type', - 'item-object' => $post_type_name, + $tab_name => 'all', + 'paged' => '%#%', + 'item-type' => 'post_type', + 'item-object' => $post_type_name, ) ), 'format' => '', @@ -394,8 +479,9 @@ $walker = new Walker_Nav_Menu_Checklist( $db_fields ); $current_tab = 'most-recent'; - if ( isset( $_REQUEST[ $post_type_name . '-tab' ] ) && in_array( $_REQUEST[ $post_type_name . '-tab' ], array( 'all', 'search' ) ) ) { - $current_tab = $_REQUEST[ $post_type_name . '-tab' ]; + + if ( isset( $_REQUEST[ $tab_name ] ) && in_array( $_REQUEST[ $tab_name ], array( 'all', 'search' ), true ) ) { + $current_tab = $_REQUEST[ $tab_name ]; } if ( ! empty( $_REQUEST[ 'quick-search-posttype-' . $post_type_name ] ) ) { @@ -411,33 +497,35 @@ '_wpnonce', ); - $most_recent_url = $view_all_url = $search_url = ''; + $most_recent_url = ''; + $view_all_url = ''; + $search_url = ''; if ( $nav_menu_selected_id ) { - $most_recent_url = esc_url( add_query_arg( $post_type_name . '-tab', 'most-recent', remove_query_arg( $removed_args ) ) ); - $view_all_url = esc_url( add_query_arg( $post_type_name . '-tab', 'all', remove_query_arg( $removed_args ) ) ); - $search_url = esc_url( add_query_arg( $post_type_name . '-tab', 'search', remove_query_arg( $removed_args ) ) ); + $most_recent_url = esc_url( add_query_arg( $tab_name, 'most-recent', remove_query_arg( $removed_args ) ) ); + $view_all_url = esc_url( add_query_arg( $tab_name, 'all', remove_query_arg( $removed_args ) ) ); + $search_url = esc_url( add_query_arg( $tab_name, 'search', remove_query_arg( $removed_args ) ) ); } ?>
-
+
-