Filter uncanny-automator

automator_action_tokens_apply_auto_formatting

Filters the value of a token before it's applied to an action, allowing custom formatting.

add_filter( 'automator_action_tokens_apply_auto_formatting', $callback, 10, 6 );

Description

Filters the token value before automatic paragraph formatting is applied. This hook allows developers to modify or prevent the default `wpautop` behavior based on the token's meta key, action log ID, or other contextual arguments, offering fine-grained control over token output formatting.


Usage

add_filter( 'automator_action_tokens_apply_auto_formatting', 'your_function_name', 10, 6 );

Parameters

$token_value (mixed)
This parameter contains the current value of the token that is being processed.
$token_value (mixed)
This parameter contains the raw value of the token that will be automatically formatted.
$action_meta_key (mixed)
This parameter appears to be a duplicate of the first parameter, `$token_value`, and likely represents the same data.
$action_log_id (mixed)
This parameter represents the meta key associated with the action's metadata.
$args (mixed)
This parameter is an array that can be used to pass additional arguments to the function.
$this (mixed)
This parameter refers to the current instance of the Action class being used within the automator service.

Return Value

The filtered value.


Examples

/**
 * Example of how to use the 'automator_action_tokens_apply_auto_formatting' filter.
 * This example checks if the token value contains a specific keyword and, if so,
 * overrides the default paragraph formatting to simply return the original value.
 *
 * @param mixed $token_value       The original token value.
 * @param mixed $original_token_value The token value before any formatting.
 * @param mixed $action_meta_key   The meta key of the action.
 * @param mixed $action_log_id     The ID of the action log.
 * @param mixed $args              Additional arguments passed to the filter.
 * @param mixed $parser_instance   The parser instance itself.
 *
 * @return mixed The potentially modified token value.
 */
add_filter(
	'automator_action_tokens_apply_auto_formatting',
	function ( $token_value, $original_token_value, $action_meta_key, $action_log_id, $args, $parser_instance ) {
		// Check if the token value contains a specific keyword, e.g., 'DO_NOT_FORMAT_THIS'.
		if ( str_contains( (string) $original_token_value, 'DO_NOT_FORMAT_THIS' ) ) {
			// If the keyword is present, bypass the automatic paragraph formatting
			// and return the original token value directly.
			return $original_token_value;
		}

		// Otherwise, allow the default formatting to be applied (which is wpautop in the source).
		// The original $token_value here will be the result of wpautop().
		return $token_value;
	},
	10, // Priority
	6  // Number of arguments accepted
);

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/core/services/recipe/action/token/parser.php:192

private function get_meta_value( $action_log_id = 0, $action_meta_key = '', $args = array(), $process_args = array(), $action_id = null ) {

		$meta_values = Automator()->db->action->get_multiple_meta( $action_log_id, $this->meta_key );

		// Combine the tokens.
		$combined_meta_valued = self::merge_meta_values( $meta_values );

		$action_meta_token = $this->stringify( apply_filters( 'automator_action_tokens_meta_token_value', $combined_meta_valued, $action_id, $process_args ) );

		$tokens = (array) json_decode( $action_meta_token, true );

		$token_value = isset( $tokens[ $action_meta_key ] ) ? $tokens[ $action_meta_key ] : '';

		if ( ! empty( $args ) && isset( $args['action_data']['should_apply_extra_formatting'] ) ) {

			if ( true === $args['action_data']['should_apply_extra_formatting'] ) {

				// Standardize newline characters to "n".
				$token_value = str_replace( array( "rn", "r" ), "n", $token_value );

				// Remove more than two contiguous line breaks.
				$token_value = preg_replace( "/nn+/", "nn", $token_value );

				// Split up the contents into an array of strings, separated by double line breaks.
				$paragraphs = preg_split( '/ns*n/', $token_value, - 1, PREG_SPLIT_NO_EMPTY );

				// Only apply automatic formatting on the value if it's a paragraph.
				if ( count( $paragraphs ) > 1 ) {

					$token_value = apply_filters(
						'automator_action_tokens_apply_auto_formatting',
						wpautop( $token_value ),
						$token_value,
						$action_meta_key,
						$action_log_id,
						$args,
						$this
					);

				}
			}
		}

		return $token_value;
	}


Scroll to Top