Skip to content

Commit

Permalink
Make sure filtered Group URLs are using customized slugs
Browse files Browse the repository at this point in the history
  • Loading branch information
imath committed Aug 20, 2021
1 parent 019645a commit da1634e
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/bp-groups/bp-groups-notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,13 @@ function groups_format_notifications() {
*/
function groups_notification_new_membership_request( $args = array() ) {
if ( isset( $args['tokens']['group-requests.url'], $args['tokens']['group'] ) && $args['tokens']['group-requests.url'] && $args['tokens']['group'] ) {
// Get potential customized slugs.
$action = bp_rewrites_get_slug( 'groups', 'bp_group_read_admin', 'admin' );
$action .= '/' . bp_rewrites_get_slug( 'groups', 'bp_group_manage_membership_requests', 'membership-requests' );

$args['tokens']['group-requests.url'] = esc_url_raw(
bp_group_rewrites_get_action_url(
'admin/membership-requests', // Should it be hardcoded?
$action,
$args['tokens']['group']
)
);
Expand Down
20 changes: 19 additions & 1 deletion src/bp-groups/bp-groups-rewrites.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ function bp_group_admin_rewrites_get_form_url( $group = null, $page = '', $rewri
array(
'component_id' => 'groups',
'single_item' => $group->slug,
'single_item_action' => 'admin',
'single_item_action' => bp_rewrites_get_slug( 'groups', 'bp_group_read_admin', 'admin' ),
'single_item_action_variables' => array( $page ),
)
);
Expand Down Expand Up @@ -281,6 +281,24 @@ function bp_groups_rewrites_get_notification_action_url( $group_url = '', $notif
$single_item_action_variables = array_filter( explode( '/', str_replace( $group_url, '', rtrim( $notification_url, '?n=1' ) ) ) );
$single_item_action = array_shift( $single_item_action_variables );

if ( $single_item_action ) {
$views = bp_get_group_views( 'read' );

if ( isset( $views[ $single_item_action ]['rewrite_id'] ) ) {
$view = $views[ $single_item_action ];
$single_item_action = bp_rewrites_get_slug( 'groups', $view['rewrite_id'], $single_item_action );

if ( isset( $single_item_action_variables[0] ) && 'admin' === $view['slug'] ) {
$first_action_variable = $single_item_action_variables[0];
$manage_views = bp_get_group_views( 'manage' );

if ( isset( $manage_views[ $first_action_variable ]['rewrite_id'] ) ) {
$single_item_action_variables[0] = bp_rewrites_get_slug( 'groups', $manage_views[ $first_action_variable ]['rewrite_id'], $first_action_variable );
}
}
}
}

return bp_rewrites_get_url(
array(
'component_id' => 'groups',
Expand Down
56 changes: 50 additions & 6 deletions src/bp-groups/bp-groups-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,18 @@ function bp_get_group_creation_previous_link( $url = '' ) {
* @return string The Group admin form action URL built for the BP Rewrites URL parser.
*/
function bp_group_admin_form_action( $url = '', $group = null ) {
return bp_group_admin_rewrites_get_form_url( $group );
$action = bp_action_variable( 0 );
$rewrite_id = '';

if ( $action ) {
$manage_views = bp_get_group_views( 'manage' );

if ( isset( $manage_views[ $action ]['rewrite_id'] ) ) {
$rewrite_id = $manage_views[ $action ]['rewrite_id'];
}
}

return bp_group_admin_rewrites_get_form_url( $group, $action, $rewrite_id );
}
add_filter( 'bp_group_admin_form_action', __NAMESPACE__ . '\bp_group_admin_form_action', 1, 2 );

Expand All @@ -166,6 +177,11 @@ function bp_group_form_action( $url = '', $group = null ) {
return $url;
}

$views = bp_get_group_views( 'read' );
if ( isset( $views[ $action ]['rewrite_id'] ) ) {
$action = bp_rewrites_get_slug( 'groups', $views[ $action ]['rewrite_id'], $action );
}

return bp_group_rewrites_get_action_url( $action, $group );
}
add_filter( 'bp_group_form_action', __NAMESPACE__ . '\bp_group_form_action', 1, 2 );
Expand All @@ -179,11 +195,31 @@ function bp_group_form_action( $url = '', $group = null ) {
*
* @param string $url URL for a group component action built for the BP Legacy URL parser.
* @param string $action Action being taken for the group.
* @param string $query_args Query arguments being passed.
* @param array $query_args Query arguments being passed.
* @param bool $nonce Whether or not to add a nonce.
* @return string URL for a group component action built for the BP Rewrites URL parser.
*/
function bp_get_groups_action_link( $url, $action, $query_args, $nonce ) {
function bp_get_groups_action_link( $url = '', $action = '', $query_args = array(), $nonce ) {
$action_variables = explode( '/', $action );
$action = array_shift( $action_variables );

$views = bp_get_group_views( 'read' );
if ( isset( $views[ $action ]['rewrite_id'] ) ) {
$view = $views[ $action ];
$action = bp_rewrites_get_slug( 'groups', $view['rewrite_id'], $action );

if ( isset( $action_variables[0] ) && 'admin' === $view['slug'] ) {
$first_action_variable = $action_variables[0];
$manage_views = bp_get_group_views( 'manage' );

if ( isset( $manage_views[ $first_action_variable ]['rewrite_id'] ) ) {
$action_variables[0] = bp_rewrites_get_slug( 'groups', $manage_views[ $first_action_variable ]['rewrite_id'], $first_action_variable );
}

$action .= '/' . implode( '/', $action_variables );
}
}

return bp_group_rewrites_get_action_url( $action, null, $query_args, $nonce );
}
add_filter( 'bp_get_groups_action_link', __NAMESPACE__ . '\bp_get_groups_action_link', 1, 4 );
Expand Down Expand Up @@ -236,7 +272,7 @@ function bp_get_group_join_button( $args = array(), $group = null ) {
);
} elseif ( ! $group->is_invited && ! $group->is_pending ) {
$args['link_href'] = bp_group_rewrites_get_action_url(
'request-membership', // Should it be hardcoded?
bp_rewrites_get_slug( 'groups', 'bp_group_read_request_membership', 'request-membership' ),
$group,
array(),
'groups_request_membership'
Expand Down Expand Up @@ -268,8 +304,12 @@ function bp_get_group_request_accept_link( $url = '' ) {
return $url;
}

// Get potential customized slugs.
$action = bp_rewrites_get_slug( 'groups', 'bp_group_read_admin', 'admin' );
$action .= '/' . bp_rewrites_get_slug( 'groups', 'bp_group_manage_membership_requests', 'membership-requests' );

return bp_group_rewrites_get_action_url(
'admin/membership-requests', // Should it be hardcoded?
$action,
groups_get_current_group(),
array(
'user_id' => $GLOBALS['requests_template']->request->user_id,
Expand All @@ -293,8 +333,12 @@ function bp_get_group_request_reject_link( $url = '' ) {
return $url;
}

// Get potential customized slugs.
$action = bp_rewrites_get_slug( 'groups', 'bp_group_read_admin', 'admin' );
$action .= '/' . bp_rewrites_get_slug( 'groups', 'bp_group_manage_membership_requests', 'membership-requests' );

return bp_group_rewrites_get_action_url(
'admin/membership-requests', // Should it be hardcoded?
$action,
groups_get_current_group(),
array(
'user_id' => $GLOBALS['requests_template']->request->user_id,
Expand Down

0 comments on commit da1634e

Please sign in to comment.