File: /wordpress/plugins/wp-cloud-client/1.1.0/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;
}
}