<?php
/**
 * The dashboard-specific functionality of the plugin.
 *
 * @package    Woocommerce_Product_Tabs
 */

namespace Barn2\Plugin\WC_Product_Tabs_Pro\Admin;

use Barn2\Plugin\WC_Product_Tabs_Pro\Plugin;
use Barn2\Plugin\WC_Product_Tabs_Pro\Dependencies\Lib\Admin\Settings_API_Helper;

use Nilambar\Welcome\Welcome;

/**
 * The dashboard-specific functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the dashboard-specific stylesheet and JavaScript.
 *
 * @package    Woocommerce_Product_Tabs
 */
class Admin_Controller {

	/**
	 * The plugin instance.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      Plugin    $plugin    The plugin instance.
	 */
	private $plugin;

	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $plugin_name    The ID of this plugin.
	 */
	private $plugin_name;

	/**
	 * The version of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $version    The current version of this plugin.
	 */
	private $version;

	private $product_tabs_list;

	private $license_setting;

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    1.0.0
	 * @var      Plugin
	 */
	public function __construct( $plugin ) {
		$this->plugin            = $plugin;
		$this->plugin_name       = $plugin->get_slug();
		$this->license_setting   = $plugin->get_license_setting();
		$this->version           = $plugin->get_version();
		$this->product_tabs_list = get_posts(
			[
				'post_type'      => WOOCOMMERCE_PRODUCT_TABS_POST_TYPE_TAB,
				'posts_per_page' => -1,
				'orderby'        => 'menu_order',
				'order'          => 'asc',
			]
		);
		if ( ! empty( $this->product_tabs_list ) ) {
			foreach ( $this->product_tabs_list as $key => $t ) {
				$this->product_tabs_list[ $key ]->post_meta = get_post_meta( $this->product_tabs_list[ $key ]->ID );
			}
		}

		$this->load();
	}

		/**
	 * Register all of the hooks related to the dashboard functionality
	 * of the plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	public function load() {
		if ( $this->plugin->is_freemius_active() ) {
			add_action( 'init', [ $this, 'do_redirect' ] );
			add_action( 'wp_welcome_init', [ $this, 'welcome_page_init' ] );		
		}

		add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );

		// Columns in Tab Listing
		add_filter( 'manage_woo_product_tab_posts_columns', [ $this, 'add_columns_in_tab_listing' ] );
		add_filter( 'manage_edit-woo_product_tab_sortable_columns', [ $this, 'sortable_tab_columns' ] );
		add_action( 'manage_woo_product_tab_posts_custom_column', [ $this, 'custom_columns_in_tab_listing' ], 10, 2 );

		// Bulk Messages
		add_filter( 'post_updated_messages', [ $this, 'tab_post_updated_messages' ], 10, 2 );

		// Row actions
		add_filter( 'post_row_actions', [ $this, 'tab_post_row_actions' ], 10, 2 );

		// Hide publishing actions.
		add_action( 'admin_head-post.php', [ $this, 'hide_publishing_actions' ] );
		add_action( 'admin_head-post-new.php', [ $this, 'hide_publishing_actions' ] );

		// Add Menu page for order
		add_action( 'admin_menu', [ $this, 'register_product_tab_menu' ] );
		add_action( 'admin_init', [ $this, 'register_plugin_option_fields' ] );

		add_action( 'wp_ajax_wpt_update_post_order', [ $this, 'post_order_update_callback' ] );
		add_action( 'wp_ajax_nopriv_wpt_update_post_order', [ $this, 'post_order_update_callback' ] );
		add_action( 'pre_get_posts', [ $this, 'post_order_list' ] );

		// Add AJAX callbacks
		add_action( 'wp_ajax_nopriv_wpc_wptp_get_plugins_list', [ $this, 'ajax_callback_plugins_list' ] );
		add_action( 'wp_ajax_wpc_wptp_get_plugins_list', [ $this, 'ajax_callback_plugins_list' ] );

		// Add an action link pointing to the options page.
		$base_file = WOOCOMMERCE_PRODUCT_TABS_BASENAME . '/' . $this->plugin_name . '.php';
		add_filter( 'plugin_action_links_' . $base_file, [ $this, 'add_plugin_action_links' ] );

		add_action( 'admin_init', [ $this, 'check_license' ], 40 );
	}

	public function add_plugin_action_links( $links ) {
		$output = array_merge(
			[
				'product-tabs'    => '<a href="' . esc_url( admin_url( 'edit.php?post_type=woo_product_tab' ) ) . '">' . esc_html__( 'Product Tabs', 'woocommerce-product-tabs' ) . '</a>',
				'plugin-settings' => '<a href="' . esc_url( admin_url( "admin.php?page=wpt-settings" ) ) . '">' . esc_html__( 'Settings', 'woocommerce-product-tabs' ) . '</a>',
			],
			$links
		);

		return $output;
	}

	/**
	 * Register the scripts for the Dashboard.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_scripts( $hook ) {

		$screen = get_current_screen();

		if ( WOOCOMMERCE_PRODUCT_TABS_POST_TYPE_TAB == $screen->id || ( 'product' == $screen->id ) || ( 'woocommerce-product-tabs-pro' == $GLOBALS['admin_page_hooks']['wpt-options'] ) ) {

			wp_enqueue_style( $this->plugin_name . '-tab', WOOCOMMERCE_PRODUCT_TABS_URL . '/assets/css/admin/admin.css', [], $this->version, 'all' );
			wp_enqueue_style( $this->plugin_name . '-fontawesome', WOOCOMMERCE_PRODUCT_TABS_URL . '/assets/css/font-awesome/all.min.css', [], $this->version, 'all' );

			wp_enqueue_script( 'jquery-ui-sortable' );
			wp_enqueue_script( $this->plugin_name . '-tab', WOOCOMMERCE_PRODUCT_TABS_URL . '/assets/js/admin/admin.js', [ 'jquery' ], $this->version, true );

			if ( function_exists( 'wp_enqueue_editor' ) ) {
				wp_enqueue_editor();
			}
		}

		if ( 'woocommerce-product-tabs-pro_page_wpt-welcome' === $hook ) {
			wp_enqueue_script( 'wpt-welcome-plugins-list', plugins_url( 'assets/js/admin/plugins-list.js', dirname( __FILE__ ) ), [ 'jquery' ], WOOCOMMERCE_PRODUCT_TABS_VERSION, true );
		}
	}

	public function add_columns_in_tab_listing( $columns ) {

		unset( $columns['date'] );
		$columns['priority']        = __( 'Priority', 'woocommerce-product-tabs' );
		$columns['category']        = __( 'Categories', 'woocommerce-product-tabs' );
		$columns['tag']             = __( 'Tags', 'woocommerce-product-tabs' );
		$columns['product']         = __( 'Products', 'woocommerce-product-tabs' );
		$columns['default-for-all'] = __( 'Default for All', 'woocommerce-product-tabs' );
		$columns['tab-key']         = __( 'Tab Key', 'woocommerce-product-tabs' );

		return $columns;
	}

	public function custom_columns_in_tab_listing( $column, $post_id ) {

		$post = get_post( $post_id );
		switch ( $column ) {
			case 'priority':
				echo $post->menu_order;
				break;
			case 'category':
				$categories    = get_post_meta( $post_id, '_wpt_conditions_category', true );
				$category_list = [];
				if ( ! empty( $categories ) ) {
					foreach ( $categories as $category ) {
						$term            = get_term_by( 'id', $category, 'product_cat' );
						$category_list[] = $term->name;
					}
					echo implode( ', ', $category_list );
				} else {
					echo '<span class="na">–</span>';
				}
				break;
			case 'tag':
				$tags = wp_get_post_terms( $post_id, 'product_tag', [ 'fields' => 'names' ] );
				if ( ! empty( $tags ) ) {
					echo implode( ', ', $tags );
				} else {
					echo '<span class="na">–</span>';
				}
				break;
			case 'tab-key':
				echo '<code>' . $post->post_name . '</code>';
				break;
			case 'product':
				$products     = get_post_meta( $post_id, '_wpt_conditions_product', true );
				$product_list = [];
				if ( ! empty( $products ) ) {
					foreach ( $products as $product ) {
						$product_list[] = get_the_title( $product );
					}
					echo implode( ', ', $product_list );
				} else {
					echo '<span class="na">–</span>';
				}
				break;
			case 'default-for-all':
				$flag_default_for_all = get_post_meta( $post_id, '_wpt_option_use_default_for_all', true );
				if ( 'yes' == $flag_default_for_all ) {
					echo '<span class="dashicons dashicons-yes"></span>';
				} else {
					echo '<span class="dashicons dashicons-no-alt"></span>';
				}
				break;
			default:
				break;
		}

	}

	public function sortable_tab_columns( $columns ) {

		$columns['priority'] = 'menu_order';
		return $columns;

	}

	public function tab_post_updated_messages( $messages ) {

		$post = get_post();

		$messages['woo_product_tab'] = [

			0  => '', // Unused. Messages start at index 1.
			1  => __( 'Tab updated.', 'woocommerce-product-tabs' ),
			2  => __( 'Custom field updated.', 'woocommerce-product-tabs' ),
			3  => __( 'Custom field deleted.', 'woocommerce-product-tabs' ),
			4  => __( 'Tab updated.', 'woocommerce-product-tabs' ),
			5  => isset( $_GET['revision'] ) ? sprintf( __( 'Tab restored to revision from %s', 'woocommerce-product-tabs' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
			6  => __( 'Tab published.', 'woocommerce-product-tabs' ),
			7  => __( 'Tab saved.', 'woocommerce-product-tabs' ),
			8  => __( 'Tab submitted.', 'woocommerce-product-tabs' ),
			9  => sprintf(
				__( 'Tab scheduled for: <strong>%1$s</strong>.', 'woocommerce-product-tabs' ),
				date_i18n( __( 'M j, Y @ G:i', 'woocommerce-product-tabs' ), strtotime( $post->post_date ) )
			),
			10 => __( 'Tab draft updated.', 'woocommerce-product-tabs' )
		];
		return $messages;

	}

	public function tab_post_row_actions( $actions, $post ) {

		if ( WOOCOMMERCE_PRODUCT_TABS_POST_TYPE_TAB == $post->post_type && isset( $actions['inline hide-if-no-js'] ) ) {
			unset( $actions['inline hide-if-no-js'] );
		}
		return $actions;

	}

	/**
	 * Hide publishing actions.
	 *
	 * @since 1.0.0
	 */
	function hide_publishing_actions() {
		global $post;
		if ( WOOCOMMERCE_PRODUCT_TABS_POST_TYPE_TAB !== $post->post_type ) {
			return;
		}
		?>
		<style type="text/css">
		#misc-publishing-actions,#minor-publishing-actions{
			display:none;
		}
		</style>
		<?php
		return;
	}

	/**
	 * Add Menu Page Reorder.
	 *
	 * @since 1.0.0
	 */
	function register_product_tab_menu() {
		add_menu_page(
			__( 'WooCommerce Product Tabs Pro', 'woocommerce-product-tabs' ),
			__( 'WooCommerce Product Tabs Pro', 'woocommerce-product-tabs' ),
			'manage_options',
			'wpt-options',
			[ $this, 'admin_product_tabs_options_page' ],
			'dashicons-editor-table',
			58
		);

		add_submenu_page(
			'wpt-options',
			__( 'WooCommerce Product Tabs Pro', 'woocommerce-product-tabs' ),
			__( 'Settings', 'woocommerce-product-tabs' ),
			'manage_options',
			'wpt-settings',
			[ $this, 'admin_product_tabs_options_page' ]
		);
	}

	function admin_product_tabs_options_page() {
		?>
		<div class="wrap wpt-options">
		<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>

		<div id="poststuff">

			<div id="post-body" class="metabox-holder columns-2">

			<div id="post-body-content">

				<div class="tab-wrapper">
				<ul class="tabs-nav">
					<li class="tab-active"><a href="#tab-settings" class="button"><?php esc_html_e( 'Settings', 'woocommerce-product-tabs' ); ?></a></li>
					<?php
					if ( $this->plugin->is_license_active() ) {
					?>
						<li><a href="#tab-reorder" class="button"><?php esc_html_e( 'Reorder', 'woocommerce-product-tabs' ); ?></a></li>
					<?php
					}
					?>
				</ul>
				</div><!-- .tab-wrapper -->

				<div class="tabs-stage">

				<div id="tab-settings" class="meta-box-sortables tab-ui-sortable">
					<div class="postbox">
					<div class="inside">

						<form action="options.php" method="post">
						<?php settings_fields( 'wpt_group' ); ?>
						<?php do_settings_sections( 'wpt-options' ); ?>
						<?php submit_button( __( 'Save Changes', 'woocommerce-product-tabs' ) ); ?>
						</form>
						<?php require WOOCOMMERCE_PRODUCT_TABS_DIR . '/templates/admin/icons.php'; ?>

					</div><!-- .inside -->
					</div><!-- .postbox -->
				</div><!-- .meta-box-sortables -->

				<?php
				if ( $this->plugin->is_license_active() ) {
				?>
					<div id="tab-reorder" class="meta-box-sortables tab-ui-sortable">
						<div class="postbox">
						<div class="inside">

							<?php
							$tab_posts = $this->product_tabs_list;

							$wpt_options = get_option( 'wpt_options' );

							if ( ! empty( $wpt_options['description_priority'] ) ) {
								$wpt_options['description_priority'] = $wpt_options['description_priority'];
							} else {
								$wpt_options['description_priority'] = 10;
							}

							if ( ! empty( $wpt_options['info_priority'] ) ) {
								$wpt_options['info_priority'] = $wpt_options['info_priority'];
							} else {
								$wpt_options['info_priority'] = 20;
							}

							if ( ! empty( $wpt_options['review_priority'] ) ) {
								$wpt_options['review_priority'] = $wpt_options['review_priority'];
							} else {
								$wpt_options['review_priority'] = 30;
							}

							$description = (object) [
								'ID'         => 'description',
								'post_title' => __( 'Description', 'woocommerce-product-tabs' ),
								'menu_order' => $wpt_options['description_priority'],
							];

							$info = (object) [
								'ID'         => 'info',
								'post_title' => __( 'Addtional Information', 'woocommerce-product-tabs' ),
								'menu_order' => $wpt_options['info_priority'],
							];

							$review = (object) [
								'ID'         => 'review',
								'post_title' => __( 'Review', 'woocommerce-product-tabs' ),
								'menu_order' => $wpt_options['review_priority'],
							];

							array_push( $tab_posts, $description );
							array_push( $tab_posts, $info );
							array_push( $tab_posts, $review );

							usort( $tab_posts, [ $this, 'comparator_default_order' ] );

							if ( ! empty( $tab_posts ) ) {
								?>
							<div class="wrap">
								<h2><?php esc_html_e( 'Reorder Tabs', 'woocommerce-product-tabs' ); ?></h2>
								<p><?php esc_html_e( 'Drag and Drop the column to reorder the tabs.', 'woocommerce-product-tabs' ); ?></p>

								<form method="post">
								<table class="wp-list-table widefat striped tabs_reorder">
									<thead>
									<tr>
										<th><?php esc_html_e( 'Product Tabs List', 'woocommerce-product-tabs' ); ?></th>
									</tr>
									</thead>
									<tbody id="the-list">

									<?php foreach ( $tab_posts as $post ) : ?>
										<tr id="post-<?php echo esc_attr( $post->ID ); ?>">
										<td><?php echo esc_html( $post->post_title ); ?></td>
										</tr>
									<?php endforeach ?>

									</tbody>
								</table>
								</form>

								<div id="reorder-output">ghanta</div><!-- #reorder-output -->

							</div>
								<?php
							}
							?>

						</div><!-- .inside -->
						</div><!-- .postbox -->
					</div><!-- .meta-box-sortables -->
				<?php
				}
				?>

				</div><!-- .tabs-stage -->


			</div><!-- #post-body-content -->

			<div id="postbox-container-1" class="postbox-container">

				<?php include WOOCOMMERCE_PRODUCT_TABS_DIR . '/templates/admin/sidebar.php'; ?>

			</div><!-- #postbox-container-1 .postbox-container -->

			</div><!-- #post-body -->
		</div><!-- #poststuff -->

		</div><!-- .wrap -->
		<?php
	}

	/**
	 * Register plugin option fields.
	 *
	 * @since 1.0.0
	 */
	function register_plugin_option_fields() {
		register_setting( 'wpt_group', 'wpt_options', 'validate_plugin_options' );

		if ( ! $this->plugin->is_freemius_active() ) {
			// Licence Key section.
			Settings_API_Helper::add_settings_section(
				'wta_license_key',
				'wpt-options',
				'',
				[ $this, 'support_links' ],
				[
					$this->license_setting->get_license_key_setting(),
					$this->license_setting->get_license_override_setting()
				]
			);
		}

		add_settings_section( 'wpt_default_section', __( 'Settings', 'woocommerce-product-tabs' ), [ $this, 'setting_default_section' ], 'wpt-options' );
		add_settings_field( 'description', __( 'Description', 'woocommerce-product-tabs' ), [ $this, 'field_description' ], 'wpt-options', 'wpt_default_section' );
		add_settings_field( 'info', __( 'Additional Information', 'woocommerce-product-tabs' ), [ $this, 'field_info' ], 'wpt-options', 'wpt_default_section' );
		add_settings_field( 'review', __( 'Review', 'woocommerce-product-tabs' ), [ $this, 'field_review' ], 'wpt-options', 'wpt_default_section' );

		add_settings_field( 'hide_description', __( 'Hide Description', 'woocommerce-product-tabs' ), [ $this, 'hide_description' ], 'wpt-options', 'wpt_default_section' );
		add_settings_field( 'hide_info', __( 'Hide Additional Information', 'woocommerce-product-tabs' ), [ $this, 'hide_info' ], 'wpt-options', 'wpt_default_section' );
		add_settings_field( 'hide_review', __( 'Hide Review', 'woocommerce-product-tabs' ), [ $this, 'hide_review' ], 'wpt-options', 'wpt_default_section' );

		add_settings_section( 'wpt_option_section', __( 'Tabs Options', 'woocommerce-product-tabs' ), [ $this, 'setting_option_section' ], 'wpt-options' );
		add_settings_field( 'search_by_tabs', __( 'Search by Product Tabs', 'woocommerce-product-tabs' ), [ $this, 'search_by_tabs' ], 'wpt-options', 'wpt_option_section' );
		add_settings_field( 'enable_accordion', __( 'Enable Accordion Tabs', 'woocommerce-product-tabs' ), [ $this, 'enable_accordion' ], 'wpt-options', 'wpt_option_section' );
		add_settings_field( 'disable_content_filter', __( 'Disable the_content Filter', 'woocommerce-product-tabs' ), [ $this, 'disable_content_filter' ], 'wpt-options', 'wpt_option_section' );
		add_settings_field( 'accordion_shown_size', __( 'Show Accordion from windows size', 'woocommerce-product-tabs' ), [ $this, 'size_accordion' ], 'wpt-options', 'wpt_option_section' );

		add_settings_section( 'wpt_icons_section', __( 'Select Icons', 'woocommerce-product-tabs' ), [ $this, 'setting_icons_section' ], 'wpt-options' );
		add_settings_field( 'description_icon', __( 'Description Icon', 'woocommerce-product-tabs' ), [ $this, 'field_description_icon' ], 'wpt-options', 'wpt_icons_section' );
		add_settings_field( 'info_icon', __( 'Additional Information Icon', 'woocommerce-product-tabs' ), [ $this, 'field_info_icon' ], 'wpt-options', 'wpt_icons_section' );
		add_settings_field( 'review_icon', __( 'Review Icon', 'woocommerce-product-tabs' ), [ $this, 'field_review_icon' ], 'wpt-options', 'wpt_icons_section' );

	}

	/**
	 * Get plugin option.
	 *
	 * @since 1.0.0
	 */
	function get_option( $key ) {
		if ( empty( $key ) ) {
			return;
		}

		$plugin_options = wp_parse_args( (array) get_option( 'wpt_options' ), [ 'description', 'hide_description', 'info', 'hide_info', 'review', 'hide_review', 'search_by_tabs', 'enable_accordion', 'accordion_shown_size', 'description_priority', 'info_priority', 'review_priority', 'license' ] );

		$value = null;

		if ( isset( $plugin_options[ $key ] ) ) {
			$value = $plugin_options[ $key ];
		}

		return $value;
	}

	/**
	 * Display section message.
	 *
	 * @since 1.0.0
	 */
	function setting_default_section() {
		echo esc_html_e( 'WooCommerce Default Tabs Settings.', 'woocommerce-product-tabs' );
	}

	/**
	 * Register description field.
	 *
	 * @since 1.0.0
	 */
	function field_description() {
		$description = $this->get_option( 'description' );
		?>
		<input type="text" name="wpt_options[description]" id="description" class="regular-text" value="<?php echo esc_attr( $description ); ?>" placeholder="<?php echo esc_attr( __( 'Description', 'woocommerce-product-tabs' ) ); ?>" />
		<p class="description"><?php esc_html_e( 'Leave empty if you dont want to override.', 'woocommerce-product-tabs' ); ?></p>
		<?php
	}

	/**
	 * Register info field.
	 *
	 * @since 1.0.0
	 */
	function field_info() {
		$info = $this->get_option( 'info' );
		?>
		<input type="text" name="wpt_options[info]" id="info" class="regular-text" value="<?php echo esc_attr( $info ); ?>" placeholder="<?php echo esc_attr( __( 'Additional information', 'woocommerce-product-tabs' ) ); ?>" />
		<p class="description"><?php esc_html_e( 'Leave empty if you dont want to override.', 'woocommerce-product-tabs' ); ?></p>
		<?php
	}

	/**
	 * Register review field.
	 *
	 * @since 1.0.0
	 */
	function field_review() {
		$review = $this->get_option( 'review' );
		?>
		<input type="text" name="wpt_options[review]" id="review" class="regular-text" value="<?php echo esc_attr( $review ); ?>" placeholder="<?php echo esc_attr( __( 'Review', 'woocommerce-product-tabs' ) ); ?>"/>
		<p class="description"><?php esc_html_e( 'Leave empty if you dont want to override.', 'woocommerce-product-tabs' ); ?></p>
		<?php
	}

	/**
	 * Register hide_description field.
	 *
	 * @since 1.0.0
	 */
	function hide_description() {
		$hide_description = $this->get_option( 'hide_description' );
		?>
		<label for="hide_description">
		<input type="checkbox" name="wpt_options[hide_description]" id="hide_description" value="1" <?php checked( 1, $hide_description ); ?> />
		<?php
		esc_html_e( 'Enable this to hide the default tab of Description.', 'woocommerce-product-tabs' );
		?>
		</label>
		<?php
	}


	/**
	 * Register hide_info field.
	 *
	 * @since 1.0.0
	 */
	function hide_info() {
		$hide_info = $this->get_option( 'hide_info' );
		?>
		<label for="hide_info">
		<input type="checkbox" name="wpt_options[hide_info]" id="hide_info" value="1" <?php checked( 1, $hide_info ); ?> />
		<?php esc_html_e( 'Enable this to hide the default tab of Addtional Information.', 'woocommerce-product-tabs' ); ?>
		</label>
		<?php
	}

	/**
	 * Register hide_review field.
	 *
	 * @since 1.0.0
	 */
	function hide_review() {
		$hide_review = $this->get_option( 'hide_review' );
		?>
		<label for="hide_review">
		<input type="checkbox" name="wpt_options[hide_review]" id="hide_review" value="1" <?php checked( 1, $hide_review ); ?> />
		<?php esc_html_e( 'Enable this to hide the default tab of Reviews.', 'woocommerce-product-tabs' ); ?>
		</label>
		<?php
	}

	/**
	 * Display section message.
	 *
	 * @since 1.0.0
	 */
	function setting_option_section() {
		echo esc_html_e( 'WooCommerce other options.', 'woocommerce-product-tabs' );
	}


	/**
	 * Register search_by_tabs field.
	 *
	 * @since 1.0.0
	 */
	function search_by_tabs() {
		$search_by_tabs = $this->get_option( 'search_by_tabs' );
		?>
		<label for="search_by_tabs">
		<input type="checkbox" name="wpt_options[search_by_tabs]" id="search_by_tabs" value="1" <?php checked( 1, $search_by_tabs ); ?> />
		<?php esc_html_e( 'Enable this checkbox to search the products by custom tabs title or content.', 'woocommerce-product-tabs' ); ?>
		</label>
		<?php
	}

	/**
	 * Register enable_accordion field.
	 *
	 * @since 1.0.0
	 */
	function enable_accordion() {
		$enable_accordion = $this->get_option( 'enable_accordion' );
		?>
		<label for="enable_accordion">
		<input type="checkbox" name="wpt_options[enable_accordion]" id="enable_accordion" value="1" <?php checked( 1, $enable_accordion ); ?> />
		<?php esc_html_e( 'Enable this checkbox to add accordions style shown in product detail page.', 'woocommerce-product-tabs' ); ?>
		</label>
		<?php
	}

	/**
	 * Register disable_content_filter field.
	 *
	 * @since 1.0.0
	 */
	function disable_content_filter() {
		$disable_content_filter = $this->get_option( 'disable_content_filter' );
		?>
		<label for="disable_content_filter">
		<input type="checkbox" name="wpt_options[disable_content_filter]" id="disable_content_filter" value="1" <?php checked( 1, $disable_content_filter ); ?> />
		<?php esc_html_e( 'Enable this checkbox if you are using a page builder and have problems with the content preview.', 'woocommerce-product-tabs' ); ?>
		</label>
		<?php
	}

	/**
	 * Register size_accordion field.
	 *
	 * @since 1.0.0
	 */
	function size_accordion() {
		$accordion_shown_size = $this->get_option( 'accordion_shown_size' );
		if ( empty( $accordion_shown_size ) ) {
			$accordion_shown_size = 767;
		}
		?>
		<input type="number" name="wpt_options[accordion_shown_size]" id="accordion_shown_size" value="<?php echo esc_attr( $accordion_shown_size ); ?>"/>
		<?php
	}

	/**
	 * Display section message.
	 *
	 * @since 1.0.0
	 */
	function setting_icons_section() {
		echo esc_html_e( 'Icon option for Default Tabs', 'woocommerce-product-tabs' );
	}

	/**
	 * Register description_icon field.
	 *
	 * @since 1.0.0
	 */
	function field_description_icon() {
		$description_icon = $this->get_option( 'description_icon' );
		?>
		<div class="icon-wrap">
		<a href="#" class="tab_icon button button-secondary"><?php esc_html_e( 'Add Icon', 'woocommerce-product-tabs' ); ?></a>
		<input type="hidden" name="wpt_options[description_icon]" class="regular-text input-icon" value="<?php echo esc_attr( $description_icon ); ?>" />
		<div class="icon-output">
			<?php if ( ! empty( $description_icon ) ) : ?>
			<i class="<?php echo esc_attr( $description_icon ); ?>"></i>
			<a href="#" class="icon-remove"><?php esc_html_e( 'Remove', 'woocommerce-product-tabs' ); ?></a>
			<?php endif ?>
		</div>
		</div>
		<?php
	}

	/**
	 * Register info_icon field.
	 *
	 * @since 1.0.0
	 */
	function field_info_icon() {
		$info_icon = $this->get_option( 'info_icon' );
		?>
		<div class="icon-wrap">
		<a href="#" class="tab_icon button button-secondary"><?php esc_html_e( 'Add Icon', 'woocommerce-product-tabs' ); ?></a>
		<input type="hidden" name="wpt_options[info_icon]" class="regular-text input-icon" value="<?php echo esc_attr( $info_icon ); ?>" />
		<div class="icon-output">
			<?php if ( ! empty( $info_icon ) ) : ?>
			<i class="<?php echo esc_attr( $info_icon ); ?>"></i>
			<a href="#" class="icon-remove"><?php esc_html_e( 'Remove', 'woocommerce-product-tabs' ); ?></a>
			<?php endif ?>
		</div>
		</div>
		<?php
	}

	/**
	 * Register review_icon field.
	 *
	 * @since 1.0.0
	 */
	function field_review_icon() {
		$review_icon = $this->get_option( 'review_icon' );
		?>
		<div class="icon-wrap">
		<a href="#" class="tab_icon button button-secondary"><?php esc_html_e( 'Add Icon', 'woocommerce-product-tabs' ); ?></a>
		<input type="hidden" name="wpt_options[review_icon]" class="regular-text input-icon" value="<?php echo esc_attr( $review_icon ); ?>" />
		<div class="icon-output">
			<?php if ( ! empty( $review_icon ) ) : ?>
			<i class="<?php echo esc_attr( $review_icon ); ?>"></i>
			<a href="#" class="icon-remove"><?php esc_html_e( 'Remove', 'woocommerce-product-tabs' ); ?></a>
			<?php endif ?>
		</div>
		</div>
		<?php
		$description_priority = $this->get_option( 'description_priority' );
		$info_priority        = $this->get_option( 'info_priority' );
		$review_priority      = $this->get_option( 'review_priority' );
		?>
		<input type="hidden" name="wpt_options[description_priority]" value="<?php echo esc_attr( $description_priority ); ?>">
		<input type="hidden" name="wpt_options[info_priority]" value="<?php echo esc_attr( $info_priority ); ?>">
		<input type="hidden" name="wpt_options[review_priority]" value="<?php echo esc_attr( $review_priority ); ?>">

		<?php
	}

	/**
	 * Validate plugin options.
	 *
	 * @since 1.0.0
	 *
	 * @param array $input Options.
	 * @return array Validated options.
	 */
	function validate_plugin_options( $input ) {
		$input['description']            = sanitize_text_field( $input['description'] );
		$input['info']                   = sanitize_text_field( $input['info'] );
		$input['review']                 = sanitize_text_field( $input['review'] );
		$input['hide_description']       = (bool) $input['hide_description'] ? 1 : 0;
		$input['hide_info']              = (bool) $input['hide_info'] ? 1 : 0;
		$input['hide_review']            = (bool) $input['hide_review'] ? 1 : 0;
		$input['search_by_tabs']         = (bool) $input['search_by_tabs'] ? 1 : 0;
		$input['enable_accordion']       = (bool) $input['enable_accordion'] ? 1 : 0;
		$input['disable_content_filter'] = (bool) $input['disable_content_filter'] ? 1 : 0;
		$input['accordion_shown_size']   = sanitize_text_field( $input['accordion_shown_size'] );
		$input['description_icon']       = sanitize_text_field( $input['description_icon'] );
		$input['info_icon']              = sanitize_text_field( $input['info_icon'] );
		$input['review_icon']            = sanitize_text_field( $input['review_icon'] );
		$input['description_priority']   = absint( $input['description_priority'] );
		$input['info_priority']          = absint( $input['info_priority'] );
		$input['review_priority']        = absint( $input['review_priority'] );

		return $input;
	}

	function comparator_default_order( $object1, $object2 ) {
		if ( $object1->menu_order > $object2->menu_order ) {
			$output = 1;
		} elseif ( $object1->menu_order < $object2->menu_order ) {
			$output = -1;
		} else {
			$output = 0;
		}

			return $output;
	}

	/**
	 * Save Reorder.
	 *
	 * @since 1.0.0
	 */
	function post_order_update_callback() {
		$output = [];
		parse_str( $_POST['order'], $data );

		if ( ! is_array( $data ) ) {
			return wp_send_json_error( $output );
		}

		foreach ( $data as $values ) {
			$i = 1;
			foreach ( $values as $id ) {

				$wpt_options                         = get_option( 'wpt_options' );
				$wpt_options['description_priority'] = $wpt_options['description_priority'] ? $wpt_options['description_priority'] : 1;
				$wpt_options['info_priority']        = $wpt_options['info_priority'] ? $wpt_options['info_priority'] : 2;
				$wpt_options['review_priority']      = $wpt_options['review_priority'] ? $wpt_options['review_priority'] : 3;

				if ( ! absint( $id ) && $id == 'description' ) {
					$wpt_options['description_priority'] = $i;
				}
				if ( ! absint( $id ) && $id == 'info' ) {
					$wpt_options['info_priority'] = $i;
				}
				if ( ! absint( $id ) && $id == 'review' ) {
					$wpt_options['review_priority'] = $i;
				}

				$update_post = [
					'ID'         => absint( $id ),
					'menu_order' => $i,
				];

				wp_update_post( $update_post );
				update_option( 'wpt_options', $wpt_options );

				$i++;
			}
		}

		$output['message'] = __( 'Saved successfully.', 'woocommerce-product-tabs' );

		wp_send_json_success( $output );
	}

	/**
	 * Product tab list as order.
	 *
	 * @since 1.0.0
	 */
	function post_order_list( $query ) {

		$post_type = $query->get( 'post_type' );

		if ( $post_type == WOOCOMMERCE_PRODUCT_TABS_POST_TYPE_TAB ) {
			$query->set( 'orderby', 'menu_order' );
			$query->set( 'order', 'ASC' );
		}

	}

	/**
	 * Redirect to welcome page.
	 *
	 * @since 1.0.0
	 */
	public function do_redirect() {
		if ( get_option( 'wpt_do_activation_redirect', false ) ) {
			delete_option( 'wpt_do_activation_redirect' );
			wp_redirect( admin_url( 'admin.php?page=wpt-welcome' ) );
			exit;
		}
	}

	/**
	 * Customize tab.
	 *
	 * @since 1.0.2
	 *
	 * @param int     $post_id Post ID.
	 * @param WP_Post $post WP_Post object.
	 * @param bool    $update Whether this is update or not.
	 */
	function woo_product_tabs_override_tab_slug( $post_id, $post, $update ) {
		// Only want to set if this is a new post.
		if ( $update ) {
			return;
		}

		// Bail out if this is an autosave.
		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
			return;
		}

		// Bail out if this is not an event item.
		if ( WOOCOMMERCE_PRODUCT_TABS_POST_TYPE_TAB !== $post->post_type ) {
			return;
		}

		// Bail out if no permission.
		if ( ! current_user_can( 'edit_post', $post_id ) ) {
			return;
		}

		remove_action( 'save_post', [ $this, 'woo_product_tabs_override_tab_slug' ], 20 );

		$unique_slug = 'wpt-' . $post_id;

		$new_data = [
			'ID'        => $post_id,
			'post_name' => $unique_slug,
		];

		wp_update_post( $new_data );

		add_action( 'save_post', [ $this, 'woo_product_tabs_override_tab_slug' ], 20, 3 );
	}

	function welcome_page_init() {
		$obj = new Welcome( 'plugin', 'woocommerce-product-tabs-pro' );

		$obj->set_page(
			[
				'page_title'    => esc_html__( 'WooCommerce Product Tabs Pro', 'woocommerce-product-tabs' ),
				'page_subtitle' => sprintf( esc_html__( 'Version: %s', 'woocommerce-product-tabs' ), WOOCOMMERCE_PRODUCT_TABS_VERSION ),
				'menu_title'    => esc_html__( 'Getting Started', 'woocommerce-product-tabs' ),
				'menu_slug'     => 'wpt-welcome',
				'parent_page'   => 'wpt-options',
			]
		);

		$obj->set_quick_links(
			[
				[
					'text' => 'Plugin Page',
					'url'  => 'https://barn2.com/wordpress-plugins/woocommerce-product-tabs/',
					'type' => 'primary',
				],
				[
					'text' => 'Documentation',
					'url'  => 'https://barn2.com/kb-categories/product-tabs-documentation/',
					'type' => 'secondary',
				],
			]
		);

		$obj->set_admin_notice(
			[
				'screens' => [ 'dashboard' ],
			]
		);

		$obj->add_tab(
			[
				'id'    => 'welcome',
				'title' => 'Welcome',
				'type'  => 'grid',
				'items' => [
					[
						'title'       => 'Custom Tabs',
						'icon'        => 'dashicons dashicons-list-view',
						'description' => 'Click button below to start creating custom tabs and assign those to products. You can also create tabs with default content for the product.',
						'button_text' => 'Product Tabs',
						'button_url'  => admin_url( 'edit.php?post_type=woo_product_tab' ),
						'button_type' => 'primary',
						'is_new_tab'  => false,
					],
					[
						'title'       => 'Having issue with page builders?',
						'icon'        => 'dashicons dashicons-info',
						'description' => 'Click here to fix the_content filter issue if custom tabs are not working with page builders.',
						'button_text' => 'Go to Settings',
						'button_url'  => admin_url( 'admin.php?page=wpt-settings' ),
						'button_type' => 'secondary',
						'is_new_tab'  => false,
					],
					[
						'title'       => 'Get Support',
						'icon'        => 'dashicons dashicons-editor-help',
						'description' => 'Got plugin support question or found bug or got some feedbacks? Please submit support request from WooCommerce Product Tabs Pro -> Settings -> Contact Us.',
					],
					[
						'title'       => 'Customization Request',
						'icon'        => 'dashicons dashicons-admin-generic',
						'description' => 'Feel free to contact us if you need any customization service.',
						'button_text' => 'Customization Request',
						'button_url'  => 'https://barn2.com/kb/plugin-customizations/',
						'button_type' => 'secondary',
						'is_new_tab'  => true,
					],
				],
			]
		);

		$obj->set_sidebar(
			[
				'render_callback' => [ $this, 'render_welcome_page_sidebar' ],
			]
		);

		$obj->run();
	}

	/**
	 * Render welcome page sidebar.
	 *
	 * @since 1.0.5
	 *
	 * @param Welcome $object Instance of Welcome class.
	 */
	function render_welcome_page_sidebar( $object ) {
		$object->render_sidebar_box(
			[
				'title'   => 'Our Plugins',
				'content' => '<div class="wpc-plugins-list"></div>',
			],
			$object
		);
	}

	/**
	 * AJAX callback for plugins list.
	 *
	 * @since 1.0.5
	 */
	function ajax_callback_plugins_list() {
		$output = [];

		$posts = $this->get_plugins_list();

		if ( ! empty( $posts ) ) {
			$output = $posts;
		}

		if ( ! empty( $output ) ) {
			wp_send_json_success( $output, 200 );
		} else {
			wp_send_json_error( $output, 404 );
		}
	}

	/**
	 * Return plugins list.
	 *
	 * @since 1.0.5
	 *
	 * @return array Plugins list array.
	 */
	function get_plugins_list() {
		$transient_key = 'wpc_wptp_plugins_list';

		$transient_period = 7 * DAY_IN_SECONDS;

		$output = get_transient( $transient_key );

		if ( false === $output ) {
			$output = [];

			$request = wp_safe_remote_get( 'https://wpconcern.com/wpc-api/plugins-list' );

			if ( is_wp_error( $request ) ) {
					return $output;
			}

			$body = wp_remote_retrieve_body( $request );
			$json = json_decode( $body, true );

			if ( isset( $json['plugins'] ) && ! empty( $json['plugins'] ) ) {
				$output = $json['plugins'];
			}

			set_transient( $transient_key, $output, $transient_period );
		}

		return $output;
	}

	public function check_license() {
		if ( ! $this->plugin->is_freemius_active() ) {
			return;
		}

		// if a Freemius license is currently active, then remove the EDD license notice
		global $wp_filter;

		foreach ( $wp_filter['admin_init']['50'] as $key => $filter ) {
			if ( is_a( $filter['function'][0], 'Barn2\Plugin\WC_Product_Tabs_Pro\Dependencies\Lib\Plugin\License\Admin\License_Notices' ) ) {
				// if a Freemius license is currently active
				remove_action( 'admin_init', $key, 50 );
			}
		}
	}
}
