Filter uncanny-automator

automator_learndash_user_quiz_questions_and_answers_unformatted_token_escape_char

Filters the escape character used for unformatted quiz questions and answers in LearnDash automations.

add_filter( 'automator_learndash_user_quiz_questions_and_answers_unformatted_token_escape_char', $callback, 10, 1 );

Description

Filters the escape character used when formatting LearnDash quiz questions and answers into a CSV string for automation tokens. Developers can modify this character to customize how special characters within quiz data are escaped, ensuring proper parsing in various automation scenarios.


Usage

add_filter( 'automator_learndash_user_quiz_questions_and_answers_unformatted_token_escape_char', 'your_function_name', 10, 1 );

Return Value

The filtered value.


Examples

add_filter( 'automator_learndash_user_quiz_questions_and_answers_unformatted_token_escape_char', 'my_custom_learndash_escape_char', 10, 1 );

/**
 * Custom function to change the escape character for LearnDash quiz data.
 *
 * This filter allows users to override the default escape character used
 * when formatting quiz questions and answers for use in automator tokens.
 * For example, if your data might contain backslashes, you might want to
 * use a different escape character to avoid conflicts.
 *
 * @param string $escape_char The current escape character.
 * @return string The modified escape character.
 */
function my_custom_learndash_escape_char( $escape_char ) {
    // Let's say we want to use a pipe symbol '|' as our escape character
    // instead of the default backslash ''.
    $new_escape_char = '|';

    return $new_escape_char;
}

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/learndash/tokens/ld-tokens.php:1193

public function format_user_quiz_questions_and_answers_csv( $data ) {
		if ( empty( $data ) ) {
			return '';
		}

		// Format Data array.
		$array = array();
		foreach ( $data as $line ) {
			// Flatten Answers array to string.
			$answer = '';
			if ( ! empty( $line['answer'] ) ) {
				if ( count( $line['answer'] ) > 1 ) {
					$answer .= implode( ', ', $line['answer'] );
				} else {
					$answer .= $line['answer'][0];
				}
			}
			// Remove all HTML and add to array.
			$array[] = array(
				'question' => wp_strip_all_tags( $line['question'] ),
				'answer'   => wp_strip_all_tags( $answer ),
			);
		}

		$delimiter   = apply_filters(
			'automator_learndash_user_quiz_questions_and_answers_unformatted_token_delimiter',
			','
		);
		$enclosure   = apply_filters(
			'automator_learndash_user_quiz_questions_and_answers_unformatted_token_enclosure',
			'"'
		);
		$escape_char = apply_filters(
			'automator_learndash_user_quiz_questions_and_answers_unformatted_token_escape_char',
			'\'
		);

		return Utilities::array_to_csv( $array, $delimiter, $enclosure, $escape_char );
	}


Scroll to Top