Filter uncanny-automator

automator_integration_icon_base_path_legacy

Filters the base path for integration icons, allowing modification before they are loaded.

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

Description

Allows modification of the base directory path for integration icons. Developers can alter the default file system path WordPress uses to locate integration icons, especially for legacy paths, by filtering `$plugin_path`. This is useful for custom integration setups or when icons are stored in non-standard locations.


Usage

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

Parameters

$plugin_path (mixed)
This parameter contains the base path of the plugin where the integration icon is located.
$file_name (mixed)
This parameter is the base path of the plugin where the integration icon is located.

Return Value

The filtered value.


Examples

// Modify the base path for integration icons if the plugin path is not what we expect.
// This filter is primarily for legacy support and might adjust the directory where icons are searched.
add_filter( 'automator_integration_icon_base_path_legacy', function ( $base_path, $file_name ) {
	// Example: If the integration is part of a specific sub-plugin or theme, adjust the base path.
	// Let's assume we want to look for icons in a specific sub-directory within the plugins folder
	// for integrations that are not standard plugins.

	// For this example, let's say we have a custom plugin structure or a different directory
	// where we've placed our integration icons that differs from the standard WP_CONTENT_DIR/plugins/
	// For instance, perhaps we have icons in wp-content/custom-integrations/icons/
	// This is a hypothetical scenario for demonstration.

	// In a real-world scenario, you'd likely inspect $file_name or perhaps have access
	// to other global variables or custom configurations to make a more informed decision.

	// Let's pretend $file_name contains a prefix indicating a special integration.
	if ( strpos( $file_name, 'special_integration_' ) === 0 ) {
		// If it's a special integration, look in a different directory.
		// We'll construct a path relative to the WordPress root.
		// Note: plugins_url() needs a file path relative to the WordPress install.
		// The second parameter to plugins_url() tells it where to find the base of the plugin.
		// Here, we're trying to override the default plugin path logic.
		// The original code uses WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . $plugin_path.
		// We'll try to return a path that can be used by plugins_url to resolve correctly.

		// A more robust approach might involve returning a file path relative to the main plugin file.
		// For simplicity in this example, we'll try to construct a valid absolute path that plugins_url can interpret.
		// However, plugins_url's second argument is typically a file path that includes the file itself or a file in the plugin's root.
		// If $base_path is already set to WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . $plugin_path,
		// and we want to change the *base* from which the $path is appended by plugins_url,
		// we need to provide a file path that represents the "root" of where our icons are.

		// A common pattern is to use __FILE__ of the main plugin file.
		// If this filter is applied within a plugin, and we want to use that plugin's directory as the base:
		// return dirname( __FILE__ ) . '/some/custom/icon/directory/'; // This would be an absolute path.

		// However, the original code implies a path modification relative to the WordPress install.
		// Let's assume $plugin_path is the relative path to the plugin folder.
		// If we need to specify a different base for icons, we'd typically point to a file within that base.
		// For example, if icons are in wp-content/custom-plugins/special-plugin/icons/
		// we might return a path like '/wp-content/custom-plugins/special-plugin/icons/' if that's what plugins_url expects.
		// But plugins_url expects a path relative to the plugin file provided as the second argument.

		// Let's reinterpret the original intent: modify the DIRECTORY_SEPARATOR part.
		// If the default is WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . $plugin_path,
		// and we want to override it, we need to provide a new $base_path that plugins_url understands.
		// The second parameter of plugins_url is the file path to be used as a base.

		// Let's consider a scenario where icons are in the theme.
		// This would be quite unusual for a plugin integration icon, but for illustrative purposes:
		// if ( current_theme_supports( 'integration-icons' ) ) {
		//    return get_template_directory(); // Or a specific sub-directory within the theme.
		// }

		// Given the original code uses WP_CONTENT_DIR, and $plugin_path is likely a relative plugin slug.
		// The fallback path is 'src/recipe-ui/dist/media/integrations/' . $file_name.
		// The base_path is WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . $plugin_path.
		// plugins_url( $path, $base_path ) will then generate a URL.

		// Let's try to simulate a different plugin directory structure for testing.
		// Suppose $plugin_path is 'my-plugin/my-plugin.php' and we want to look for icons in 'my-plugin/assets/icons/'
		// and the default $base_path is WP_CONTENT_DIR . '/plugins/my-plugin/my-plugin.php'
		// We want to modify the base path that plugins_url uses.

		// A common scenario is that the icon is in a sub-directory of the plugin.
		// If $plugin_path is 'your-plugin-slug/your-plugin-main-file.php'
		// And icons are in 'your-plugin-slug/assets/images/icons/'
		// The original $base_path would be something like WP_CONTENT_DIR . '/plugins/your-plugin-slug/your-plugin-main-file.php'
		// The filter is applied to $base_path.
		// So, we need to return a value that, when passed as the second argument to plugins_url, resolves correctly.

		// Let's assume $plugin_path in the context of the original code is the *file path* of the main plugin file.
		// So, if the original $base_path is derived from WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . $plugin_path,
		// this implies $plugin_path is the relative path to the plugin's main file from the plugins directory.
		// Example: $plugin_path = 'automator/automator.php';
		// Then $base_path becomes WP_CONTENT_DIR . '/plugins/automator/automator.php'

		// If we want to change the *location* of icons within that plugin, we'd often modify the $path filter.
		// However, this hook is for $base_path.
		// This suggests we might be changing the *plugin* that plugins_url considers as the base.

		// Consider a case where the icon is not in the plugin's root directory but in a subfolder,
		// and we need plugins_url to resolve it correctly.
		// If $base_path is 'path/to/plugin/main.php', and icons are in 'path/to/plugin/assets/icons/'.
		// The original code would use plugins_url( 'assets/icons/' . $file_name, 'path/to/plugin/main.php' )
		// which would likely result in /wp-content/plugins/path/to/plugin/assets/icons/your-icon.png

		// Let's assume a scenario where integration icons are stored in a separate, known plugin.
		// For example, if we have a dedicated "Integrations Icon Pack" plugin.
		// If this filter is used, it means the standard plugin path is not sufficient.

		// A realistic example could be for a highly customized setup or a specific integration plugin.
		// Let's say we have a plugin at 'wp-content/mu-plugins/integrations-base/icon-resolver.php'
		// And integration icons are in 'wp-content/uploads/integration-icons/'.
		// This filter might be used to tell plugins_url to look at the 'icon-resolver.php' plugin,
		// but then the $path filter would need to be adjusted to point to '/uploads/integration-icons/'.
		// This seems a bit convoluted for $base_path.

		// A more direct use of $base_path modification:
		// What if the $plugin_path is actually a directory, not a file?
		// e.g., $plugin_path = 'my-integration-suite/'
		// And the $base_path derived is WP_CONTENT_DIR . '/plugins/my-integration-suite/'
		// And the filter needs to return a file path for plugins_url to work.

		// Let's assume a scenario where $plugin_path is the relative path to a plugin directory, e.g., 'my-awesome-plugin/'.
		// The original code is trying to use `WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . $plugin_path` as the base path.
		// But `plugins_url()` expects a file path as the second argument.
		// If `$plugin_path` is the directory name, we need to append a file path within that directory to make it a valid second argument for `plugins_url`.
		// Let's say we append the main plugin file, or just a placeholder file in that directory.

		// If the original $plugin_path is just the directory slug (e.g., 'my-plugin'),
		// then the $base_path derived in the source is `WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . $plugin_path`
		// which would be like `/wp-content/plugins/my-plugin`.
		// This is a directory. `plugins_url` expects a file.
		// So, we need to append a file name to it.
		// Let's assume the main plugin file is named after the slug, e.g., 'my-plugin.php'.
		// The original $base_path would then be like `WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . $plugin_path . DIRECTORY_SEPARATOR . $plugin_path . '.php'`
		// This hook is `automator_integration_icon_base_path_legacy`, so it's likely to handle older structures.

		// Let's simulate a scenario where a specific integration's icons are in a separate plugin.
		// If `$file_name` contains 'stripe', we want to look in a plugin called 'stripe-integration-icons'.
		if ( strpos( $file_name, 'stripe' ) !== false ) {
			// In this case, the default `$plugin_path` might not be 'stripe-integration-icons'.
			// We want `plugins_url` to resolve paths relative to the 'stripe-integration-icons' plugin.
			// So, we should return a file path within that plugin.
			// The simplest is the main plugin file.
			// Assuming the main file is 'stripe-integration-icons.php' in its root.
			return WP_PLUGIN_DIR . '/stripe-integration-icons/stripe-integration-icons.php';
		}

		// For other integrations, we might want to append a specific file to the directory.
		// If $plugin_path is 'my-plugin' (the directory name), the source code will build
		// WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . $plugin_path.
		// We need to append a file path here.
		// Let's assume the main plugin file is named $plugin_path . '.php'.
		// So, if $plugin_path is 'my-plugin', we return 'my-plugin/my-plugin.php' relative to WP_PLUGIN_DIR.
		// The resulting $base_path from source would be WP_PLUGIN_DIR . '/my-plugin/my-plugin.php'.
		// If this hook is called to *modify* that, it implies we are changing that base file.

		// Let's consider the example `src/core/class-utilities.php:228` from the source context.
		// The code shown is:
		// `$path = apply_filters( 'automator_integration_icon_path_legacy', 'src/recipe-ui/dist/media/integrations/' . $file_name, $file_name, $plugin_path );`
		// `$base_path = apply_filters( 'automator_integration_icon_base_path_legacy', WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . $plugin_path, $file_name );`
		// Here, `$plugin_path` is passed to the `$base_path` filter.
		// The source code in `class-utilities.php` likely calculates `$plugin_path` before passing it to the filter.
		// A typical `$plugin_path` could be the relative path to the plugin's main file, e.g., 'automator/automator.php'.
		// So, `$base_path` would be `/wp-content/plugins/automator/automator.php`. This is a file path.

		// If we want to change the *base file* that `plugins_url` uses, we'd return a different file path.
		// For example, if we want to use a file in a subdirectory as the base.
		// Suppose the icon is in `/wp-content/plugins/automator/assets/images/icons/` and the main file is `/wp-content/plugins/automator/automator.php`.
		// We want `plugins_url` to resolve paths relative to the `automator.php` file.
		// The `$path` would then be `'assets/images/icons/' . $file_name`.
		// This filter is for `$base_path`, not `$path`.

		// What if the `$plugin_path` variable itself isn't what we expect or need for `plugins_url`?
		// Let's assume `$plugin_path` is passed by reference and is the relative path from `WP_PLUGIN_DIR` to the plugin's root directory (e.g., 'my-plugin-directory').
		// The source code snippet provided implies `$plugin_path` is used to construct the base path.
		// Let's stick to the idea that `$plugin_path` is the relative path from `WP_PLUGIN_DIR` to the plugin's main file.

		// Real-world scenario:
		// Suppose you have a custom setup where integration icons for a specific integration
		// (identified by `$file_name`) are stored in a specific plugin, not necessarily the one
		// `$plugin_path` is pointing to.
		// For example, if `$file_name` is 'mailchimp-logo.png', and the default `$plugin_path`
		// is 'automator/automator.php', but you want the icon to be resolved from
		// 'integrations-pack/integrations-pack.php'.
		// This filter allows you to change the base file path.

		// Check if the file name suggests a special integration that uses a different base plugin.
		if ( strpos( $file_name, 'slack_icon_' ) === 0 ) {
			// If it's a Slack integration icon, assume it's provided by a dedicated plugin.
			// The second parameter to `plugins_url()` should be a file path within that plugin's directory.
			// We'll use the main plugin file as the base.
			return WP_PLUGIN_DIR . '/slack-integration-plugin/slack-integration-plugin.php';
		}

		// If no special handling is needed, return the original $base_path.
		return $base_path;

	}
	// The number of accepted arguments should match the filter signature.
	// The 'automator_integration_icon_base_path_legacy' hook provides $base_path and $file_name.
	// So, the callback function should accept two arguments.
, 2 ); // 2 represents the number of arguments accepted by this filter.

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/class-utilities.php:228

public static function automator_get_integration_icon( $file_name, $plugin_path = AUTOMATOR_BASE_FILE ) {
		/**
		 * Integration icons are now moved in to integrations itself
		 *
		 * @since 3.0
		 */
		if ( ! empty( $file_name ) && is_dir( dirname( $file_name ) ) ) {
			$icon            = basename( $file_name ); // icon with extension.
			$integration_dir = basename( dirname( $file_name ) ); // integration folder path.
			$path            = self::cleanup_icon_path( $plugin_path, $icon, $file_name ); // path relative to plugin.
			$path            = apply_filters( 'automator_integration_icon_path', $path . $icon, $icon, $integration_dir, $plugin_path );
			$base_path       = apply_filters( 'automator_integration_icon_base_path', $plugin_path, $path, $icon, $integration_dir );
			if ( ! empty( $path ) && ! empty( $base_path ) ) {
				return plugins_url( $path, $base_path );
			}
		}

		// fallback
		$path      = apply_filters( 'automator_integration_icon_path_legacy', 'src/recipe-ui/dist/media/integrations/' . $file_name, $file_name, $plugin_path );
		$base_path = apply_filters( 'automator_integration_icon_base_path_legacy', WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . $plugin_path, $file_name );

		if ( ! empty( $path ) && ! empty( $base_path ) ) {
			return plugins_url( $path, $base_path );
		}

		return '';
	}


Scroll to Top