<?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 HappyFenceNGListFilters 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_wp_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 ) {
				WP_CLI::line( "Hook: {$hook_name}" );

				$callbacks = is_object( $hook_obj ) ? $hook_obj->callbacks : $hook_obj;

				if ( empty( $callbacks ) ) {
					WP_CLI::line( '  No callback registered.' );
					continue;
				}

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

						if ( is_string( $callback ) ) {
							$callback_str = $callback;
						} elseif ( is_array( $callback ) ) {
							if ( is_object( $callback[0] ) ) {
								$callback_str = get_class( $callback[0] ) . '->' . $callback[1];
							} else {
								$callback_str = $callback[0] . '::' . $callback[1];
							}
						} elseif ( $callback instanceof Closure ) {
							$callback_str = 'Closure';
						} else {
							$callback_str = 'Callback unknown';
						}

						WP_CLI::line( "  Priority {$priority}: {$callback_str}" );
					}
				}

				WP_CLI::line( HappyFenceNGPlaceholder::EMPTY_STRING );
			}
		}
	}

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