_get_plugin_from_callback

函式
_get_plugin_from_callback ( $callback )
Access
Private
引數
  • (callable) $callback The callback function to check.
    Required:
返回值
  • (array|null) The plugin that the callback belongs to, or null if it doesn't belong to a plugin.
定義位置
相關方法
wp_targeted_link_rel_callback_sort_uname_callbackget_plugin_datawp_get_plugin_error_sort_name_callback
引入
5.0.0
棄用
-

_get_plugin_from_callback是一個WordPress函式,它返回撥用某個回撥函式的外掛的名稱: 這個函式被用來確定哪個外掛導致一個特定的動作或過濾器被觸發。它需要一個引數,即回撥函式的名稱。

內部輔助函式,從元框回撥中找到外掛。

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;
}

常見問題

FAQs
檢視更多 >