<?php

/**
 * The admin-specific functionality of the plugin.
 *
 * @link       https://www.linkedin.com/in/devendrasinghbhandari/
 * @since      1.0.0
 *
 * @package    Disable_Fast_Velocity_Minify
 * @subpackage Disable_Fast_Velocity_Minify/admin
 */

/**
 * The admin-specific functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the admin-specific stylesheet and JavaScript.
 *
 * @package    Disable_Fast_Velocity_Minify
 * @subpackage Disable_Fast_Velocity_Minify/admin
 * @author     Devendra Singh Bhandari <devendra21986@gmail.com>
 */
class Disable_Fast_Velocity_Minify_Admin {

	/**
	 * 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;

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    1.0.0
	 * @param      string    $plugin_name       The name of this plugin.
	 * @param      string    $version    The version of this plugin.
	 */
	public function __construct( $plugin_name, $version ) {

		$this->plugin_name = $plugin_name;
		$this->version = $version;

	}

	/**
	 * Register the stylesheets for the admin area.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_styles() {

		/**
		 * This function is provided for demonstration purposes only.
		 *
		 * An instance of this class should be passed to the run() function
		 * defined in Disable_Fast_Velocity_Minify_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The Disable_Fast_Velocity_Minify_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */

		wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/disable-fast-velocity-minify-admin.css', array(), $this->version, 'all' );

	}

	/**
	 * Register the JavaScript for the admin area.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_scripts() {

		/**
		 * This function is provided for demonstration purposes only.
		 *
		 * An instance of this class should be passed to the run() function
		 * defined in Disable_Fast_Velocity_Minify_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The Disable_Fast_Velocity_Minify_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */

		wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/disable-fast-velocity-minify-admin.js', array( 'jquery' ), $this->version, false );

	}

	/**
	 * Add plugin setting page.
	 * 
	 * @since    1.0.0
	 */
	public function add_admin_menu() {

		add_options_page(
			esc_html__( 'Disable Fast Velocity Minify', 'disable-fast-velocity-minify' ),
			esc_html__( 'Disable Fast Velocity Minify', 'disable-fast-velocity-minify' ),
			'manage_options',
			'dfvm',
			array( $this, 'page_layout' )
		);

	}

	/**
	 * Render plugin page layout.
	 * 
	 * @since    1.0.0
	 */
	public function page_layout() {

		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		$tabs = array(
			'settings' => __( 'Settings', 'disable-fast-velocity-minify' ),
			'faq' => __( 'FAQ', 'disable-fast-velocity-minify' ),
		);

		$requested_tab = '';
		if ( isset( $_GET['tab'] ) ) {
			$requested_tab = sanitize_key( $_GET['tab'] );
		}

		$current_tab = ( ! empty ( $requested_tab ) && array_key_exists( $requested_tab, $tabs ) ) ? $requested_tab : 'settings';

		switch ( $current_tab ) {
			case 'settings':
				require_once 'partials/disable-fast-velocity-minify-admin-settings-display.php';
				break;
			case 'faq':
				require_once 'partials/disable-fast-velocity-minify-admin-faq-display.php';
				break;
		}

	}

	/**
	 * Add plugin setting page.
	 * 
	 * @since    1.0.0
	 */
	public function register_option_page() {

		// Create cmb2 box
		$cmb  = new_cmb2_box(
			[
				'id'           => 'dfvm_settings',
				'capability'   => 'manage_options',
				'object_types' => [ 'options-page' ],
				'option_key'   => 'dfvm',
				'hookup'       => false,
			]
		);

		// Get all post types
		$args = array(
			'public' => true
		);
		$post_types = get_post_types( $args, 'objects' );
		unset( $post_types['attachment'] );

		$cpt_options = [];
		foreach ( $post_types as $key => $post_type ) {
			$label = $post_type->labels->singular_name;
			$cpt_options[$key] = $label;
		}

		$cmb->add_field( array(
			'name'              => 'Post Type',
			'desc'              => '',
			'id'                => 'cmb2_post_types',
			'type'              => 'multicheck',
			'options'           => $cpt_options,
			'select_all_button' => false,
			'before_row'        => '<h2 class="dfvm-field-heading">Select post type to exclude all of its posts from fast velocity minify</h2>',
		) );

		foreach ( $post_types as $key => $post_type ) {
			$cmb->add_field( array(
				'name'    => __( $post_type->labels->singular_name, 'disable-fast-velocity-minify' ),
				'desc'    => __( 'Drag ' . strtolower( $post_type->label ) . ' from the left column to the right column to exclude it from FVM.', 'disable-fast-velocity-minify' ),
				'id'      => 'cmb2_' . $key,
				'type'    => 'custom_attached_posts',
				'column'  => false,
				'options' => array(
					'filter_boxes'       => true,
					'query_args'         => array(
						'posts_per_page' => 10,
						'post_type'      => $key,
					),
				),
				'before_row' => '<h2 class="dfvm-field-heading">Select any ' . strtolower( $post_type->labels->singular_name ) . ' to exclude it from fast velocity minify</h2>',
			) );
		}

		// Get all taxonomies
		$args = array(
			'public' => true
		);
		$taxonomies = get_taxonomies( $args, 'objects' );
		unset( $taxonomies['post_format'] );

		$tax_options = [];
		foreach ( $taxonomies as $key => $taxonomy ) {
			$label = $taxonomy->labels->singular_name;
			$tax_options[$key] = $label;
		}

		$cmb->add_field( array(
			'name'              => 'Taxonomy',
			'desc'              => '',
			'id'                => 'cmb2_taxonomies',
			'type'              => 'multicheck',
			'options'           => $tax_options,
			'select_all_button' => false,
			'before_row'        => '<h2 class="dfvm-field-heading">Select taxonomy to exclude its archive page from fast velocity minify</h2>',
		) );

	}

}
