Filter uncanny-automator

automator_clickup_update_action_read_from_repeater

Filters data when updating ClickUp actions, allowing modification of repeater field values before they are saved.

add_filter( 'automator_clickup_update_action_read_from_repeater', $callback, 10, 4 );

Description

Filters data read from a repeater field for ClickUp task updates. Use this to modify or override the default repeater value before it's applied to the task, ensuring accurate data handling based on your specific needs. The hook fires after a repeater value is identified for update.


Usage

add_filter( 'automator_clickup_update_action_read_from_repeater', 'your_function_name', 10, 4 );

Parameters

$parsed (mixed)
This parameter is reserved and not used by the function.
$key (mixed)
This parameter holds the parsed data, likely an array, from which the repeater field value is extracted.
$cb (mixed)
This parameter is an empty string and appears to be unused.
$this (mixed)
This parameter represents the instance of the class where the `read_from_repeater` method is being called.

Return Value

The filtered value.


Examples

/**
 * Example: Modify the value read from a ClickUp repeater field before it's updated.
 *
 * This filter allows developers to intercept and modify the data extracted
 * from a repeater field within the ClickUp action for updating tasks.
 * For instance, you might want to format a date, concatenate strings,
 * or perform some validation before the data is used.
 *
 * @param null $null             Placeholder, currently unused by the hook.
 * @param array $parsed         The parsed repeater data.
 * @param string $key           The current key of the repeater field being processed.
 * @param string $empty_string  Placeholder, currently unused by the hook.
 * @param callable $cb          The callback function associated with the action.
 * @param object $this          The instance of the action class.
 *
 * @return mixed The modified or original value to be used for the update.
 */
add_filter( 'automator_clickup_update_action_read_from_repeater', function ( $null, $parsed, $key, $empty_string, $cb, $this_instance ) {

	// Example logic: If the key is 'due_date' and the value is a string,
	// attempt to reformat it to a more specific date format.
	// In a real scenario, you would have more robust parsing and validation.
	if ( $key === 'due_date' && is_string( $parsed[ $key ] ) ) {
		$original_date_string = $parsed[ $key ];

		// Attempt to parse the date string. This is a very basic example.
		// In a real application, you'd likely use DateTime::createFromFormat or a library.
		$timestamp = strtotime( $original_date_string );

		if ( $timestamp !== false ) {
			// Reformat to YYYY-MM-DD format.
			$formatted_date = date( 'Y-m-d', $timestamp );
			// Update the value to be used.
			$parsed[ $key ] = $formatted_date;
		}
	}

	// If the key is 'assignee_email', and we are working with a specific callback
	// that expects an array of user IDs, we might want to fetch and return IDs.
	// This is highly dependent on the 'cb' and '$this_instance' context.
	if ( $key === 'assignee_email' && is_callable( $cb ) && method_exists( $this_instance, 'get_user_id_from_email' ) ) {
		$email = $parsed[ $key ];
		if ( is_string( $email ) && filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
			$user_id = $this_instance->get_user_id_from_email( $email );
			if ( $user_id ) {
				// Assuming the action expects an array of user IDs.
				$parsed[ $key ] = [ $user_id ];
			} else {
				// Handle case where email doesn't map to a user.
				unset( $parsed[ $key ] ); // Remove if not found.
			}
		}
	}

	// Always return the potentially modified value.
	return $parsed[ $key ] ?? '';

}, 10, 6 );

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/clickup/actions/space-list-task-update.php:201

private function read_from_repeater( $parsed, $key, $cb = 'sanitize_text_field' ) {
		$repeater_key = $key . '_REPEATER';

		if ( ! isset( $parsed[ $repeater_key ] ) ) {
			return null;
		}

		// Convert br to nl.
		$field_value = str_replace( array( '<br>', '<br/>', '<br />' ), PHP_EOL, $parsed[ $repeater_key ] );

		$restore_slashes = addcslashes( $cb( $field_value ), PHP_EOL );
		$repeater_fields = (array) json_decode( $restore_slashes, true );
		$repeater_fields = end( $repeater_fields );

		if ( isset( $repeater_fields[ $key . '_UPDATE' ] ) && true === $repeater_fields[ $key . '_UPDATE' ] ) {
			return $repeater_fields[ $key ] ?? '';
		}

		return apply_filters( 'automator_clickup_update_action_read_from_repeater', null, $parsed, $key, '', $cb, $this );
	}

Scroll to Top