Filter
uncanny-automator
automator_external_class_with_namespace
Filters the class name with namespace when an external class is used.
add_filter( 'automator_external_class_with_namespace', $callback, 10, 4 );
Description
Filters the generated class namespace for external integrations. Developers can use this to override the default 'Uncanny_Automator' namespace and define their own custom namespace based on the class name, file name, and file path, enabling custom integration class resolution.
Usage
add_filter( 'automator_external_class_with_namespace', 'your_function_name', 10, 4 );
Parameters
-
$custom_namespace(mixed) - This parameter contains the custom namespace string for the class.
-
$class_name(mixed) - This parameter contains the custom namespace for the class, if one is defined and applicable.
-
$file_name(mixed) - This parameter contains the derived class name from the filename, potentially converted to uppercase.
-
$file(mixed) - This parameter contains the name of the file being processed, excluding its extension.
Return Value
The filtered value.
Examples
<?php
/**
* Example of how to use the automator_external_class_with_namespace filter.
*
* This filter allows you to modify the fully qualified class name (namespace + class name)
* for external integration classes loaded by Uncanny Automator.
*
* In this example, we'll prepend a custom prefix to the namespace if a specific
* integration is detected, making it easier to manage custom integrations.
*
* @param string $class_name The full class name, including the determined namespace.
* @param string $original_class_name The original class name without any namespace.
* @param string $file_name The name of the file containing the class.
* @param string $file The full path to the file containing the class.
* @return string The modified fully qualified class name.
*/
add_filter(
'automator_external_class_with_namespace',
function ( $class_name, $original_class_name, $file_name, $file ) {
// Define a specific integration name you want to target.
$target_integration = 'my_custom_plugin';
// Extract the integration name from the file path if it's not directly available.
// This is a simplified example; a more robust solution might involve
// parsing the file path more carefully or relying on other data if available.
$integration_name_from_file = '';
if ( strpos( $file_name, 'integrations/' . $target_integration ) !== false ) {
$integration_name_from_file = $target_integration;
}
// If the target integration is found, prepend a custom namespace.
if ( $integration_name_from_file === $target_integration ) {
$custom_prefix = 'My_Custom_Integrations_Namespace\';
// Ensure we don't double-prefix if a namespace already exists and matches our prefix.
if ( strpos( $class_name, $custom_prefix ) !== 0 ) {
$class_name = $custom_prefix . $class_name;
}
}
// You can also add logic here to modify class names based on the file name or other parameters.
// For example, if you wanted to rename a class for a specific file:
// if ( 'MySpecificFile.php' === basename( $file ) ) {
// $class_name = 'Uncanny_Automator\Renamed_Class_For_Specific_File';
// }
return $class_name;
},
10, // Priority: standard priority
4 // Accepted args: $class_name, $original_class_name, $file_name, $file
);
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-class-resolver.php:80
public function resolve( $file, $uppercase = false, $integration_name = '' ) {
$file_name = basename( $file, '.php' );
$class_name = self::file_name_to_class( $file_name );
if ( $uppercase ) {
$class_name = strtoupper( $class_name );
}
// Check if it's an internal Automator file.
$esc_characters = apply_filters( 'automator_esc_with_slash_characters', '/-:\()_,.' );
$pattern = '/(' . addcslashes( $this->integrations_directory_path, $esc_characters ) . ')/';
if ( preg_match( $pattern, $file ) ) {
$class_name = 'Uncanny_Automator\' . $class_name;
} else {
$custom_namespace = isset( Set_Up_Automator::$external_integrations_namespace[ $integration_name ] )
? Set_Up_Automator::$external_integrations_namespace[ $integration_name ]
: '';
$custom_namespace = apply_filters( 'automator_external_class_namespace', $custom_namespace, $class_name, $file_name, $file );
$class_name = apply_filters( 'automator_external_class_with_namespace', $custom_namespace . '\' . $class_name, $class_name, $file_name, $file );
if ( self::validate_namespace( $class_name, $file_name, $file, $integration_name ) ) {
return apply_filters( 'automator_recipes_class_name', $class_name, $file, $file_name );
}
}
return apply_filters( 'automator_recipes_class_name', $class_name, $file, $file_name );
}