内部帮助函数从元框回调中查找插件。

原型

_get_plugin_from_callback( callable $callback )

参数

$callback (callable) (Required) 要检查的回调函数。

返回值

(array|null)  回调所属的插件,如果不属于插件则为null。

源文件

路径:wp-admin/includes/template.php

<?php
...
function _get_plugin_from_callback( $callback ) {
	try {
		if ( is_array( $callback ) ) {
			$reflection = new ReflectionMethod( $callback[0], $callback[1] );
		} elseif ( is_string( $callback) && false !== strpos( $callback, '::' ) ) {
			$reflection = new ReflectionMethod( $callback );
		} else {
			$reflection = new ReflectionFunction( $callback );
		}
	} catch ( ReflectionException $exception ) {
		// We could not properly reflect on the callable, so we abort here.
		return null;
	}
	// Don't show an error if it's an internal PHP function.
	if ( ! $reflection->isInternal() ) {
		// Only show errors if the meta box was registered by a plugin.
		$filename = wp_normalize_path( $reflection->getFileName() );
		$plugin_dir = wp_normalize_path( WP_PLUGIN_DIR );
		if ( strpos( $filename, $plugin_dir ) === 0 ) {
			$filename = str_replace( $plugin_dir, '', $filename );
			$filename = preg_replace( '|^/([^/]*/).*$|', '\1', $filename );
			$plugins = get_plugins();
			foreach ( $plugins as $name => $plugin ) {
				if ( strpos( $name, $filename ) === 0 ) {
					return $plugin;
				}
			}
		}
	}
	return null;
}
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/_get_plugin_from_callback/