Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
07/30/2015 07:17:13 PM (9 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

Fixes #6509 (trunk)

File:
1 edited

Legend:

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

    r10012 r10029  
    860860    }
    861861
     862    // Prevent a notice error when using the customizer
     863    $menu_classes = $menu_item->classes;
     864
     865    if ( is_array( $menu_classes ) ) {
     866        $menu_classes = implode( ' ', $menu_item->classes);
     867    }
     868
    862869    // We use information stored in the CSS class to determine what kind of
    863870    // menu item this is, and how it should be treated
    864     preg_match( '/\sbp-(.*)-nav/', implode( ' ', $menu_item->classes), $matches );
     871    preg_match( '/\sbp-(.*)-nav/', $menu_classes, $matches );
    865872
    866873    // If this isn't a BP menu item, we can stop here
     
    916923        $current = bp_get_requested_url();
    917924        if ( strpos( $current, $menu_item->url ) !== false ) {
    918             $menu_item->classes[] = 'current_page_item';
     925            if ( is_array( $menu_item->classes ) ) {
     926                $menu_item->classes[] = 'current_page_item';
     927                $menu_item->classes[] = 'current-menu-item';
     928            } else {
     929                $menu_item->classes = array( 'current_page_item', 'current-menu-item' );
     930            }
    919931        }
    920932    }
     
    923935}
    924936add_filter( 'wp_setup_nav_menu_item', 'bp_setup_nav_menu_item', 10, 1 );
     937
     938/**
     939 * Populate BuddyPress user nav items for the customizer
     940 *
     941 * @since  BuddyPress (2.3.3)
     942 *
     943 * @param  array   $items  The array of menu items
     944 * @param  string  $type   The requested type
     945 * @param  string  $object The requested object name
     946 * @param  integer $page   The page num being requested
     947 * @return array           The paginated BuddyPress user nav items.
     948 */
     949function bp_customizer_nav_menus_get_items( $items = array(), $type = '', $object = '', $page = 0 ) {
     950    if ( 'bp_loggedin_nav' === $object ) {
     951        $bp_items = bp_nav_menu_get_loggedin_pages();
     952    } elseif ( 'bp_loggedout_nav' === $object ) {
     953        $bp_items = bp_nav_menu_get_loggedout_pages();
     954    } else {
     955        return $items;
     956    }
     957
     958    foreach ( $bp_items as $bp_item ) {
     959        $items[] = array(
     960            'id'         => "bp-{$bp_item->post_excerpt}",
     961            'title'      => html_entity_decode( $bp_item->post_title, ENT_QUOTES, get_bloginfo( 'charset' ) ),
     962            'type'       => $type,
     963            'url'        => esc_url_raw( $bp_item->guid ),
     964            'classes'    => "bp-menu bp-{$bp_item->post_excerpt}-nav",
     965            'type_label' => _x( 'Custom Link', 'customizer menu type label', 'buddypress' ),
     966            'object'     => $object,
     967            'object_id'  => -1,
     968        );
     969    }
     970
     971    return array_slice( $items, 10 * $page, 10 );
     972}
     973add_filter( 'customize_nav_menu_available_items', 'bp_customizer_nav_menus_get_items', 10, 4 );
     974
     975/**
     976 * Set BuddyPress item navs for the customizer
     977 *
     978 * @since  BuddyPress (2.3.3)
     979 *
     980 * @param  array $item_types an associative array structured for the customizer
     981 */
     982function bp_customizer_nav_menus_set_item_types( $item_types = array() ) {
     983    $item_types = array_merge( $item_types, array(
     984        'bp_loggedin_nav' => array(
     985            'title'  => _x( 'BuddyPress (logged-in)', 'customizer menu section title', 'buddypress' ),
     986            'type'   => 'bp_nav',
     987            'object' => 'bp_loggedin_nav',
     988        ),
     989        'bp_loggedout_nav' => array(
     990            'title'  => _x( 'BuddyPress (logged-out)', 'customizer menu section title', 'buddypress' ),
     991            'type'   => 'bp_nav',
     992            'object' => 'bp_loggedout_nav',
     993        ),
     994    ) );
     995
     996    return $item_types;
     997}
     998add_filter( 'customize_nav_menu_available_item_types', 'bp_customizer_nav_menus_set_item_types', 10, 1 );
    925999
    9261000/**
Note: See TracChangeset for help on using the changeset viewer.