File: /wordpress/plugins/wp-cloud-client/1.0.0/src/Handler/CheckThemeUpdatesHandler.php
<?php
declare(strict_types=1);
namespace VPlugins\WPCloudClient\Handler;
/**
* Checks for available updates for WordPress themes.
*
* Action name: check_theme_updates
*
* Optional params:
* - slug (string) - Specific theme stylesheet to check (e.g., "twentytwentyfour").
* If not provided, checks all installed themes for updates.
*
* Example response for all themes:
* {
* "has_updates": true,
* "updates": [
* {
* "slug": "twentytwentyfive",
* "name": "Twenty Twenty-Five",
* "current_version": "1.0",
* "new_version": "1.1",
* "download_url": "https://...",
* "requires_wp": "6.0",
* "requires_php": "7.4"
* }
* ]
* }
*/
final class CheckThemeUpdatesHandler extends AbstractHandler {
/**
* Return the action name.
*
* @return string
*/
public function action(): string {
return 'check_theme_updates';
}
/**
* Check for theme updates.
*
* @param array<string, mixed> $params Action parameters.
* @return array<string, mixed> Update status.
* @throws \InvalidArgumentException When required parameters are invalid.
*/
public function execute( array $params ): array {
$slug = isset( $params['slug'] ) ? (string) $params['slug'] : null;
if ( null !== $slug && empty( $slug ) ) {
throw new \InvalidArgumentException( 'Theme slug cannot be empty.' );
}
// Get the theme update transient.
$transient = get_site_transient( 'update_themes' );
$updates = [];
if ( null !== $slug ) {
// Check specific theme.
$theme = wp_get_theme( $slug );
if ( ! $theme->exists() ) {
throw new \InvalidArgumentException( sprintf( 'Theme "%s" not found.', $slug ) );
}
if ( $this->hasUpdate( $transient, $slug ) ) {
$updateData = $this->extractUpdateInfo( $slug, $theme, $transient );
if ( null !== $updateData ) {
$updates[] = $updateData;
}
}
} else {
// Check all themes.
$themes = wp_get_themes();
foreach ( $themes as $themeSlug => $theme ) {
if ( $this->hasUpdate( $transient, $themeSlug ) ) {
$updateData = $this->extractUpdateInfo( $themeSlug, $theme, $transient );
if ( null !== $updateData ) {
$updates[] = $updateData;
}
}
}
}
return [
'has_updates' => ! empty( $updates ),
'updates' => $updates,
];
}
/**
* Check if a theme has an available update.
*
* @param mixed $transient Update transient object.
* @param string $slug Theme slug.
* @return bool True if update is available.
*/
private function hasUpdate( $transient, string $slug ): bool {
if ( ! is_object( $transient ) || ! isset( $transient->response ) ) {
return false;
}
if ( ! is_array( $transient->response ) && ! is_object( $transient->response ) ) {
return false;
}
return isset( $transient->response[ $slug ] );
}
/**
* Extract update information from transient.
*
* @param string $slug Theme slug.
* @param \WP_Theme $theme Theme object.
* @param mixed $transient Update transient.
* @return array<string, mixed>|null Extracted update info or null.
*/
private function extractUpdateInfo( string $slug, \WP_Theme $theme, $transient ): ?array {
if ( ! is_object( $transient ) || ! isset( $transient->response[ $slug ] ) ) {
return null;
}
$updateData = $transient->response[ $slug ];
$currentVersion = $theme->get( 'Version' );
$newVersion = $updateData['new_version'] ?? '';
$themeName = $theme->get( 'Name' );
return [
'slug' => $slug,
'name' => $themeName,
'current_version' => $currentVersion,
'new_version' => $newVersion,
'download_url' => $updateData['package'] ?? '',
'requires_wp' => $updateData['requires'] ?? '',
'requires_php' => $updateData['requires_php'] ?? '',
'tested_up_to' => $updateData['tested'] ?? '',
'theme_url' => $theme->get( 'ThemeURI' ),
];
}
}