Filter uncanny-automator

uap_option_list_presto_videos

Filters the options list for Presto videos before it's saved or displayed.

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

Description

This filter allows developers to modify the available options for Presto video tokens before they are displayed in Uncanny Automator recipes. Developers can add, remove, or alter the labels of video-related tokens, such as title and ID, providing more granular control over how Presto video data is used in automations.


Usage

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

Parameters

$option (mixed)
This parameter represents the current value of the option being filtered.

Return Value

The filtered value.


Examples

<?php
/**
 * Modify the available tokens for Presto videos to include additional metadata.
 *
 * For example, if the original option code is 'PRESTO_VIDEO_PLAYED',
 * we want to ensure that 'PRESTO_VIDEO_PLAYED_DURATION' is also available.
 *
 * @param array $option The original option array containing relevant tokens.
 * @return array The modified option array with additional tokens.
 */
add_filter( 'uap_option_list_presto_videos', 'my_custom_presto_video_tokens', 10, 1 );

function my_custom_presto_video_tokens( $option ) {
    // Check if $option is an array and has the 'relevant_tokens' key
    if ( ! is_array( $option ) || ! isset( $option['relevant_tokens'] ) || ! is_array( $option['relevant_tokens'] ) ) {
        return $option;
    }

    // Iterate through existing tokens to find video-related ones
    foreach ( $option['relevant_tokens'] as $key => $label ) {
        // Assuming option codes related to videos end with '_ID' or '_POST_TITLE'
        if ( substr( $key, -3 ) === '_ID' || substr( $key, -12 ) === '_POST_TITLE' ) {
            // Extract the base option code without suffix
            $base_option_code = substr( $key, 0, - ( substr( $key, -3 ) === '_ID' ? 3 : 12 ) );

            // Add a new token for video duration if not already present
            $duration_token_key = $base_option_code . '_DURATION';
            if ( ! isset( $option['relevant_tokens'][$duration_token_key] ) ) {
                $option['relevant_tokens'][$duration_token_key] = esc_html_x( 'Video duration', 'Presto', 'uncanny-automator' );
            }

            // Add a new token for video completion percentage if not already present
            $completion_token_key = $base_option_code . '_COMPLETION_PERCENT';
            if ( ! isset( $option['relevant_tokens'][$completion_token_key] ) ) {
                $option['relevant_tokens'][$completion_token_key] = esc_html_x( 'Video completion percentage', 'Presto', 'uncanny-automator' );
            }
        }
    }

    return $option;
}
?>

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/presto/helpers/presto-helpers.php:141

public function list_presto_videos( $label = null, $option_code = 'PRESTOVIDEO', $args = array() ) {

		if ( ! $label ) {
			$label = esc_html_x( 'Video', 'Presto', 'uncanny-automator' );
		}

		$token        = key_exists( 'token', $args ) ? $args['token'] : false;
		$is_ajax      = key_exists( 'is_ajax', $args ) ? $args['is_ajax'] : false;
		$target_field = key_exists( 'target_field', $args ) ? $args['target_field'] : '';
		$end_point    = key_exists( 'endpoint', $args ) ? $args['endpoint'] : '';

		$options = $this->get_all_presto_videos( true );

		$option = array(
			'option_code'     => $option_code,
			'label'           => $label,
			'input_type'      => 'select',
			'required'        => true,
			'supports_tokens' => $token,
			'is_ajax'         => $is_ajax,
			'fill_values_in'  => $target_field,
			'endpoint'        => $end_point,
			'options'         => $options,
			'relevant_tokens' => array(
				$option_code                 => esc_html_x( 'Video title', 'Presto', 'uncanny-automator' ),
				$option_code . '_ID'         => esc_html_x( 'Video ID', 'Presto', 'uncanny-automator' ),
				$option_code . '_POST_TITLE' => esc_html_x( 'Media hub title', 'Presto', 'uncanny-automator' ),
			),
		);

		return apply_filters( 'uap_option_list_presto_videos', $option );
	}

Scroll to Top