<?php
/**
 * HappyFenceNG Total Enterprise Security Plugin
 * HAPPYWPRESS is a RazorVue Trademark.
 *
 * CLI Command to view runtime module dashboard
 *
 * @package     HappyFenceNG
 * @developer   Simonluca Scillitani
 * @copyright   2025 RazorVue di Scillitani Simonluca
 * @link        https://razorvue.it
 * @author      RazorVue di Scillitani Simonluca
 * @brand       HappyWPress
 * @uri         https://happywpress.com
 * @set all
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
if ( defined( 'WP_CLI' ) && WP_CLI ) {
	/**PHPCS -> code compliance: we put useful comments where needed.. */
	class HappyFenceNGListHFFilters extends HappyFenceNGRuntimeExceptions {

		/**PHPCS -> code compliance: we put useful comments where needed..
		 *
		 * @param string $args ...are arguments.
		 * @param string $assoc_args ..uhm maybe an array of arguments?.
		 */
		public function list_hf_filters( $args, $assoc_args ) {
			global $wp_filter;
			$input_pass_raw  = $assoc_args['cli-pass'] ?? null;
			$expected_length = 35;

			$input_pass = HappyFenceNGCleaner::sanitize_cli_password( $input_pass_raw, $expected_length );

			if ( is_null( $input_pass ) ) {
				WP_CLI::error( 'Invalid CLI password format or length.' );
				return;
			}

			$stored_pass = HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->PCORE->wpcli_secpass, HappyFenceNGUtils::generateStrongPassword( HappyFenceNGPcoreProps::_PWD_LEN ) );

			if ( ! hash_equals( $stored_pass, $input_pass ) ) {
				WP_CLI::error( 'Invalid CLI password.' );
				return;
			}

			if ( empty( $wp_filter ) ) {
				WP_CLI::warning( 'No filter registered.' );
				return;
			}

			foreach ( $wp_filter as $hook_name => $hook_obj ) {
				$callbacks = is_object( $hook_obj ) ? $hook_obj->callbacks : $hook_obj;

				if ( empty( $callbacks ) ) {
					continue;
				}

				$found = false;

				foreach ( $callbacks as $priority => $functions ) {
					foreach ( $functions as $id => $data ) {
						$callback = $data['function'];

						if ( is_array( $callback ) ) {
							$class_name = is_object( $callback[0] ) ? get_class( $callback[0] ) : $callback[0];

							if ( HappyFenceNGPROC::not_equals( strpos( $class_name, 'HappyFenceNG' ), false ) ) {
								if ( ! $found ) {
									$found = true;
								}
								$method_name = $callback[1];
								if ( method_exists( $class_name, 'getStatus' ) ) {
									$status = $class_name::getStatus( $method_name );
									WP_CLI::line( "Hook: {$hook_name} -  Priority {$priority}: {$class_name}->{$method_name}, enabled=> " . ( $status ? 'true' : 'false' ) );

								}
							}
						}
					}
				}
			}
		}
	}

	WP_CLI::add_command( 'happyfenceng', 'HappyFenceNGListHFFilters' );
}
