Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
04/20/2023 02:30:11 AM (20 months ago)
Author:
imath
Message:

Start implementing parse_query() methods for directory components

First concerned commponents are: Activity, Blogs & Members.

  • Remove the notice used to force pretty permalinks
  • Use plain links to test parse_query() methods are rightly setting BuddyPress URI globals
  • Introduce some helper functions used during the BP Rewrites API parsing process :
    • bp_is_directory_homepage() is checking if a BP Directory is used as the site's homepage
    • bp_rewrites_get_custom_slug_rewrite_id() finds a Rewrite ID out of a custom slug.
    • bp_rewrites_get_member_data() returns the field to use to get the user by.
    • bp_reset_query() makes it possible to parse again a request in specific cases (eg: root profiles)

Props r-a-y, johnjamesjacoby, boonebgorges

Closes https://github.com/buddypress/buddypress/pull/87
See #4954

File:
1 edited

Legend:

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

    r13452 r13455  
    116116
    117117    return $slug;
     118}
     119
     120/**
     121 * Returns the rewrite ID of a customized slug.
     122 *
     123 * @since 12.0.0
     124 *
     125 * @param string $component_id The component ID (eg: `activity` for the BP Activity component).
     126 * @param string $slug         The customized slug.
     127 * @param string $context      The context for the customized slug, useful when the same slug is used
     128 *                             for more than one rewrite ID of the same component.
     129 * @return string              The rewrite ID matching the customized slug.
     130 */
     131function bp_rewrites_get_custom_slug_rewrite_id( $component_id = '', $slug = '', $context = '' ) {
     132    $directory_pages = bp_core_get_directory_pages();
     133
     134    if ( ! isset( $directory_pages->{$component_id}->custom_slugs ) || ! $slug ) {
     135        return null;
     136    }
     137
     138    $custom_slugs = (array) $directory_pages->{$component_id}->custom_slugs;
     139    $rewrite_ids  = array_keys( $custom_slugs, $slug, true );
     140
     141    if ( 1 < count( $rewrite_ids ) && isset( $context ) && $context ) {
     142        foreach ( $rewrite_ids as $rewrite_id_key => $rewrite_id ) {
     143            if ( false !== strpos( $rewrite_id, $context ) ) {
     144                continue;
     145            }
     146
     147            unset( $rewrite_ids[ $rewrite_id_key ] );
     148        }
     149    }
     150
     151    // Always return the first match.
     152    return reset( $rewrite_ids );
    118153}
    119154
     
    284319    return apply_filters( 'bp_rewrites_get_root_url', $url );
    285320}
     321
     322/**
     323 * Get needed data to find a member single item from the requested URL.
     324 *
     325 * @since 12.0.0
     326 *
     327 * @param string $request The request used during parsing.
     328 * @return array          Data to use to find a member single item from the request.
     329 */
     330function bp_rewrites_get_member_data( $request = '' ) {
     331    $member_data = array( 'field' => 'slug' );
     332
     333    if ( bp_is_username_compatibility_mode() ) {
     334        $member_data = array( 'field' => 'login' );
     335    }
     336
     337    if ( bp_core_enable_root_profiles() ) {
     338        if ( ! $request ) {
     339            $request = $GLOBALS['wp']->request;
     340        }
     341
     342        $request_chunks = explode( '/', ltrim( $request, '/' ) );
     343        $member_chunk   = reset( $request_chunks );
     344
     345        // Try to get an existing member to eventually reset the WP Query.
     346        $member_data['object'] = get_user_by( $member_data['field'], $member_chunk );
     347    }
     348
     349    return $member_data;
     350}
Note: See TracChangeset for help on using the changeset viewer.