Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
03/18/2023 09:42:31 AM (3 years ago)
Author:
imath
Message:

BP Rewrites: start the migration process for the Groups component

  • Add rewrite tags & rules for the Groups directory type and the create URLs.
  • Introduce bp_get_group_url()/bp_group_url() & to retrieve/output a Groups single item URL using BP Rewrites.
  • Introduce bp_get_group_restricted_screens(), bp_get_group_extension_screens() & bp_get_group_screens() to get information about the Groups screens (in particular each screen rewrite ID). These functions will ease slug customizations from the BuddyPress URL settings tab.
  • Improve the Group creation process making sure it's using BP Rewrites to build URLs.
  • Perform easiest replacements for bp_get_group_permalink()/bp_group_permalink() & bp_get_groups_directory_permalink()/bp_groups_directory_permalink() in favor of bp_get_group_url()/bp_group_url() & bp_get_groups_directory_url()/bp_groups_directory_url()`.
  • Improve code formatting & properly escape single group URLs into templates.
  • Update impacted Unit Tests.

Props r-a-y, johnjamesjacoby, boonebgorges

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-groups/bp-groups-template.php

    r13436 r13437  
    118118 */
    119119function bp_get_groups_directory_url( $path_chunks = array() ) {
    120         $supported_chunks = array_fill_keys( array( 'create_single_item', 'directory_type' ), true );
     120        $supported_chunks = array_fill_keys( array( 'create_single_item', 'create_single_item_variables', 'directory_type' ), true );
    121121
    122122        $path_chunks = bp_parse_args(
     
    12371237
    12381238/**
    1239  * Output the permalink for the group.
    1240  *
    1241  * @since 1.0.0
     1239 * Output the URL for the group.
     1240 *
     1241 * @since 12.0.0
    12421242 *
    12431243 * @param false|int|string|BP_Groups_Group $group (Optional) The Group ID, the Group Slug or the Group object.
    12441244 *                                                Default: false.
    12451245 */
     1246function bp_group_url( $group = false ) {
     1247        echo esc_url( bp_get_group_url( $group ) );
     1248}
     1249
     1250/**
     1251 * Returns the Groups single item's URL.
     1252 *
     1253 * @since 12.0.0
     1254 *
     1255 * @param integer|BP_Groups_Group $group The group ID or the Group object.
     1256 * @param array                   $path_chunks {
     1257 *     An array of arguments. Optional.
     1258 *
     1259 *     @type string $single_item_component        The component slug the action is relative to.
     1260 *     @type string $single_item_action           The slug of the action to perform.
     1261 *     @type array  $single_item_action_variables An array of additional informations about the action to perform.
     1262 * }
     1263 * @return string The URL built for the BP Rewrites URL parser.
     1264 */
     1265function bp_get_group_url( $group = 0, $path_chunks = array() ) {
     1266        $url  = '';
     1267        $slug = groups_get_slug( $group );
     1268
     1269        if ( $group instanceof BP_Groups_Group ) {
     1270                $group_id = (int) $group->id;
     1271        } else {
     1272                $group_id = (int) $group;
     1273        }
     1274
     1275        if ( $slug ) {
     1276                $supported_chunks = array_fill_keys( array( 'single_item_component', 'single_item_action', 'single_item_action_variables' ), true );
     1277                $path_chunks      = bp_parse_args(
     1278                        array_intersect_key( $path_chunks, $supported_chunks ),
     1279                        array(
     1280                                'component_id' => 'groups',
     1281                                'single_item'  => $slug,
     1282                        )
     1283                );
     1284
     1285                $url = bp_rewrites_get_url( $path_chunks );
     1286        }
     1287
     1288        /**
     1289         * Filters the URL for the passed group.
     1290         *
     1291         * @since 12.0.0
     1292         *
     1293         * @param string  $url      The group url.
     1294         * @param integer $group_id The group ID.
     1295         * @param string  $slug     The group slug.
     1296         * @param array   $path_chunks {
     1297         *     An array of arguments. Optional.
     1298         *
     1299         *     @type string $single_item_component        The component slug the action is relative to.
     1300         *     @type string $single_item_action           The slug of the action to perform.
     1301         *     @type array  $single_item_action_variables An array of additional informations about the action to perform.
     1302         * }
     1303         */
     1304        return apply_filters( 'bp_get_group_url', $url, $group_id, $slug, $path_chunks );
     1305}
     1306
     1307/**
     1308 * Output the permalink for the group.
     1309 *
     1310 * @since 1.0.0
     1311 * @deprecated 12.0.0
     1312 *
     1313 * @param false|int|string|BP_Groups_Group $group (Optional) The Group ID, the Group Slug or the Group object.
     1314 *                                                Default: false.
     1315 */
    12461316function bp_group_permalink( $group = false ) {
    1247         echo bp_get_group_permalink( $group );
     1317        _deprecated_function( __FUNCTION__, '12.0.0', 'bp_group_url' );
     1318        bp_group_url( $group );
    12481319}
    12491320        /**
     
    12581329         */
    12591330        function bp_get_group_permalink( $group = false ) {
    1260                 $group = bp_get_group( $group );
    1261 
    1262                 if ( empty( $group->id ) ) {
    1263                         return '';
    1264                 }
     1331                /*
     1332                 * This function is used at many places and we need to review all this
     1333                 * places during the 12.0 development cycle. Using BP Rewrites means we
     1334                 * cannot concatenate URL chunks to build our URL anymore. We now need
     1335                 * to use `bp_get_group_url( $group, $array )` and make sure to use
     1336                 * the right arguments inside this `$array`.
     1337                 *
     1338                 * @todo Once every link reviewed, we'll be able to remove this check
     1339                 *       and let PHPUnit tell us the one we forgot, eventually!
     1340                 */
     1341                if ( ! buddypress()->is_phpunit_running ) {
     1342                        _deprecated_function( __FUNCTION__, '12.0.0', 'bp_get_group_url' );
     1343                }
     1344
     1345                $url = bp_get_group_url( $group );
    12651346
    12661347                /**
     
    12691350                 * @since 1.0.0
    12701351                 * @since 2.5.0 Added the `$group` parameter.
    1271                  *
    1272                  * @param string          $permalink Permalink for the group.
    1273                  * @param BP_Groups_Group $group     The group object.
    1274                  */
    1275                 return apply_filters( 'bp_get_group_permalink', trailingslashit( bp_get_groups_directory_permalink() . bp_get_group_slug( $group ) . '/' ), $group );
     1352                 * @deprecated 12.0.0
     1353                 *
     1354                 * @param string          $url   Permalink for the group.
     1355                 * @param BP_Groups_Group $group The group object.
     1356                 */
     1357                return apply_filters_deprecated( 'bp_get_group_permalink', array( $url, $group ), '12.0.0', 'bp_get_group_url' );
    12761358        }
    12771359
     
    13061388                $link = sprintf(
    13071389                        '<a href="%s" class="bp-group-home-link %s-home-link">%s</a>',
    1308                         esc_url( bp_get_group_permalink( $group ) ),
     1390                        esc_url( bp_get_group_url( $group ) ),
    13091391                        esc_attr( bp_get_group_slug( $group ) ),
    13101392                        esc_html( bp_get_group_name( $group ) )
     
    31993281                }
    32003282
     3283                $views = bp_get_group_screens( 'read' );
     3284                if ( isset( $views[ $page ]['rewrite_id'] ) ) {
     3285                        $page = bp_rewrites_get_slug( 'groups', $views[ $page ]['rewrite_id'], $page );
     3286                }
     3287
     3288                $url = bp_get_group_url(
     3289                        $group,
     3290                        array(
     3291                                'single_item_action' => $page,
     3292                        )
     3293                );
     3294
    32013295                /**
    32023296                 * Filters the 'action' attribute for a group form.
     
    32053299                 * @since 2.5.0 Added the `$group` parameter.
    32063300                 *
    3207                  * @param string          $value Action attribute for a group form.
     3301                 * @param string          $url  Action attribute for a group form.
    32083302                 * @param BP_Groups_Group $group The group object.
    32093303                 * @param int|string|bool $page  Page slug.
    32103304                 */
    3211                 return apply_filters( 'bp_group_form_action', trailingslashit( bp_get_group_permalink( $group ) . $page ), $group, $page );
     3305                return apply_filters( 'bp_group_form_action', $url, $group, $page );
    32123306        }
    32133307
     
    35593653                 * @param object $group Group object.
    35603654                 */
    3561                 return apply_filters( 'bp_get_group_leave_reject_link', bp_get_group_permalink( $group ), $group );
     3655                return apply_filters( 'bp_get_group_leave_reject_link', bp_get_group_url( $group ), $group );
    35623656        }
    35633657
     
    37193813                                                        'wrapper_class'     => 'group-button ' . $group->status,
    37203814                                                        'wrapper_id'        => 'groupbutton-' . $group->id,
    3721                                                         'link_href'         => add_query_arg( 'redirect_to', bp_get_group_permalink( $group ), bp_get_group_accept_invite_link( $group ) ),
     3815                                                        'link_href'         => add_query_arg( 'redirect_to', bp_get_group_url( $group ), bp_get_group_accept_invite_link( $group ) ),
    37223816                                                        'link_text'         => __( 'Accept Invitation', 'buddypress' ),
    37233817                                                        'link_title'        => __( 'Accept Invitation', 'buddypress' ),
     
    37353829                                                        'wrapper_class'     => 'group-button pending ' . $group->status,
    37363830                                                        'wrapper_id'        => 'groupbutton-' . $group->id,
    3737                                                         'link_href'         => bp_get_group_permalink( $group ),
     3831                                                        'link_href'         => bp_get_group_url( $group ),
    37383832                                                        'link_text'         => __( 'Request Sent', 'buddypress' ),
    37393833                                                        'link_title'        => __( 'Request Sent', 'buddypress' ),
     
    38283922                        'link_text'  => __( 'Create a Group', 'buddypress' ),
    38293923                        'link_class' => 'group-create no-ajax',
    3830                         'link_href'  => trailingslashit( bp_get_groups_directory_permalink() . 'create' ),
     3924                        'link_href'  => bp_get_groups_directory_url(
     3925                                array(
     3926                                        'create_single_item' => 1,
     3927                                )
     3928                        ),
    38313929                        'wrapper'    => false,
    38323930                        'block_self' => false,
     
    48904988
    48914989/**
    4892  * @since 1.0.0
    4893  *
    4894  * @return bool
     4990 * Outputs the Group creation tabs.
     4991 *
     4992 * @since 1.0.0
    48954993 */
    48964994function bp_group_creation_tabs() {
    4897         $bp = buddypress();
    4898 
    4899         if ( !is_array( $bp->groups->group_creation_steps ) ) {
     4995        $bp           = buddypress();
     4996        $create_steps = $bp->groups->group_creation_steps;
     4997
     4998        if ( ! is_array( $create_steps ) ) {
    49004999                return false;
    49015000        }
    49025001
    4903         if ( !bp_get_groups_current_create_step() ) {
    4904                 $keys = array_keys( $bp->groups->group_creation_steps );
     5002        if ( ! bp_get_groups_current_create_step() ) {
     5003                $keys                            = array_keys( $create_steps );
    49055004                $bp->groups->current_create_step = array_shift( $keys );
    49065005        }
     
    49085007        $counter = 1;
    49095008
    4910         foreach ( (array) $bp->groups->group_creation_steps as $slug => $step ) {
    4911                 $is_enabled = bp_are_previous_group_creation_steps_complete( $slug ); ?>
    4912 
    4913                 <li<?php if ( bp_get_groups_current_create_step() == $slug ) : ?> class="current"<?php endif; ?>><?php if ( $is_enabled ) : ?><a href="<?php bp_groups_directory_permalink(); ?>create/step/<?php echo $slug ?>/"><?php else: ?><span><?php endif; ?><?php echo $counter ?>. <?php echo $step['name'] ?><?php if ( $is_enabled ) : ?></a><?php else: ?></span><?php endif ?></li><?php
     5009        foreach ( (array) $create_steps as $create_step => $step ) {
     5010                $is_enabled    = bp_are_previous_group_creation_steps_complete( $create_step );
     5011                $current_class = '';
     5012                $step_name     = $step['name'];
     5013
     5014                if ( bp_get_groups_current_create_step() === $create_step ) {
     5015                        $current_class = ' class="current"';
     5016                }
     5017
     5018                if ( $is_enabled && isset( $create_steps[ $create_step ]['rewrite_id'], $create_steps[ $create_step ]['default_slug'] ) ) {
     5019                        $create_step_slug = bp_rewrites_get_slug( 'groups', 'bp_group_create_step', 'step' );
     5020                        $step_slug        = bp_rewrites_get_slug( 'groups', $create_steps[ $create_step ]['rewrite_id'], $create_steps[ $create_step ]['default_slug'] );
     5021                        $url              = bp_get_groups_directory_url(
     5022                                array(
     5023                                        'create_single_item'           => 1,
     5024                                        'create_single_item_variables' => array( $create_step_slug, $step_slug ),
     5025                                )
     5026                        );
     5027
     5028                        $step_name = sprintf( '<a href="%1$s">%2$s. %3$s</a>', esc_url( $url ), absint( $counter ), esc_html( $step_name ) );
     5029                } else {
     5030                        $step_name = sprintf( '<span>%1$s. %2$s</span>', absint( $counter ), esc_html( $step_name ) );
     5031                }
     5032
     5033
     5034                printf( '<li%1$s>%2$s</li>', $current_class, $step_name );
    49145035                $counter++;
    4915         }
    4916 
    4917         unset( $is_enabled );
     5036                unset( $is_enabled );
     5037        }
    49185038
    49195039        /**
     
    49545074 */
    49555075        function bp_get_group_creation_form_action() {
    4956                 $bp = buddypress();
    4957 
    4958                 if ( !bp_action_variable( 1 ) ) {
    4959                         $keys = array_keys( $bp->groups->group_creation_steps );
     5076                $bp           = buddypress();
     5077                $create_steps = $bp->groups->group_creation_steps;
     5078                $url          = '';
     5079
     5080                if ( ! bp_action_variable( 1 ) ) {
     5081                        $keys = array_keys( $create_steps );
    49605082                        $bp->action_variables[1] = array_shift( $keys );
    49615083                }
    49625084
     5085                $create_step  = bp_action_variable( 1 );
     5086                if ( $create_step && isset( $create_steps[ $create_step ]['rewrite_id'], $create_steps[ $create_step ]['default_slug'] ) ) {
     5087                        $create_step_slug = bp_rewrites_get_slug( 'groups', 'bp_group_create_step', 'step' );
     5088                        $step_slug        = bp_rewrites_get_slug( 'groups', $create_steps[ $create_step ]['rewrite_id'], $create_steps[ $create_step ]['default_slug'] );
     5089
     5090                        $url = bp_get_groups_directory_url(
     5091                                array(
     5092                                        'create_single_item'           => 1,
     5093                                        'create_single_item_variables' => array( $create_step_slug, $step_slug ),
     5094                                )
     5095                        );
     5096                }
     5097
    49635098                /**
    49645099                 * Filters the group creation form action.
     
    49665101                 * @since 1.1.0
    49675102                 *
    4968                  * @param string $value Action to be used with group creation form.
    4969                  */
    4970                 return apply_filters( 'bp_get_group_creation_form_action', trailingslashit( bp_get_groups_directory_permalink() . 'create/step/' . bp_action_variable( 1 ) ) );
     5103                 * @param string $url Action to be used with group creation form.
     5104                 */
     5105                return apply_filters( 'bp_get_group_creation_form_action', $url );
    49715106        }
    49725107
     
    52985433         */
    52995434        function bp_get_group_creation_previous_link() {
    5300                 $bp    = buddypress();
    5301                 $steps = array_keys( $bp->groups->group_creation_steps );
     5435                $create_steps = buddypress()->groups->group_creation_steps;
     5436                $steps        = array_keys( $create_steps );
     5437                $url          = '';
    53025438
    53035439                // Loop through steps.
     
    53145450
    53155451                // Generate the URL for the previous step.
    5316                 $group_directory = bp_get_groups_directory_permalink();
    5317                 $create_step     = 'create/step/';
    5318                 $previous_step   = array_pop( $previous_steps );
    5319                 $url             = trailingslashit( $group_directory . $create_step . $previous_step );
     5452                $previous_step = array_pop( $previous_steps );
     5453
     5454                if ( isset( $create_steps[ $previous_step ]['rewrite_id'], $create_steps[ $previous_step ]['default_slug'] ) ) {
     5455                        $create_step_slug = bp_rewrites_get_slug( 'groups', 'bp_group_create_step', 'step' );
     5456                        $previous_step    = bp_rewrites_get_slug( 'groups', $create_steps[ $previous_step ]['rewrite_id'], $create_steps[ $previous_step ]['default_slug'] );
     5457
     5458                        $url = bp_get_groups_directory_url(
     5459                                array(
     5460                                        'create_single_item'           => 1,
     5461                                        'create_single_item_variables' => array( $create_step_slug, $previous_step ),
     5462                                )
     5463                        );
     5464                }
    53205465
    53215466                /**
     
    65826727                                $url = bp_get_group_permalink( $current_group ) . $action;
    65836728                        } else {
    6584                                 $url = bp_get_group_permalink( $current_group );
     6729                                $url = bp_get_group_url( $current_group );
    65856730                        }
    65866731
Note: See TracChangeset for help on using the changeset viewer.