Skip to:
Content

BuddyPress.org

Ticket #5122: 5122.04.patch

File 5122.04.patch, 14.3 KB (added by boonebgorges, 13 years ago)
  • bp-core/admin/bp-core-functions.php

    diff --git bp-core/admin/bp-core-functions.php bp-core/admin/bp-core-functions.php
    index 7e145fa..a148d60 100644
    function bp_admin_list_table_current_bulk_action() { 
    642642
    643643        return $action;
    644644}
     645
     646/** Menus *********************************************************************/
     647
     648/**
     649 * Registers a meta box for BuddyPress WP Nav Menu and a js at the bottom of
     650 * the page
     651 *
     652 * @since BuddyPress (1.9)
     653 *
     654 * @global integer the current blog id
     655 * @uses bp_is_root_blog() to check whether this is the root blog
     656 * @uses add_meta_box() to register the BuddyPress WP Nav accordeon
     657 * @uses add_action() to hook to admin footer scripts and load a piece of
     658 *   javascript
     659 */
     660function bp_admin_wp_nav_menu_meta_box() {
     661        if ( ! bp_is_root_blog() ) {
     662                return;
     663        }
     664
     665        add_meta_box( 'add-buddypress-nav-menu', __( 'BuddyPress', 'buddypress' ), 'bp_admin_do_wp_nav_menu_meta_box', 'nav-menus', 'side', 'default' );
     666
     667        add_action( 'admin_print_footer_scripts', 'bp_admin_wp_nav_menu_restrict_class_attr' );
     668}
     669
     670/**
     671 * Builds and populates the BuddyPress accordion on Appearance > Menus
     672 *
     673 * @since BuddyPress (1.9)
     674 *
     675 * @global $nav_menu_selected_id
     676 * @uses BP_Walker_Nav_Menu_Checklist to use our adapted walker
     677 * @uses bp_get_nav_item_pages() to get the menu items
     678 */
     679function bp_admin_do_wp_nav_menu_meta_box() {
     680        global $nav_menu_selected_id;
     681
     682        $walker = new BP_Walker_Nav_Menu_Checklist( false );
     683        $args = array( 'walker' => $walker );
     684        $menu_pages = bp_get_nav_item_pages();
     685
     686        ?>
     687        <div id="buddypress-menu">
     688                <div class="tabs-panel tabs-panel-active">
     689                        <ul id="buddypress-menu-checklist" class="categorychecklist form-no-clear">
     690                                <?php echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $menu_pages ), 0, (object) $args );?>
     691                        </ul>
     692                </div>
     693                <span class="add-to-menu">
     694                        <input type="submit"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e( 'Add to Menu' ); ?>" name="add-custom-menu-item" id="submit-buddypress-menu" />
     695                        <span class="spinner"></span>
     696                </span>
     697        </div><!-- /#buddypress-menu -->
     698
     699        <?php
     700}
     701
     702/**
     703 * Don't let the BP menu item CSS classes be edited
     704 *
     705 * We use the bp-slug-nav class on output, so we want to avoid having it
     706 * removed by the admin.
     707 *
     708 * @since BuddyPress (1.9)
     709 */
     710function bp_admin_wp_nav_menu_restrict_class_attr() {
     711        ?>
     712        <script type="text/javascript">
     713        jQuery( '#menu-to-edit').on( 'click', 'a.item-edit', function() {
     714                jQuery('.edit-menu-item-classes').each( function() {
     715                        if( jQuery(this).val().indexOf( 'bp-menu') !=1 )
     716                                jQuery(this).attr( 'readonly', 'readonly' );
     717                });
     718        });
     719        </script>
     720        <?php
     721}
  • bp-core/bp-core-admin.php

    diff --git bp-core/bp-core-admin.php bp-core/bp-core-admin.php
    index 7fb67f5..6bcb1f0 100644
    class BP_Admin { 
    124124
    125125                /** BuddyPress Actions ************************************************/
    126126
     127                // Load the BuddyPress metabox in the WP Nav Menu Admin UI
     128                add_action( 'load-nav-menus.php', 'bp_admin_wp_nav_menu_meta_box' );
     129
    127130                // Add settings
    128131                add_action( 'bp_register_admin_settings', array( $this, 'register_admin_settings' ) );
    129132
  • bp-core/bp-core-classes.php

    diff --git bp-core/bp-core-classes.php bp-core/bp-core-classes.php
    index 6d2f35a..b3f91cd 100644
    class BP_Walker_Nav_Menu extends Walker_Nav_Menu { 
    20102010                $output .= apply_filters( 'bp_walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    20112011        }
    20122012}
     2013
     2014/**
     2015 * Create a set of BuddyPress-specific links for use in the Menus admin UI
     2016 *
     2017 * Borrowed heavily from WP's Walker_Nav_Menu_Checklist, but modified so as not
     2018 * to require an actual post type or taxonomy, and to force certain CSS classes
     2019 *
     2020 * @since BuddyPress (1.9)
     2021 */
     2022class BP_Walker_Nav_Menu_Checklist extends Walker_Nav_Menu {
     2023        public function __construct( $fields = false ) {
     2024                if ( $fields ) {
     2025                        $this->db_fields = $fields;
     2026                }
     2027        }
     2028
     2029        public function start_lvl( &$output, $depth = 0, $args = array() ) {
     2030                $indent = str_repeat( "\t", $depth );
     2031                $output .= "\n$indent<ul class='children'>\n";
     2032        }
     2033
     2034        public function end_lvl( &$output, $depth = 0, $args = array() ) {
     2035                $indent = str_repeat( "\t", $depth );
     2036                $output .= "\n$indent</ul>";
     2037        }
     2038
     2039        /**
     2040         * @see Walker::start_el()
     2041         *
     2042         * @param string $output Passed by reference. Used to append additional content.
     2043         * @param object $item Menu item data object.
     2044         * @param int $depth Depth of menu item. Used for padding.
     2045         * @param object $args
     2046         */
     2047        function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
     2048                global $_nav_menu_placeholder;
     2049
     2050                $_nav_menu_placeholder = ( 0 > $_nav_menu_placeholder ) ? intval($_nav_menu_placeholder) - 1 : -1;
     2051                $possible_object_id = isset( $item->post_type ) && 'nav_menu_item' == $item->post_type ? $item->object_id : $_nav_menu_placeholder;
     2052                $possible_db_id = ( ! empty( $item->ID ) ) && ( 0 < $possible_object_id ) ? (int) $item->ID : 0;
     2053
     2054                $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
     2055
     2056                $output .= $indent . '<li>';
     2057                $output .= '<label class="menu-item-title">';
     2058                $output .= '<input type="checkbox" class="menu-item-checkbox';
     2059
     2060                if ( property_exists( $item, 'label' ) ) {
     2061                        $title = $item->label;
     2062                }
     2063
     2064                $output .= '" name="menu-item[' . $possible_object_id . '][menu-item-object-id]" value="'. esc_attr( $item->object_id ) .'" /> ';
     2065                $output .= isset( $title ) ? esc_html( $title ) : esc_html( $item->title );
     2066                $output .= '</label>';
     2067
     2068                if ( empty( $item->url ) ) {
     2069                        $item->url = $item->guid;
     2070                }
     2071
     2072                if ( ! in_array( array( 'bp-menu', 'bp-'. $item->post_excerpt .'-nav' ), $item->classes ) ) {
     2073                        $item->classes[] = 'bp-menu';
     2074                        $item->classes[] = 'bp-'. $item->post_excerpt .'-nav';
     2075                }
     2076
     2077                // Menu item hidden fields
     2078                $output .= '<input type="hidden" class="menu-item-db-id" name="menu-item[' . $possible_object_id . '][menu-item-db-id]" value="' . $possible_db_id . '" />';
     2079                $output .= '<input type="hidden" class="menu-item-object" name="menu-item[' . $possible_object_id . '][menu-item-object]" value="'. esc_attr( $item->object ) .'" />';
     2080                $output .= '<input type="hidden" class="menu-item-parent-id" name="menu-item[' . $possible_object_id . '][menu-item-parent-id]" value="'. esc_attr( $item->menu_item_parent ) .'" />';
     2081                $output .= '<input type="hidden" class="menu-item-type" name="menu-item[' . $possible_object_id . '][menu-item-type]" value="custom" />';
     2082                $output .= '<input type="hidden" class="menu-item-title" name="menu-item[' . $possible_object_id . '][menu-item-title]" value="'. esc_attr( $item->title ) .'" />';
     2083                $output .= '<input type="hidden" class="menu-item-url" name="menu-item[' . $possible_object_id . '][menu-item-url]" value="'. esc_attr( $item->url ) .'" />';
     2084                $output .= '<input type="hidden" class="menu-item-target" name="menu-item[' . $possible_object_id . '][menu-item-target]" value="'. esc_attr( $item->target ) .'" />';
     2085                $output .= '<input type="hidden" class="menu-item-attr_title" name="menu-item[' . $possible_object_id . '][menu-item-attr_title]" value="'. esc_attr( $item->attr_title ) .'" />';
     2086                $output .= '<input type="hidden" class="menu-item-classes" name="menu-item[' . $possible_object_id . '][menu-item-classes]" value="'. esc_attr( implode( ' ', $item->classes ) ) .'" />';
     2087                $output .= '<input type="hidden" class="menu-item-xfn" name="menu-item[' . $possible_object_id . '][menu-item-xfn]" value="'. esc_attr( $item->xfn ) .'" />';
     2088        }
     2089}
  • bp-core/bp-core-filters.php

    diff --git bp-core/bp-core-filters.php bp-core/bp-core-filters.php
    index f8f05d0..75eb028 100644
    add_filter( 'wp_title', 'bp_modify_page_title', 10, 3 ); 
    386386add_filter( 'bp_modify_page_title', 'wptexturize'     );
    387387add_filter( 'bp_modify_page_title', 'convert_chars'   );
    388388add_filter( 'bp_modify_page_title', 'esc_html'        );
     389
     390/**
     391 * Add BuddyPress-specific items to the wp_nav_menu
     392 *
     393 * If user is not logged in regiser menu and login menu item must show
     394 * If user is logged in register menu must be unset and logout must replace login
     395 *
     396 * @param  WP_Post $menu_item the menu item
     397 * @uses is_admin() to check we're in backend
     398 * @uses is_user_logged_in() to check if we have a user logged in
     399 * @uses wp_logout_url() to build the log out url
     400 * @uses bp_get_root_domain() to be redirected to root domain url once logged out
     401 * @uses wp_login_url() to build log in url
     402 * @uses wp_guess_url() to be redirected to be redirected at the best place once logged in
     403 * @uses bp_get_nav_item_pages() to get the url for the user's nav
     404 * @uses bp_get_requested_url() to get the current url
     405 * @return obj The modified WP_Post object
     406 */
     407function bp_setup_nav_menu_item( $menu_item ) {
     408        if ( is_admin() ) {
     409                return $menu_item;
     410        }
     411
     412        // We use information stored in the CSS class to determine what kind of
     413        // menu item this is, and how it should be treated
     414        $css_target = preg_match( '/\sbp-(.*)-nav/', implode( ' ', $menu_item->classes), $matches );
     415
     416        // If this isn't a BP menu item, we can stop here
     417        if ( empty( $matches[1] ) ) {
     418                return $menu_item;
     419        }
     420
     421        switch ( $matches[1] ) {
     422                // The 'login' link appears as 'Log Out' to logged-in users,
     423                // 'Log In' to those who are not logged-in
     424                case 'login' :
     425                        if ( is_user_logged_in() ) {
     426                                $menu_item->url = wp_logout_url( bp_get_root_domain() );
     427                                $menu_item->title = __( 'Log Out', 'buddypress' );
     428                        } else {
     429                                $menu_item->url = wp_login_url( wp_guess_url() );
     430                                $menu_item->title = __( 'Log In', 'buddypress' );
     431                        }
     432
     433                        break;
     434
     435                // Don't show the Register link to logged-in users
     436                case 'register' :
     437                        if ( is_user_logged_in() ) {
     438                                $menu_item->_invalid = true;
     439                        }
     440
     441                        break;
     442
     443                // All other BP nav items are specific to the logged-in user,
     444                // and so are not relevant to logged-out users
     445                default:
     446                        if ( is_user_logged_in() ) {
     447                                $menu_item->url = bp_get_nav_item_url( $matches[1] );
     448                        } else {
     449                                $menu_item->_invalid = true;
     450                        }
     451
     452                        break;
     453        }
     454
     455        // Highlight the current page
     456        $current = bp_get_requested_url();
     457        if ( strpos( $current, $menu_item->url ) !== false ) {
     458                $menu_item->classes[] = 'current_page_item';
     459        }
     460
     461        return $menu_item;
     462}
     463
     464add_filter( 'wp_setup_nav_menu_item', 'bp_setup_nav_menu_item', 10, 1 );
  • bp-core/bp-core-functions.php

    diff --git bp-core/bp-core-functions.php bp-core/bp-core-functions.php
    index 80d7889..04980ce 100644
    function bp_core_print_generation_time() { 
    14111411        <?php
    14121412}
    14131413add_action( 'wp_footer', 'bp_core_print_generation_time' );
     1414
     1415/** Nav Menu ******************************************************************/
     1416
     1417/**
     1418 * Create fake "post" objects for use in a WordPress nav menu
     1419 *
     1420 * WordPress nav menus work by representing post or tax term data as a custom
     1421 * post type, which is then used to populate the checkboxes that appear on
     1422 * Dashboard > Appearance > Menu as well as the menu as rendered on the front
     1423 * end. Most of the items in the BuddyPress set of nav items are neither posts
     1424 * nor tax terms, so we fake a post-like object so as to be compatible with the
     1425 * menu.
     1426 *
     1427 * This technique also allows us to generate links dynamically, so that, for
     1428 * example, "My Profile" will always point to the URL of the profile of the
     1429 * logged-in user.
     1430 *
     1431 * @since BuddyPress (1.9)
     1432 *
     1433 * @uses bp_core_get_directory_page_ids() to get all the directory pages, and
     1434 *   check for the proper location of the Register page
     1435 * @uses get_the_title() to get the title for the registration page
     1436 * @uses get_permalink() to get the link to the registration page
     1437 * @uses buddypress() to get the bp_nav items
     1438 * @return mixed A URL or an array of dummy pages
     1439 */
     1440function bp_get_nav_item_pages() {
     1441
     1442        // Try to catch the cached version first
     1443        if ( ! empty( buddypress()->wp_nav_menu_items ) ) {
     1444                return buddypress()->wp_nav_menu_items;
     1445        }
     1446
     1447        // Pull up a list of items registered in BP's top-level nav array
     1448        $bp_menu_items = buddypress()->bp_nav;
     1449
     1450        // Some BP nav menu items will not be represented in bp_nav, because
     1451        // they are not real BP components. We add them manually here.
     1452        $bp_menu_items[] = array(
     1453                'name' => __( 'Log in/Log out', 'buddypress' ),
     1454                'slug' => 'login',
     1455                'link' => wp_login_url()
     1456        );
     1457
     1458        // The Register page will not always be available (ie, when
     1459        // registration is disabled)
     1460        $bp_directory_page_ids = bp_core_get_directory_page_ids();
     1461
     1462        if( ! empty( $bp_directory_page_ids['register'] ) ) {
     1463                $register_page = get_post( $bp_directory_page_ids['register'] );
     1464                $bp_menu_items[] = array(
     1465                        'name' => $register_page->post_title,
     1466                        'slug' => $register_page->post_name,
     1467                        'link' => get_permalink( $register_page->ID ),
     1468                );
     1469        }
     1470
     1471        // If there's nothing to show, we're done
     1472        if ( count( $bp_menu_items ) < 1 ) {
     1473                return false;
     1474        }
     1475
     1476        $page_args = array();
     1477
     1478        foreach ( $bp_menu_items as $bp_item ) {
     1479                $item_name = '';
     1480
     1481                // Remove <span>number</span>
     1482                $item_name = preg_replace( '/([.0-9]+)/', '', $bp_item['name'] );
     1483                $item_name = trim( strip_tags( $item_name ) );
     1484
     1485                $page_args[ $bp_item['slug'] ] = (object) array(
     1486                        'ID'             => -1,
     1487                        'post_title'     => $item_name,
     1488                        'post_author'    => 0,
     1489                        'post_date'      => 0,
     1490                        'post_excerpt'   => $bp_item['slug'],
     1491                        'post_type'      => 'page',
     1492                        'post_status'    => 'publish',
     1493                        'comment_status' => 'closed',
     1494                        'guid'           => $bp_item['link']
     1495                );
     1496        }
     1497
     1498        buddypress()->wp_nav_menu_items = $page_args;
     1499
     1500        return buddypress()->wp_nav_menu_items;
     1501}
     1502
     1503/**
     1504 * Get the URL for a BuddyPress WP nav menu item, based on slug
     1505 *
     1506 * BuddyPress-specific WP nav menu items have dynamically generated URLs,
     1507 * based on the identity of the current user. This function lets you fetch the
     1508 * proper URL for a given nav item slug (such as 'login' or 'messages').
     1509 *
     1510 * @since BuddyPress (1.9)
     1511 *
     1512 * @param string $slug The slug of the nav item: login, register, or one of the
     1513 *   slugs from buddypress()->bp_nav
     1514 * @return string $nav_item_url The URL generated for the current user
     1515 */
     1516function bp_get_nav_item_url( $slug ) {
     1517        $nav_item_url = '';
     1518        $nav_menu_items = bp_get_nav_item_pages();
     1519
     1520        if ( isset( $nav_menu_items[ $slug ] ) ) {
     1521                $nav_item_url = $nav_menu_items[ $slug ]->guid;
     1522        }
     1523
     1524        return $nav_item_url;
     1525}