Filter
uncanny-automator
automator_recipe_parts_class_name
Filters the class name for recipe parts before they are loaded, allowing modification of the class used.
add_filter( 'automator_recipe_parts_class_name', $callback, 10, 2 );
Description
Filters the resolved class name for a recipe part. Modify this hook to change how the class name is generated, allowing for custom naming conventions or dynamic class resolution before a recipe part is loaded.
Usage
add_filter( 'automator_recipe_parts_class_name', 'your_function_name', 10, 2 );
Parameters
-
$this(mixed) - This parameter contains the resolved class name for a recipe part file.
-
$file(mixed) - This parameter holds the dynamically generated class name for a recipe part, which can be modified by the 'automator_recipe_parts_class_name' filter.
Return Value
The filtered value.
Examples
/**
* Filters the class name for recipe parts to add a prefix for specific integrations.
*
* For example, if a recipe part is intended for the "WooCommerce" integration,
* this filter might prepend "WC_" to the class name to avoid potential conflicts
* with parts from other integrations.
*
* @param string $class The resolved class name for the recipe part.
* @param string $file The file path of the recipe part.
* @return string The modified class name.
*/
add_filter( 'automator_recipe_parts_class_name', function( $class, $file ) {
// Assuming $file contains the integration code as part of its path,
// e.g., '/path/to/wp-content/plugins/my-plugin/automator-parts/integrations/woocommerce/triggers/new-order.php'
// We want to extract 'woocommerce' from this path.
$integration_code = '';
$path_parts = explode( DIRECTORY_SEPARATOR, $file );
// Look for a directory named 'integrations' and then get the next segment as the integration code.
$integrations_key = array_search( 'integrations', $path_parts );
if ( $integrations_key !== false && isset( $path_parts[ $integrations_key + 1 ] ) ) {
$integration_code = $path_parts[ $integrations_key + 1 ];
}
// If a specific integration code is found, prepend it to the class name.
if ( ! empty( $integration_code ) ) {
// Capitalize the first letter of the integration code for a cleaner prefix.
$prefix = ucfirst( $integration_code ) . '_';
// Check if the class name already starts with the intended prefix to avoid double-prefixing.
if ( strpos( $class, $prefix ) !== 0 ) {
$class = $prefix . $class;
}
}
return $class;
}, 10, 2 );
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/core/integration-loader/class-recipe-part-loader.php:261
private function load_single_gated_file( $file, $dir_name, $integration_code, $manifest, $load_all ) {
if ( is_array( $file ) || ! is_file( $file ) ) {
return;
}
$class = apply_filters(
'automator_recipe_parts_class_name',
$this->resolver->resolve( $file, true, $dir_name ),
$file
);
// Loop-filter classes need a Loop_Filters sub-namespace to avoid collisions.
if ( false !== strpos( $file, 'loop-filter' ) ) {
$class = Class_Resolver::prepend_loop_filter_namespace( $class );
}
// In targeted mode: skip files whose class isn't in the active item map.
if ( ! $load_all && ! self::is_class_in_active_map( $class, $manifest, $integration_code ) ) {
return;
}
// Check Utilities tracker, not class_exists.
if ( false !== Utilities::get_class_instance( $class ) ) {
return;
}
include_once $file;
try {
Utilities::add_class_instance( $class, new $class() );
} catch ( Throwable $e ) {
$this->error_handler->handle( $class, $e );
}
}