Filter
uncanny-automator
fluent_community/space/join_status_for_private
Filters the join status for private spaces, allowing modification before user access is determined.
add_filter( 'fluent_community/space/join_status_for_private', $callback, 10, 2 );
Description
Filters the initial join status for users joining private Fluent Community spaces. Developers can modify the default 'pending' status, for example, to automatically activate certain users. This hook is applied when a user lacks community roles and attempts to join a private space.
Usage
add_filter( 'fluent_community/space/join_status_for_private', 'your_function_name', 10, 2 );
Parameters
-
$space(mixed) - This parameter represents the initial join status, typically 'pending', for a user attempting to join a private space.
-
$user(mixed) - This parameter contains the Space object that the user is attempting to join.
Return Value
The filtered value.
Examples
/**
* Customize the join status for users joining private spaces.
*
* This filter allows you to change the default 'pending' status for users
* joining private spaces, for example, to automatically activate users
* who meet certain criteria (e.g., have a specific role in the site).
*
* @param string $status The current join status, defaults to 'pending'.
* @param object $space The community space object.
* @param object $user The user object.
* @return string The new join status ('active' or 'pending').
*/
add_filter(
'fluent_community/space/join_status_for_private',
function ( $status, $space, $user ) {
// Automatically activate users who have the 'administrator' role on the WordPress site.
if ( user_can( $user->ID, 'administrator' ) ) {
return 'active';
}
// Otherwise, keep the default 'pending' status.
return $status;
},
10, // Priority
3 // Accepted arguments: $status, $space, $user
);
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/integrations/fluent-community/actions/fluentcommunity-add-user-to-space.php:98
protected function process_action( $user_id, $action_data, $recipe_id, $args, $parsed ) {
$space_id = absint( $parsed[ $this->get_action_meta() ] );
if ( ! $space_id || ! $user_id ) {
throw new Exception( esc_html_x( 'Missing space ID or user ID.', 'FluentCommunity', 'uncanny-automator' ) );
}
$space = Space::where( 'status', 'published' )->find( $space_id );
if ( ! $space ) {
throw new Exception( esc_html_x( 'The specified space does not exist.', 'FluentCommunity', 'uncanny-automator' ) );
}
$membership = $space->getMembership( $user_id );
if ( $membership ) {
throw new Exception( esc_html_x( 'The user is already a member of this space.', 'FluentCommunity', 'uncanny-automator' ) );
}
$user = User::find( $user_id );
if ( ! $user ) {
throw new Exception( esc_html_x( 'User not found.', 'FluentCommunity', 'uncanny-automator' ) );
}
$roles = $user->getCommunityRoles();
$status = 'active';
if ( ! $roles && 'public' !== $space->privacy ) {
// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
$status = apply_filters( 'fluent_community/space/join_status_for_private', 'pending', $space, $user );
if ( ! in_array( $status, array( 'active', 'pending' ), true ) ) {
$status = 'pending';
}
}
$role = 'member';
if ( $roles ) {
$role = $user->isCommunityAdmin() ? 'admin' : 'moderator';
}
// Attach user to the space
$space->members()->attach(
$user_id,
array(
'role' => $role,
'status' => $status,
)
);
$user->cacheAccessSpaces();
if ( 'pending' === $status ) {
// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
do_action( 'fluent_community/space/join_requested', $space, $user_id, 'by_automation' );
} else {
// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
do_action( 'fluent_community/space/joined', $space, $user_id, 'by_automation' );
}
return true;
}