Skip to:
Content

BuddyPress.org

Changeset 12892


Ignore:
Timestamp:
04/18/2021 10:18:06 AM (9 months ago)
Author:
imath
Message:

Nouveau: improve the way customizable slugs are handled

BuddyPress uses constants such as BP_FRIENDS_SLUG to let advanced users customize component URL slugs. The Nouveau template pack was wrongly checking hardcoded component names at various places into its code, in particular into the following functions and template tags:

  • bp_nouveau_current_object()
  • bp_nouveau_filter_options()
  • bp_nouveau_wrapper()

This commit also introduces a new BP Core function to get the active BP Component objects: bp_core_get_active_components(). The $args argument can be used to filter the active components according to their slugs, names, ids or root slugs. Nouveau uses it to determine the component ID out of its slug and use this component ID instead of slugs to create its needed dynamic selectors, classes and data attributes.

Props mattneil

Fixes #8133

Location:
trunk/src
Files:
7 edited

Legend:

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

    r12797 r12892  
    948948     */
    949949    return apply_filters( 'bp_core_get_component_search_query_arg', $query_arg, $component );
     950}
     951
     952/**
     953 * Get a list of all active component objects.
     954 *
     955 * @since 8.0.0
     956 *
     957 * @param array $args {
     958 *     Optional. An array of key => value arguments to match against the component objects.
     959 *     Default empty array.
     960 *
     961 *     @type string $name          Translatable name for the component.
     962 *     @type string $id            Unique ID for the component.
     963 *     @type string $slug          Unique slug for the component, for use in query strings and URLs.
     964 *     @type bool   $has_directory True if the component has a top-level directory. False otherwise.
     965 *     @type string $root_slug     Slug used by the component's directory page.
     966 * }
     967 * @param string $output   Optional. The type of output to return. Accepts 'ids'
     968 *                         or 'objects'. Default 'ids'.
     969 * @param string $operator Optional. The logical operation to perform. 'or' means only one
     970 *                         element from the array needs to match; 'and' means all elements
     971 *                         must match. Accepts 'or' or 'and'. Default 'and'.
     972 * @return array A list of component ids or objects.
     973 */
     974function bp_core_get_active_components( $args = array(), $output = 'ids', $operator = 'and' ) {
     975    $bp = buddypress();
     976
     977    $active_components = array_keys( $bp->active_components );
     978
     979    $xprofile_id = array_search( 'xprofile', $active_components, true );
     980    if ( false !== $xprofile_id ) {
     981        $active_components[ $xprofile_id ] = 'profile';
     982    }
     983
     984    $components = array();
     985    foreach ( $active_components as $id ) {
     986        if ( isset( $bp->{$id} ) && $bp->{$id} instanceof BP_Component ) {
     987            $components[ $id ] = $bp->{$id};
     988        }
     989    }
     990
     991    $components = wp_filter_object_list( $components, $args, $operator );
     992
     993    if ( 'ids' === $output ) {
     994        $components = wp_list_pluck( $components, 'id' );
     995    }
     996
     997    return $components;
    950998}
    951999
  • trunk/src/bp-members/bp-members-adminbar.php

    r12495 r12892  
    103103            'id'     => $bp->user_admin_menu_id . '-edit-profile',
    104104            'title'  => __( "Edit Profile", 'buddypress' ),
    105             'href'   => bp_get_members_component_link( 'profile', 'edit' )
     105            'href'   => bp_get_members_component_link( $bp->profile->id, 'edit' )
    106106        ) );
    107107
     
    112112                'id'     => $bp->user_admin_menu_id . '-change-avatar',
    113113                'title'  => __( "Edit Profile Photo", 'buddypress' ),
    114                 'href'   => bp_get_members_component_link( 'profile', 'change-avatar' )
     114                'href'   => bp_get_members_component_link( $bp->profile->id, 'change-avatar' )
    115115            ) );
    116116        }
     
    122122                'id'     => $bp->user_admin_menu_id . '-change-cover-image',
    123123                'title'  => __( 'Edit Cover Image', 'buddypress' ),
    124                 'href'   => bp_get_members_component_link( 'profile', 'change-cover-image' )
     124                'href'   => bp_get_members_component_link( $bp->profile->id, 'change-cover-image' )
    125125            ) );
    126126        }
  • trunk/src/bp-members/bp-members-template.php

    r12799 r12892  
    28012801     */
    28022802    function bp_get_members_component_link( $component, $action = '', $query_args = '', $nonce = false ) {
    2803 
    28042803        // Must be displayed user.
    2805         if ( !bp_displayed_user_id() )
     2804        if ( ! bp_displayed_user_id() ) {
    28062805            return;
     2806        }
    28072807
    28082808        $bp = buddypress();
    28092809
     2810        if ( 'xprofile' === $component ) {
     2811            $component = 'profile';
     2812        }
     2813
    28102814        // Append $action to $url if there is no $type.
    2811         if ( !empty( $action ) )
     2815        if ( ! empty( $action ) ) {
    28122816            $url = bp_displayed_user_domain() . $bp->{$component}->slug . '/' . $action;
    2813         else
     2817        } else {
    28142818            $url = bp_displayed_user_domain() . $bp->{$component}->slug;
     2819        }
    28152820
    28162821        // Add a slash at the end of our user url.
     
    28182823
    28192824        // Add possible query arg.
    2820         if ( !empty( $query_args ) && is_array( $query_args ) )
     2825        if ( ! empty( $query_args ) && is_array( $query_args ) ) {
    28212826            $url = add_query_arg( $query_args, $url );
     2827        }
    28222828
    28232829        // To nonce, or not to nonce...
    2824         if ( true === $nonce )
     2830        if ( true === $nonce ) {
    28252831            $url = wp_nonce_url( $url );
    2826         elseif ( is_string( $nonce ) )
     2832        } elseif ( is_string( $nonce ) ) {
    28272833            $url = wp_nonce_url( $url, $nonce );
     2834        }
    28282835
    28292836        // Return the url, if there is one.
    2830         if ( !empty( $url ) )
     2837        if ( ! empty( $url ) ) {
    28312838            return $url;
     2839        }
    28322840    }
    28332841
  • trunk/src/bp-members/classes/class-bp-members-component.php

    r12771 r12892  
    315315        $access       = bp_core_can_edit_settings();
    316316        $slug         = bp_get_profile_slug();
    317         $profile_link = bp_get_members_component_link( $slug );
     317        $profile_link = bp_get_members_component_link( buddypress()->profile->id );
    318318
    319319        // Change Avatar.
  • trunk/src/bp-templates/bp-nouveau/buddypress/common/search-and-filters-bar.php

    r12156 r12892  
    44 *
    55 * @since 3.0.0
    6  * @version 3.1.0
     6 * @version 8.0.0
    77 */
    88?>
    99<div class="subnav-filters filters no-ajax" id="subnav-filters">
    1010
    11     <?php if ( 'friends' !== bp_current_component() ) : ?>
    12     <div class="subnav-search clearfix">
     11    <?php if ( bp_get_friends_slug() !== bp_current_component() ) : ?>
     12        <div class="subnav-search clearfix">
    1313
    14         <?php if ( 'activity' === bp_current_component() ) : ?>
    15             <div class="feed"><a href="<?php bp_sitewide_activity_feed_link(); ?>" class="bp-tooltip" data-bp-tooltip="<?php esc_attr_e( 'RSS Feed', 'buddypress' ); ?>"><span class="bp-screen-reader-text"><?php esc_html_e( 'RSS', 'buddypress' ); ?></span></a></div>
    16         <?php endif; ?>
     14            <?php if ( bp_get_activity_slug() === bp_current_component() ) : ?>
     15                <div class="feed"><a href="<?php bp_sitewide_activity_feed_link(); ?>" class="bp-tooltip" data-bp-tooltip="<?php esc_attr_e( 'RSS Feed', 'buddypress' ); ?>"><span class="bp-screen-reader-text"><?php esc_html_e( 'RSS', 'buddypress' ); ?></span></a></div>
     16            <?php endif; ?>
    1717
    18         <?php bp_nouveau_search_form(); ?>
     18            <?php bp_nouveau_search_form(); ?>
    1919
    20     </div>
     20        </div>
    2121    <?php endif; ?>
    2222
    2323        <?php if ( bp_is_user() && ! bp_is_current_action( 'requests' ) ) : ?>
    2424            <?php bp_get_template_part( 'common/filters/user-screens-filters' ); ?>
    25         <?php elseif ( 'groups' === bp_current_component() ) : ?>
     25        <?php elseif ( bp_get_groups_slug() === bp_current_component() ) : ?>
    2626            <?php bp_get_template_part( 'common/filters/groups-screens-filters' ); ?>
    2727        <?php else : ?>
  • trunk/src/bp-templates/bp-nouveau/includes/functions.php

    r12595 r12892  
    44 *
    55 * @since 3.0.0
    6  * @version 3.1.0
     6 * @version 8.0.0
    77 */
    88
     
    238238 */
    239239function bp_nouveau_wrapper( $args = array() ) {
    240  /**
    241     * Classes need to be determined & set by component to a certain degree
    242     *
    243     * Check the component to find a default container_class to add
    244     */
    245     $current_component_class = bp_current_component() . '-meta';
     240    /**
     241     * Classes need to be determined & set by component to a certain degree.
     242     *
     243     * Check the component to find a default container_class based on the component ID to add.
     244     * We need to to this because bp_current_component() is using the component slugs which can differ
     245     * from the component ID.
     246     */
     247    $current_component_id = bp_core_get_active_components( array( 'slug' => bp_current_component() ) );
     248    if ( $current_component_id && 1 === count( $current_component_id ) ) {
     249        $current_component_id = reset( $current_component_id );
     250    } else {
     251        $current_component_id = bp_current_component();
     252    }
     253
     254    $current_component_class = $current_component_id . '-meta';
    246255
    247256    if ( bp_is_group_activity() ) {
     
    256265            'container'         => 'div',
    257266            'container_id'      => '',
    258             'container_classes' => array( $generic_class, $current_component_class   ),
     267            'container_classes' => array( $generic_class, $current_component_class ),
    259268            'output'            => '',
    260269        ),
     
    533542
    534543    if ( empty( $component ) ) {
    535         if ( 'directory' === $context || 'user' === $context ) {
    536             $component = bp_current_component();
     544        if ( 'user' === $context ) {
     545            $component = bp_core_get_active_components( array( 'slug' => bp_current_component() ) );
     546            $component = reset( $component );
    537547
    538548            if ( 'friends' === $component ) {
     
    544554        } elseif ( 'group' === $context && bp_is_group_members() ) {
    545555            $component = 'members';
     556        } else {
     557            $component = bp_current_component();
    546558        }
    547559    }
  • trunk/src/bp-templates/bp-nouveau/includes/template-tags.php

    r12887 r12892  
    44 *
    55 * @since 3.0.0
    6  * @version 7.0.0
     6 * @version 8.0.0
    77 */
    88
     
    20692069
    20702070    } else {
    2071         $data_filter = bp_current_component();
     2071        $component_id = bp_current_component();
     2072        if ( ! bp_is_directory() ) {
     2073            $component_id = bp_core_get_active_components( array( 'slug' => $component_id ) );
     2074            $component_id = reset( $component_id );
     2075        }
     2076
     2077        $data_filter  = $component_id;
     2078
    20722079        if ( 'friends' === $data_filter && bp_is_user_friend_requests() ) {
    20732080            $data_filter = 'friend_requests';
     
    20762083        $component['members_select']   = 'members-order-select';
    20772084        $component['members_order_by'] = 'members-order-by';
    2078         $component['object']           = bp_current_component();
     2085        $component['object']           = $component_id;
    20792086        $component['data_filter']      = $data_filter;
    20802087    }
     
    22332240        $output = '';
    22342241
    2235         if ( 'notifications' === bp_current_component() ) {
     2242        if ( bp_get_notifications_slug() === bp_current_component() ) {
    22362243            $output = bp_nouveau_get_notifications_filters();
    22372244
Note: See TracChangeset for help on using the changeset viewer.