<?php
/**
 * Class rpgal_settings_config
 *
 * This class handles the plugin's settings configuration in the WordPress admin dashboard. 
 * It utilizes the Settings API to create and manage settings sections, fields, and pages.
 * It also includes functionality for displaying a help page, adding menu items, and retrieving pages.
 * 
 * @package Responsive Portfolio Image Gallery 1.3 - 10 July, 2025
 * @link https://www.realwebcare.com/
 */
if (!defined('ABSPATH')) {
	exit; // Exit if accessed directly
}

if (!class_exists('rpgal_settings_config')) {
	class rpgal_settings_config
	{

		private $settings_api;
		private $get_functions;
		private $sidebar_info;

		public function __construct()
		{
			// Load necessary required files.
			$this->required_files();

			// Access the Settings API
			$this->settings_api = new RPGAL_WeDevs_Settings_API;

			// Access the Functions
			$this->get_functions = RPGAL_Functions::get_instances();

			// Access the Sidebar
			$this->sidebar_info = RPGAL_Sidebar::get_instances();

			// Sets up the settings sections and fields
			add_action('admin_init', array($this, 'admin_init'));

			// Plugin's menu and submenu
			add_action('admin_menu', array($this, 'admin_menu'));
		}

		/**
		 * Includes necessary files for the settings API and sidebar classes.
		 *
		 * This method ensures that the required dependencies for the settings API and 
		 * sidebar functionalities are properly loaded.
		 *
		 * @return void
		 */
		private function required_files()
		{
			// Settings API wrapper class
			require_once RPGAL_PLUGIN_PATH . 'init/rpgal-class.settings-api.php';
		}


		/**
		 * Initializes the plugin's settings sections and fields.
		 *
		 * This method sets up the settings sections and fields using the settings API
		 * and registers them for use in the admin panel.
		 *
		 * @return void
		 */
		public function admin_init()
		{
			//set the settings
			$this->settings_api->set_sections($this->get_settings_sections());
			$this->settings_api->set_fields($this->get_settings_fields());

			//initialize settings
			$this->settings_api->admin_init();
		}

		function admin_menu()
		{
			// Options
			add_submenu_page(
				'edit.php?post_type=rpgal',
				esc_html__('Generate Portfolio', 'responsive-portfolio-image-gallery'),
				esc_html__('Generate Portfolio', 'responsive-portfolio-image-gallery'),
				'administrator',
				'rpgal-process',
				array($this, 'rpgal_plugin_page')
			);

			// Help
			add_submenu_page(
				'edit.php?post_type=rpgal',
				esc_html__('Portfolio Guide', 'responsive-portfolio-image-gallery'),
				esc_html__('Guide', 'responsive-portfolio-image-gallery'),
				'administrator',
				'rpgal-guide',
				array($this, 'rpgal_guide_page')
			);
		}

		/**
		 * Retrieves all the settings sections for the plugin.
		 *
		 * This method defines and returns the sections used in the plugin's settings page.
		 * Each section contains a unique ID and a title, which are displayed in the settings interface.
		 *
		 * @return array Array of settings sections with their IDs and titles.
		 */
		function get_settings_sections()
		{
			$sections = array(
				array(
					'id' => 'rpgal_general',
					'title' => __('General Settings', 'responsive-portfolio-image-gallery')
				),
				array(
					'id' => 'rpgal_advanced',
					'title' => __('Advance Settings', 'responsive-portfolio-image-gallery')
				),
				array(
					'id' => 'rpgal_style',
					'title' => __('Style Settings', 'responsive-portfolio-image-gallery')
				)
			);
			return $sections;
		}

		/**
		 * Retrieves all the settings fields from the settings API class.
		 *
		 * This method provides the list of settings fields configured in the plugin's settings API.
		 *
		 * @return array Array of settings fields.
		 */
		function get_settings_fields()
		{
			$settings_fields = array(
				'rpgal_general' => array(
					array(
						'name' => 'rpgal_enable',
						'label' => __('Enable Portfolio Gallery', 'responsive-portfolio-image-gallery'),
						'desc' => __('Mark if you want to show Portfolio Gallery.', 'responsive-portfolio-image-gallery'),
						'type' => 'checkbox',
						'default' => 'on',
						'sanitize_callback' => 'sanitize_text_field'
					),
					array(
						'name' => 'rpgal_categories',
						'label' => __('Include Categories', 'responsive-portfolio-image-gallery'),
						'desc' => __('Select the categories you want to show in your portfolio.', 'responsive-portfolio-image-gallery'),
						'type' => 'multicheck',
						'options' => $this->get_functions->rpgal_include_categories(),
					),
					array(
						'name' => 'rpgal_postno',
						'label' => __('Number of post', 'responsive-portfolio-image-gallery'),
						'desc' => __('Number of post to show. Default -1, means show all.', 'responsive-portfolio-image-gallery'),
						'placeholder' => __('10', 'responsive-portfolio-image-gallery'),
						'min' => -1,
						'max' => 100,
						'type' => 'number',
						'default' => -1,
						'sanitize_callback' => 'intval'
					),
					array(
						'name' => 'rpgal_postorder',
						'label' => __('Select Post Order', 'responsive-portfolio-image-gallery'),
						'desc' => '',
						'type' => 'select',
						'default' => 'DESC',
						'options' => array(
							'ASC' => __('Ascending', 'responsive-portfolio-image-gallery'),
							'DESC' => __('Descending', 'responsive-portfolio-image-gallery')
						),
						'sanitize_callback' => 'sanitize_text_field'
					),
					array(
						'name' => 'rpgal_postorder_by',
						'label' => __('Select Post Order By', 'responsive-portfolio-image-gallery'),
						'desc' => '',
						'type' => 'select',
						'default' => 'date',
						'options' => array(
							'none' => __('No Order', 'responsive-portfolio-image-gallery'),
							'ID' => __('Post ID', 'responsive-portfolio-image-gallery'),
							'name' => __('Post Name (post slug)', 'responsive-portfolio-image-gallery'),
							'date' => __('Post Date', 'responsive-portfolio-image-gallery'),
							'rand' => __('Random', 'responsive-portfolio-image-gallery')
						),
						'sanitize_callback' => 'sanitize_text_field'
					),
					array(
						'name' => 'rpgal_show_plain',
						'label' => __('Display Content in Plain Text', 'responsive-portfolio-image-gallery'),
						'desc' => '',
						'type' => 'checkbox',
						'default' => 'on',
						'sanitize_callback' => 'sanitize_text_field'
					),
					array(
						'name' => 'rpgal_excerpt_ln',
						'label' => __('Enter Excerpt Length', 'responsive-portfolio-image-gallery'),
						'desc' => __('Number of words to show for portfolio description.', 'responsive-portfolio-image-gallery'),
						'placeholder' => __('30', 'responsive-portfolio-image-gallery'),
						'min' => 10,
						'max' => 200,
						'type' => 'number',
						'default' => 30,
						'sanitize_callback' => 'intval'
					),
					array(
						'name' => 'rpgal_img_width',
						'label' => __('Portfolio Thumbnail Width', 'responsive-portfolio-image-gallery'),
						'desc' => __('Portfolio thumbnail width in Px. Minimum 50. Default 250', 'responsive-portfolio-image-gallery'),
						'placeholder' => __('250', 'responsive-portfolio-image-gallery'),
						'type' => 'number',
						'min' => 50,
						'max' => 1000,
						'default' => 250,
						'sanitize_callback' => 'intval'
					),
					array(
						'name' => 'rpgal_img_height',
						'label' => __('Portfolio Thumbnail Height', 'responsive-portfolio-image-gallery'),
						'desc' => __('Portfolio thumbnail height in Px. Minimum 50. Default 250', 'responsive-portfolio-image-gallery'),
						'placeholder' => __('250', 'responsive-portfolio-image-gallery'),
						'type' => 'number',
						'min' => 30,
						'max' => 1000,
						'default' => 250,
						'sanitize_callback' => 'intval'
					),
				),
				'rpgal_advanced' => array(
					array(
						'name' => 'rpgal_filter_effect',
						'label' => __('Filter Effect.', 'responsive-portfolio-image-gallery'),
						'desc' => __('Select a filter effect for Mixitup.', 'responsive-portfolio-image-gallery'),
						'type' => 'select',
						'default' => 'popup',
						'options' => array(
							'none' => 'None',
							'moveup' => 'Move Up',
							'popup' => 'Popup',
							'scaleup' => 'Scale Up',
							'helix' => 'Helix'
						),
						'sanitize_callback' => 'sanitize_text_field'
					),
					array(
						'name' => 'rpgal_hover_dir',
						'label' => __('Portfolio Hover Direction', 'responsive-portfolio-image-gallery'),
						'desc' => __('Enable portfolio hover on mouse hover. Default: On.', 'responsive-portfolio-image-gallery'),
						'type' => 'checkbox',
						'default' => 'on',
						'sanitize_callback' => 'sanitize_text_field'
					),
					array(
						'name' => 'rpgal_hover_inv',
						'label' => __('Portfolio Hover Inverse', 'responsive-portfolio-image-gallery'),
						'desc' => __('Portfolio hover in reverse direction on mouse hover. Default: Off.', 'responsive-portfolio-image-gallery'),
						'type' => 'checkbox',
						'default' => 'off',
						'sanitize_callback' => 'sanitize_text_field'
					),
					array(
						'name' => 'rpgal_hover_effect',
						'label' => __('Portfolio Hover Effect.', 'responsive-portfolio-image-gallery'),
						'desc' => __('Select a hover effect for portfolio images.', 'responsive-portfolio-image-gallery'),
						'type' => 'select',
						'default' => 'none',
						'options' => array(
							'none' => 'None',
							'zoompan' => 'Zoom and Pan',
							'zoomhide' => 'Zoom and Hide',
							'shrink' => 'Shrink',
							'slideout' => 'Slide Right'
						),
						'sanitize_callback' => 'sanitize_text_field'
					),
					array(
						'name' => 'rpgal_hide_excerpt',
						'label' => __('Hide Portfolio Description', 'responsive-portfolio-image-gallery'),
						'desc' => __('Select Yes if want to hide portfolio description in expanding preview.', 'responsive-portfolio-image-gallery'),
						'type' => 'checkbox',
						'default' => 'off',
						'sanitize_callback' => 'sanitize_text_field'
					),
					array(
						'name' => 'rpgal_exp_height',
						'label' => __('Expanding Preview Height', 'responsive-portfolio-image-gallery'),
						'desc' => __('Enter the height of the preview which expand when visitors click on a thumbnail.', 'responsive-portfolio-image-gallery'),
						'placeholder' => __('500', 'responsive-portfolio-image-gallery'),
						'type' => 'number',
						'min' => 100,
						'max' => 1000,
						'default' => 500,
						'sanitize_callback' => 'intval'
					),
					array(
						'name' => 'rpgal_show_wrap',
						'label' => __('Display Image Carousel', 'responsive-portfolio-image-gallery'),
						'desc' => __('Select On if want to display carousel images in expanding preview.', 'responsive-portfolio-image-gallery'),
						'type' => 'checkbox',
						'default' => 'on',
						'sanitize_callback' => 'sanitize_text_field'
					),
				),
				'rpgal_style' => array(
					array(
						'name' => 'rpgal_css_style',
						'label' => __('Select Portfolio Style', 'responsive-portfolio-image-gallery'),
						'desc' => __('Select a style for portfolio color scheme.', 'responsive-portfolio-image-gallery'),
						'type' => 'select',
						'default' => 'dark',
						'options' => array(
							'dark' => 'Dark',
							'light' => 'Light'
						),
						'sanitize_callback' => 'sanitize_text_field'
					),
					array(
						'name' => 'rpgal_custom_css',
						'label' => __('Portfolio Custom CSS', 'responsive-portfolio-image-gallery'),
						'desc' => __('You can write you own custom css code here.', 'responsive-portfolio-image-gallery'),
						'type' => 'textarea',
						'sanitize_callback' => 'sanitize_textarea_field'
					),
				),
			);
			return $settings_fields;
		}

		/**
		 * Wraps the settings page for display.
		 *
		 * This method outputs the HTML for the settings page and sidebar content,
		 * providing a container for the settings fields and sidebar to be displayed.
		 */
		function rpgal_plugin_page()
		{
			?>
			<div class="rpgal_settings_area">
				<div class="wrap rpgal_settings">
					<h1 class="main-header"><?php esc_html_e('Portfolio Gallery Options', 'responsive-portfolio-image-gallery'); ?></h1>
					<?php
					$this->settings_api->show_navigation();
					$this->settings_api->show_forms();
					$this->rpgal_admin_footer_note();
					?>
				</div>
				<?php
				$this->rpgal_sidebar_page(); ?>
			</div>
			<?php
		}

		/**
		 * Porfolio Sidebar for WP Admin
		 * @return void
		 */
		public function rpgal_sidebar_page()
		{

			$sidebar_content = $this->sidebar_info->rpgal_sidebar(false, true, false, 'rpgal-main');
			echo wp_kses_post($sidebar_content);
		}

		/**
		 * Includes the help page for the plugin.
		 *
		 * This method loads the HTML or template for the plugin's help page, providing 
		 * users with instructions or support information.
		 *
		 * @return void
		 */
		public function rpgal_guide_page()
		{
			if (!current_user_can('manage_options')) {
				wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'responsive-portfolio-image-gallery'));
			}

			$rpgal_guide = new RPGAL_Guide();
			$rpgal_guide->render_rpgal_guide_page();
		}

		/**
		 * Adds a custom footer note below the settings panel.
		 *
		 * This method appends a footer message to the settings page, typically used 
		 * for branding or providing additional information.
		 *
		 * @return void
		 */
		public function rpgal_admin_footer_note()
		{
			?>
			<div id="rpgal-narration" class="postbox-container">
				<div id="rpgalusage-note" class="rpgalusage-maincontent">
					<div class="rpgal"><?php
					printf(
						/* translators: %1$s: Opening HTML tag for heading, %2$s: Closing HTML tag for heading, %3$s: Opening paragraph tag, %4$s: Link to the YouTube video, %5$s: Contact Us link, %6$s: Closing paragraph tag, %7$s: Opening paragraph tag for feedback request, %8$s: Link to leave a review */
						esc_html__('
								%1$sWatch Our YouTube Tutorial for Easy Setup!%2$s
								%3$sThere is a %4$s available that explains how the User Front-End plugin works. If you have any trouble understanding, feel free to %5$s at any time.%6$s
								%7$sWe greatly value your feedback! Please spare a moment to rate your recent experience with our plugin. Your input is highly appreciated and helps us improve. Thank you for your support!%8$s',
							'responsive-portfolio-image-gallery'
						),
						'<h3>',
						'</h3>',
						'<p>',
						'<a href="' . esc_url(admin_url("admin.php?page=rpgal-guide")) . '"> YouTube video</a>',
						'<a href="' . esc_url("https://wordpress.org/support/plugin/responsive-portfolio-image-gallery/") . '" target="_blank">Contact Us</a>',
						'</p>',
						'<p class="likeit">',
						'<a target="_blank" href="' . esc_url("https://wordpress.org/support/plugin/responsive-portfolio-image-gallery/reviews/?filter=5/#new-post") . '">&#9733;&#9733;&#9733;&#9733;&#9733;</a></p>',
					); ?>
					</div>
				</div>
			</div>
			<?php
		}

		/**
		 * Retrieves a list of pages from the WordPress site.
		 *
		 * This method fetches the pages available on the site and returns them as an 
		 * associative array with the page ID as the key and the page title as the value.
		 *
		 * @return array Array of page names with key-value pairs.
		 */
		function get_pages()
		{
			$pages = get_pages();
			$pages_options = array();
			if ($pages) {
				foreach ($pages as $page) {
					$pages_options[$page->ID] = $page->post_title;
				}
			}
			return $pages_options;
		}
	}
}

$settings = new rpgal_settings_config();

/**
 * Retrieves the value of a settings field.
 *
 * This function fetches a specific option from the WordPress options table.
 * If the option is not found, it falls back to the specified default value.
 *
 * @param string $option The name of the settings field.
 * @param string $section The section name the field belongs to.
 * @param string $default The default value to return if the option is not found.
 *
 * @return mixed The value of the option or the default value.
 */
if (!function_exists('rpgal_get_option')) {
	function rpgal_get_option($option, $section, $default = '')
	{
		$options = get_option($section);

		if (isset($options[$option])) {
			return $options[$option];
		}

		return $default;
	}
}