Skip to:
Content

BuddyPress.org

Changeset 10028


Ignore:
Timestamp:
07/30/2015 07:10:45 PM (6 years ago)
Author:
imath
Message:

Add BuddyPress dynamic menus management to the customizer

Since WordPress 4.3 nav menus can be managed inside the customizer. Using two specific filters, we are now making sure BuddyPress dynamic menus can also be managed inside the customizer.

Props mercime, valendesigns, johnjamesjacoby

See #6509 (branch 2.3)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.3/src/bp-core/bp-core-filters.php

    r9848 r10028  
    849849    }
    850850
     851    // Prevent a notice error when using the customizer
     852    $menu_classes = $menu_item->classes;
     853
     854    if ( is_array( $menu_classes ) ) {
     855        $menu_classes = implode( ' ', $menu_item->classes);
     856    }
     857
    851858    // We use information stored in the CSS class to determine what kind of
    852859    // menu item this is, and how it should be treated
    853     preg_match( '/\sbp-(.*)-nav/', implode( ' ', $menu_item->classes), $matches );
     860    preg_match( '/\sbp-(.*)-nav/', $menu_classes, $matches );
    854861
    855862    // If this isn't a BP menu item, we can stop here
     
    905912        $current = bp_get_requested_url();
    906913        if ( strpos( $current, $menu_item->url ) !== false ) {
    907             $menu_item->classes[] = 'current_page_item';
     914            if ( is_array( $menu_item->classes ) ) {
     915                $menu_item->classes[] = 'current_page_item';
     916                $menu_item->classes[] = 'current-menu-item';
     917            } else {
     918                $menu_item->classes = array( 'current_page_item', 'current-menu-item' );
     919            }
    908920        }
    909921    }
     
    912924}
    913925add_filter( 'wp_setup_nav_menu_item', 'bp_setup_nav_menu_item', 10, 1 );
     926
     927/**
     928 * Populate BuddyPress user nav items for the customizer
     929 *
     930 * @since  BuddyPress (2.3.3)
     931 *
     932 * @param  array   $items  The array of menu items
     933 * @param  string  $type   The requested type
     934 * @param  string  $object The requested object name
     935 * @param  integer $page   The page num being requested
     936 * @return array           The paginated BuddyPress user nav items.
     937 */
     938function bp_customizer_nav_menus_get_items( $items = array(), $type = '', $object = '', $page = 0 ) {
     939    if ( 'bp_loggedin_nav' === $object ) {
     940        $bp_items = bp_nav_menu_get_loggedin_pages();
     941    } elseif ( 'bp_loggedout_nav' === $object ) {
     942        $bp_items = bp_nav_menu_get_loggedout_pages();
     943    } else {
     944        return $items;
     945    }
     946
     947    foreach ( $bp_items as $bp_item ) {
     948        $items[] = array(
     949            'id'         => "bp-{$bp_item->post_excerpt}",
     950            'title'      => html_entity_decode( $bp_item->post_title, ENT_QUOTES, get_bloginfo( 'charset' ) ),
     951            'type'       => $type,
     952            'url'        => esc_url_raw( $bp_item->guid ),
     953            'classes'    => "bp-menu bp-{$bp_item->post_excerpt}-nav",
     954            'type_label' => _x( 'Custom Link', 'customizer menu type label', 'buddypress' ),
     955            'object'     => $object,
     956            'object_id'  => -1,
     957        );
     958    }
     959
     960    return array_slice( $items, 10 * $page, 10 );
     961}
     962add_filter( 'customize_nav_menu_available_items', 'bp_customizer_nav_menus_get_items', 10, 4 );
     963
     964/**
     965 * Set BuddyPress item navs for the customizer
     966 *
     967 * @since  BuddyPress (2.3.3)
     968 *
     969 * @param  array $item_types an associative array structured for the customizer
     970 */
     971function bp_customizer_nav_menus_set_item_types( $item_types = array() ) {
     972    $item_types = array_merge( $item_types, array(
     973        'bp_loggedin_nav' => array(
     974            'title'  => _x( 'BuddyPress (logged-in)', 'customizer menu section title', 'buddypress' ),
     975            'type'   => 'bp_nav',
     976            'object' => 'bp_loggedin_nav',
     977        ),
     978        'bp_loggedout_nav' => array(
     979            'title'  => _x( 'BuddyPress (logged-out)', 'customizer menu section title', 'buddypress' ),
     980            'type'   => 'bp_nav',
     981            'object' => 'bp_loggedout_nav',
     982        ),
     983    ) );
     984
     985    return $item_types;
     986}
     987add_filter( 'customize_nav_menu_available_item_types', 'bp_customizer_nav_menus_set_item_types', 10, 1 );
    914988
    915989/**
Note: See TracChangeset for help on using the changeset viewer.