Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
03/01/2023 08:17:11 AM (3 years ago)
Author:
imath
Message:

BP Rewrites: introduce the bp_rewrites_get_url() function

Its role is to build every BuddyPress URL using the BP Rewrites API.

This commit also deprecates softly some key functions like bp_get_root_domain() to let us review (thanks to deprecated notices) all BuddyPress links during 12.0 development cycle and make them use the introduced bp_rewrites_get_url() function or a wrapper of it. Once all replacements achieved, we'll need to fully deprecate:

  • bp_get_root_domain()
  • bp_root_domain()
  • bp_core_get_root_domain()

Slug constants have also been completely deprecated as we will be able to customize every slugs from the future "URL" tab of the BuddyPress settings page.

The $bp->root_domain BuddyPress global has been deprecated in favor of $bp->root_url.

Finally, the Components $rewrite_ids properties are now in place and corresponding rewrite rules are successfully generated.

Props r-a-y, johnjamesjacoby, boonebgorges

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

File:
1 edited

Legend:

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

    r13422 r13432  
    4444    );
    4545}
     46
     47/**
     48 * Delete rewrite rules, so that they are automatically rebuilt on
     49 * the subsequent page load.
     50 *
     51 * @since 12.0.0
     52 */
     53function bp_delete_rewrite_rules() {
     54    delete_option( 'rewrite_rules' );
     55}
     56
     57/**
     58 * Are Pretty URLs active?
     59 *
     60 * @since 12.0.0
     61 *
     62 * @return bool True if Pretty URLs are on. False otherwise.
     63 */
     64function bp_has_pretty_urls() {
     65    $has_plain_urls = ! get_option( 'permalink_structure', '' );
     66    return ! $has_plain_urls;
     67}
     68
     69/**
     70 * Returns the slug to use for the view belonging to the requested component.
     71 *
     72 * @since 12.0.0
     73 *
     74 * @param string $component_id The BuddyPress component's ID.
     75 * @param string $rewrite_id   The view rewrite ID.
     76 * @param string $default_slug The view default slug.
     77 * @return string The slug to use for the view belonging to the requested component.
     78 */
     79function bp_rewrites_get_slug( $component_id = '', $rewrite_id = '', $default_slug = '' ) {
     80    $directory_pages = bp_core_get_directory_pages();
     81    $slug            = $default_slug;
     82
     83    if ( ! isset( $directory_pages->{$component_id}->custom_slugs ) || ! $rewrite_id ) {
     84        return $slug;
     85    }
     86
     87    $custom_slugs = (array) $directory_pages->{$component_id}->custom_slugs;
     88    if ( isset( $custom_slugs[ $rewrite_id ] ) && $custom_slugs[ $rewrite_id ] ) {
     89        $slug = $custom_slugs[ $rewrite_id ];
     90    }
     91
     92    return $slug;
     93}
     94
     95/**
     96 * Builds a BuddyPress link using the WP Rewrite API.
     97 *
     98 * @since 12.0.0
     99 *
     100 * @param array $args {
     101 *      Optional. An array of arguments.
     102 *
     103 *      @type string $component_id                The BuddyPress component ID. Defaults ''.
     104 *      @type string $directory_type              Whether it's an object type URL. Defaults ''.
     105 *                                                Accepts '' (no object type), 'members' or 'groups'.
     106 *      @type string $single_item                 The BuddyPress single item's URL chunk. Defaults ''.
     107 *                                                Eg: the member's user nicename for Members or the group's slug for Groups.
     108 *      @type string $single_item_component       The BuddyPress single item's component URL chunk. Defaults ''.
     109 *                                                Eg: the member's Activity page.
     110 *      @type string $single_item_action          The BuddyPress single item's action URL chunk. Defaults ''.
     111 *                                                Eg: the member's Activity mentions page.
     112 *      @type array $single_item_action_variables The list of BuddyPress single item's action variable URL chunks. Defaults [].
     113 * }
     114 * @return string The BuddyPress link.
     115 */
     116function bp_rewrites_get_url( $args = array() ) {
     117    $bp   = buddypress();
     118    $url = get_home_url( bp_get_root_blog_id() );
     119
     120    $r = bp_parse_args(
     121        $args,
     122        array(
     123            'component_id'                 => '',
     124            'directory_type'               => '',
     125            'single_item'                  => '',
     126            'single_item_component'        => '',
     127            'single_item_action'           => '',
     128            'single_item_action_variables' => array(),
     129        )
     130    );
     131
     132    if ( $r['component_id'] && isset( $bp->{$r['component_id']}->rewrite_ids ) ) {
     133        $component = $bp->{$r['component_id']};
     134        unset( $r['component_id'] );
     135
     136        // Using plain links.
     137        if ( ! bp_has_pretty_urls() ) {
     138            if ( ! isset( $r['member_register'] ) && ! isset( $r['member_activate'] ) ) {
     139                $r['directory'] = 1;
     140            }
     141
     142            $r  = array_filter( $r );
     143            $qv = array();
     144
     145            foreach ( $component->rewrite_ids as $key => $rewrite_id ) {
     146                if ( ! isset( $r[ $key ] ) ) {
     147                    continue;
     148                }
     149
     150                $qv[ $rewrite_id ] = $r[ $key ];
     151            }
     152
     153            $url = add_query_arg( $qv, $url );
     154
     155            // Using pretty URLs.
     156        } else {
     157            if ( ! isset( $component->rewrite_ids['directory'] ) || ! isset( $component->directory_permastruct ) ) {
     158                return $url;
     159            }
     160
     161            if ( isset( $r['member_register'] ) ) {
     162                $url = str_replace( '%' . $component->rewrite_ids['member_register'] . '%', '', $component->register_permastruct );
     163                unset( $r['member_register'] );
     164            } elseif ( isset( $r['member_activate'] ) ) {
     165                $url = str_replace( '%' . $component->rewrite_ids['member_activate'] . '%', '', $component->activate_permastruct );
     166                unset( $r['member_activate'] );
     167            } elseif ( isset( $r['create_single_item'] ) ) {
     168                $create_slug = 'create';
     169                if ( 'groups' === $component->id ) {
     170                    $create_slug = bp_rewrites_get_slug( 'groups', 'bp_group_create', 'create' );
     171                }
     172
     173                $url = str_replace( '%' . $component->rewrite_ids['directory'] . '%', $create_slug, $component->directory_permastruct );
     174                unset( $r['create_single_item'] );
     175            } else {
     176                $url = str_replace( '%' . $component->rewrite_ids['directory'] . '%', $r['single_item'], $component->directory_permastruct );
     177
     178                // Remove the members directory slug when root profiles are on.
     179                if ( bp_core_enable_root_profiles() && 'members' === $component->id && isset( $r['single_item'] ) && $r['single_item'] ) {
     180                    $url = str_replace( $bp->members->root_slug . '/', '', $url );
     181                }
     182
     183                unset( $r['single_item'] );
     184            }
     185
     186            $r = array_filter( $r );
     187
     188            if ( isset( $r['directory_type'] ) && $r['directory_type'] ) {
     189                if ( 'members' === $component->id ) {
     190                    array_unshift( $r, bp_get_members_member_type_base() );
     191                } elseif ( 'groups' === $component->id && bp_is_active( 'groups' ) ) {
     192                    array_unshift( $r, bp_get_groups_group_type_base() );
     193                } else {
     194                    unset( $r['directory_type'] );
     195                }
     196            }
     197
     198            if ( isset( $r['single_item_action_variables'] ) && $r['single_item_action_variables'] ) {
     199                $r['single_item_action_variables'] = join( '/', (array) $r['single_item_action_variables'] );
     200            }
     201
     202            if ( isset( $r['create_single_item_variables'] ) && $r['create_single_item_variables'] ) {
     203                $r['create_single_item_variables'] = join( '/', (array) $r['create_single_item_variables'] );
     204            }
     205
     206            $url = get_home_url( bp_get_root_blog_id(), user_trailingslashit( '/' . rtrim( $url, '/' ) . '/' . join( '/', $r ) ) );
     207        }
     208    }
     209
     210    /**
     211     * Filter here to edit any BudyPress URL.
     212     *
     213     * @since 12.0.0
     214     *
     215     * @param string $url The BudyPress URL.
     216     * @param array  $r {
     217     *      Optional. An array of arguments.
     218     *
     219     *      @type string $component_id                The BuddyPress component ID. Defaults ''.
     220     *      @type string $directory_type              Whether it's an object type URL. Defaults ''.
     221     *                                                Accepts '' (no object type), 'members' or 'groups'.
     222     *      @type string $single_item                 The BuddyPress single item's URL chunk. Defaults ''.
     223     *                                                Eg: the member's user nicename for Members or the group's slug for Groups.
     224     *      @type string $single_item_component       The BuddyPress single item's component URL chunk. Defaults ''.
     225     *                                                Eg: the member's Activity page.
     226     *      @type string $single_item_action          The BuddyPress single item's action URL chunk. Defaults ''.
     227     *                                                Eg: the member's Activity mentions page.
     228     *      @type array $single_item_action_variables The list of BuddyPress single item's action variable URL chunks. Defaults [].
     229     * }
     230     */
     231    return apply_filters( 'bp_rewrites_get_url', $url, $r );
     232}
     233
     234/**
     235 * Gets the BP root site URL, using BP Rewrites.
     236 *
     237 * @since 12.0.0
     238 *
     239 * @return string The BP root site URL.
     240 */
     241function bp_rewrites_get_root_url() {
     242    $url = bp_rewrites_get_url( array() );
     243
     244    /**
     245     * Filter here to edit the BP root site URL.
     246     *
     247     * @since 12.0.0
     248     *
     249     * @param string $url The BP root site URL.
     250     */
     251    return apply_filters( 'bp_rewrites_get_root_url', $url );
     252}
Note: See TracChangeset for help on using the changeset viewer.