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.2.2/src/Handler/ListPluginsHandler.php
<?php

declare(strict_types=1);

namespace VPlugins\WPCloudClient\Handler;

/**
 * Lists every installed WordPress plugin with update metadata.
 *
 * Action name: list_plugins
 *
 * Response example:
 * {
 *   "plugins": [
 *     {
 *       "id":               "woocommerce/woocommerce.php",
 *       "name":             "WooCommerce",
 *       "current_version":  "8.0.0",
 *       "latest_version":   "8.1.0",
 *       "update_available": true,
 *       "status":           "active"
 *     }
 *   ]
 * }
 */
final class ListPluginsHandler extends AbstractHandler {

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

	/**
	 * Collect every installed plugin with current/latest version and active status.
	 *
	 * @param array<string, mixed> $params Action parameters.
	 * @return array<string, mixed> Plugin list.
	 */
	public function execute( array $params ): array {
		if ( ! function_exists( 'get_plugins' ) ) {
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
		}

		$activePluginFiles = (array) get_option( 'active_plugins', [] );
		$updateResponse    = $this->getUpdateResponse();

		$plugins = [];
		foreach ( get_plugins() as $id => $info ) {
			$currentVersion  = (string) ( $info['Version'] ?? '' );
			$latestVersion   = $currentVersion;
			$updateAvailable = false;

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

			$plugins[] = [
				'id'               => (string) $id,
				'name'             => (string) ( $info['Name'] ?? $id ),
				'current_version'  => $currentVersion,
				'latest_version'   => $latestVersion,
				'update_available' => $updateAvailable,
				'status'           => in_array( $id, $activePluginFiles, true ) ? 'active' : 'inactive',
			];
		}

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

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

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