Filter uncanny-automator-pro

uap_option_all_h5p_contents

Filters the data for all H5P content options before they are displayed or processed.

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

Description

Filters the array of H5P content options before they are presented in Uncanny Automator. Developers can modify this array to include, exclude, or alter the H5P content available for use in automations. This hook fires when the H5P integration is preparing its content selection for automation triggers or actions.


Usage

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

Parameters

$option (mixed)
This parameter holds the value being filtered, likely an array of H5P content options.

Return Value

The filtered value.


Examples

<?php
/**
 * Example of filtering the 'uap_option_all_h5p_contents' hook to modify H5P content options.
 *
 * This function demonstrates how to add, remove, or modify H5P content items
 * that are available within Uncanny Automator Pro for use in automations.
 *
 * @param array $h5p_contents An array of H5P content items. Each item is typically an array
 *                            with 'label' and 'value' keys.
 *
 * @return array The modified array of H5P content items.
 */
function my_uncanny_automator_filter_h5p_contents( $h5p_contents ) {
    // Example: Let's say we want to remove a specific H5P content item
    // identified by its 'value' (which might be the H5P content ID).
    $h5p_content_to_remove_id = '123'; // Replace with a real H5P content ID you want to exclude

    if ( ! empty( $h5p_contents ) && is_array( $h5p_contents ) ) {
        foreach ( $h5p_contents as $key => $content ) {
            // Ensure the content item has a 'value' key and it's a string before comparison
            if ( isset( $content['value'] ) && is_string( $content['value'] ) ) {
                // The actual value in the original code is usually the ID of the H5P content.
                // We need to check if the value matches the ID we want to remove.
                if ( $content['value'] === $h5p_content_to_remove_id ) {
                    unset( $h5p_contents[ $key ] );
                    // If we've found and removed it, we can break the loop for efficiency.
                    break;
                }
            }
        }
        // Re-index the array after removing an element to maintain sequential keys.
        $h5p_contents = array_values( $h5p_contents );
    }

    // Example: Let's also add a new H5P content item (hypothetically, this would
    // be dynamically generated or selected based on some criteria).
    // In a real scenario, you'd likely fetch this dynamically.
    $new_h5p_content = array(
        'label' => 'My Custom Interactive Quiz (Filtered)',
        'value' => '999', // A hypothetical ID for the new content
    );

    // Add the new content if the array is not empty or initialize it if it is.
    if ( empty( $h5p_contents ) ) {
        $h5p_contents = array();
    }
    $h5p_contents[] = $new_h5p_content;

    // Return the modified array of H5P content options.
    return $h5p_contents;
}
add_filter( 'uap_option_all_h5p_contents', 'my_uncanny_automator_filter_h5p_contents', 10, 1 );

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

uncanny-automator-pro/src/integrations/h5p/helpers/h5p-pro-helpers.php:80

public function all_h5p_contents( $label = null, $option_code = 'H5P_CONTENT', $any_option = true ) {

		if ( ! $label ) {
			$label = __( 'Content', 'uncanny-automator-pro' );
		}

		global $wpdb;
		$options = array();

		if ( Automator()->helpers->recipe->load_helpers ) {
			// Get the library content
			$contents = $wpdb->get_results(
				"SELECT c.id,c.title FROM {$wpdb->prefix}h5p_contents c"
			);

			if ( $any_option ) {
				$options['-1'] = __( 'Any content', 'uncanny-automator-pro' );
			}
			if ( ! empty( $contents ) ) {
				foreach ( $contents as $content ) {
					$options[ $content->id ] = $content->title;
				}
			}
		}
		$option = array(
			'option_code'     => $option_code,
			'label'           => $label,
			'input_type'      => 'select',
			'required'        => true,
			// to setup example, lets define the value the child will be based on
			'current_value'   => false,
			'validation_type' => 'text',
			'options'         => $options,
		);

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

Scroll to Top