is_self( $file ) ) { continue; } $slug = $this->slug_from_path( $file ); if ( '' !== $slug ) { return $slug; } if ( $this->is_core_ai_client( $file ) ) { $saw_core_ai_client = true; } } return $saw_core_ai_client ? self::CORE : self::UNKNOWN; } /** * Whether a file is part of core's bundled AI client. * * @param string $file Normalized absolute path. * @return bool True when the file is core's AI client. */ private function is_core_ai_client( string $file ): bool { if ( null === $this->core_ai_roots ) { $this->core_ai_roots = array(); if ( defined( 'ABSPATH' ) && defined( 'WPINC' ) ) { $includes = trailingslashit( wp_normalize_path( ABSPATH . WPINC ) ); $this->core_ai_roots = array( $includes . 'ai-client', $includes . 'php-ai-client', ); } } foreach ( $this->core_ai_roots as $root ) { if ( 0 === strpos( $file, $root ) ) { return true; } } return false; } /** * Whether a file belongs to this plugin. * * @param string $file Normalized absolute path. * @return bool True when the file is ours. */ private function is_self( string $file ): bool { if ( null === $this->self_root ) { $this->self_root = defined( 'AIBLACKOUT_PATH' ) ? trailingslashit( wp_normalize_path( AIBLACKOUT_PATH ) ) : ''; } return '' !== $this->self_root && 0 === strpos( $file, $this->self_root ); } /** * Extracts a component slug from an absolute file path. * * @param string $file Normalized absolute path. * @return string The slug, or an empty string when the path is not inside a known root. */ private function slug_from_path( string $file ): string { foreach ( $this->get_roots() as $root ) { if ( '' === $root || 0 !== strpos( $file, $root ) ) { continue; } $relative = ltrim( substr( $file, strlen( $root ) ), '/' ); if ( '' === $relative ) { continue; } $segments = explode( '/', $relative ); return $segments[0]; } return ''; } /** * Returns the directories a caller can live in, longest first. * * @return string[] Normalized directory paths with trailing slashes. */ private function get_roots(): array { if ( null !== $this->roots ) { return $this->roots; } $roots = array(); if ( defined( 'WPMU_PLUGIN_DIR' ) ) { $roots[] = trailingslashit( wp_normalize_path( WPMU_PLUGIN_DIR ) ); } if ( defined( 'WP_PLUGIN_DIR' ) ) { $roots[] = trailingslashit( wp_normalize_path( WP_PLUGIN_DIR ) ); } foreach ( $this->get_theme_root_paths() as $path ) { $roots[] = trailingslashit( wp_normalize_path( $path ) ); } $roots = array_values( array_unique( array_filter( $roots ) ) ); usort( $roots, static function ( $a, $b ) { return strlen( $b ) - strlen( $a ); } ); $this->roots = $roots; return $this->roots; } /** * Returns every absolute theme-root directory registered on this site. * * @return string[] Absolute directory paths. May contain duplicates. */ private function get_theme_root_paths(): array { if ( ! function_exists( 'get_theme_root' ) ) { return array(); } // The active theme and its parent. $paths = array( get_theme_root( get_stylesheet() ), get_theme_root( get_template() ), ); $roots = function_exists( 'get_theme_roots' ) ? get_theme_roots() : array(); if ( is_array( $roots ) ) { foreach ( array_keys( $roots ) as $theme_dir ) { $paths[] = get_theme_root( (string) $theme_dir ); } } else { // A single registered theme directory. $paths[] = get_theme_root(); } return array_filter( array_map( 'strval', $paths ) ); } /** * Returns a human-readable name for a stored source slug. * * @param string $slug Stored source slug. * @return string Display name. */ public function get_display_name( string $slug ): string { if ( self::CORE === $slug ) { return __( 'WordPress core', 'ai-blackout' ); } if ( self::UNKNOWN === $slug || '' === $slug ) { return __( 'Unidentified code', 'ai-blackout' ); } if ( ! function_exists( 'get_plugins' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } foreach ( get_plugins() as $file => $data ) { $dir = strtok( $file, '/' ); if ( $dir === $slug && ! empty( $data['Name'] ) ) { return (string) $data['Name']; } } $theme = wp_get_theme( $slug ); if ( $theme->exists() ) { return (string) $theme->get( 'Name' ); } return $slug; } }