<?php

/**
 * The public-facing 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/public
 */

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

	/**
	 * 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 the 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 public-facing side of the site.
	 *
	 * @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-public.css', array(), $this->version, 'all' );

	}

	/**
	 * Register the JavaScript for the public-facing side of the site.
	 *
	 * @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-public.js', array( 'jquery' ), $this->version, false );

	}

	/**
	 * Check whether the requested URL is excluded from fvm.
	 * 
	 * @since    1.0.0
	 */
	function is_request_excluded_from_fvm() {

		$opts = get_option( 'dfvm' );

		if ( empty( $opts ) || ! is_array( $opts ) ) {
			return false;
		}

		if ( is_archive() ) {
			/**
			 * Taxonomies to exclude from fvm
			 */
			$selected_taxonomies = array();
			$current_taxonomy_name = get_queried_object()->taxonomy;

			// Get list of taxonomy to exclude from FVM
			$key = 'cmb2_taxonomies';
			if ( array_key_exists( $key, $opts ) && false !== $opts[ $key ] ) {
				$selected_taxonomies = $opts[ $key ];
			}

			if ( ! empty( $selected_taxonomies ) && ! empty( $current_taxonomy_name ) ) {
				foreach ($selected_taxonomies as $taxonomy) {
					if ( $current_taxonomy_name === $taxonomy ) {
						return true;
					}
				}
			}
		} else if ( is_singular() ) {
			/**
			 * Post types to exclude from FVM
			 */
			$current_post_type = get_post_type();
			$selected_post_types = array();

			// Get list of post type to exclude from FVM
			$key = 'cmb2_post_types';
			if ( array_key_exists( $key, $opts ) && false !== $opts[ $key ] ) {
				$selected_post_types = $opts[ $key ];
			}

			if ( ! empty( $selected_post_types ) && ! empty( $current_post_type ) ) {
				foreach ($selected_post_types as $post_type) {
					if ( $current_post_type === $post_type ) {
						return true;
					}
				}
			}

			/**
			 * Specific posts to exclude from FVM.
			 */
			$current_post_id = get_the_ID();

			// Get list of posts to exclude from FVM
			$key = 'cmb2_' . $current_post_type;
			if ( array_key_exists( $key, $opts ) && false !== $opts[ $key ] ) {
				$selected_post_ids = $opts[ $key ];
			}

			if ( ! empty( $selected_post_ids ) && ! empty( $current_post_id ) ) {
				foreach ($selected_post_ids as $post_id) {
					if ( $current_post_id === (int) $post_id ) {
						return true;
					}
				}
			}
		}

		return false;

	}

	/**
	 * Remove all attached functions to disable the processing of fvm.
	 * 
	 * @since    1.0.0
	 */
	function remove_fvm_frontend_hooks() {

		if ( ! is_admin() && $this->is_request_excluded_from_fvm() ) {
			// fvm version 2
			remove_action( 'wp_print_scripts', 'fastvelocity_min_merge_header_scripts', PHP_INT_MAX );
			remove_action( 'wp_print_footer_scripts', 'fastvelocity_min_merge_footer_scripts', PHP_INT_MAX );
			remove_action( 'wp_print_styles', 'fastvelocity_min_merge_header_css', PHP_INT_MAX );
			remove_action( 'wp_print_footer_scripts', 'fastvelocity_min_merge_footer_css', PHP_INT_MAX );
			remove_action( 'wp_print_styles', 'fastvelocity_add_google_fonts_merged', PHP_INT_MAX );
			remove_action( 'wp_print_footer_scripts', 'fastvelocity_add_google_fonts_merged', PHP_INT_MAX );
			remove_action( 'init', 'fastvelocity_min_disable_wp_emojicons', PHP_INT_MAX );
			remove_action( 'template_redirect', 'fastvelocity_min_html_compression_start', PHP_INT_MAX );
			remove_filter( 'style_loader_src', 'fastvelocity_remove_cssjs_ver', PHP_INT_MAX );
			remove_filter( 'script_loader_tag', 'fastvelocity_min_defer_js', PHP_INT_MAX );
			remove_action( 'wp_footer', 'fvm_add_loadcss', PHP_INT_MAX );
			remove_action( 'style_loader_tag', 'fastvelocity_optimizecss', PHP_INT_MAX );

			// fvm version 3
			remove_action( 'template_redirect', 'fvm_start_buffer', -PHP_INT_MAX );
		}

	}

}
