The original $wp_filter, to hand to restore(). */ public static function isolate(): array { global $wp_filter; $original = is_array( $wp_filter ) ? $wp_filter : array(); $roots = self::roots(); $verdicts = array(); $filtered = array(); foreach ( $original as $name => $hook ) { if ( ! $hook instanceof \WP_Hook ) { $filtered[ $name ] = $hook; continue; } $copy = clone $hook; foreach ( $copy->callbacks as $priority => $items ) { foreach ( $items as $id => $item ) { if ( self::foreign( $item['function'], $roots, $verdicts ) ) { unset( $copy->callbacks[ $priority ][ $id ] ); } } if ( ! $copy->callbacks[ $priority ] ) { unset( $copy->callbacks[ $priority ] ); } } $filtered[ $name ] = $copy; } $wp_filter = $filtered; return $original; } /** * @param array $original What isolate() returned. */ public static function restore( array $original ): void { global $wp_filter; $wp_filter = $original; } /** * Folders whose code is not WordPress and not MigrateX. * * @return string[] */ private static function roots(): array { $roots = array(); if ( defined( 'WP_PLUGIN_DIR' ) ) { $roots[] = WP_PLUGIN_DIR; } if ( defined( 'WPMU_PLUGIN_DIR' ) ) { $roots[] = WPMU_PLUGIN_DIR; } if ( function_exists( 'get_theme_root' ) ) { $roots[] = get_theme_root(); } $roots = (array) apply_filters( 'migratex_isolated_roots', $roots ); return array_values( array_filter( array_map( static function ( $root ) { return rtrim( wp_normalize_path( (string) $root ), '/' ) . '/'; }, array_filter( $roots ) ) ) ); } /** * Whether a callback is defined in a plugin or theme file (and not in MigrateX's own). * * @param mixed $callback * @param string[] $roots * @param array $verdicts Per file, so each file is looked at once. */ private static function foreign( $callback, array $roots, array &$verdicts ): bool { $file = self::fileOf( $callback ); if ( null === $file ) { return false; // internal PHP function, or one that cannot be inspected: leave it. } if ( ! isset( $verdicts[ $file ] ) ) { $path = wp_normalize_path( $file ); $own = defined( 'MIGRATEX_DIR' ) && 0 === strpos( $path, wp_normalize_path( (string) MIGRATEX_DIR ) ); $hit = false; if ( ! $own ) { foreach ( $roots as $root ) { if ( 0 === strpos( $path, $root ) ) { $hit = true; break; } } } $verdicts[ $file ] = $hit; } return $verdicts[ $file ]; } /** * @param mixed $callback */ private static function fileOf( $callback ): ?string { try { if ( $callback instanceof \Closure ) { return ( new \ReflectionFunction( $callback ) )->getFileName() ?: null; } if ( is_string( $callback ) && false !== strpos( $callback, '::' ) ) { $callback = explode( '::', $callback, 2 ); } if ( is_string( $callback ) ) { return function_exists( $callback ) ? ( ( new \ReflectionFunction( $callback ) )->getFileName() ?: null ) : null; } if ( is_array( $callback ) && 2 === count( $callback ) ) { return ( new \ReflectionMethod( $callback[0], (string) $callback[1] ) )->getFileName() ?: null; } if ( is_object( $callback ) && method_exists( $callback, '__invoke' ) ) { return ( new \ReflectionMethod( $callback, '__invoke' ) )->getFileName() ?: null; } } catch ( \Throwable $e ) { return null; } return null; } }