Skip to:
Content

BuddyPress.org

Ticket #5122: 5122.06.patch

File 5122.06.patch, 18.4 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..0c2f14d 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_items' );
     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_nav_menu_get_loggedin_pages() to get the logged-in menu items
     678 * @uses bp_nav_menu_get_loggedout_pages() to get the logged-out menu items
     679 */
     680function bp_admin_do_wp_nav_menu_meta_box() {
     681        global $nav_menu_selected_id;
     682
     683        $walker = new BP_Walker_Nav_Menu_Checklist( false );
     684        $args   = array( 'walker' => $walker );
     685
     686        $post_type_name = 'buddypress';
     687        $current_tab    = 'loggedin';
     688
     689        $tabs = array();
     690
     691        $tabs['loggedin']['label']  = __( 'Logged-In', 'buddypress' );
     692        $tabs['loggedin']['pages']  = bp_nav_menu_get_loggedin_pages();
     693
     694        $tabs['loggedout']['label'] = __( 'Logged-Out', 'buddypress' );
     695        $tabs['loggedout']['pages'] = bp_nav_menu_get_loggedout_pages();
     696
     697        ?>
     698
     699        <div id="buddypress-menu" class="posttypediv">
     700                <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>
     701                <p><?php _e( '<em>Logged-Out</em> links are not visible to users who are logged in.', 'buddypress' ) ?></p>
     702
     703                <ul id="posttype-<?php echo $post_type_name; ?>-tabs" class="posttype-tabs add-menu-item-tabs">
     704                        <?php foreach ( $tabs as $tab => $item ) : ?>
     705                                <li <?php echo ( $tab == $current_tab ? ' class="tabs"' : '' ); ?>>
     706                                        <a class="nav-tab-link" data-type="tabs-panel-posttype-<?php echo esc_attr( $post_type_name ); ?>-<?php echo $tab; ?>" href="<?php if ( $nav_menu_selected_id ) echo esc_url(add_query_arg($post_type_name . '-tab', $tab, remove_query_arg($removed_args))); ?>#tabs-panel-posttype-<?php echo $post_type_name; ?>-<?php echo $tab; ?>">
     707                                                <?php echo $item['label']; ?>
     708                                        </a>
     709                                </li>
     710
     711                        <?php endforeach; ?>
     712                </ul><!-- .posttype-tabs -->
     713
     714                <?php foreach ( $tabs as $tab => $item ) : ?>
     715
     716                        <div id="tabs-panel-posttype-<?php echo $post_type_name; ?>-<?php echo $tab; ?>" class="tabs-panel <?php
     717                                echo ( $tab == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' );
     718                        ?>">
     719                                <ul id="buddypress-menu-checklist-<?php echo $tab; ?>" class="categorychecklist form-no-clear">
     720                                        <?php echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $item['pages'] ), 0, (object) $args );?>
     721                                </ul>
     722                        </div>
     723
     724                <?php endforeach; ?>
     725
     726                <p class="button-controls">
     727                        <span class="add-to-menu">
     728                                <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" />
     729                                <span class="spinner"></span>
     730                        </span>
     731                </p>
     732        </div><!-- /#buddypress-menu -->
     733
     734        <?php
     735}
     736
     737/**
     738 * Restrict various items from view if editing a BuddyPress menu.
     739 *
     740 * If a person is editing a BP menu item, that person should not be able to
     741 * see or edit the following fields:
     742 *
     743 * - CSS Classes - We use the 'bp-menu' CSS class to determine if the
     744 *   menu item belongs to BP, so we cannot allow manipulation of this field to
     745 *   occur.
     746 * - URL - This field is automatically generated by BP on output, so this
     747 *   field is useless and can cause confusion.
     748 *
     749 * Note: These restrictions are only enforced if javascript is enabled.
     750 *
     751 * @since BuddyPress (1.9)
     752 */
     753function bp_admin_wp_nav_menu_restrict_items() {
     754        ?>
     755        <script type="text/javascript">
     756        jQuery( '#menu-to-edit').on( 'click', 'a.item-edit', function() {
     757                var settings  = jQuery(this).closest( '.menu-item-bar' ).next( '.menu-item-settings' );
     758                var css_class = settings.find( '.edit-menu-item-classes' );
     759
     760                if( css_class.val().indexOf( 'bp-menu' ) == 0 ) {
     761                        css_class.attr( 'readonly', 'readonly' );
     762                        settings.find( '.field-url' ).css( 'display', 'none' );
     763                }
     764        });
     765        </script>
     766        <?php
     767}
  • 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..e59a0e3 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                case 'login' :
     423                        if ( is_user_logged_in() ) {
     424                                $menu_item->_invalid = true;
     425                        } else {
     426                                $menu_item->url = wp_login_url( wp_guess_url() );
     427                        }
     428
     429                        break;
     430
     431                case 'logout' :
     432                        if ( ! is_user_logged_in() ) {
     433                                $menu_item->_invalid = true;
     434                        } else {
     435                                $menu_item->url = wp_logout_url( wp_guess_url() );
     436                        }
     437
     438                        break;
     439
     440                // Don't show the Register link to logged-in users
     441                case 'register' :
     442                        if ( is_user_logged_in() ) {
     443                                $menu_item->_invalid = true;
     444                        }
     445
     446                        break;
     447
     448                // All other BP nav items are specific to the logged-in user,
     449                // and so are not relevant to logged-out users
     450                default:
     451                        if ( is_user_logged_in() ) {
     452                                $menu_item->url = bp_nav_menu_get_item_url( $matches[1] );
     453                        } else {
     454                                $menu_item->_invalid = true;
     455                        }
     456
     457                        break;
     458        }
     459
     460        // Highlight the current page
     461        $current = bp_get_requested_url();
     462        if ( strpos( $current, $menu_item->url ) !== false ) {
     463                $menu_item->classes[] = 'current_page_item';
     464        }
     465
     466        return $menu_item;
     467}
     468
     469add_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..3237f22 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 BP's logged-in nav menu for use in
     1419 * the WordPress "Menus" settings page.
     1420 *
     1421 * WordPress nav menus work by representing post or tax term data as a custom
     1422 * post type, which is then used to populate the checkboxes that appear on
     1423 * Dashboard > Appearance > Menu as well as the menu as rendered on the front
     1424 * end. Most of the items in the BuddyPress set of nav items are neither posts
     1425 * nor tax terms, so we fake a post-like object so as to be compatible with the
     1426 * menu.
     1427 *
     1428 * This technique also allows us to generate links dynamically, so that, for
     1429 * example, "My Profile" will always point to the URL of the profile of the
     1430 * logged-in user.
     1431 *
     1432 * @since BuddyPress (1.9)
     1433 *
     1434 * @uses buddypress() to get the bp_nav items
     1435 * @return mixed A URL or an array of dummy pages
     1436 */
     1437function bp_nav_menu_get_loggedin_pages() {
     1438
     1439        // Try to catch the cached version first
     1440        if ( ! empty( buddypress()->wp_nav_menu_items ) ) {
     1441                return buddypress()->wp_nav_menu_items;
     1442        }
     1443
     1444        // Pull up a list of items registered in BP's top-level nav array
     1445        $bp_menu_items = buddypress()->bp_nav;
     1446
     1447        // Some BP nav menu items will not be represented in bp_nav, because
     1448        // they are not real BP components. We add them manually here.
     1449        $bp_menu_items[] = array(
     1450                'name' => __( 'Log Out', 'buddypress' ),
     1451                'slug' => 'logout',
     1452                'link' => wp_logout_url(),
     1453        );
     1454
     1455        // If there's nothing to show, we're done
     1456        if ( count( $bp_menu_items ) < 1 ) {
     1457                return false;
     1458        }
     1459
     1460        $page_args = array();
     1461
     1462        foreach ( $bp_menu_items as $bp_item ) {
     1463                $item_name = '';
     1464
     1465                // Remove <span>number</span>
     1466                $item_name = preg_replace( '/([.0-9]+)/', '', $bp_item['name'] );
     1467                $item_name = trim( strip_tags( $item_name ) );
     1468
     1469                $page_args[ $bp_item['slug'] ] = (object) array(
     1470                        'ID'             => -1,
     1471                        'post_title'     => $item_name,
     1472                        'post_author'    => 0,
     1473                        'post_date'      => 0,
     1474                        'post_excerpt'   => $bp_item['slug'],
     1475                        'post_type'      => 'page',
     1476                        'post_status'    => 'publish',
     1477                        'comment_status' => 'closed',
     1478                        'guid'           => $bp_item['link']
     1479                );
     1480        }
     1481
     1482        if ( empty( buddypress()->wp_nav_menu_items ) ) {
     1483                buddypress()->wp_nav_menu_items = new stdClass;
     1484        }
     1485
     1486        buddypress()->wp_nav_menu_items->loggedin = $page_args;
     1487
     1488        return $page_args;
     1489}
     1490
     1491/**
     1492 * Create fake "post" objects for BP's logged-out nav menu for use in
     1493 * the WordPress "Menus" settings page.
     1494 *
     1495 * WordPress nav menus work by representing post or tax term data as a custom
     1496 * post type, which is then used to populate the checkboxes that appear on
     1497 * Dashboard > Appearance > Menu as well as the menu as rendered on the front
     1498 * end. Most of the items in the BuddyPress set of nav items are neither posts
     1499 * nor tax terms, so we fake a post-like object so as to be compatible with the
     1500 * menu.
     1501 *
     1502 * @since BuddyPress (1.9)
     1503 *
     1504 * @uses bp_core_get_directory_page_ids() to get all the directory pages, and
     1505 *   check for the proper location of the Register page
     1506 * @uses get_the_title() to get the title for the registration page
     1507 * @uses get_permalink() to get the link to the registration page
     1508 * @uses buddypress() to get the bp_nav items
     1509 * @return mixed A URL or an array of dummy pages
     1510 */
     1511function bp_nav_menu_get_loggedout_pages() {
     1512
     1513        // Try to catch the cached version first
     1514        if ( ! empty( buddypress()->wp_nav_menu_items->loggedout ) ) {
     1515                return buddypress()->wp_nav_menu_items->loggedout;
     1516        }
     1517
     1518        $bp_menu_items = array();
     1519
     1520        // Some BP nav menu items will not be represented in bp_nav, because
     1521        // they are not real BP components. We add them manually here.
     1522        $bp_menu_items[] = array(
     1523                'name' => __( 'Log In', 'buddypress' ),
     1524                'slug' => 'login',
     1525                'link' => wp_login_url(),
     1526        );
     1527
     1528        // The Register page will not always be available (ie, when
     1529        // registration is disabled)
     1530        $bp_directory_page_ids = bp_core_get_directory_page_ids();
     1531
     1532        if( ! empty( $bp_directory_page_ids['register'] ) ) {
     1533                $register_page = get_post( $bp_directory_page_ids['register'] );
     1534                $bp_menu_items[] = array(
     1535                        'name' => $register_page->post_title,
     1536                        'slug' => $register_page->post_name,
     1537                        'link' => get_permalink( $register_page->ID ),
     1538                );
     1539        }
     1540
     1541        // If there's nothing to show, we're done
     1542        if ( count( $bp_menu_items ) < 1 ) {
     1543                return false;
     1544        }
     1545
     1546        $page_args = array();
     1547
     1548        foreach ( $bp_menu_items as $bp_item ) {
     1549                $page_args[ $bp_item['slug'] ] = (object) array(
     1550                        'ID'             => -1,
     1551                        'post_title'     => $bp_item['name'],
     1552                        'post_author'    => 0,
     1553                        'post_date'      => 0,
     1554                        'post_excerpt'   => $bp_item['slug'],
     1555                        'post_type'      => 'page',
     1556                        'post_status'    => 'publish',
     1557                        'comment_status' => 'closed',
     1558                        'guid'           => $bp_item['link']
     1559                );
     1560        }
     1561
     1562        if ( empty( buddypress()->wp_nav_menu_items ) ) {
     1563                buddypress()->wp_nav_menu_items = new stdClass;
     1564        }
     1565
     1566        buddypress()->wp_nav_menu_items->loggedout = $page_args;
     1567
     1568        return $page_args;
     1569}
     1570
     1571/**
     1572 * Get the URL for a BuddyPress WP nav menu item, based on slug
     1573 *
     1574 * BuddyPress-specific WP nav menu items have dynamically generated URLs,
     1575 * based on the identity of the current user. This function lets you fetch the
     1576 * proper URL for a given nav item slug (such as 'login' or 'messages').
     1577 *
     1578 * @since BuddyPress (1.9)
     1579 *
     1580 * @param string $slug The slug of the nav item: login, register, or one of the
     1581 *   slugs from buddypress()->bp_nav
     1582 * @return string $nav_item_url The URL generated for the current user
     1583 */
     1584function bp_nav_menu_get_item_url( $slug ) {
     1585        $nav_item_url   = '';
     1586        $nav_menu_items = bp_nav_menu_get_loggedin_pages();
     1587
     1588        if ( isset( $nav_menu_items[ $slug ] ) ) {
     1589                $nav_item_url = $nav_menu_items[ $slug ]->guid;
     1590        }
     1591
     1592        return $nav_item_url;
     1593}