Filter uncanny-automator-pro

uncanny_automator_pro_loop_filter_class

Filters the class mapping used for entity filtering within Uncanny Automator loops to customize how entities are processed.

add_filter( 'uncanny_automator_pro_loop_filter_class', $callback, 10, 2 );

Description

Allows developers to modify the class mapping used for Uncanny Automator Pro loop filters. This filter intercepts the default class name and lets you provide your own custom class, enabling advanced customization of loop filter behavior.


Usage

add_filter( 'uncanny_automator_pro_loop_filter_class', 'your_function_name', 10, 2 );

Parameters

$default_class_mapping (mixed)
This parameter provides a default mapping of entity filter names to their corresponding class names.
$entity_filter (mixed)
This parameter contains the default class mapping for the current entity filter, used to determine the full path of the filter class.

Return Value

The filtered value.


Examples

<?php
/**
 * Example of how to use the uncanny_automator_pro_loop_filter_class filter hook.
 *
 * This filter allows you to change the PHP class used for a specific loop filter.
 * For instance, you might want to use a custom class for a particular filter
 * to add custom logic or override existing behavior.
 */
add_filter(
	'uncanny_automator_pro_loop_filter_class',
	function ( $default_class_mapping, $entity_filter_data ) {
		// $default_class_mapping is the default class name determined by Uncanny Automator Pro.
		// $entity_filter_data is an array containing details about the current filter being processed.
		// Example: $entity_filter_data = ['filter' => 'Post', 'filter_id' => 1, 'entity_filters' => [...]]

		// Check if the filter being processed is for 'Post' entities and if it's a specific filter_id.
		if ( isset( $entity_filter_data['filter'] ) && 'Post' === $entity_filter_data['filter'] &&
			isset( $entity_filter_data['filter_id'] ) && 1 === $entity_filter_data['filter_id'] ) {

			// If it's our target filter, replace the default class with a custom one.
			// Ensure your custom class `My_Custom_Post_Loop_Filter` exists and is autoloaded.
			return 'Your_Plugin_Namespace\Loop_Filters\My_Custom_Post_Loop_Filter';
		}

		// For all other filters, return the default class mapping provided by Uncanny Automator Pro.
		return $default_class_mapping;
	},
	10, // Priority: Default priority is 10.
	2   // Accepted args: The callback function accepts 2 arguments.
);

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/core/loops/filter/model/active-record/entity-filter-record.php:119

public function get_entities() {

		$loop_entity_filters = $this->find_all();

		$entity_list = array();

		foreach ( $loop_entity_filters as $filter_id => $entity_filter ) {

			$default_class_mapping = 'Uncanny_Automator_Pro\Loop_Filters\' . $entity_filter;

			$filter_class = apply_filters(
				'uncanny_automator_pro_loop_filter_class',
				$default_class_mapping,
				array(
					'filter'         => $entity_filter,
					'filter_id'      => $filter_id,
					'entity_filters' => $loop_entity_filters,
				)
			);

			$base_class = 'Uncanny_Automator_Pro\Loops\Filter\Base\Loop_Filter';

			if ( is_subclass_of( $filter_class, $base_class ) ) {

				$loop_id = $this->get_loop_id();

				$filter = new $filter_class( $filter_id, $this->run_args, $loop_id );

				$filter->set_run_args( $this->run_args );
				$filter->set_loop_id( $loop_id );

				$entities_from_filter = $filter->get_entities();

				if ( ! is_wp_error( $entities_from_filter ) ) {
					$entity_list[] = $entities_from_filter;
				}
			}
		}

		if ( empty( $entity_list ) ) {
			return array();
		}

		// Get the intersection of the array to find the entities that are common in two or more filter conditions.
		if ( count( $entity_list ) >= 2 ) {
			if ( Entity_Factory::TYPE_TOKEN === $this->get_loop_type() ) {
				// Return the associative intersection.
				return $this->get_loopable_token_intersection( $entity_list );
			}
			// Return the numeric intersection.
			return $this->get_intersection( $entity_list );
		}

		// Just shift the array if there is only 1 filter condition.
		$entity_list = array_shift( $entity_list );

		return $entity_list;

	}

Scroll to Top