<?php
/**
 * Wp ULike Admin Pages Class.
 * 
 * @package    wp-ulike
 * @author     TechnoWich 2025
 * @link       https://wpulike.com
*/

// no direct access allowed
if ( ! defined('ABSPATH') ) {
    die();
}

if ( ! class_exists( 'wp_ulike_admin_pages' ) ) {
	/**
	 *  Class to register admin menus
	 */
	class wp_ulike_admin_pages {

		private $submenus, $views;

		/**
		 * __construct
		 */
		function __construct() {

			$this->submenus = apply_filters( 'wp_ulike_admin_pages', array(
				'statistics'      => array(
					'title'       => esc_html__( 'Statistics', 'wp-ulike' ),
					'parent_slug' => 'wp-ulike-settings',
					'capability'  => wp_ulike_get_user_access_capability('stats'),
					'path'        => WP_ULIKE_ADMIN_DIR . '/includes/templates/statistics.php',
					'menu_slug'   => 'wp-ulike-statistics',
					'load_screen' => false
				),
				'about'           => array(
					'title'       => sprintf( '<span class="wp-ulike-menu-icon"><span class="dashicons dashicons-info"></span> %s</span>', esc_html__( 'About', 'wp-ulike' ) ),
					'parent_slug' => 'wp-ulike-settings',
					'capability'  => wp_ulike_get_user_access_capability('stats'),
					'path'        => WP_ULIKE_ADMIN_DIR . '/includes/templates/about.php',
					'menu_slug'   => 'wp-ulike-about',
					'load_screen' => false
				)
			) );

			add_action( 'wp_ulike_settings_loaded', function(){
				add_action( 'admin_menu', array( $this, 'menus' ) );
			} );

		}

		/**
		 * register admin menus
		 *
		 * @return void
		 */
		public function menus() {

			// Register submenus
			foreach ( $this->submenus as $key => $args) {
				// Extract variables explicitly per WordPress coding standards
				$parent_slug = isset( $args['parent_slug'] ) ? $args['parent_slug'] : '';
				$title = isset( $args['title'] ) ? $args['title'] : '';
				$menu_slug = isset( $args['menu_slug'] ) ? $args['menu_slug'] : '';
				$capability = isset( $args['capability'] ) ? $args['capability'] : 'manage_options';
				$load_screen = isset( $args['load_screen'] ) ? $args['load_screen'] : false;
				$path = isset( $args['path'] ) ? $args['path'] : '';

				$hook_suffix = add_submenu_page(
					$parent_slug,
					$title,
					apply_filters( 'wp_ulike_admin_sub_menu_title', $title, $menu_slug ),
					$capability,
					$menu_slug,
					array( &$this, 'load_template' )
				);

				$this->views[ $hook_suffix ] = $path;

				if( $load_screen ) {
					add_action( "load-$hook_suffix", array( $this, 'add_screen_option' ) );
				}
			}

			$this->menu_badge();
		}

		/**
		 * Add custom badges to a menu name
		 *
		 * @return void
		 */
		public function menu_badge(){
			global $menu;

			if( 0 !== ( $badge_count = apply_filters( 'wp_ulike_menu_badge_count', 0 ) ) ) {
				$menu[313][0] .= wp_ulike_badge_count_format( $badge_count );
			}
		}

		/**
		 * Add screen options
		 *
		 * @return void
		 */
		public function add_screen_option(){
			add_screen_option( 'per_page', array(
					'label'   => esc_html__('Logs','wp-ulike'),
					'default' => 30,
					'option'  => 'wp_ulike_logs_per_page'
				)
			);
		}

		/**
		 * Load admin templates
		 *
		 * @return void
		 */
		public function load_template(){
			load_template( $this->views[ current_filter() ] );
		}

	}

}