Filter
uncanny-automator-pro
uncanny_automator_pro_entity_factory_items
Filters the entity factory class map for automations, allowing customization before entities are generated.
add_filter( 'uncanny_automator_pro_entity_factory_items', $callback, 10, 2 );
Description
Allows developers to modify the entity class mapping for Uncanny Automator Pro loops. This filter provides access to the current class map and loop type, enabling customization of how loop entities are resolved. It's crucial for extending or altering entity behavior within the Automator loop system.
Usage
add_filter( 'uncanny_automator_pro_entity_factory_items', 'your_function_name', 10, 2 );
Parameters
-
$class_map(mixed) - This parameter is an array that maps different loop types to their corresponding entity classes.
-
$loop_type(mixed) - This parameter contains an array that maps loop types to their corresponding entity classes.
Return Value
The filtered value.
Examples
<?php
/**
* Example: Add a custom entity type to the Uncanny Automator Pro entity factory.
*
* This filter allows developers to register new entity types that can be used
* within Uncanny Automator Pro loops. For instance, you might want to add
* support for a custom post type or a specific set of user roles.
*
* @param array $class_map The current mapping of loop types to their corresponding entity classes.
* @param array $loop_context An array containing the current $loop_type and $entities being processed.
*
* @return array The modified class map, potentially including new entity types.
*/
add_filter(
'uncanny_automator_pro_entity_factory_items',
function( $class_map, $loop_context ) {
// Extract loop type and entities from the context for clarity.
list( $loop_type, $entities ) = $loop_context;
// Define a hypothetical custom entity class. Replace 'My_Custom_Entity'
// with the actual namespace and class name of your custom entity.
// Ensure this class extends the appropriate base class expected by Uncanny Automator.
// For demonstration, we'll assume a class 'My_Automator_EntitiesCustomWooProducts' exists.
if ( ! class_exists( 'My_Automator_EntitiesCustomWooProducts' ) ) {
// If the custom class doesn't exist, we can't add it.
// In a real scenario, you'd likely have this class defined elsewhere.
return $class_map;
}
// Add our custom entity type to the class map.
// 'custom_woo_products' is the unique identifier for this loop type.
$class_map['custom_woo_products'] = 'My_Automator_EntitiesCustomWooProducts';
// You can also conditionally add or modify entries based on $loop_type or $entities.
// For example, if you wanted to handle a specific user role differently:
// if ( $loop_type === 'users' && isset( $entities['role'] ) && 'contributor' === $entities['role'] ) {
// // Potentially map to a different entity class for contributors
// }
return $class_map;
},
10, // Priority: 10 is the default, adjust as needed.
2 // Accepted arguments: $class_map and $loop_context.
);
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/loop/entity-factory.php:49
public function make( $loop_type, $entities ) {
// Populate default class map.
$class_map = array(
self::TYPE_POSTS => Posts::class,
self::TYPE_USERS => Users::class,
self::TYPE_TOKEN => Data::class,
);
// Allow plugins to modify the class map dynamically.
$class_map = apply_filters( 'uncanny_automator_pro_entity_factory_items', $class_map, array( $loop_type, $entities ) );
// Ensure the loop type exists in the class map.
if ( ! array_key_exists( $loop_type, $class_map ) ) {
throw new Loops_Exception( 'Invalid loop type detected', 400 );
}
// Get the class name from the class map.
$class = $class_map[ $loop_type ];
$entities = new $class( $entities );
if ( ! ( $entities instanceof Entity_Loopable ) ) {
throw new Loops_Exception( 'Invalid loop entities. Loop entities must be loopable token.', 400 );
}
// Return a new instance of the specified loop type.
return $entities;
}