paths = $paths ?? new WordPressPathProvider(); } /** * @return array,requires_wp:string,requires_php:string,update_uri:string}> */ public function themes(): array { $directories = $this->paths->theme_roots(); sort( $directories ); $stylesheet = get_stylesheet(); $template = get_template(); $themes = array(); $active_seen = false; foreach ( $directories as $directory ) { $root = realpath( $directory ); if ( false === $root || ! is_dir( $root ) ) { continue; } $root = rtrim( wp_normalize_path( $root ), '/' ); foreach ( new DirectoryIterator( $root ) as $entry ) { if ( $entry->isDot() || ! $entry->isDir() ) { continue; } $path = realpath( $entry->getPathname() ); if ( false === $path ) { continue; } $path = wp_normalize_path( $path ); if ( 0 !== strpos( $path . '/', $root . '/' ) ) { continue; } $identifier = $entry->getFilename(); $theme = new WP_Theme( $identifier, $root ); $errors = $theme->errors(); $state = $identifier === $stylesheet ? 'active' : ( $identifier === $template ? 'parent' : 'inactive' ); $themes[] = array( 'identifier' => $path, 'slug' => $identifier, 'name' => (string) $theme->get( 'Name' ), 'version' => (string) $theme->get( 'Version' ), 'state' => $state, 'errors' => is_wp_error( $errors ) ? $errors->get_error_codes() : array(), 'requires_wp' => (string) $theme->get( 'RequiresWP' ), 'requires_php' => (string) $theme->get( 'RequiresPHP' ), 'update_uri' => (string) $theme->get( 'UpdateURI' ), ); if ( 'active' === $state ) { $active_seen = true; } } } if ( ! $active_seen ) { $themes[] = array( 'identifier' => 'missing-active:' . $stylesheet, 'slug' => $stylesheet, 'name' => $stylesheet, 'version' => 'unknown', 'state' => 'active', 'errors' => array( 'active_theme_not_found' ), 'requires_wp' => '', 'requires_php' => '', 'update_uri' => '', ); } $priority = array( 'active' => 0, 'parent' => 1, 'inactive' => 2, ); usort( $themes, static function ( array $left, array $right ) use ( $priority ): int { return array( $priority[ $left['state'] ], $left['name'], $left['identifier'] ) <=> array( $priority[ $right['state'] ], $right['name'], $right['identifier'] ); } ); return $themes; } }