Skip to content

Commit

Permalink
AJAX-friendly technique for detecting "current" group on BP 12+.
Browse files Browse the repository at this point in the history
See #748.
  • Loading branch information
boonebgorges committed Mar 5, 2024
1 parent 85974a2 commit e0b3c54
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
8 changes: 6 additions & 2 deletions includes/addon-folders.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ function bp_docs_enable_folders() {
*/
function bp_docs_enable_folders_for_current_context() {
// Enabled only in groups by default. Enable elsewhere only at your own risk.
$enable = function_exists( 'bp_is_group' ) && bp_is_group();
$enable = (bool) bp_docs_get_current_group_id();

return apply_filters( 'bp_docs_enable_folders_for_current_context', (bool) $enable );
}

Expand Down Expand Up @@ -750,8 +751,11 @@ function bp_docs_delete_folder_contents( $folder_id ) {
*/
function bp_docs_get_folders( $args = array() ) {
$group_id = null;
if ( bp_is_active( 'groups' ) && bp_is_group() ) {
if ( bp_is_active( 'groups' ) ) {
$group_id = bp_get_current_group_id();
if ( ! $group_id && defined( 'DOING_AJAX' ) && DOING_AJAX ) {
$group_id = bp_get_group_id( bp_docs_get_current_group_id() );

This comment has been minimized.

Copy link
@boonebgorges

boonebgorges Mar 5, 2024

Author Owner

This is a typo and should just be $group_id = bp_docs_get_current_group_id()

}
}

$user_id = null;
Expand Down
49 changes: 49 additions & 0 deletions includes/integration-groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -2028,3 +2028,52 @@ function bp_docs_groups_map_meta_caps( $caps, $cap, $user_id, $args ) {
return $caps;
}
add_filter( 'bp_docs_map_meta_caps', 'bp_docs_groups_map_meta_caps', 10, 4 );

/**
* AJAX-friendly function to get the ID of the current group.
*
* @since 2.2.3
*
* @return bool
*/
function bp_docs_get_current_group_id() {
if ( ! function_exists( 'bp_get_current_group_id' ) ) {
return false;
}

$group_id = bp_get_current_group_id();

if ( ! $group_id && defined( 'DOING_AJAX' ) && DOING_AJAX && isset( buddypress()->groups->rewrite_ids['single_item'] ) ) {

// Identify single-group rewrite rules.
$group_rewrite_rules = [];
$all_rewrite_rules = $GLOBALS['wp_rewrite']->wp_rewrite_rules();
$group_rewrite_id = buddypress()->groups->rewrite_ids['single_item'];
foreach ( $all_rewrite_rules as $regex => $query ) {
$query_string = parse_url( $query, PHP_URL_QUERY );
if ( $query_string ) {
parse_str( $query_string, $query_args );
if ( $query_args && isset( $query_args[ $group_rewrite_id ] ) ) {
$group_rewrite_rules[ $regex ] = $query_args;
}
}
}

$referer = wp_get_referer();

// Figure out the requested group based on the referer.
foreach ( $group_rewrite_rules as $regex => $query_args ) {
if ( preg_match( '|' . $regex . '|', $referer, $matches ) ) {
$group_query_arg = $query_args[ $group_rewrite_id ];
if ( preg_match( '/\[(\d+)\]/', $group_query_arg, $id_matches ) ) {
$match_id = (int) $id_matches[1];
$group_slug = $matches[ $match_id ];
$group_id = groups_get_id( $group_slug );
break;
}
}
}
}

return $group_id;
}

0 comments on commit e0b3c54

Please sign in to comment.