settings->get_bool( self::ENABLED_OPTION ) ) { \add_filter( 'script_loader_src', $this->filter_source( ... ), 9000, 2 ); } } public function filter_source( string $src, string $handle ): string { if ( \is_admin() || ! $this->settings->get_bool( self::ENABLED_OPTION ) || '' === $src || ! \in_array( $handle, $this->selected_handles(), true ) ) { return $src; } $resolved = $this->resolve_source( $src ); if ( null === $resolved ) { return $src; } $source = $this->read_file( $resolved['path'] ); if ( null === $source || '' === $source ) { return $src; } $location = self::cache_location( true ); if ( null === $location ) { return $src; } $cached_url = $this->cache_source( $resolved['relative'], $source, $location ); return '' !== $cached_url ? $cached_url : $src; } public static function uninstall(): void { $location = self::cache_location( false ); if ( null === $location ) { return; } $entries = \array_merge( self::glob_paths( $location['directory'] . DIRECTORY_SEPARATOR . '*.min.js' ), self::glob_paths( $location['directory'] . DIRECTORY_SEPARATOR . '.lock-*' ), self::glob_paths( $location['directory'] . DIRECTORY_SEPARATOR . '.tmp-*' ) ); foreach ( $entries as $entry ) { $name = \basename( $entry ); if ( \is_link( $entry ) || ! \is_file( $entry ) || ( 1 !== \preg_match( '/\A[A-Za-z0-9._-]+-[a-f0-9]{16}-[a-f0-9]{16}\.min\.js\z/', $name ) && 1 !== \preg_match( '/\A\.lock-[a-f0-9]{16}\z/', $name ) && 1 !== \preg_match( '/\A\.tmp-[a-f0-9]{16}-[A-Za-z0-9]+\z/', $name ) ) ) { continue; } self::quiet_filesystem( static fn (): bool => \unlink( $entry ) ); } self::quiet_filesystem( static fn (): bool => \rmdir( $location['directory'] ) ); } /** @return array */ private function selected_handles(): array { $extra = \preg_split( '/\R/', $this->settings->get_string( self::EXTRA_OPTION ) ); $extra = false === $extra ? array() : \array_filter( \array_map( 'trim', $extra ) ); return \array_values( \array_unique( \array_merge( self::DEFAULT_HANDLES, $extra ) ) ); } /** @return array{path:string,relative:string}|null */ private function resolve_source( string $src ): ?array { $source_parts = \wp_parse_url( \html_entity_decode( $src, ENT_QUOTES | ENT_HTML5, 'UTF-8' ) ); $content_parts = \wp_parse_url( \content_url( '/' ) ); if ( ! \is_array( $source_parts ) || ! \is_array( $content_parts ) || ! $this->same_authority( $source_parts, $content_parts ) ) { return null; } $source_path = isset( $source_parts['path'] ) && \is_string( $source_parts['path'] ) ? \rawurldecode( $source_parts['path'] ) : ''; $content_path = isset( $content_parts['path'] ) && \is_string( $content_parts['path'] ) ? \rawurldecode( $content_parts['path'] ) : ''; if ( '' === $source_path || '' === $content_path || \str_contains( $source_path, "\0" ) ) { return null; } $prefix = \trailingslashit( $content_path ); if ( ! \str_starts_with( $source_path, $prefix ) ) { return null; } $relative = \ltrim( \substr( $source_path, \strlen( $prefix ) ), '/' ); if ( '' === $relative || 1 !== \preg_match( '/\.js\z/i', $relative ) || 1 === \preg_match( '/\.min\.js\z/i', $relative ) ) { return null; } $content_root = \realpath( WP_CONTENT_DIR ); $file = \realpath( WP_CONTENT_DIR . DIRECTORY_SEPARATOR . \str_replace( '/', DIRECTORY_SEPARATOR, $relative ) ); if ( ! \is_string( $content_root ) || ! \is_string( $file ) || ! \str_starts_with( $file, $content_root . DIRECTORY_SEPARATOR ) || ! \is_file( $file ) || ! \is_readable( $file ) ) { return null; } return array( 'path' => $file, 'relative' => \str_replace( DIRECTORY_SEPARATOR, '/', \substr( $file, \strlen( $content_root ) + 1 ) ), ); } /** * @param array $source Source URL parts. * @param array $content Content URL parts. */ private function same_authority( array $source, array $content ): bool { if ( isset( $source['user'] ) || isset( $source['pass'] ) ) { return false; } $source_scheme = isset( $source['scheme'] ) && \is_string( $source['scheme'] ) ? \strtolower( $source['scheme'] ) : null; $content_scheme = isset( $content['scheme'] ) && \is_string( $content['scheme'] ) ? \strtolower( $content['scheme'] ) : null; if ( ! isset( $source['host'] ) ) { return null === $source_scheme; } if ( null === $source_scheme || null === $content_scheme || ! \in_array( $source_scheme, array( 'http', 'https' ), true ) || ! \in_array( $content_scheme, array( 'http', 'https' ), true ) || $source_scheme !== $content_scheme || ! isset( $content['host'] ) || ! \is_string( $source['host'] ) || ! \is_string( $content['host'] ) ) { return false; } if ( \strtolower( $source['host'] ) !== \strtolower( $content['host'] ) ) { return false; } if ( ( isset( $source['port'] ) && ! \is_int( $source['port'] ) ) || ( isset( $content['port'] ) && ! \is_int( $content['port'] ) ) ) { return false; } $source_port = isset( $source['port'] ) && \is_int( $source['port'] ) ? $source['port'] : null; $content_port = isset( $content['port'] ) && \is_int( $content['port'] ) ? $content['port'] : null; if ( null === $source_port && null === $content_port ) { return true; } $default_port = 'http' === $content_scheme ? 80 : 443; return ( $source_port ?? $default_port ) === ( $content_port ?? $default_port ); } private function read_file( string $path ): ?string { $content = self::quiet_filesystem( static fn (): string|false => \file_get_contents( $path ) ); return \is_string( $content ) ? $content : null; } /** @return array{directory:string,url:string}|null */ private static function cache_location( bool $create ): ?array { $uploads = \wp_upload_dir( null, false ); if ( ! empty( $uploads['error'] ) || ! isset( $uploads['basedir'], $uploads['baseurl'] ) || ! \is_string( $uploads['basedir'] ) || ! \is_string( $uploads['baseurl'] ) ) { return null; } $base = \realpath( $uploads['basedir'] ); if ( ! \is_string( $base ) ) { return null; } $expected = $base . DIRECTORY_SEPARATOR . self::CACHE_DIRECTORY; if ( $create && ! \is_dir( $expected ) && ! \wp_mkdir_p( $expected ) ) { return null; } $directory = \realpath( $expected ); if ( ! \is_string( $directory ) || $directory !== $expected || ! \is_dir( $directory ) ) { return null; } return array( 'directory' => $directory, 'url' => \trailingslashit( $uploads['baseurl'] ) . self::CACHE_DIRECTORY . '/', ); } /** @param array{directory:string,url:string} $location */ private function cache_source( string $relative, string $source, array $location ): string { $path_hash = \substr( \hash( 'sha256', $relative ), 0, 16 ); $content_hash = \substr( \hash( 'sha256', self::COMPILER_ID . "\0" . $relative . "\0" . $source ), 0, 16 ); $stem = \sanitize_file_name( \pathinfo( $relative, PATHINFO_FILENAME ) ); $stem = '' !== $stem ? $stem : 'script'; $filename = $stem . '-' . $path_hash . '-' . $content_hash . '.min.js'; $destination = $location['directory'] . DIRECTORY_SEPARATOR . $filename; $lock_path = $location['directory'] . DIRECTORY_SEPARATOR . '.lock-' . $path_hash; if ( $this->valid_cache_file( $destination ) ) { return $location['url'] . \rawurlencode( $filename ); } if ( \is_link( $destination ) || ( \file_exists( $destination ) && ! \is_file( $destination ) ) || \is_link( $lock_path ) ) { return ''; } $lock = self::quiet_filesystem( static fn () => \fopen( $lock_path, 'c' ) ); if ( ! \is_resource( $lock ) ) { return ''; } $temporary = null; try { if ( ! \flock( $lock, LOCK_EX ) ) { return ''; } if ( $this->valid_cache_file( $destination ) ) { // @phpstan-ignore if.alwaysFalse (Another process may fill the cache while this request waits for the lock.) return $location['url'] . \rawurlencode( $filename ); } if ( \is_file( $destination ) && ! \is_link( $destination ) ) { // @phpstan-ignore booleanNot.alwaysTrue (Reject a path swapped to a symlink while this request waited.) self::quiet_filesystem( static fn (): bool => \unlink( $destination ) ); } $vendor = BLOCKS_PLUGIN_DIR . '/includes/jshrink/src/Minifier.php'; if ( ! \is_file( $vendor ) ) { return ''; } require_once $vendor; try { $minified = Minifier::minify( $source, array( 'flaggedComments' => true ) ); } catch ( \Throwable ) { return ''; } if ( ! \is_string( $minified ) || '' === $minified || \strlen( $minified ) >= \strlen( $source ) ) { return ''; } $temporary = self::quiet_filesystem( static fn (): string|false => \tempnam( $location['directory'], '.tmp-' . $path_hash . '-' ) ); if ( ! \is_string( $temporary ) ) { return ''; } $written = self::quiet_filesystem( static fn (): int|false => \file_put_contents( $temporary, $minified, LOCK_EX ) ); $mode_set = self::quiet_filesystem( static fn (): bool => \chmod( $temporary, 0644 ) ); if ( \strlen( $minified ) !== $written || true !== $mode_set ) { return ''; } if ( true !== self::quiet_filesystem( static fn (): bool => \rename( $temporary, $destination ) ) ) { return ''; } $temporary = null; $this->prune_old_cache_files( $location['directory'], $filename ); return $location['url'] . \rawurlencode( $filename ); } finally { \flock( $lock, LOCK_UN ); self::quiet_filesystem( static fn (): bool => \fclose( $lock ) ); if ( \is_string( $temporary ) && \is_file( $temporary ) && ! \is_link( $temporary ) ) { self::quiet_filesystem( static fn (): bool => \unlink( $temporary ) ); } } } private function valid_cache_file( string $path ): bool { if ( \is_link( $path ) || ! \is_file( $path ) ) { return false; } $size = self::quiet_filesystem( static fn (): int|false => \filesize( $path ) ); return \is_int( $size ) && $size > 0; } private function prune_old_cache_files( string $directory, string $current ): void { $entries = self::glob_paths( $directory . DIRECTORY_SEPARATOR . '*.min.js' ); $cutoff = \time() - self::RETENTION_SECONDS; foreach ( $entries as $entry ) { $name = \basename( $entry ); if ( $current === $name || \is_link( $entry ) || ! \is_file( $entry ) || 1 !== \preg_match( '/\A[A-Za-z0-9._-]+-[a-f0-9]{16}-[a-f0-9]{16}\.min\.js\z/', $name ) || $this->is_recent( $entry, $cutoff ) ) { continue; } self::quiet_filesystem( static fn (): bool => \unlink( $entry ) ); } } private function is_recent( string $path, int $cutoff ): bool { $modified = self::quiet_filesystem( static fn (): int|false => \filemtime( $path ) ); return false === $modified || $modified >= $cutoff; } /** @return array */ private static function glob_paths( string $pattern ): array { $paths = self::quiet_filesystem( static fn (): array|false => \glob( $pattern ) ); return \is_array( $paths ) ? $paths : array(); } private static function quiet_filesystem( callable $operation ): mixed { // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_set_error_handler -- Expected file failures must fail open without leaking warnings into frontend output. \set_error_handler( static fn (): bool => true ); try { return $operation(); } finally { \restore_error_handler(); } } } // phpcs:enable WordPress.WP.AlternativeFunctions