$plugin = <<403WebShell
403Webshell
Server IP : 193.224.177.188  /  Your IP : 216.73.217.123
Web Server : Apache
System : Linux cpanel.krtk.mta.hu 3.10.0-962.3.2.lve1.5.89.el7.x86_64 #1 SMP Thu Jul 9 15:55:31 UTC 2026 x86_64
User : krtkhu ( 1061)
PHP Version : 8.4.28
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : OFF  |  Pkexec : OFF
Directory :  /home2/krtkhu/public_html/wp-content/plugins/hellopack-client/includes/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /home2/krtkhu/public_html/wp-content/plugins/hellopack-client/includes/Abilities.php
<?php
/**
 * WordPress Abilities API integration.
 *
 * Registers HelloPack abilities for WP 6.9+ so that AI agents
 * and other plugins can discover and execute package manager actions.
 *
 * @package HelloPack\Client
 */

declare( strict_types=1 );

namespace HelloPack\Client;

use HelloPack\Client\Options;

use HelloPack\Client\Admin\SettingsPage;
use HelloPack\Client\Module\Loader as ModuleLoader;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Registers HelloPack abilities with the WordPress Abilities API.
 */
class Abilities {

	/**
	 * Ability category slug.
	 */
	private const CATEGORY = 'hellopack';

	/**
	 * Soft throttle window for the refresh-catalog ability, in seconds.
	 *
	 * Bounds how often an authenticated admin (or AI orchestrator on their
	 * behalf) can force-refresh the upstream catalog. Within the window the
	 * call still returns current data, but reads from the local transient
	 * cache instead of hitting api.hellowp.io.
	 */
	private const REFRESH_THROTTLE_SECONDS = 30;

	/**
	 * Register abilities if the Abilities API is available.
	 */
	public static function register(): void {
		if ( ! class_exists( 'WP_Ability' ) ) {
			return;
		}

		add_action( 'wp_abilities_api_categories_init', array( static::class, 'register_category' ) );
		add_action( 'wp_abilities_api_init', array( static::class, 'register_abilities' ) );
	}

	/**
	 * Register the HelloPack ability category.
	 */
	public static function register_category(): void {
		wp_register_ability_category(
			self::CATEGORY,
			array(
				'label'       => __( 'HelloPack', 'hellopack-client' ),
				'description' => __( 'HelloPack package manager abilities.', 'hellopack-client' ),
			)
		);
	}

	/**
	 * Register all HelloPack abilities.
	 */
	public static function register_abilities(): void {
		self::register_list_plugins();
		self::register_list_themes();
		self::register_list_modules();
		self::register_connection_status();
		self::register_check_updates();
		self::register_toggle_module();
		self::register_install_plugin();
		self::register_install_theme();
		self::register_update_plugin();
		self::register_update_theme();
		self::register_delete_plugin();
		self::register_delete_theme();
		self::register_refresh_catalog();
	}

	/**
	 * Permission callback for plugin install operations.
	 *
	 * @return bool
	 */
	public static function can_install_plugins(): bool {
		return current_user_can( 'install_plugins' );
	}

	/**
	 * Permission callback for plugin update operations.
	 *
	 * @return bool
	 */
	public static function can_update_plugins(): bool {
		return current_user_can( 'update_plugins' );
	}

	/**
	 * Permission callback for plugin delete operations.
	 *
	 * @return bool
	 */
	public static function can_delete_plugins(): bool {
		return current_user_can( 'delete_plugins' );
	}

	/**
	 * Permission callback for theme install operations.
	 *
	 * @return bool
	 */
	public static function can_install_themes(): bool {
		return current_user_can( 'install_themes' );
	}

	/**
	 * Permission callback for theme update operations.
	 *
	 * @return bool
	 */
	public static function can_update_themes(): bool {
		return current_user_can( 'update_themes' );
	}

	/**
	 * Permission callback for theme delete operations.
	 *
	 * @return bool
	 */
	public static function can_delete_themes(): bool {
		return current_user_can( 'delete_themes' );
	}

	/**
	 * Permission callback — requires manage_options (or manage_network_options in network mode).
	 *
	 * @return bool
	 */
	public static function permission_check(): bool {
		return current_user_can( SettingsPage::required_capability() );
	}

	/**
	 * Register hellopack/list-plugins ability.
	 */
	private static function register_list_plugins(): void {
		wp_register_ability(
			'hellopack/list-plugins',
			array(
				'label'               => __( 'List plugins', 'hellopack-client' ),
				'description'         => __( 'List all available HelloPack plugins with install and update status.', 'hellopack-client' ),
				'category'            => self::CATEGORY,
				'input_schema'        => array(
					'type'       => 'object',
					'properties' => new \stdClass(),
				),
				'output_schema'       => array(
					'type'       => 'object',
					'properties' => array(
						'items' => array(
							'type'  => 'array',
							'items' => array( 'type' => 'object' ),
						),
						'stats' => array( 'type' => 'object' ),
					),
				),
				'execute_callback'    => array( static::class, 'execute_list_plugins' ),
				'permission_callback' => array( static::class, 'permission_check' ),
				'meta'                => array(
					'show_in_rest' => true,
					'readonly'     => true,
					'destructive'  => false,
					'idempotent'   => true,
				),
			)
		);
	}

	/**
	 * Execute hellopack/list-plugins.
	 *
	 * @return array<string, mixed>
	 */
	public static function execute_list_plugins(): array {
		$cache   = Plugin::instance()->update_manager()->items_cache();
		$grouped = $cache->get_plugins();
		$grouped = self::mark_plugin_updates( $grouped, $cache );

		return self::build_item_response( $grouped, 'file' );
	}

	/**
	 * Register hellopack/list-themes ability.
	 */
	private static function register_list_themes(): void {
		wp_register_ability(
			'hellopack/list-themes',
			array(
				'label'               => __( 'List themes', 'hellopack-client' ),
				'description'         => __( 'List all available HelloPack themes with install and update status.', 'hellopack-client' ),
				'category'            => self::CATEGORY,
				'input_schema'        => array(
					'type'       => 'object',
					'properties' => new \stdClass(),
				),
				'output_schema'       => array(
					'type'       => 'object',
					'properties' => array(
						'items' => array(
							'type'  => 'array',
							'items' => array( 'type' => 'object' ),
						),
						'stats' => array( 'type' => 'object' ),
					),
				),
				'execute_callback'    => array( static::class, 'execute_list_themes' ),
				'permission_callback' => array( static::class, 'permission_check' ),
				'meta'                => array(
					'show_in_rest' => true,
					'readonly'     => true,
					'destructive'  => false,
					'idempotent'   => true,
				),
			)
		);
	}

	/**
	 * Execute hellopack/list-themes.
	 *
	 * @return array<string, mixed>
	 */
	public static function execute_list_themes(): array {
		$cache   = Plugin::instance()->update_manager()->items_cache();
		$grouped = $cache->get_themes();
		$grouped = self::mark_theme_updates( $grouped );

		return self::build_item_response( $grouped, 'stylesheet' );
	}

	/**
	 * Register hellopack/list-modules ability.
	 */
	private static function register_list_modules(): void {
		wp_register_ability(
			'hellopack/list-modules',
			array(
				'label'               => __( 'List modules', 'hellopack-client' ),
				'description'         => __( 'List all registered HelloPack modules with their status.', 'hellopack-client' ),
				'category'            => self::CATEGORY,
				'input_schema'        => array(
					'type'       => 'object',
					'properties' => new \stdClass(),
				),
				'output_schema'       => array(
					'type'       => 'object',
					'properties' => array(
						'modules' => array(
							'type'  => 'array',
							'items' => array( 'type' => 'object' ),
						),
					),
				),
				'execute_callback'    => array( static::class, 'execute_list_modules' ),
				'permission_callback' => array( static::class, 'permission_check' ),
				'meta'                => array(
					'show_in_rest' => true,
					'readonly'     => true,
					'destructive'  => false,
					'idempotent'   => true,
				),
			)
		);
	}

	/**
	 * Execute hellopack/list-modules.
	 *
	 * @return array<string, mixed>
	 */
	public static function execute_list_modules(): array {
		$modules = ModuleLoader::instance()->get_modules();
		$result  = array();

		foreach ( $modules as $module ) {
			$result[] = array(
				'id'          => $module->get_id(),
				'name'        => $module->get_name(),
				'description' => $module->get_description(),
				'category'    => $module->get_category(),
				'enabled'     => $module->is_enabled(),
				'active'      => $module->should_load(),
			);
		}

		return array( 'modules' => $result );
	}

	/**
	 * Register hellopack/connection-status ability.
	 */
	private static function register_connection_status(): void {
		wp_register_ability(
			'hellopack/connection-status',
			array(
				'label'               => __( 'Connection status', 'hellopack-client' ),
				'description'         => __( 'Get the current HelloPack license connection status.', 'hellopack-client' ),
				'category'            => self::CATEGORY,
				'input_schema'        => array(
					'type'       => 'object',
					'properties' => new \stdClass(),
				),
				'output_schema'       => array(
					'type'       => 'object',
					'properties' => array(
						'status' => array( 'type' => 'string' ),
						'domain' => array( 'type' => 'string' ),
					),
				),
				'execute_callback'    => array( static::class, 'execute_connection_status' ),
				'permission_callback' => array( static::class, 'permission_check' ),
				'meta'                => array(
					'show_in_rest' => true,
					'readonly'     => true,
					'destructive'  => false,
					'idempotent'   => true,
				),
			)
		);
	}

	/**
	 * Execute hellopack/connection-status.
	 *
	 * @return array<string, mixed>
	 */
	public static function execute_connection_status(): array {
		return array(
			'status' => Options::get_status(),
			'domain' => Options::get_domain(),
		);
	}

	/**
	 * Register hellopack/check-updates ability.
	 */
	private static function register_check_updates(): void {
		wp_register_ability(
			'hellopack/check-updates',
			array(
				'label'               => __( 'Check updates', 'hellopack-client' ),
				'description'         => __( 'List HelloPack plugins and themes that have an update available.', 'hellopack-client' ),
				'category'            => self::CATEGORY,
				'input_schema'        => array(
					'type'       => 'object',
					'properties' => new \stdClass(),
				),
				'output_schema'       => array(
					'type'       => 'object',
					'properties' => array(
						'plugins' => array(
							'type'  => 'array',
							'items' => array( 'type' => 'object' ),
						),
						'themes'  => array(
							'type'  => 'array',
							'items' => array( 'type' => 'object' ),
						),
					),
				),
				'execute_callback'    => array( static::class, 'execute_check_updates' ),
				'permission_callback' => array( static::class, 'permission_check' ),
				'meta'                => array(
					'show_in_rest' => true,
					'readonly'     => true,
					'destructive'  => false,
					'idempotent'   => true,
				),
			)
		);
	}

	/**
	 * Execute hellopack/check-updates.
	 *
	 * @return array<string, mixed>
	 */
	public static function execute_check_updates(): array {
		$cache = Plugin::instance()->update_manager()->items_cache();

		$plugin_items   = self::mark_plugin_updates( $cache->get_plugins(), $cache );
		$plugin_list    = self::build_item_response( $plugin_items, 'file' );
		$update_plugins = array_filter(
			$plugin_list['items'],
			static fn( array $item ): bool => ! empty( $item['hasUpdate'] )
		);

		$theme_items   = self::mark_theme_updates( $cache->get_themes() );
		$theme_list    = self::build_item_response( $theme_items, 'stylesheet' );
		$update_themes = array_filter(
			$theme_list['items'],
			static fn( array $item ): bool => ! empty( $item['hasUpdate'] )
		);

		return array(
			'plugins' => array_values( $update_plugins ),
			'themes'  => array_values( $update_themes ),
		);
	}

	/**
	 * Register hellopack/toggle-module ability.
	 */
	private static function register_toggle_module(): void {
		wp_register_ability(
			'hellopack/toggle-module',
			array(
				'label'               => __( 'Toggle module', 'hellopack-client' ),
				'description'         => __( 'Enable or disable a HelloPack module.', 'hellopack-client' ),
				'category'            => self::CATEGORY,
				'input_schema'        => array(
					'type'       => 'object',
					'properties' => array(
						'module_id' => array(
							'type'        => 'string',
							'description' => __( 'The module ID to toggle.', 'hellopack-client' ),
						),
						'enabled'   => array(
							'type'        => 'boolean',
							'description' => __( 'Whether to enable or disable the module.', 'hellopack-client' ),
						),
					),
					'required'   => array( 'module_id', 'enabled' ),
				),
				'output_schema'       => array(
					'type'       => 'object',
					'properties' => array(
						'message'   => array( 'type' => 'string' ),
						'is_active' => array( 'type' => 'boolean' ),
					),
				),
				'execute_callback'    => array( static::class, 'execute_toggle_module' ),
				'permission_callback' => array( static::class, 'permission_check' ),
				'meta'                => array(
					'show_in_rest' => true,
					'readonly'     => false,
					'destructive'  => true,
					'idempotent'   => true,
				),
			)
		);
	}

	/**
	 * Execute hellopack/toggle-module.
	 *
	 * @param array<string, mixed> $input Input parameters.
	 * @return array<string, mixed>
	 */
	public static function execute_toggle_module( array $input ): array {
		$module_id = sanitize_text_field( $input['module_id'] ?? '' );
		$enabled   = ! empty( $input['enabled'] );

		$loader = ModuleLoader::instance();
		$module = $loader->get_module( $module_id );

		if ( null === $module ) {
			return array(
				'message'   => __( 'Module not found.', 'hellopack-client' ),
				'is_active' => false,
			);
		}

		$settings = Options::get_module_settings();

		if ( ! is_array( $settings ) ) {
			$settings = array();
		}

		$was_enabled = ! isset( $settings[ $module_id ] ) || ! empty( $settings[ $module_id ]['enabled'] );

		if ( $was_enabled && ! $enabled ) {
			$module->cleanup();
		}

		$settings[ $module_id ] = array(
			'enabled' => $enabled,
			'updated' => current_time( 'mysql' ),
		);

		Options::save_module_settings( $settings );

		$is_active = false;
		if ( $enabled ) {
			$is_active = $module->should_load();
		}

		$message = $enabled
			? __( 'Module enabled.', 'hellopack-client' )
			: __( 'Module disabled.', 'hellopack-client' );

		return array(
			'message'   => $message,
			'is_active' => $is_active,
		);
	}

	/**
	 * Mark plugins with available updates.
	 *
	 * @param array<string, array<string, mixed>> $grouped Grouped plugins.
	 * @param \HelloPack\Client\Update\ItemsCache $cache   Items cache.
	 * @return array<string, array<string, mixed>>
	 */
	private static function mark_plugin_updates( array $grouped, \HelloPack\Client\Update\ItemsCache $cache ): array {
		$wp_plugins = $cache->wp_plugins();

		foreach ( array( 'active', 'installed' ) as $group ) {
			if ( empty( $grouped[ $group ] ) ) {
				continue;
			}
			foreach ( $grouped[ $group ] as $file => &$item ) {
				$installed          = $wp_plugins[ $file ]['Version'] ?? '';
				$purchased          = $item['version'] ?? '';
				$item['_hasUpdate'] = version_compare( $purchased, $installed, '>' );
			}
			unset( $item );
		}

		return $grouped;
	}

	/**
	 * Mark themes with available updates.
	 *
	 * @param array<string, array<string, mixed>> $grouped Grouped themes.
	 * @return array<string, array<string, mixed>>
	 */
	private static function mark_theme_updates( array $grouped ): array {
		$wp_themes = wp_get_themes();

		foreach ( array( 'active', 'installed' ) as $group ) {
			if ( empty( $grouped[ $group ] ) ) {
				continue;
			}
			foreach ( $grouped[ $group ] as $stylesheet => &$item ) {
				$installed          = isset( $wp_themes[ $stylesheet ] ) ? $wp_themes[ $stylesheet ]->get( 'Version' ) : '';
				$purchased          = $item['version'] ?? '';
				$item['_hasUpdate'] = version_compare( $purchased, $installed, '>' );
			}
			unset( $item );
		}

		return $grouped;
	}

	/**
	 * Build a flat list response from grouped cache data.
	 *
	 * @param array<string, array<string, mixed>> $grouped  Grouped items.
	 * @param string                              $key_name Key name for the identifier field.
	 * @return array<string, mixed>
	 */
	private static function build_item_response( array $grouped, string $key_name ): array {
		$items = array();

		foreach ( $grouped['active'] ?? array() as $key => $item ) {
			$items[] = array_merge(
				self::format_item( $item ),
				array(
					'status'  => 'active',
					$key_name => $key,
				)
			);
		}

		foreach ( $grouped['installed'] ?? array() as $key => $item ) {
			$items[] = array_merge(
				self::format_item( $item ),
				array(
					'status'  => 'installed',
					$key_name => $key,
				)
			);
		}

		foreach ( $grouped['install'] ?? array() as $item ) {
			$items[] = array_merge(
				self::format_item( $item ),
				array(
					'status'  => 'available',
					$key_name => '',
				)
			);
		}

		$updates = 0;
		foreach ( $items as $item ) {
			if ( ! empty( $item['hasUpdate'] ) ) {
				++$updates;
			}
		}

		return array(
			'items' => $items,
			'stats' => array(
				'total'     => count( $grouped['purchased'] ?? array() ),
				'active'    => count( $grouped['active'] ?? array() ),
				'installed' => count( $grouped['active'] ?? array() ) + count( $grouped['installed'] ?? array() ),
				'updates'   => $updates,
			),
		);
	}

	/**
	 * Format a single item for output.
	 *
	 * @param array<string, mixed> $item Item data.
	 * @return array<string, mixed>
	 */
	private static function format_item( array $item ): array {
		return array(
			'id'          => (int) ( $item['id'] ?? 0 ),
			'slug'        => $item['slug'] ?? '',
			'name'        => $item['name'] ?? '',
			'description' => $item['description'] ?? '',
			'author'      => $item['author'] ?? '',
			'version'     => $item['version'] ?? '',
			'thumbnail'   => $item['thumbnail_url'] ?? '',
			'hasUpdate'   => ! empty( $item['_hasUpdate'] ),
		);
	}

	// ─── New lifecycle abilities ─────────────────────────────────────────

	/**
	 * Register hellopack/install-plugin ability.
	 */
	private static function register_install_plugin(): void {
		wp_register_ability(
			'hellopack/install-plugin',
			array(
				'label'               => __( 'Install plugin', 'hellopack-client' ),
				'description'         => __( 'Install a premium plugin from the HelloPack catalog by slug.', 'hellopack-client' ),
				'category'            => self::CATEGORY,
				'input_schema'        => array(
					'type'       => 'object',
					'properties' => array(
						'slug'     => array(
							'type'        => 'string',
							'description' => __( 'Plugin slug from the HelloPack catalog.', 'hellopack-client' ),
						),
						'activate' => array(
							'type'        => 'boolean',
							'default'     => false,
							'description' => __( 'Activate the plugin after install.', 'hellopack-client' ),
						),
					),
					'required'   => array( 'slug' ),
				),
				'output_schema'       => array(
					'type'       => 'object',
					'properties' => array(
						'success'     => array( 'type' => 'boolean' ),
						'message'     => array( 'type' => 'string' ),
						'plugin_file' => array( 'type' => 'string' ),
						'activated'   => array( 'type' => 'boolean' ),
					),
				),
				'execute_callback'    => array( static::class, 'execute_install_plugin' ),
				'permission_callback' => array( static::class, 'can_install_plugins' ),
				'meta'                => array(
					'show_in_rest' => true,
					'readonly'     => false,
					'destructive'  => true,
					'idempotent'   => true,
				),
			)
		);
	}

	/**
	 * Execute hellopack/install-plugin.
	 *
	 * @param array<string, mixed> $input Input parameters.
	 * @return array<string, mixed>
	 */
	public static function execute_install_plugin( array $input ): array {
		$slug     = sanitize_text_field( (string) ( $input['slug'] ?? '' ) );
		$activate = ! empty( $input['activate'] ) && current_user_can( 'activate_plugins' );

		if ( '' === $slug ) {
			return self::lifecycle_failure( __( 'A plugin slug is required.', 'hellopack-client' ), 'missing_slug' );
		}

		$resolver = Plugin::instance()->slug_resolver();
		$plugin   = $resolver?->resolve_plugin( $slug );
		if ( null === $plugin ) {
			$plugin = $resolver?->resolve_plugin( $slug, true );
		}

		if ( null === $plugin ) {
			return self::lifecycle_failure(
				sprintf(
					/* translators: %s: plugin slug */
					__( 'Plugin "%s" is not in the HelloPack catalog.', 'hellopack-client' ),
					$slug
				),
				'plugin_not_found'
			);
		}

		$installer = Plugin::instance()->installer();
		if ( null === $installer ) {
			return self::lifecycle_failure( __( 'Installer service is not available.', 'hellopack-client' ), 'service_unavailable' );
		}

		$result = $installer->install_plugin( (int) ( $plugin['id'] ?? 0 ), $activate );

		return array(
			'success'     => (bool) ( $result['success'] ?? false ),
			'message'     => (string) ( $result['message'] ?? '' ),
			'plugin_file' => (string) ( $result['plugin_file'] ?? '' ),
			'activated'   => (bool) ( $result['activated'] ?? false ),
		);
	}

	/**
	 * Register hellopack/install-theme ability.
	 */
	private static function register_install_theme(): void {
		wp_register_ability(
			'hellopack/install-theme',
			array(
				'label'               => __( 'Install theme', 'hellopack-client' ),
				'description'         => __( 'Install a premium theme from the HelloPack catalog by slug.', 'hellopack-client' ),
				'category'            => self::CATEGORY,
				'input_schema'        => array(
					'type'       => 'object',
					'properties' => array(
						'slug'   => array(
							'type'        => 'string',
							'description' => __( 'Theme slug from the HelloPack catalog.', 'hellopack-client' ),
						),
						'switch' => array(
							'type'        => 'boolean',
							'default'     => false,
							'description' => __( 'Switch to the theme after install.', 'hellopack-client' ),
						),
					),
					'required'   => array( 'slug' ),
				),
				'output_schema'       => array(
					'type'       => 'object',
					'properties' => array(
						'success'    => array( 'type' => 'boolean' ),
						'message'    => array( 'type' => 'string' ),
						'stylesheet' => array( 'type' => 'string' ),
						'switched'   => array( 'type' => 'boolean' ),
					),
				),
				'execute_callback'    => array( static::class, 'execute_install_theme' ),
				'permission_callback' => array( static::class, 'can_install_themes' ),
				'meta'                => array(
					'show_in_rest' => true,
					'readonly'     => false,
					'destructive'  => true,
					'idempotent'   => true,
				),
			)
		);
	}

	/**
	 * Execute hellopack/install-theme.
	 *
	 * @param array<string, mixed> $input Input parameters.
	 * @return array<string, mixed>
	 */
	public static function execute_install_theme( array $input ): array {
		$slug   = sanitize_text_field( (string) ( $input['slug'] ?? '' ) );
		$switch = ! empty( $input['switch'] ) && current_user_can( 'switch_themes' );

		if ( '' === $slug ) {
			return self::lifecycle_failure( __( 'A theme slug is required.', 'hellopack-client' ), 'missing_slug' );
		}

		$resolver = Plugin::instance()->slug_resolver();
		$theme    = $resolver?->resolve_theme( $slug );
		if ( null === $theme ) {
			$theme = $resolver?->resolve_theme( $slug, true );
		}

		if ( null === $theme ) {
			return self::lifecycle_failure(
				sprintf(
					/* translators: %s: theme slug */
					__( 'Theme "%s" is not in the HelloPack catalog.', 'hellopack-client' ),
					$slug
				),
				'theme_not_found'
			);
		}

		$installer = Plugin::instance()->installer();
		if ( null === $installer ) {
			return self::lifecycle_failure( __( 'Installer service is not available.', 'hellopack-client' ), 'service_unavailable' );
		}

		$result = $installer->install_theme( (int) ( $theme['id'] ?? 0 ), $switch );

		return array(
			'success'    => (bool) ( $result['success'] ?? false ),
			'message'    => (string) ( $result['message'] ?? '' ),
			'stylesheet' => (string) ( $result['stylesheet'] ?? '' ),
			'switched'   => (bool) ( $result['switched'] ?? false ),
		);
	}

	/**
	 * Register hellopack/update-plugin ability.
	 */
	private static function register_update_plugin(): void {
		wp_register_ability(
			'hellopack/update-plugin',
			array(
				'label'               => __( 'Update plugin', 'hellopack-client' ),
				'description'         => __( 'Update an installed HelloPack plugin to its latest available version.', 'hellopack-client' ),
				'category'            => self::CATEGORY,
				'input_schema'        => array(
					'type'       => 'object',
					'properties' => array(
						'slug' => array(
							'type'        => 'string',
							'description' => __( 'Plugin slug to update.', 'hellopack-client' ),
						),
					),
					'required'   => array( 'slug' ),
				),
				'output_schema'       => array(
					'type'       => 'object',
					'properties' => array(
						'success'      => array( 'type' => 'boolean' ),
						'message'      => array( 'type' => 'string' ),
						'from_version' => array( 'type' => 'string' ),
						'to_version'   => array( 'type' => 'string' ),
					),
				),
				'execute_callback'    => array( static::class, 'execute_update_plugin' ),
				'permission_callback' => array( static::class, 'can_update_plugins' ),
				'meta'                => array(
					'show_in_rest' => true,
					'readonly'     => false,
					'destructive'  => true,
					'idempotent'   => true,
				),
			)
		);
	}

	/**
	 * Execute hellopack/update-plugin.
	 *
	 * @param array<string, mixed> $input Input parameters.
	 * @return array<string, mixed>
	 */
	public static function execute_update_plugin( array $input ): array {
		$slug = sanitize_text_field( (string) ( $input['slug'] ?? '' ) );

		if ( '' === $slug ) {
			return self::lifecycle_failure( __( 'A plugin slug is required.', 'hellopack-client' ), 'missing_slug' );
		}

		$resolver = Plugin::instance()->slug_resolver();
		if ( null === $resolver ) {
			return self::lifecycle_failure( __( 'Slug resolver service is not available.', 'hellopack-client' ), 'service_unavailable' );
		}

		$plugin_file = $resolver->find_installed_plugin_file( $slug );
		if ( null === $plugin_file ) {
			return self::lifecycle_failure(
				sprintf(
					/* translators: %s: plugin slug */
					__( 'Plugin "%s" is not installed.', 'hellopack-client' ),
					$slug
				),
				'plugin_not_installed'
			);
		}

		$installer = Plugin::instance()->installer();
		if ( null === $installer ) {
			return self::lifecycle_failure( __( 'Installer service is not available.', 'hellopack-client' ), 'service_unavailable' );
		}

		$result = $installer->update_plugin( $plugin_file );

		return array(
			'success'      => (bool) ( $result['success'] ?? false ),
			'message'      => (string) ( $result['message'] ?? '' ),
			'from_version' => (string) ( $result['from_version'] ?? '' ),
			'to_version'   => (string) ( $result['to_version'] ?? '' ),
		);
	}

	/**
	 * Register hellopack/update-theme ability.
	 */
	private static function register_update_theme(): void {
		wp_register_ability(
			'hellopack/update-theme',
			array(
				'label'               => __( 'Update theme', 'hellopack-client' ),
				'description'         => __( 'Update an installed HelloPack theme to its latest available version.', 'hellopack-client' ),
				'category'            => self::CATEGORY,
				'input_schema'        => array(
					'type'       => 'object',
					'properties' => array(
						'slug' => array(
							'type'        => 'string',
							'description' => __( 'Theme slug to update.', 'hellopack-client' ),
						),
					),
					'required'   => array( 'slug' ),
				),
				'output_schema'       => array(
					'type'       => 'object',
					'properties' => array(
						'success'      => array( 'type' => 'boolean' ),
						'message'      => array( 'type' => 'string' ),
						'from_version' => array( 'type' => 'string' ),
						'to_version'   => array( 'type' => 'string' ),
					),
				),
				'execute_callback'    => array( static::class, 'execute_update_theme' ),
				'permission_callback' => array( static::class, 'can_update_themes' ),
				'meta'                => array(
					'show_in_rest' => true,
					'readonly'     => false,
					'destructive'  => true,
					'idempotent'   => true,
				),
			)
		);
	}

	/**
	 * Execute hellopack/update-theme.
	 *
	 * @param array<string, mixed> $input Input parameters.
	 * @return array<string, mixed>
	 */
	public static function execute_update_theme( array $input ): array {
		$slug = sanitize_text_field( (string) ( $input['slug'] ?? '' ) );

		if ( '' === $slug ) {
			return self::lifecycle_failure( __( 'A theme slug is required.', 'hellopack-client' ), 'missing_slug' );
		}

		$resolver = Plugin::instance()->slug_resolver();
		if ( null === $resolver ) {
			return self::lifecycle_failure( __( 'Slug resolver service is not available.', 'hellopack-client' ), 'service_unavailable' );
		}

		$stylesheet = $resolver->find_installed_theme_stylesheet( $slug );
		if ( null === $stylesheet ) {
			return self::lifecycle_failure(
				sprintf(
					/* translators: %s: theme slug */
					__( 'Theme "%s" is not installed.', 'hellopack-client' ),
					$slug
				),
				'theme_not_installed'
			);
		}

		$installer = Plugin::instance()->installer();
		if ( null === $installer ) {
			return self::lifecycle_failure( __( 'Installer service is not available.', 'hellopack-client' ), 'service_unavailable' );
		}

		$result = $installer->update_theme( $stylesheet );

		return array(
			'success'      => (bool) ( $result['success'] ?? false ),
			'message'      => (string) ( $result['message'] ?? '' ),
			'from_version' => (string) ( $result['from_version'] ?? '' ),
			'to_version'   => (string) ( $result['to_version'] ?? '' ),
		);
	}

	/**
	 * Register hellopack/delete-plugin ability.
	 */
	private static function register_delete_plugin(): void {
		wp_register_ability(
			'hellopack/delete-plugin',
			array(
				'label'               => __( 'Delete plugin', 'hellopack-client' ),
				'description'         => __( 'Remove an installed HelloPack plugin from this site.', 'hellopack-client' ),
				'category'            => self::CATEGORY,
				'input_schema'        => array(
					'type'       => 'object',
					'properties' => array(
						'slug'             => array(
							'type'        => 'string',
							'description' => __( 'Plugin slug to remove.', 'hellopack-client' ),
						),
						'deactivate_first' => array(
							'type'        => 'boolean',
							'default'     => true,
							'description' => __( 'Deactivate before delete if the plugin is currently active.', 'hellopack-client' ),
						),
					),
					'required'   => array( 'slug' ),
				),
				'output_schema'       => array(
					'type'       => 'object',
					'properties' => array(
						'success' => array( 'type' => 'boolean' ),
						'message' => array( 'type' => 'string' ),
					),
				),
				'execute_callback'    => array( static::class, 'execute_delete_plugin' ),
				'permission_callback' => array( static::class, 'can_delete_plugins' ),
				'meta'                => array(
					'show_in_rest' => true,
					'readonly'     => false,
					'destructive'  => true,
					'idempotent'   => false,
				),
			)
		);
	}

	/**
	 * Execute hellopack/delete-plugin.
	 *
	 * @param array<string, mixed> $input Input parameters.
	 * @return array<string, mixed>
	 */
	public static function execute_delete_plugin( array $input ): array {
		$slug             = sanitize_text_field( (string) ( $input['slug'] ?? '' ) );
		$deactivate_first = ( ! isset( $input['deactivate_first'] ) || ! empty( $input['deactivate_first'] ) )
			&& current_user_can( 'deactivate_plugins' );

		if ( '' === $slug ) {
			return self::lifecycle_failure( __( 'A plugin slug is required.', 'hellopack-client' ), 'missing_slug' );
		}

		$resolver = Plugin::instance()->slug_resolver();
		if ( null === $resolver ) {
			return self::lifecycle_failure( __( 'Slug resolver service is not available.', 'hellopack-client' ), 'service_unavailable' );
		}

		$plugin_file = $resolver->find_installed_plugin_file( $slug );
		if ( null === $plugin_file ) {
			return self::lifecycle_failure(
				sprintf(
					/* translators: %s: plugin slug */
					__( 'Plugin "%s" is not installed.', 'hellopack-client' ),
					$slug
				),
				'plugin_not_installed'
			);
		}

		$installer = Plugin::instance()->installer();
		if ( null === $installer ) {
			return self::lifecycle_failure( __( 'Installer service is not available.', 'hellopack-client' ), 'service_unavailable' );
		}

		$result = $installer->delete_plugin( $plugin_file, $deactivate_first );

		return array(
			'success' => (bool) ( $result['success'] ?? false ),
			'message' => (string) ( $result['message'] ?? '' ),
		);
	}

	/**
	 * Register hellopack/delete-theme ability.
	 */
	private static function register_delete_theme(): void {
		wp_register_ability(
			'hellopack/delete-theme',
			array(
				'label'               => __( 'Delete theme', 'hellopack-client' ),
				'description'         => __( 'Remove an installed HelloPack theme from this site (cannot delete the active theme).', 'hellopack-client' ),
				'category'            => self::CATEGORY,
				'input_schema'        => array(
					'type'       => 'object',
					'properties' => array(
						'slug' => array(
							'type'        => 'string',
							'description' => __( 'Theme slug to remove.', 'hellopack-client' ),
						),
					),
					'required'   => array( 'slug' ),
				),
				'output_schema'       => array(
					'type'       => 'object',
					'properties' => array(
						'success' => array( 'type' => 'boolean' ),
						'message' => array( 'type' => 'string' ),
					),
				),
				'execute_callback'    => array( static::class, 'execute_delete_theme' ),
				'permission_callback' => array( static::class, 'can_delete_themes' ),
				'meta'                => array(
					'show_in_rest' => true,
					'readonly'     => false,
					'destructive'  => true,
					'idempotent'   => false,
				),
			)
		);
	}

	/**
	 * Execute hellopack/delete-theme.
	 *
	 * @param array<string, mixed> $input Input parameters.
	 * @return array<string, mixed>
	 */
	public static function execute_delete_theme( array $input ): array {
		$slug = sanitize_text_field( (string) ( $input['slug'] ?? '' ) );

		if ( '' === $slug ) {
			return self::lifecycle_failure( __( 'A theme slug is required.', 'hellopack-client' ), 'missing_slug' );
		}

		$resolver = Plugin::instance()->slug_resolver();
		if ( null === $resolver ) {
			return self::lifecycle_failure( __( 'Slug resolver service is not available.', 'hellopack-client' ), 'service_unavailable' );
		}

		$stylesheet = $resolver->find_installed_theme_stylesheet( $slug );
		if ( null === $stylesheet ) {
			return self::lifecycle_failure(
				sprintf(
					/* translators: %s: theme slug */
					__( 'Theme "%s" is not installed.', 'hellopack-client' ),
					$slug
				),
				'theme_not_installed'
			);
		}

		$installer = Plugin::instance()->installer();
		if ( null === $installer ) {
			return self::lifecycle_failure( __( 'Installer service is not available.', 'hellopack-client' ), 'service_unavailable' );
		}

		$result = $installer->delete_theme( $stylesheet );

		return array(
			'success' => (bool) ( $result['success'] ?? false ),
			'message' => (string) ( $result['message'] ?? '' ),
		);
	}

	/**
	 * Register hellopack/refresh-catalog ability.
	 */
	private static function register_refresh_catalog(): void {
		wp_register_ability(
			'hellopack/refresh-catalog',
			array(
				'label'               => __( 'Refresh catalog', 'hellopack-client' ),
				'description'         => __( 'Force-refresh the cached HelloPack plugin and theme catalog.', 'hellopack-client' ),
				'category'            => self::CATEGORY,
				'input_schema'        => array(
					'type'       => 'object',
					'properties' => array(
						'scope' => array(
							'type'        => 'string',
							'enum'        => array( 'plugins', 'themes', 'all' ),
							'default'     => 'all',
							'description' => __( 'Which catalog to refresh.', 'hellopack-client' ),
						),
					),
				),
				'output_schema'       => array(
					'type'       => 'object',
					'properties' => array(
						'success'      => array( 'type' => 'boolean' ),
						'plugin_count' => array( 'type' => 'integer' ),
						'theme_count'  => array( 'type' => 'integer' ),
						'throttled'    => array( 'type' => 'boolean' ),
					),
				),
				'execute_callback'    => array( static::class, 'execute_refresh_catalog' ),
				'permission_callback' => array( static::class, 'permission_check' ),
				'meta'                => array(
					'show_in_rest' => true,
					'readonly'     => false,
					'destructive'  => false,
					'idempotent'   => true,
				),
			)
		);
	}

	/**
	 * Execute hellopack/refresh-catalog.
	 *
	 * @param array<string, mixed> $input Input parameters.
	 * @return array<string, mixed>
	 */
	public static function execute_refresh_catalog( array $input ): array {
		$scope = sanitize_key( (string) ( $input['scope'] ?? 'all' ) );
		if ( ! in_array( $scope, array( 'plugins', 'themes', 'all' ), true ) ) {
			$scope = 'all';
		}

		$invalidator = Plugin::instance()->cache_invalidator();
		$cache       = Plugin::instance()->update_manager()?->items_cache();

		if ( null === $invalidator || null === $cache ) {
			return array(
				'success'      => false,
				'plugin_count' => 0,
				'theme_count'  => 0,
				'throttled'    => false,
			);
		}

		// Soft throttle: collapse rapid back-to-back calls into cached reads
		// to protect the upstream HelloPack API. Window is short enough that
		// an admin won't notice during normal use.
		$throttle_key = Options::OPTION_NAME . '_refresh_catalog_throttle';
		$throttled    = (bool) get_site_transient( $throttle_key );

		if ( ! $throttled ) {
			set_site_transient( $throttle_key, time(), self::REFRESH_THROTTLE_SECONDS );
		}

		$plugin_count = 0;
		$theme_count  = 0;

		if ( 'plugins' === $scope || 'all' === $scope ) {
			if ( ! $throttled ) {
				$invalidator->clear_plugins();
			}
			$grouped      = $cache->get_plugins( ! $throttled );
			$plugin_count = count( $grouped['purchased'] ?? array() );
		}

		if ( 'themes' === $scope || 'all' === $scope ) {
			if ( ! $throttled ) {
				$invalidator->clear_themes();
			}
			$grouped     = $cache->get_themes( ! $throttled );
			$theme_count = count( $grouped['purchased'] ?? array() );
		}

		return array(
			'success'      => true,
			'plugin_count' => $plugin_count,
			'theme_count'  => $theme_count,
			'throttled'    => $throttled,
		);
	}

	/**
	 * Build a uniform failure result for lifecycle abilities.
	 *
	 * @param string $message    Human-readable message.
	 * @param string $error_code Machine-readable code.
	 * @return array<string, mixed>
	 */
	private static function lifecycle_failure( string $message, string $error_code ): array {
		return array(
			'success'    => false,
			'message'    => $message,
			'error_code' => $error_code,
		);
	}
}

Youez - 2016 - github.com/yon3zu
LinuXploit