Filter uncanny-automator

uap_option_get_all_wpmd_files

Filters the options for retrieving all WordPress Download Manager files, allowing modification before they are returned.

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

Description

Filters the array of available WordPress Download Manager files. Developers can use this hook to modify the list of files presented in Uncanny Automator actions and triggers, allowing custom filtering or adding external file sources. The `$option` parameter contains the array of files to be filtered.


Usage

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

Parameters

$option (mixed)
This parameter represents the retrieved option code which is used to fetch specific files from the WP Download Manager.

Return Value

The filtered value.


Examples

<?php
/**
 * Example of using the uap_option_get_all_wpmd_files filter hook.
 * This example adds a custom option to the list of available WP Download Manager files,
 * for instance, to include a specific category or a file that might not be automatically listed.
 */
add_filter( 'uap_option_get_all_wpmd_files', 'my_custom_wpmd_files_option', 10, 1 );

function my_custom_wpmd_files_option( $options ) {
	// Assuming $options is an array of existing WP Download Manager files.
	// We'll add a hypothetical 'custom-file-id' with a 'Custom File' label.

	// First, let's check if $options is actually an array and has expected structure.
	if ( ! is_array( $options ) ) {
		// If not an array, return it as is to avoid errors.
		return $options;
	}

	// Add a custom file option. In a real scenario, you might query WP_Query
	// or use a custom method to get specific files.
	$options['custom-file-id'] = array(
		'label' => __( 'My Special Downloadable File', 'uncanny-automator' ),
		// Other properties might be needed depending on how Uncanny Automator uses this data.
		// For this example, we'll just add the label.
	);

	// You could also filter out existing options based on certain criteria.
	// For example, to remove all files that are not in a specific category:
	/*
	$filtered_options = [];
	foreach ( $options as $file_id => $file_data ) {
		// Replace 'your-category-slug' with the actual category slug you want to keep.
		// You'd need a way to retrieve the category for each file, which might involve
		// another WP_Query or a function like get_the_terms().
		$categories = get_the_terms( $file_id, 'wpdmcategory' ); // Assuming 'wpdmcategory' is the taxonomy slug.
		if ( $categories && in_array( 'your-category-slug', wp_list_pluck( $categories, 'slug' ) ) ) {
			$filtered_options[ $file_id ] = $file_data;
		}
	}
	return $filtered_options;
	*/

	// Return the modified options array.
	return $options;
}
?>

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/wp-download-manager/helpers/wp-download-manager-helpers.php:55

public function get_all_wpmd_files( $option_code, $is_any = false, $is_all = false ) {

		$options = array();

		if ( true === $is_any ) {
			$options['-1'] = esc_html__( 'Any file', 'uncanny-automator' );
		}

		if ( true === $is_all ) {
			$options['-1'] = esc_html__( 'All files', 'uncanny-automator' );
		}
		$args  = array(
			'post_type'      => 'wpdmpro',
			'posts_per_page' => 9999,
			'orderby'        => 'title',
			'order'          => 'ASC',
			'post_status'    => 'publish',
		);
		$files = get_posts( $args );
		if ( ! is_wp_error( $files ) ) {
			if ( ! empty( $files ) ) {
				foreach ( $files as $file ) {
					$options[ $file->ID ] = esc_html( $file->post_title );
				}
			}
		}

		$option = array(
			'input_type'            => 'select',
			'option_code'           => $option_code,
			/* translators: HTTP request method */
			'label'                 => esc_attr__( 'File', 'uncanny-automator' ),
			'required'              => true,
			'supports_custom_value' => false,
			'relevant_tokens'       => array(),
			'options'               => $options,
		);

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

Scroll to Top