HEX
Server: nginx
System: Linux pool195-106-36.bur.atomicsites.net 6.12.57+deb12-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.12.57-1~bpo12+1 (2025-11-17) x86_64
User: (0)
PHP: 8.3.32
Disabled: pcntl_fork
Upload Files
File: //wordpress/plugins/wp-cloud-client/1.1.9/src/Handler/ListThemesHandler.php
<?php

declare(strict_types=1);

namespace VPlugins\WPCloudClient\Handler;

/**
 * Lists every installed WordPress theme with update metadata.
 *
 * Action name: list_themes
 *
 * Response example:
 * {
 *   "themes": [
 *     {
 *       "id":               "twentytwentyfour",
 *       "name":             "Twenty Twenty-Four",
 *       "current_version":  "1.0",
 *       "latest_version":   "1.1",
 *       "update_available": true,
 *       "status":           "active"
 *     }
 *   ]
 * }
 */
final class ListThemesHandler extends AbstractHandler {

	/**
	 * Return the action name.
	 *
	 * @return string
	 */
	public function action(): string {
		return 'list_themes';
	}

	/**
	 * Collect every installed theme with current/latest version and active status.
	 *
	 * @param array<string, mixed> $params Action parameters.
	 * @return array<string, mixed> Theme list.
	 */
	public function execute( array $params ): array {
		$activeStylesheet = $this->getActiveStylesheet();
		$updateResponse   = $this->getUpdateResponse();

		$themes = [];
		foreach ( wp_get_themes() as $stylesheet => $theme ) {
			$currentVersion  = (string) $theme->get( 'Version' );
			$latestVersion   = $currentVersion;
			$updateAvailable = false;

			if ( isset( $updateResponse[ $stylesheet ] ) ) {
				$update     = $updateResponse[ $stylesheet ];
				$newVersion = is_array( $update ) && isset( $update['new_version'] ) ? (string) $update['new_version'] : '';
				if ( '' !== $newVersion ) {
					$updateAvailable = true;
					$latestVersion   = $newVersion;
				}
			}

			$themes[] = [
				'id'               => (string) $stylesheet,
				'name'             => (string) $theme->get( 'Name' ),
				'current_version'  => $currentVersion,
				'latest_version'   => $latestVersion,
				'update_available' => $updateAvailable,
				'status'           => $stylesheet === $activeStylesheet ? 'active' : 'inactive',
			];
		}

		return [ 'themes' => $themes ];
	}

	/**
	 * Resolve the stylesheet slug of the active theme.
	 *
	 * @return string Active stylesheet slug (empty if none resolvable).
	 */
	private function getActiveStylesheet(): string {
		$theme = wp_get_theme();
		if ( ! $theme->exists() ) {
			return '';
		}

		return (string) $theme->get_stylesheet();
	}

	/**
	 * Extract the response map from the update_themes transient.
	 *
	 * @return array<string, mixed> Map of stylesheet → update data.
	 */
	private function getUpdateResponse(): array {
		$transient = get_site_transient( 'update_themes' );
		if ( ! is_object( $transient ) || ! isset( $transient->response ) ) {
			return [];
		}

		return (array) $transient->response;
	}
}