<?php
/**
 * Plugin Name: WPS AdBlocker Detector
 * Plugin URI: https://www.wp-script.com/adult-wordpress-plugins/adblocker-detector/
 * Description: Adblocker detector plugin with customization options.
 * Author: WP-Script
 * Author URI: https://www.wp-script.com
 * Version: 1.0.1
 * Text Domain: adde
 * Domain Path: /languages
 *
 * @package ADDE\Main
 */

// Exit if accessed directly.
defined( 'ABSPATH' ) || die( 'Cheatin&#8217; uh?' );

/**
 * Singleton Class.
 *
 * @since 1.0.1
 *
 * @final
 */
final class ADDE {

	/**
	 * The instance of WPS ADBLOCK DETECTOR plugin
	 *
	 * @var instanceof WPSCORE $instance
	 * @access private
	 * @static
	 */
	private static $instance;

	/**
	 * The config of WPS ADBLOCK DETECTOR plugin
	 *
	 * @var array $config
	 * @access private
	 * @static
	 */
	private static $config;

	/**
	 * __clone method
	 *
	 * @return void
	 */
	public function __clone() {
		_doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin&#8217; huh?', 'adde_lang' ), '1.0' );}

	/**
	 * __wakeup method
	 *
	 * @return void
	 */
	public function __wakeup() {
		_doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin&#8217; huh?', 'adde_lang' ), '1.0' );}

	/**
	 * Instance method
	 *
	 * @since 1.0.0
	 *
	 * @return self::$instance
	 */
	public static function instance() {
		if ( ! isset( self::$instance ) && ! ( self::$instance instanceof ADDE ) ) {
			include_once ABSPATH . 'wp-admin/includes/plugin.php';

			if ( ! is_plugin_active( 'wp-script-core/wp-script-core.php' ) ) {
				require plugin_dir_path( __FILE__ ) . 'admin/vendors/tgm-activation-x/plugin-activation.php';
				require plugin_dir_path( __FILE__ ) . 'admin/vendors/tgm-activation-x/class-tgm-plugin-activation.php';
			} else {
				self::$instance = new ADDE();
				// load config file.
				require plugin_dir_path( __FILE__ ) . 'config.php';
				// load options file.
				// phpcs:disable
				eval( WPSCORE()->eval_product_data( 'ADDE', 'load_options' ) );
				// phpcs:enable
				// Load text domain.
				self::$instance->load_textdomain();
				if ( is_admin() ) {
					// Load admin filters.
					self::$instance->load_admin_filters();
					// Load admin hooks.
					self::$instance->load_admin_hooks();
					// Auto-load admin php files.
					self::$instance->auto_load_php_files( 'admin' );
					// Load admin features.
					self::$instance->admin_init();
				}
				if ( ! is_admin() && 'wp-login.php' !== $GLOBALS['pagenow'] ) {
					// Load public filters.
					// self::$instance->load_public_filters();
					// Load public hooks.
					self::$instance->load_public_hooks();
					// Auto-load admin php files.
					self::$instance->auto_load_php_files( 'public' );
					// Load public features.
					self::$instance->public_init();
				}
			}
		}
		return self::$instance;
	}

	/**
	 * Add js and css files, tabs, pages, php files in admin mode.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public function load_admin_filters() {
		add_filter( 'WPSCORE-scripts', array( $this, 'add_admin_scripts' ) );
		add_filter( 'WPSCORE-tabs', array( $this, 'add_admin_navigation' ) );
		add_filter( 'WPSCORE-pages', array( $this, 'add_admin_navigation' ) );
	}

	/**
	 * Add admin js and css scripts. This is a WPSCORE-scripts filter callback function.
	 *
	 * @since 1.0.0
	 *
	 * @param array $scripts List of all WPS CORE CSS / JS to load.
	 * @return array $scripts List of all WPS CORE + WPS ADBLOCK DETECTOR CSS / JS to load.
	 */
	public function add_admin_scripts( $scripts ) {
		if ( isset( self::$config['scripts'] ) ) {
			if ( isset( self::$config['scripts']['js'] ) ) {
				$scripts += (array) self::$config['scripts']['js'];
			}
			if ( isset( self::$config['scripts']['css'] ) ) {
				$scripts += (array) self::$config['scripts']['css'];
			}
		}
		return $scripts;
	}

	/**
	 * Add WPS ADBLOCK DETECTOR admin navigation tab. This is a WPSCORE-tabs and WPSCORE-pages filters callback function.
	 *
	 * @since 1.0.0
	 *
	 * @param array $nav List of all WPS CORE navigation tabs to add.
	 * @return array $nav List of all WPS CORE + WPS ADBLOCK DETECTOR navigation tabs to add.
	 */
	public function add_admin_navigation( $nav ) {
		if ( isset( self::$config['nav'] ) ) {
			// phpcs:disable
			eval( WPSCORE()->eval_product_data( 'ADDE', 'add_admin_navigation' ) );
			// phpcs:enable
		}
		return $nav;
	}

	/**
	 * Auto-loader for PHP files
	 *
	 * @since 1.0.0
	 *
	 * @param string{'admin','public'} $dir Directory where to find PHP files to load.
	 * @static
	 * @return void
	 */
	public static function auto_load_php_files( $dir ) {
		$dirs = (array) ( plugin_dir_path( __FILE__ ) . $dir . '/' );
		foreach ( (array) $dirs as $dir ) {
			$files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir ) );
			if ( ! empty( $files ) ) {
				foreach ( $files as $file ) {
					// exlude dir.
					if ( $file->isDir() ) {
						continue; }
					// exlude index.php.
					if ( $file->getPathname() === 'index.php' ) {
						continue; }
					// exlude files != .php.
					if ( substr( $file->getPathname(), -4 ) !== '.php' ) {
						continue; }
					// exlude files from -x suffixed directories.
					if ( substr( $file->getPath(), -2 ) === '-x' ) {
						continue; }
					// exlude -x suffixed files.
					if ( substr( $file->getPathname(), -6 ) === '-x.php' ) {
						continue; }
					// else require file.
					require $file->getPathname();
				}
			}
		}
	}

	/**
	 * Registering WPS ADBLOCK DETECTOR activation / deactivation / uninstall hooks.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public function load_admin_hooks() {
		register_activation_hook( __FILE__, array( 'ADDE', 'activation' ) );
		register_deactivation_hook( __FILE__, array( 'ADDE', 'deactivation' ) );
		register_uninstall_hook( __FILE__, array( 'ADDE', 'uninstall' ) );
	}

	/**
	 * Stuff to do on WPS ADBLOCK DETECTOR activation. This is a register_activation_hook callback function.
	 *
	 * @since 1.0.0
	 *
	 * @access private
	 * @static
	 * @return void
	 */
	public static function activation() {
		WPSCORE()->update_client_signature();
		WPSCORE()->init( true );
	}

	/**
	 * Stuff to do on WPS ADBLOCK DETECTOR deactivation. This is a register_deactivation_hook callback function.
	 *
	 * @since 1.0.0
	 *
	 * @access private
	 * @static
	 * @return void
	 */
	public static function deactivation() {
		WPSCORE()->update_client_signature();
		WPSCORE()->init( true );
	}

	/**
	 * Stuff to do on WPS ADBLOCK DETECTOR deactivation. This is a register_deactivation_hook callback function.
	 *
	 * @since 1.0.0
	 *
	 * @access private
	 * @static
	 * @return void
	 */
	public static function uninstall() {
		WPSCORE()->update_client_signature();
		WPSCORE()->init( true );
	}

	/**
	 * Text domain function
	 *
	 * @since 1.0.0
	 *
	 * @return false by default
	 */
	public function load_textdomain() {
		// Set filter for plugin's languages directory.
		$lang_dir = dirname( plugin_basename( ADDE_FILE ) ) . '/languages/';

		// Traditional WordPress plugin locale filter.
		$mofile = sprintf( '%1$s-%2$s.mo', 'adde_lang', get_locale() );

		// Setup paths to current locale file.
		$mofile_local  = $lang_dir . $mofile;
		$mofile_global = WP_LANG_DIR . '/adde_lang/' . $mofile;

		if ( file_exists( $mofile_global ) ) {
			// Look in global /wp-content/languages/WPSCORE/ folder.
			load_textdomain( 'adde_lang', $mofile_global );
		} elseif ( file_exists( $mofile_local ) ) {
			// Look in local /wp-content/plugins/WPSCORE/languages/ folder.
			load_textdomain( 'adde_lang', $mofile_local );
		} else {
			// Load the default language files.
			load_plugin_textdomain( 'adde_lang', false, $lang_dir );
		}
		return false;
	}

	/**
	 * Load hooks on public mode.
	 *
	 * @since 1.0.0
	 *
	 * @return   void
	 */
	public function load_public_hooks() {
		add_action( 'wp_enqueue_scripts', array( $this, 'load_public_js' ), 100 );
		add_action( 'wp_head', array( $this, 'load_public_head_hook' ), 100 );
		add_action( 'wp_footer', array( $this, 'load_public_footer_hook' ), 100 );
	}

	public function load_public_head_hook() {
		require plugin_dir_path( __FILE__ ) . 'public/hooks-x/header.php';
	}

	public function load_public_footer_hook() {
		require plugin_dir_path( __FILE__ ) . 'public/hooks-x/footer.php';
	}

	/**
	 * Load JS files on public mode.
	 *
	 * @since 1.0.0
	 *
	 * @return   void
	 */
	public function load_public_js() {
		// Load temp style.
		require_once plugin_dir_path( __FILE__ ) . 'dynamic-style.php';
	}

	/**
	 * Get the xbox options with default value from the option declaration default value.
	 *
	 * @param string $xbox_id The xbox id.
	 * @param string $field_id The field id.
	 * @return mixed The xbox option with the default value if not set.
	 */
	private function get_xbox_field_with_default( $xbox_id, $field_id ) {
		$options = xbox_get( $xbox_id );
		if ( ! isset( $options->fields[ $field_id ] ) ) {
			return '';
		}
		if ( ! isset( $options->fields[ $field_id ]['default'] ) ) {
			return '';
		}
		$default_value = $options->fields[ $field_id ]['default'];
		return xbox_get_field_value( 'adde-options', $field_id, $default_value );
	}

	/**
	 * Stuff to do on admin init.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	private function admin_init() {}

	/**
	 * Stuff to do on public init.
	 *
	 * @since 1.0.0
	 *
	 * @access private
	 * @return void
	 */
	private function public_init() {}

	/**
	 * Check if current theme is a theme from WP-Script.
	 *
	 * @since 1.1.4
	 *
	 * @return bool true if current theme is a theme from WP-Script, false if not
	 */
	public static function is_wps_theme() {
		$current_theme = wp_get_theme();
		return 'WP-Script' === $current_theme->get( 'Author' );
	}
}
// End of singleton Class.

/**
 * Create the WPS ADBLOCK DETECTOR instance in a function and call it.
 *
 * @return ADDE::instance();
 */
// phpcs:disable
function ADDE() {
	return ADDE::instance();
}

add_action( 'init', 'adde_check_plugin_status' );
/**
 * Init hook callback to run the plugin only if it is connected.
 */
function adde_check_plugin_status() {
	// Do not run the plugin if it is not connected.
	if ( 'connected' !== WPSCORE()->get_product_status( 'ADDE' ) ) {
		return;
	}

	// Do not run the plugin if we are in widget iframe preview.
	if ( isset( $_SERVER['REQUEST_URI'] ) && false !== strpos( $_SERVER['REQUEST_URI'], 'widget' ) ) {
		return;
	}

	ADDE();
}
