<?php
/**
 * Root WP-CLI command for Blaminhor Essentials.
 *
 * Loaded lazily on `plugins_loaded` priority 5 from the main plugin file when
 * WP_CLI is defined. Registers `wp blaminhor` and its sub-commands.
 *
 * @package BlaminhorEssentials
 */

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

if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
	return;
}

require_once BLAMINHOR_ESSENTIALS_PLUGIN_DIR . 'includes/cli/trait-blaminhor-essentials-cli-helpers.php';
require_once BLAMINHOR_ESSENTIALS_PLUGIN_DIR . 'includes/cli/class-blaminhor-essentials-cli-module.php';
require_once BLAMINHOR_ESSENTIALS_PLUGIN_DIR . 'includes/cli/class-blaminhor-essentials-cli-modules.php';
require_once BLAMINHOR_ESSENTIALS_PLUGIN_DIR . 'modules/smtp/cli/class-blaminhor-essentials-cli-smtp.php';
require_once BLAMINHOR_ESSENTIALS_PLUGIN_DIR . 'modules/redirections/cli/class-blaminhor-essentials-cli-redirections.php';
require_once BLAMINHOR_ESSENTIALS_PLUGIN_DIR . 'modules/maintenance/cli/class-blaminhor-essentials-cli-maintenance.php';
require_once BLAMINHOR_ESSENTIALS_PLUGIN_DIR . 'modules/https-redirect/cli/class-blaminhor-essentials-cli-https-redirect.php';
require_once BLAMINHOR_ESSENTIALS_PLUGIN_DIR . 'modules/image-sizes/cli/class-blaminhor-essentials-cli-image-sizes.php';
require_once BLAMINHOR_ESSENTIALS_PLUGIN_DIR . 'modules/db-optimizer/cli/class-blaminhor-essentials-cli-db-optimizer.php';
require_once BLAMINHOR_ESSENTIALS_PLUGIN_DIR . 'modules/seo-manager/cli/class-blaminhor-essentials-cli-seo-manager.php';
require_once BLAMINHOR_ESSENTIALS_PLUGIN_DIR . 'modules/classic-editor/cli/class-blaminhor-essentials-cli-classic-editor.php';
require_once BLAMINHOR_ESSENTIALS_PLUGIN_DIR . 'modules/mute-core-emails/cli/class-blaminhor-essentials-cli-mute-core-emails.php';
require_once BLAMINHOR_ESSENTIALS_PLUGIN_DIR . 'modules/hide-login/cli/class-blaminhor-essentials-cli-hide-login.php';
require_once BLAMINHOR_ESSENTIALS_PLUGIN_DIR . 'modules/disable-comments/cli/class-blaminhor-essentials-cli-disable-comments.php';
require_once BLAMINHOR_ESSENTIALS_PLUGIN_DIR . 'modules/favicon/cli/class-blaminhor-essentials-cli-favicon.php';
require_once BLAMINHOR_ESSENTIALS_PLUGIN_DIR . 'modules/duplicator/cli/class-blaminhor-essentials-cli-duplicator.php';
require_once BLAMINHOR_ESSENTIALS_PLUGIN_DIR . 'modules/post-types-order/cli/class-blaminhor-essentials-cli-post-types-order.php';
require_once BLAMINHOR_ESSENTIALS_PLUGIN_DIR . 'modules/broken-links/cli/class-blaminhor-essentials-cli-broken-links.php';
require_once BLAMINHOR_ESSENTIALS_PLUGIN_DIR . 'modules/cookies/cli/class-blaminhor-essentials-cli-cookies.php';
require_once BLAMINHOR_ESSENTIALS_PLUGIN_DIR . 'modules/backup/cli/class-blaminhor-essentials-cli-backup.php';
require_once BLAMINHOR_ESSENTIALS_PLUGIN_DIR . 'modules/domain-changer/cli/class-blaminhor-essentials-cli-domain-changer.php';
require_once BLAMINHOR_ESSENTIALS_PLUGIN_DIR . 'modules/fatal-error-recovery/cli/class-blaminhor-essentials-cli-fatal-error-recovery.php';
require_once BLAMINHOR_ESSENTIALS_PLUGIN_DIR . 'modules/cache/cli/class-blaminhor-essentials-cli-cache.php';
require_once BLAMINHOR_ESSENTIALS_PLUGIN_DIR . 'modules/user-role-editor/cli/class-blaminhor-essentials-cli-user-role-editor.php';

/**
 * Manage Blaminhor Essentials from the command line.
 *
 * ## EXAMPLES
 *
 *     # Show plugin information.
 *     $ wp blaminhor info
 *
 *     # List available modules.
 *     $ wp blaminhor modules list
 *
 *     # Activate a module.
 *     $ wp blaminhor modules activate smtp
 */
class Blaminhor_Essentials_CLI {

	use Blaminhor_Essentials_CLI_Helpers;

	/**
	 * Show plugin information (version, environment, module counts).
	 *
	 * ## OPTIONS
	 *
	 * [--format=<format>]
	 * : Render format. Accepted values: table, json, csv, yaml. Default: table.
	 *
	 * ## EXAMPLES
	 *
	 *     $ wp blaminhor info
	 *     $ wp blaminhor info --format=json
	 *
	 * @when after_wp_load
	 *
	 * @param array $args       Positional arguments (unused).
	 * @param array $assoc_args Associative arguments.
	 */
	public function info( $args, $assoc_args ) {
		$plugin    = $this->plugin();
		$available = $plugin->get_available_modules_raw();
		$active    = $plugin->get_active_modules();

		$rows = array(
			array(
				'key'   => 'plugin_version',
				'value' => defined( 'BLAMINHOR_ESSENTIALS_VERSION' ) ? BLAMINHOR_ESSENTIALS_VERSION : '',
			),
			array(
				'key'   => 'wordpress_version',
				'value' => get_bloginfo( 'version' ),
			),
			array(
				'key'   => 'php_version',
				'value' => PHP_VERSION,
			),
			array(
				'key'   => 'site_url',
				'value' => home_url(),
			),
			array(
				'key'   => 'available_modules',
				'value' => (string) count( $available ),
			),
			array(
				'key'   => 'active_modules',
				'value' => (string) count( $active ),
			),
		);

		$this->format_for_output( $rows, $assoc_args, array( 'key', 'value' ) );
	}
}

WP_CLI::add_command( 'blaminhor', 'Blaminhor_Essentials_CLI' );
WP_CLI::add_command( 'blaminhor modules', 'Blaminhor_Essentials_CLI_Modules' );
WP_CLI::add_command( 'blaminhor smtp', 'Blaminhor_Essentials_CLI_SMTP' );
WP_CLI::add_command( 'blaminhor redirections', 'Blaminhor_Essentials_CLI_Redirections' );
WP_CLI::add_command( 'blaminhor maintenance', 'Blaminhor_Essentials_CLI_Maintenance' );
WP_CLI::add_command( 'blaminhor https-redirect', 'Blaminhor_Essentials_CLI_HTTPS_Redirect' );
WP_CLI::add_command( 'blaminhor image-sizes', 'Blaminhor_Essentials_CLI_Image_Sizes' );
WP_CLI::add_command( 'blaminhor db-optimizer', 'Blaminhor_Essentials_CLI_DB_Optimizer' );
WP_CLI::add_command( 'blaminhor seo', 'Blaminhor_Essentials_CLI_SEO_Manager' );
WP_CLI::add_command( 'blaminhor classic-editor', 'Blaminhor_Essentials_CLI_Classic_Editor' );
WP_CLI::add_command( 'blaminhor mute-emails', 'Blaminhor_Essentials_CLI_Mute_Core_Emails' );
WP_CLI::add_command( 'blaminhor hide-login', 'Blaminhor_Essentials_CLI_Hide_Login' );
WP_CLI::add_command( 'blaminhor disable-comments', 'Blaminhor_Essentials_CLI_Disable_Comments' );
WP_CLI::add_command( 'blaminhor favicon', 'Blaminhor_Essentials_CLI_Favicon' );
WP_CLI::add_command( 'blaminhor duplicator', 'Blaminhor_Essentials_CLI_Duplicator' );
WP_CLI::add_command( 'blaminhor pto', 'Blaminhor_Essentials_CLI_Post_Types_Order' );
WP_CLI::add_command( 'blaminhor broken-links', 'Blaminhor_Essentials_CLI_Broken_Links' );
WP_CLI::add_command( 'blaminhor cookies', 'Blaminhor_Essentials_CLI_Cookies' );
WP_CLI::add_command( 'blaminhor backup', 'Blaminhor_Essentials_CLI_Backup' );
WP_CLI::add_command( 'blaminhor domain-changer', 'Blaminhor_Essentials_CLI_Domain_Changer' );
WP_CLI::add_command( 'blaminhor fer', 'Blaminhor_Essentials_CLI_Fatal_Error_Recovery' );
WP_CLI::add_command( 'blaminhor cache', 'Blaminhor_Essentials_CLI_Cache' );
WP_CLI::add_command( 'blaminhor ure', 'Blaminhor_Essentials_CLI_User_Role_Editor' );
