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/SiteSummaryHandler.php
<?php

declare(strict_types=1);

namespace VPlugins\WPCloudClient\Handler;

use VPlugins\WPCloudClient\Settings\SettingsRepository;

final class SiteSummaryHandler extends AbstractHandler {

	private const CACHE_DIRS = [
		WP_CONTENT_DIR . '/cache',
	];

	/**
	 * Class constructor.
	 *
	 * @param SettingsRepository $settings Settings repository.
	 */
	public function __construct(
		private readonly SettingsRepository $settings,
	) {}

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

	/**
	 * Gather and return a site summary.
	 *
	 * @param array<string, mixed> $params Action parameters.
	 * @return array<string, mixed> Site summary data.
	 */
	public function execute( array $params ): array {
		if ( ! function_exists( 'get_plugins' ) ) {
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
		}

		$summary = [
			'site_id'    => $this->settings->getSiteId(),
			'wp_version' => get_bloginfo( 'version' ),
			'n_posts'    => $this->countPosts( 'post' ),
			'n_pages'    => $this->countPosts( 'page' ),
			'plugins'    => $this->getPlugins(),
		];

		$skipDbSize = ! empty( $params['skip_db_size'] ) && filter_var( $params['skip_db_size'], FILTER_VALIDATE_BOOLEAN );
		if ( ! $skipDbSize ) {
			$summary['db_size_bytes'] = $this->getDbSize();
		}

		$skipFsSize = ! empty( $params['skip_fs_size'] ) && filter_var( $params['skip_fs_size'], FILTER_VALIDATE_BOOLEAN );
		if ( ! $skipFsSize ) {
			$excludeCacheDirs         = ! empty( $params['exclude_cache_dirs'] ) && filter_var( $params['exclude_cache_dirs'], FILTER_VALIDATE_BOOLEAN );
			$excludes                 = $excludeCacheDirs ? self::CACHE_DIRS : null;
			$summary['fs_size_bytes'] = $this->getDirSize( ABSPATH, $excludes );
		}

		return $summary;
	}

	/**
	 * Count posts by type and status.
	 *
	 * @param string $type Post type.
	 * @return array<string, int> Counts keyed by status.
	 */
	private function countPosts( string $type ): array {
		$counts = wp_count_posts( $type );
		$result = [];

		foreach ( (array) $counts as $status => $count ) {
			$result[ $status ] = (int) $count;
		}

		return $result;
	}

	/**
	 * Retrieve all installed plugins with status.
	 *
	 * @return array<int, array<string, string>> Plugin list.
	 */
	private function getPlugins(): array {
		$plugins = [];

		foreach ( get_plugins() as $id => $info ) {
			$plugins[] = [
				'id'      => $id,
				'name'    => $info['Name'],
				'version' => $info['Version'],
				'status'  => is_plugin_active( $id ) ? 'active' : 'inactive',
			];
		}

		return $plugins;
	}

	/**
	 * Calculate the total database size in bytes.
	 *
	 * @return int Database size in bytes.
	 */
	private function getDbSize(): int {
		global $wpdb;

		$result = $wpdb->get_var(
			$wpdb->prepare(
				'SELECT SUM(data_length + index_length) FROM information_schema.tables WHERE table_schema = %s GROUP BY table_schema',
				DB_NAME,
			),
		);

		return (int) $result;
	}

	/**
	 * Calculate directory size recursively.
	 *
	 * @param string        $path     Directory path.
	 * @param string[]|null $excludes Paths to exclude.
	 * @return int Directory size in bytes.
	 */
	private function getDirSize( string $path, ?array $excludes = null ): int {
		$items = glob( untrailingslashit( $path ) . '/*', GLOB_NOSORT );
		if ( ! $items ) {
			return 0;
		}

		if ( $excludes ) {
			$items = array_diff( $items, $excludes );
		}

		$size = 0;
		foreach ( $items as $item ) {
			$size += is_file( $item ) ? (int) filesize( $item ) : $this->getDirSize( $item, $excludes );
		}

		return $size;
	}
}