*/ private $units = array(); /** * Name patterns excluded at any depth (extra exclusion lines that start with **\/). * * @var string[] */ private $name_patterns = array(); /** * Patterns for one root. * * @var string[] */ private $patterns = array(); /** * Absolute storage dir (excluded wherever it lives). * * @var string */ private $storage = ''; /** * Absolute root dir. * * @var string */ private $root = ''; /** * Builds the rules for a logical root. * * @param string $root_key Root key (content, uploads, plugins, mu-plugins, themes). * @param string $root_dir Absolute root directory. */ public function __construct( $root_key, $root_dir ) { $this->root = trailingslashit( wp_normalize_path( $root_dir ) ); if ( 'content' === $root_key ) { $this->patterns = self::CONTENT_PATTERNS; $this->dev_files = (bool) Settings::get( 'leave_dev_files' ); foreach ( preg_split( '/\R/', (string) Settings::get( 'extra_excludes' ) ) as $line ) { $line = trim( $line ); if ( 0 === strpos( $line, '**/' ) && strlen( $line ) > 3 && false === strpos( substr( $line, 3 ), '/' ) ) { $this->name_patterns[] = substr( $line, 3 ); // A name, wherever it is. continue; } $line = trim( $line, '/' ); if ( '' !== $line && false === strpos( $line, '..' ) ) { $this->patterns[] = $line; } } } try { $this->storage = untrailingslashit( Storage::dir() ); } catch ( \RuntimeException $e ) { $this->storage = ''; } } /** * Whether a root-relative path is excluded. * * @param string $rel Relative path. * @param string $name Base name. */ public function excluded( $rel, $name ) { if ( in_array( $name, self::ANY_DEPTH, true ) ) { return true; } foreach ( self::ANY_DEPTH_PREFIXES as $prefix ) { if ( 0 === strpos( $name, $prefix ) ) { return true; } } foreach ( $this->name_patterns as $pattern ) { if ( fnmatch( $pattern, $name ) ) { return true; } } foreach ( $this->patterns as $pattern ) { if ( fnmatch( $pattern, $rel, FNM_PATHNAME ) ) { return true; } } if ( $this->dev_files && $this->developer_file( $rel, $name ) ) { return true; } if ( '' !== $this->storage && $this->root . $rel === $this->storage ) { return true; } return false; } /** * Whether a path is a developer file of a theme or plugin folder that is a git working copy. * * Only inside themes and plugins, and only in folders with a .git (developed in place): a theme * or plugin installed from a ZIP keeps everything it came with. CLAUDE.md goes at any depth; * the rest at the top of the folder. vendor/ goes only when composer.json asks for nothing but * development packages (its production code, if any, would be in require). * * @param string $rel Path relative to wp-content. * @param string $name Base name. */ private function developer_file( $rel, $name ) { if ( ! preg_match( '#^(themes|plugins)/([^/]+)/(.+)$#', $rel, $m ) ) { return false; } $unit = $m[1] . '/' . $m[2]; if ( ! isset( $this->units[ $unit ] ) ) { $dir = $this->root . $unit; $this->units[ $unit ] = array( file_exists( $dir . '/.git' ), self::composer_dev_only( $dir . '/composer.json' ) ); } if ( ! $this->units[ $unit ][0] ) { return false; } if ( 'CLAUDE.md' === $name ) { return true; } $inner = $m[3]; $top = false === strpos( $inner, '/' ) ? $inner : strstr( $inner, '/', true ); if ( 'vendor' === $top && $this->units[ $unit ][1] ) { return true; } foreach ( self::DEV_TOP as $pattern ) { if ( fnmatch( $pattern, $top ) ) { return true; } } return false; } /** * Whether a composer.json has packages only under require-dev (nothing the site runs on). * * @param string $file Path. */ private static function composer_dev_only( $file ) { if ( ! is_file( $file ) ) { return false; } $json = json_decode( (string) file_get_contents( $file ), true ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents if ( ! is_array( $json ) || empty( $json['require-dev'] ) ) { return false; } foreach ( array_keys( (array) ( $json['require'] ?? array() ) ) as $package ) { if ( 'php' !== $package && 0 !== strpos( (string) $package, 'ext-' ) ) { return false; } } return true; } /** * Theme and plugin folders in a list of index paths that look developed in place, with their * developer files, to warn before they are sent. Names only: a backup does not hold .git. * * @param string[] $paths Paths relative to wp-content. * @return array Folder => developer files (a few). */ public static function developer_files_in( array $paths ) { $units = array(); $marked = array(); foreach ( $paths as $rel ) { if ( ! preg_match( '#^(themes|plugins)/([^/]+)/([^/]+)(/|$)#', $rel, $m ) ) { continue; } $unit = $m[1] . '/' . $m[2]; $top = $m[3]; if ( in_array( $top, self::DEV_MARKERS, true ) ) { $marked[ $unit ] = true; } foreach ( self::DEV_TOP as $pattern ) { if ( fnmatch( $pattern, $top ) ) { $units[ $unit ][ $top ] = true; break; } } } $out = array(); foreach ( $units as $unit => $names ) { if ( isset( $marked[ $unit ] ) ) { $out[ $unit ] = array_slice( array_keys( $names ), 0, 6 ); } } return $out; } }