<?php
defined('ABSPATH') || exit;

/**
 * Main Gridpress Plugin Bootstrap
 */
if (! class_exists('Gridpress_Plugin')) {

	class Gridpress_Plugin
	{

		/**
		 * Plugin version
		 *
		 * @var string
		 */
		public $version = '1.0.0';

		/**
		 * Constructor
		*/
		public function __construct()
		{

			$this->gridpress_define_constants();
			$this->gridpress_includes();
			$this->gridpress_init_hooks();

			add_filter('custom_menu_order', '__return_true');
			add_filter('menu_order', [$this, 'gridpress_menu_order'], 9);
		}

		/**
		 * Define constants
		 */
		private function gridpress_define_constants()
		{

			if (! defined('GRIDPRESS_ABSPATH')) {
				define('GRIDPRESS_ABSPATH', dirname(GRIDPRESS_PLUGIN_FILE) . '/');
			}

			if (! defined('GRIDPRESS_URL')) {
				define('GRIDPRESS_URL', self::gridpress_root_url());
			}

			if (! defined('GRIDPRESS_VERSION')) {
				define('GRIDPRESS_VERSION', $this->version);
			}
		}

		/**
		 * Admin menu order
		 */
		public function gridpress_menu_order($gridpress_menu_order)
		{

			$gridpress_items = [
				'gridpress-options',
				'edit.php?post_type=gridpress_grid',
			];

			$gridpress_items = apply_filters(
				'gridpress_main_wp_menu_list',
				$gridpress_items
			);

			$gridpress_final = [];
			$gridpress_custom = [];

			if (! is_array($gridpress_menu_order)) {
				return $gridpress_menu_order;
			}

			$gridpress_menu_order = (array) $gridpress_menu_order;

			foreach ($gridpress_menu_order as $gridpress_item) {

				if ('gridpress-options' === $gridpress_item) {

					$gridpress_custom[] = $gridpress_item;
					$gridpress_custom[] = 'edit.php?post_type=gridpress_grid';

					$gridpress_custom = apply_filters(
						'gridpress_main_wp_menu_order',
						$gridpress_custom
					);

					$gridpress_final = array_merge(
						$gridpress_final,
						$gridpress_custom
					);
				} elseif (! in_array($gridpress_item, $gridpress_items, true)) {
					$gridpress_final[] = $gridpress_item;
				}
			}

			return $gridpress_final;
		}

		/**
		 * Include files
		 */
		private function gridpress_includes()
		{

			$gridpress_files = [
				'includes/classes/class-form-fields.php',
				'includes/common-functions.php',
				'includes/global-options.php',
				'admin/admin.php',
				'includes/classes/class-post-types.php',
				'includes/classes/class-grid-settings.php',
				'includes/classes/class-grid-shortcode.php',
				'admin/admin-notices.php',
				'includes/wp-bakery.php',
				'includes/elementor/class-elementor-init.php',
			];

			foreach ($gridpress_files as $gridpress_file) {

				$gridpress_path = GRIDPRESS_ABSPATH . $gridpress_file;

				if (file_exists($gridpress_path)) {
					include_once $gridpress_path;
				}
			}
		}
		/**
		 * Hooks
		 */
		private function gridpress_init_hooks()
		{

			register_activation_hook(
				GRIDPRESS_PLUGIN_FILE,
				[$this, 'gridpress_start_setup']
			);

			add_action('wp_enqueue_scripts', [$this, 'gridpress_front_enqueues']);
			add_action('admin_enqueue_scripts', [$this, 'gridpress_admin_enqueues']);
		}

		/**
		 * Activation setup
		 */
		public function gridpress_start_setup()
		{

			if (! get_option('gridpress_plugin_setup_installed')) {
				set_transient('_gridpress_activation_redirect', 1, 30);
			}
		}

		/**
		 * JS localized variables
		 */
		public static function gridpress_common_localize_vars()
		{

			return [
				'ajax_url' => esc_url(admin_url('admin-ajax.php')),
				'site_url' => esc_url(home_url('/')),
				'front_nonce' => wp_create_nonce('gridpress_front_nonce'),

				'confirm_common_title' => esc_html_x('Confirmation', 'Confirm box title', 'gridpress-grid-layout-design'),
				'delete_common_msg' => esc_html__('Do you really want to delete it?', 'gridpress-grid-layout-design'),
				'delete_btn_lbl' => esc_html_x('Delete', 'Confirm Popup', 'gridpress-grid-layout-design'),
				'cancel_btn_lbl' => esc_html_x('Cancel', 'Confirm Popup', 'gridpress-grid-layout-design'),
				'conf_ok_lbl' => esc_html_x('OK', 'Confirm Popup', 'gridpress-grid-layout-design'),
				'type_to_search' => esc_html_x('Please enter 1 or more characters', 'Select2 dropdown', 'gridpress-grid-layout-design'),
				'select2_searching' => esc_html_x('Searching...', 'Select2 dropdown', 'gridpress-grid-layout-design'),
				'select2_loading_more' => esc_html_x('Loading more results...', 'Select2 dropdown', 'gridpress-grid-layout-design'),
				'select2_no_results' => esc_html_x('No results found', 'Select2 dropdown', 'gridpress-grid-layout-design'),
				'select2_only_select' => esc_html_x('You can only select', 'Select2 dropdown', 'gridpress-grid-layout-design'),
				'select2_sectors' => esc_html_x('sectors', 'Select2 dropdown', 'gridpress-grid-layout-design'),
			];
		}

		public function gridpress_front_enqueues()
		{
			wp_enqueue_style('gridpress-icons', GRIDPRESS_URL . 'css/icons.css', [], GRIDPRESS_VERSION);
			wp_enqueue_style('select2', GRIDPRESS_URL . 'css/select2/select2.min.css', [], GRIDPRESS_VERSION);
			wp_enqueue_style('gridpress-styles', GRIDPRESS_URL . 'css/common-styles.css', [], GRIDPRESS_VERSION);
			wp_enqueue_style('gridpress-cards', GRIDPRESS_URL . 'css/cards.css', [], GRIDPRESS_VERSION);

			wp_enqueue_script('select2', GRIDPRESS_URL . 'js/select2/select2.full.min.js', array('jquery'), GRIDPRESS_VERSION, true);
			wp_enqueue_script('gridpress-scripts', GRIDPRESS_URL . 'js/common-functions.js', array('jquery'), GRIDPRESS_VERSION, true);
			wp_localize_script('gridpress-scripts', 'gridpress_func_str', self::gridpress_common_localize_vars());
		}

		public function gridpress_admin_enqueues()
		{

			wp_enqueue_style('gridpress-icons', GRIDPRESS_URL . 'css/icons.css', [], GRIDPRESS_VERSION);
			wp_enqueue_style('select2', GRIDPRESS_URL . 'css/select2/select2.min.css', [], GRIDPRESS_VERSION);
			wp_enqueue_style('gridpress-styles', GRIDPRESS_URL . 'css/common-styles.css', [], GRIDPRESS_VERSION);
			wp_enqueue_style('gridpress-admin-styles', GRIDPRESS_URL . 'css/admin/admin-styles.css', array('gridpress-styles'), GRIDPRESS_VERSION);

			wp_enqueue_script('select2', GRIDPRESS_URL . 'js/select2/select2.full.min.js', array('jquery'), GRIDPRESS_VERSION, true);
			wp_enqueue_script('gridpress-scripts', GRIDPRESS_URL . 'js/common-functions.js', array('jquery'), GRIDPRESS_VERSION, true);
			wp_localize_script('gridpress-scripts', 'gridpress_func_str', self::gridpress_common_localize_vars());

			wp_enqueue_script('gridpress-admin-scripts', GRIDPRESS_URL . 'js/admin/admin-scripts.js', array('jquery', 'gridpress-scripts'), GRIDPRESS_VERSION, true);
			$gridpress_localize_str = [
				'admin_url' => esc_url(admin_url('/')),
				'ajax_url' => esc_url(admin_url( 'admin-ajax.php' ) ),
				'insert_img_lbl' => esc_html__('Insert image', 'gridpress-grid-layout-design'),
				'use_img_lbl' => esc_html__('Use this image', 'gridpress-grid-layout-design'),
				'insert_video_lbl' => esc_html__('Insert video', 'gridpress-grid-layout-design'),
				'use_video_lbl' => esc_html__('Use this video', 'gridpress-grid-layout-design'),
				'confirm_common_title' => esc_html_x('Confirmation', 'Confirm box title', 'gridpress-grid-layout-design'),
				'srch_post_itms' => esc_html__('Search for post items', 'gridpress-grid-layout-design'),
				'srch_term_itms' => esc_html__('Search for terms', 'gridpress-grid-layout-design'),
				'nonce' => wp_create_nonce('gridpress_admin_nonce'),
			];
			wp_localize_script('gridpress-admin-scripts', 'gridpress_adminscript_str', $gridpress_localize_str);
		}

		/**
		 * Base plugin URL
		 */
		public static function gridpress_root_url()
		{
			return trailingslashit(plugins_url('', GRIDPRESS_PLUGIN_FILE));
		}
	}
	new Gridpress_Plugin();
}