Action uncanny-automator

fluent_community/space/joined

Fires after a user joins a Fluent Community space, passing the space and user ID.

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

Description

Fires after a user is successfully added to a Fluent Community space, bypassing the request process. Developers can use this hook to trigger custom actions, such as sending notifications, updating user metadata, or integrating with other services based on the space and user ID. The 'by_automation' parameter indicates if the join was automated.


Usage

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

Parameters

$space (mixed)
This parameter represents the Fluent Community space object that the user has joined.
$user_id (mixed)
This parameter contains information about the Fluent Community space that the user has joined.

Examples

<?php

/**
 * Example function to handle the 'fluent_community/space/joined' action.
 * This function will be triggered when a user successfully joins a Fluent Community space,
 * potentially via an automated process.
 *
 * @param object $space The Fluent Community space object that the user joined.
 * @param int    $user_id The ID of the user who joined the space.
 * @param bool   $by_automation A flag indicating if the join was by automation.
 */
function my_fluent_community_space_joined_handler( $space, $user_id, $by_automation ) {
	// Ensure we have valid data before proceeding.
	if ( ! is_object( $space ) || ! isset( $space->ID ) || ! is_numeric( $user_id ) ) {
		error_log( 'Invalid data received for fluent_community/space/joined hook.' );
		return;
	}

	// Example: Log the user joining the space.
	$user_info = get_userdata( $user_id );
	$user_display_name = $user_info ? $user_info->display_name : 'User ID: ' . $user_id;

	$message = sprintf(
		__( 'User "%s" has successfully joined Fluent Community space "%s" (ID: %d).', 'your-text-domain' ),
		$user_display_name,
		$space->title, // Assuming $space object has a 'title' property.
		$space->ID
	);

	if ( $by_automation ) {
		$message .= __( ' This was an automated join.', 'your-text-domain' );
	}

	// You could also perform other actions here, such as:
	// - Sending a notification to the user.
	// - Updating user meta.
	// - Triggering other integrations.
	error_log( $message );

	// If this were a filter hook, you would return the modified data.
	// For example: return $new_data;
}

// Hook the function to the 'fluent_community/space/joined' action.
// We specify 3 arguments to ensure our function receives all parameters.
add_action( 'fluent_community/space/joined', 'my_fluent_community_space_joined_handler', 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:125

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