Action uncanny-automator

fluent_community/space/join_requested

Fires when a user requests to join a Fluent Community space, passing the space and user ID.

add_action( 'fluent_community/space/join_requested', $callback, 10, 2 );

Description

Fires when a user's request to join a Fluent Community space is initiated, typically by automation. Developers can use this hook to trigger custom actions upon join requests, such as sending notifications or updating external systems, before the user is officially added to the space.


Usage

add_action( 'fluent_community/space/join_requested', 'your_function_name', 10, 2 );

Parameters

$space (mixed)
This parameter contains the Fluent Community space object.
$user_id (mixed)
This parameter contains the ID of the Fluent Community space the user is attempting to join.

Examples

<?php
/**
 * Example function to handle the 'fluent_community/space/join_requested' action.
 * This function demonstrates how to react when a user's join request to a Fluent Community space is initiated by automation.
 *
 * @param object $space     The Fluent Community space object the user is requesting to join.
 * @param int    $user_id   The ID of the user requesting to join the space.
 * @param string $automation_flag A flag indicating if the request was made by automation (expected to be 'by_automation').
 */
function my_fluent_community_handle_join_request_by_automation( $space, $user_id, $automation_flag ) {
    // Ensure the request is indeed coming from automation as expected by the hook.
    if ( 'by_automation' !== $automation_flag ) {
        // If not by automation, do nothing or log a warning, as this hook is specific to automation.
        return;
    }

    // Get the user object for further processing.
    $user = get_user_by( 'id', $user_id );

    if ( ! $user ) {
        // Log an error if the user cannot be found.
        error_log( "Fluent Community: User with ID {$user_id} not found when handling join request for space '{$space->get_name()}'." );
        return;
    }

    // You can perform various actions here, such as:
    // 1. Sending a notification to the space administrator.
    // 2. Logging the join request for auditing purposes.
    // 3. Checking for specific conditions before allowing the automated join (though the hook context suggests it's already a pending request).

    // Example: Log the join request event.
    $log_message = sprintf(
        'Fluent Community: User "%s" (ID: %d) has requested to join space "%s" (ID: %d) via automation.',
        $user->user_login,
        $user_id,
        $space->get_name(), // Assuming $space object has a get_name() method.
        $space->get_id()     // Assuming $space object has a get_id() method.
    );
    error_log( $log_message );

    // If there are specific automated approval rules, they might be checked here.
    // For this example, we are just acknowledging the request initiation.
}

// Hook the function to the 'fluent_community/space/join_requested' action.
// We specify 3 arguments as the hook provides $space, $user_id, and 'by_automation'.
add_action( 'fluent_community/space/join_requested', 'my_fluent_community_handle_join_request_by_automation', 10, 3 );
?>

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:122

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;
	}


Scroll to Top