Filter
uncanny-automator
automator_external_class_namespace
Filters the namespace for external classes, allowing customization before they are loaded.
add_filter( 'automator_external_class_namespace', $callback, 10, 4 );
Description
Allows developers to modify the default 'Uncanny_Automator' namespace for external integration classes. This filter is applied when a class file doesn't match the core pattern, enabling custom namespace definitions for third-party integrations. Provides the original namespace, class name, file name, and full file path.
Usage
add_filter( 'automator_external_class_namespace', 'your_function_name', 10, 4 );
Parameters
-
$custom_namespace(mixed) - This parameter allows you to define a custom namespace for classes when using the `automator_external_class_namespace` filter.
-
$class_name(mixed) - This parameter holds the custom namespace to be used for the class, if one is defined and applied through a filter.
-
$file_name(mixed) - This parameter contains the resolved class name, derived from the file name.
-
$file(mixed) - This parameter contains the name of the file without its `.php` extension.
Return Value
The filtered value.
Examples
/**
* Example of using the 'automator_external_class_namespace' filter hook.
*
* This filter allows you to modify the namespace used for external classes
* loaded by Uncanny Automator. In this example, we'll prepend a custom
* prefix to the namespace if the class name contains "addon_feature".
* This could be useful for organizing specific types of external integrations.
*/
add_filter( 'automator_external_class_namespace', function( $custom_namespace, $class_name, $file_name, $file ) {
// Check if the class name contains "addon_feature"
if ( strpos( $class_name, 'addon_feature' ) !== false ) {
// Prepend a custom namespace for these specific addon features
return 'My_Custom_Addon_Namespace\' . $custom_namespace;
}
// Otherwise, return the original custom namespace
return $custom_namespace;
}, 10, 4 ); // Priority 10, accepts 4 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
src/core/integration-loader/class-class-resolver.php:79
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 );
}