*/ public static function describe( string $hook, int $max = self::MAX ): array { $out = array(); foreach ( self::raw( $hook ) as $item ) { if ( count( $out ) >= $max ) { break; } $file = self::file_of( $item['function'] ); $out[] = array( 'name' => self::name_of( $item['function'] ), 'priority' => $item['priority'], 'file' => $file, 'owner' => '' === $file ? self::unknown_owner() : self::owner_of_file( $file ), ); } return $out; } /** * Files of every callback on a hook (unknown ones left out). * * @since 1.0.0 * @param string $hook Cron hook. * @return string[] */ public static function files( string $hook ): array { $files = array(); foreach ( self::raw( $hook ) as $item ) { $file = self::file_of( $item['function'] ); if ( '' !== $file ) { $files[ $file ] = true; } } return array_keys( $files ); } /** * Who a PHP file belongs to, from its path. * * @since 1.0.0 * @param string $file Absolute file path. * @return array{type: string, slug: string, name: string} Type: core, woocommerce, plugin, * mu-plugin, theme or unknown. */ public static function owner_of_file( string $file ): array { $file = wp_normalize_path( $file ); if ( '' === $file ) { return self::unknown_owner(); } if ( defined( 'WC_ABSPATH' ) && self::under( $file, (string) WC_ABSPATH ) ) { return self::owner( 'woocommerce', 'woocommerce', 'WooCommerce' ); } if ( defined( 'WPMU_PLUGIN_DIR' ) && self::under( $file, (string) WPMU_PLUGIN_DIR ) ) { return self::owner( 'mu-plugin', self::first_segment( $file, (string) WPMU_PLUGIN_DIR ), '' ); } if ( defined( 'WP_PLUGIN_DIR' ) && self::under( $file, (string) WP_PLUGIN_DIR ) ) { $slug = self::first_segment( $file, (string) WP_PLUGIN_DIR ); $slug = (string) preg_replace( '/\.php$/', '', $slug ); if ( 'woocommerce' === $slug ) { return self::owner( 'woocommerce', 'woocommerce', 'WooCommerce' ); } return self::owner( 'plugin', $slug, '' ); } foreach ( self::theme_roots() as $root ) { if ( self::under( $file, $root ) ) { return self::owner( 'theme', self::first_segment( $file, $root ), '' ); } } if ( self::under( $file, ABSPATH . 'wp-includes' ) || self::under( $file, ABSPATH . 'wp-admin' ) ) { return self::owner( 'core', 'core', 'WordPress' ); } return self::unknown_owner(); } /** * The unknown owner. * * @since 1.0.0 * @return array{type: string, slug: string, name: string} */ public static function unknown_owner(): array { return self::owner( 'unknown', '', '' ); } /** * Raw callbacks on a hook, in run order. * * @since 1.0.0 * @param string $hook Cron hook. * @return array */ private static function raw( string $hook ): array { global $wp_filter; if ( ! isset( $wp_filter[ $hook ] ) || ! $wp_filter[ $hook ] instanceof \WP_Hook ) { return array(); } $out = array(); foreach ( (array) $wp_filter[ $hook ]->callbacks as $priority => $callbacks ) { foreach ( (array) $callbacks as $callback ) { if ( is_array( $callback ) && array_key_exists( 'function', $callback ) ) { $out[] = array( 'function' => $callback['function'], 'priority' => (int) $priority, ); } } } return $out; } /** * A readable name for a callback, from class and method names only. * * @since 1.0.0 * @param mixed $callback Callback. * @return string */ private static function name_of( $callback ): string { if ( is_string( $callback ) ) { return $callback; } if ( $callback instanceof \Closure ) { return 'Closure'; } if ( is_array( $callback ) && 2 === count( $callback ) && isset( $callback[0], $callback[1] ) && is_string( $callback[1] ) ) { if ( is_object( $callback[0] ) ) { return get_class( $callback[0] ) . '->' . $callback[1]; } if ( is_string( $callback[0] ) ) { return $callback[0] . '::' . $callback[1]; } } if ( is_object( $callback ) ) { return get_class( $callback ) . '->__invoke'; } return __( '(unknown callback)', 'admin-powerups-for-woocommerce' ); } /** * The file a callback is defined in, or '' when it can't be told without loading code. * * @since 1.0.0 * @param mixed $callback Callback. * @return string */ private static function file_of( $callback ): string { try { $reflection = null; if ( $callback instanceof \Closure ) { $reflection = new \ReflectionFunction( $callback ); } elseif ( is_string( $callback ) ) { if ( false !== strpos( $callback, '::' ) ) { list( $class, $method ) = explode( '::', $callback, 2 ); $reflection = self::method( $class, $method ); } elseif ( function_exists( $callback ) ) { $reflection = new \ReflectionFunction( $callback ); } } elseif ( is_array( $callback ) && 2 === count( $callback ) && isset( $callback[0], $callback[1] ) && is_string( $callback[1] ) ) { $reflection = self::method( $callback[0], $callback[1] ); } elseif ( is_object( $callback ) && method_exists( $callback, '__invoke' ) ) { $reflection = new \ReflectionMethod( $callback, '__invoke' ); } if ( ! $reflection ) { return ''; } $file = $reflection->getFileName(); return is_string( $file ) ? $file : ''; } catch ( \ReflectionException $e ) { return ''; } catch ( \Error $e ) { return ''; } } /** * Reflection of a method on an object or an already loaded class (never autoloads). * * @since 1.0.0 * @param object|string $target Object or class name. * @param string $method Method. * @return \ReflectionMethod|null */ private static function method( $target, string $method ) { if ( is_string( $target ) ) { $target = ltrim( $target, '\\' ); if ( '' === $target || ! class_exists( $target, false ) ) { return null; } if ( 0 === strpos( $method, 'parent::' ) ) { return null; } } elseif ( ! is_object( $target ) ) { return null; } if ( ! method_exists( $target, $method ) ) { return null; // Handled by __call(): no file to point at. } return new \ReflectionMethod( $target, $method ); } /** * Theme root folders. * * @since 1.0.0 * @return string[] */ private static function theme_roots(): array { $roots = array( WP_CONTENT_DIR . '/themes' ); if ( function_exists( 'get_theme_root' ) ) { $roots[] = (string) get_theme_root(); } return array_values( array_unique( array_filter( $roots ) ) ); } /** * Whether a normalized file path is inside a folder. * * @since 1.0.0 * @param string $file Normalized file path. * @param string $folder Folder. * @return bool */ private static function under( string $file, string $folder ): bool { $folder = trailingslashit( wp_normalize_path( $folder ) ); return '/' !== $folder && 0 === strpos( $file, $folder ); } /** * First path segment below a folder (plugin or theme folder name). * * @since 1.0.0 * @param string $file Normalized file path. * @param string $folder Folder. * @return string */ private static function first_segment( string $file, string $folder ): string { $rest = substr( $file, strlen( trailingslashit( wp_normalize_path( $folder ) ) ) ); $part = explode( '/', (string) $rest ); return (string) $part[0]; } /** * Build an owner. * * @since 1.0.0 * @param string $type Type. * @param string $slug Slug. * @param string $name Display name. * @return array{type: string, slug: string, name: string} */ private static function owner( string $type, string $slug, string $name ): array { return array( 'type' => $type, 'slug' => $slug, 'name' => $name, ); } }