Skip to:
Content

BuddyPress.org

Changeset 12678


Ignore:
Timestamp:
07/04/2020 11:42:49 AM (6 years ago)
Author:
imath
Message:

Administration: improve how BP Member Nav Menu items are managed

Since WordPress 5.3, the WP nav menu meta boxes had their output modified to improve a11y. The most visible improvement was to use a checkbox to select all box's current panel menu items instead of a link. This change also modified the JavaScript causing the BP Member nav menu meta box to fail at selecting all menu items.

As we support WordPress versions that are older than 5.3, we had to review the way we were extending the Accordion Menu box so that we can enjoy WP 5.3 improvements and keeps on being back compatible with previous versions of WordPress.

As this new way does not need the BP_Walker_Nav_Menu_Checklist class anymore, we are deprecating the class's corresponding file without removing the class from our Classes Autoloader in case some plugin developers are using it directly.

Once we will be sure no plugin developers are using it directly, we will need to delete the class's file and remove the class from our Classes Autoloader.

Props vapvarun

Fixes #8291

Location:
trunk/src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/admin/bp-core-admin-functions.php

    r12668 r12678  
    756756        }
    757757
    758         add_meta_box( 'add-buddypress-nav-menu', __( 'BuddyPress', 'buddypress' ), 'bp_admin_do_wp_nav_menu_meta_box', 'nav-menus', 'side', 'default' );
     758        add_meta_box( 'add-buddypress-nav-menu', __( 'BuddyPress Member', 'buddypress' ), 'bp_admin_do_wp_nav_menu_meta_box', 'nav-menus', 'side', 'default' );
    759759
    760760        add_action( 'admin_print_footer_scripts', 'bp_admin_wp_nav_menu_restrict_items' );
     
    762762
    763763/**
     764 * BP Member nav menu filter to short-circuit WP's query.
     765 *
     766 * @since 7.0.0
     767 *
     768 * @param null     $null     A null value.
     769 * @param WP_Query $wp_query The WP_Query instance (passed by reference).
     770 * @return array   The BP Member nav items to short-circuit WP's query,
     771 */
     772function bp_admin_get_wp_nav_menu_items( $null = null, $wp_query ) {
     773        if ( isset( $wp_query->query['orderby'], $wp_query->query['order'] ) && 'post_date' === $wp_query->query['orderby'] && 'DESC' === $wp_query->query['order'] ) {
     774                return bp_nav_menu_get_loggedin_pages();
     775        } elseif ( isset( $wp_query->query['nopaging'] ) && true === $wp_query->query['nopaging'] ) {
     776                return array_merge( bp_nav_menu_get_loggedin_pages(), bp_nav_menu_get_loggedout_pages() );
     777        }
     778
     779        return bp_nav_menu_get_loggedout_pages();
     780}
     781
     782/**
    764783 * Build and populate the BuddyPress accordion on Appearance > Menus.
    765784 *
    766785 * @since 1.9.0
     786 * @since 7.0.0 Uses wp_nav_menu_item_post_type_meta_box()
    767787 *
    768788 * @global $nav_menu_selected_id
    769789 */
    770 function bp_admin_do_wp_nav_menu_meta_box() {
     790function bp_admin_do_wp_nav_menu_meta_box( $object = '', $box = array() ) {
    771791        global $nav_menu_selected_id;
    772792
    773         $walker = new BP_Walker_Nav_Menu_Checklist( false );
    774         $args   = array( 'walker' => $walker );
    775 
    776         $post_type_name = 'buddypress';
    777 
    778         $tabs = array();
    779 
    780         $tabs['loggedin']['label'] = __( 'Logged-In', 'buddypress' );
    781         $tabs['loggedin']['pages'] = bp_nav_menu_get_loggedin_pages();
    782 
    783         $tabs['loggedout']['label'] = __( 'Logged-Out', 'buddypress' );
    784         $tabs['loggedout']['pages'] = bp_nav_menu_get_loggedout_pages();
    785 
    786         ?>
    787 
    788         <div id="buddypress-menu" class="posttypediv">
    789                 <h4><?php _e( 'Logged-In', 'buddypress' ); ?></h4>
    790                 <p><?php _e( '<em>Logged-In</em> links are relative to the current user, and are not visible to visitors who are not logged in.', 'buddypress' ); ?></p>
    791 
    792                 <div id="tabs-panel-posttype-<?php echo $post_type_name; ?>-loggedin" class="tabs-panel tabs-panel-active">
    793                         <ul id="buddypress-menu-checklist-loggedin" class="categorychecklist form-no-clear">
    794                                 <?php echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $tabs['loggedin']['pages'] ), 0, (object) $args ); ?>
     793        $box['args'] = (object) array(
     794                'name'           => 'bp_nav_menu_item',
     795                '_default_query' => array(),
     796        );
     797
     798        // Temporarly register a post type.
     799        register_post_type(
     800                'bp_nav_menu_item',
     801                array(
     802                        'label'  => 'BuddyPress',
     803                        'labels' => array(
     804                                'search_items' => __( 'Search BuddyPress member menu items', 'buddypress' ),
     805                                'all_items'    => __( 'All BuddyPress Member menu items', 'buddypress' ),
     806                        ),
     807                        'public' => true,
     808                        'hierarchical' => false,
     809                        'has_archive'  => false,
     810                        'rewrite'      => false,
     811                )
     812        );
     813
     814        // Temporarly override the posts query results.
     815        add_filter( 'posts_pre_query', 'bp_admin_get_wp_nav_menu_items', 10, 2 );
     816
     817        ob_start();
     818        wp_nav_menu_item_post_type_meta_box( 'buddypress', $box );
     819        $output = ob_get_clean();
     820
     821        $get_bp_items = new WP_Query;
     822        $all_bp_items = $get_bp_items->query( array( 'nopaging' => true ) );
     823        $walker       = new Walker_Nav_Menu_Checklist();
     824        $all_bp_tabs  = sprintf(
     825                '<div id="bp_nav_menu_item-all" class="tabs-panel tabs-panel-view-all tabs-panel-inactive" role="region" aria-label="%1$s" tabindex="0">
     826                        <ul id="bp_nav_menu_itemchecklist" data-wp-lists="list:bp_nav_menu_item" class="categorychecklist form-no-clear">
     827                                %2$s
    795828                        </ul>
    796                 </div>
    797 
    798                 <h4><?php _e( 'Logged-Out', 'buddypress' ); ?></h4>
    799                 <p><?php _e( '<em>Logged-Out</em> links are not visible to users who are logged in.', 'buddypress' ); ?></p>
    800 
    801                 <div id="tabs-panel-posttype-<?php echo $post_type_name; ?>-loggedout" class="tabs-panel tabs-panel-active">
    802                         <ul id="buddypress-menu-checklist-loggedout" class="categorychecklist form-no-clear">
    803                                 <?php echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $tabs['loggedout']['pages'] ), 0, (object) $args ); ?>
    804                         </ul>
    805                 </div>
    806 
    807                 <?php
    808                 $removed_args = array(
    809                         'action',
    810                         'customlink-tab',
    811                         'edit-menu-item',
    812                         'menu-item',
    813                         'page-tab',
    814                         '_wpnonce',
     829                </div>',
     830                esc_html__( 'All BuddyPress Member menu items', 'buddypress' ),
     831                walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $all_bp_items ), 0, (object) array( 'walker' => $walker ) )
     832        );
     833
     834        // Remove temporary post type and filter.
     835        unregister_post_type( 'bp_nav_menu_item' );
     836        remove_filter( 'posts_pre_query', 'bp_admin_get_wp_nav_menu_items', 10, 2 );
     837
     838        $tab_name    = 'bp_nav_menu_item-tab';
     839        $current_tab = 'logged-in';
     840        $tabs        = array(
     841                'logged-in'  => __( 'Logged-In', 'buddypress' ),
     842                'logged-out' => __( 'Logged-Out', 'buddypress' ),
     843                'all'        => __( 'All', 'buddypress' ),
     844        );
     845        $tab_urls    = array(
     846                'all'        => '',
     847                'logged-in'  => '',
     848                'logged-out' => '',
     849        );
     850
     851        if ( isset( $_REQUEST[ $tab_name ] ) && in_array( $_REQUEST[ $tab_name ], array_keys( $tabs ), true ) ) {
     852                $current_tab = $_REQUEST[ $tab_name ];
     853        }
     854
     855        $removed_args = array(
     856                'action',
     857                'customlink-tab',
     858                'edit-menu-item',
     859                'menu-item',
     860                'page-tab',
     861                '_wpnonce',
     862        );
     863
     864        if ( $nav_menu_selected_id ) {
     865                $tab_urls['all']        = esc_url( add_query_arg( $tab_name, 'all', remove_query_arg( $removed_args ) ) );
     866                $tab_urls['logged-in']  = esc_url( add_query_arg( $tab_name, 'logged-in', remove_query_arg( $removed_args ) ) );
     867                $tab_urls['logged-out'] = esc_url( add_query_arg( $tab_name, 'logged-out', remove_query_arg( $removed_args ) ) );
     868        }
     869
     870        $bp_tabs_nav = '';
     871        foreach ( $tabs as $tab => $tab_text ) {
     872                $class    = '';
     873                $datatype = 'bp_nav_menu_item-' . $tab;
     874
     875                if ( $current_tab === $tab ) {
     876                        $class = ' class="tabs"';
     877                }
     878
     879                if ( 'all' !== $tab ) {
     880                        $datatype = 'tabs-panel-posttype-bp_nav_menu_item-' . $tab;
     881                }
     882
     883                $bp_tabs_nav .= sprintf(
     884                        '<li%1$s>
     885                                <a class="nav-tab-link" data-type="%2$s" href="%3$s">
     886                                        %4$s
     887                                </a>
     888                        </li>',
     889                        $class,
     890                        $datatype,
     891                        esc_url( $tab_urls[ $tab ] ) . '#' . $datatype,
     892                        esc_html( $tab_text )
    815893                );
    816                 ?>
    817 
    818                 <p class="button-controls">
    819                         <span class="list-controls">
    820                                 <a href="
    821                                 <?php
    822                                 echo esc_url(
    823                                         add_query_arg(
    824                                                 array(
    825                                                         $post_type_name . '-tab' => 'all',
    826                                                         'selectall'              => 1,
    827                                                 ),
    828                                                 remove_query_arg( $removed_args )
    829                                         )
    830                                 );
    831                                 ?>
    832                                 #buddypress-menu" class="select-all"><?php _e( 'Select All', 'buddypress' ); ?></a>
    833                         </span>
    834                         <span class="add-to-menu">
    835                                 <input type="submit"
    836                                 <?php
    837                                 if ( function_exists( 'wp_nav_menu_disabled_check' ) ) :
    838                                         wp_nav_menu_disabled_check( $nav_menu_selected_id );
    839 endif;
    840                                 ?>
    841                                  class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e( 'Add to Menu', 'buddypress' ); ?>" name="add-custom-menu-item" id="submit-buddypress-menu" />
    842                                 <span class="spinner"></span>
    843                         </span>
    844                 </p>
    845         </div><!-- /#buddypress-menu -->
    846 
    847         <?php
     894        }
     895
     896        $output = str_replace(
     897                array(
     898                        'tabs-panel-posttype-bp_nav_menu_item-most-recent',
     899                        'bp_nav_menu_itemchecklist-most-recent',
     900                        'bp_nav_menu_item-all',
     901                        'bp_nav_menu_itemchecklist',
     902                ),
     903                array(
     904                        'tabs-panel-posttype-bp_nav_menu_item-logged-in',
     905                        'bp_nav_menu_itemchecklist-logged-in',
     906                        'tabs-panel-posttype-bp_nav_menu_item-logged-out',
     907                        'bp_nav_menu_itemchecklist-logged-out',
     908                ),
     909                $output
     910        );
     911
     912        preg_match( '/\<ul\sid=\"posttype-bp_nav_menu_item-tabs\"[^>]*>(.*?)\<\/ul\>\<!-- \.posttype-tabs --\>/s', $output, $tabs_nav );
     913
     914        if ( isset( $tabs_nav[1] ) ) {
     915                $output = str_replace( $tabs_nav[1], $bp_tabs_nav, $output );
     916        }
     917
     918        echo preg_replace( '/\<div\sclass=\".*\"\sid=\"tabs-panel-posttype-bp_nav_menu_item-search\"[^>]*>(.*?)\<\/div\>/s', $all_bp_tabs, $output );
    848919}
    849920
  • trunk/src/bp-core/bp-core-filters.php

    r12588 r12678  
    692692function bp_setup_nav_menu_item( $menu_item ) {
    693693        if ( is_admin() ) {
     694                if ( 'bp_nav_menu_item' === $menu_item->object ) {
     695                        $menu_item->type = 'custom';
     696                        $menu_item->url  = $menu_item->guid;
     697
     698                        if ( ! in_array( array( 'bp-menu', 'bp-'. $menu_item->post_excerpt .'-nav' ), $menu_item->classes ) ) {
     699                                $menu_item->classes[] = 'bp-menu';
     700                                $menu_item->classes[] = 'bp-'. $menu_item->post_excerpt .'-nav';
     701                        }
     702                }
     703
    694704                return $menu_item;
    695705        }
  • trunk/src/bp-core/bp-core-functions.php

    r12603 r12678  
    25812581                        'post_date'      => 0,
    25822582                        'post_excerpt'   => $bp_item['slug'],
    2583                         'post_type'      => 'page',
     2583                        'post_type'      => 'bp_nav_menu_item',
    25842584                        'post_status'    => 'publish',
    25852585                        'comment_status' => 'closed',
     
    26562656                        'post_date'      => 0,
    26572657                        'post_excerpt'   => $bp_item['slug'],
    2658                         'post_type'      => 'page',
     2658                        'post_type'      => 'bp_nav_menu_item',
    26592659                        'post_status'    => 'publish',
    26602660                        'comment_status' => 'closed',
  • trunk/src/bp-core/classes/class-bp-walker-nav-menu-checklist.php

    r10417 r12678  
    66 * @subpackage Core
    77 * @since 1.9.0
     8 * @deprecated 7.0.0 This file is no longer used as of the improved way to generate BuddyPress Member's nav menu items in #8291.
     9 * @todo Remove this file as it is no longer used.
    810 */
    911
    1012// Exit if accessed directly.
    1113defined( 'ABSPATH' ) || exit;
     14
     15_deprecated_file( basename( __FILE__ ), '7.0.0' );
    1216
    1317/**
     
    1822 *
    1923 * @since 1.9.0
     24 * @deprecated 7.0.0 This class is no longer used as of the improved way to generate BuddyPress Member's nav menu items in #8291.
    2025 */
    2126class BP_Walker_Nav_Menu_Checklist extends Walker_Nav_Menu {
     
    2429         * Constructor.
    2530         *
     31         * @deprecated 7.0.0
     32         *
    2633         * @see Walker_Nav_Menu::__construct() for a description of parameters.
    2734         *
     
    2936         */
    3037        public function __construct( $fields = false ) {
     38                _deprecated_function( __METHOD__, '7.0.0' );
    3139                if ( $fields ) {
    3240                        $this->db_fields = $fields;
     
    3745         * Create the markup to start a tree level.
    3846         *
     47         * @deprecated 7.0.0
     48         *
    3949         * @see Walker_Nav_Menu::start_lvl() for description of parameters.
    4050         *
     
    4454         */
    4555        public function start_lvl( &$output, $depth = 0, $args = array() ) {
     56                _deprecated_function( __METHOD__, '7.0.0' );
    4657                $indent = str_repeat( "\t", $depth );
    4758                $output .= "\n$indent<ul class='children'>\n";
     
    5061        /**
    5162         * Create the markup to end a tree level.
     63         *
     64         * @deprecated 7.0.0
    5265         *
    5366         * @see Walker_Nav_Menu::end_lvl() for description of parameters.
     
    5871         */
    5972        public function end_lvl( &$output, $depth = 0, $args = array() ) {
     73                _deprecated_function( __METHOD__, '7.0.0' );
    6074                $indent = str_repeat( "\t", $depth );
    6175                $output .= "\n$indent</ul>";
     
    6478        /**
    6579         * Create the markup to start an element.
     80         *
     81         * @deprecated 7.0.0
    6682         *
    6783         * @see Walker::start_el() for description of parameters.
     
    7591         */
    7692        function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
     93                _deprecated_function( __METHOD__, '7.0.0' );
    7794                global $_nav_menu_placeholder;
    7895
  • trunk/src/class-buddypress.php

    r12656 r12678  
    578578                        'BP_User_Query'                => 'core',
    579579                        'BP_Walker_Category_Checklist' => 'core',
     580                        /**
     581                         * BP_Walker_Nav_Menu_Checklist class.
     582                         *
     583                         * As this class corresponding file is deprecated, it will trigger a deprecation notice if instantiated.
     584                         * In a subsequent release, we'll remove it from our Classes Autoloader.
     585                         *
     586                         * @todo Remove the BP_Walker_Nav_Menu_Checklist from our Classes Autoloader.
     587                         */
    580588                        'BP_Walker_Nav_Menu_Checklist' => 'core',
    581589                        'BP_Walker_Nav_Menu'           => 'core',
Note: See TracChangeset for help on using the changeset viewer.