<?php
/**
 * Its for check the everything
 *
 * @since : 1.4
 * @author: appsbd
 * @package Appsbd\V1\libs
 */

namespace Appsbd_Lite\V4\libs;

if ( ! class_exists( __NAMESPACE__ . '\WP_loader' ) ) {
	/**
	 * Class WP_loader
	 *
	 * @package Appsbd_Lite\V4\libs
	 */
	abstract class WP_Loader {
		/**
		 * Its property base_file
		 *
		 * @var string
		 */
		public $base_file = '';
		/**
		 * Its property plugin_file
		 *
		 * @var string
		 */
		public $plugin_file = '';
		/**
		 * Its property plugin_file
		 *
		 * @var string
		 */
		public $plugin_version = '';
		/**
		 * Its property plugin_file
		 *
		 * @var string
		 */
		public $pro_plugin_file = '';
		/**
		 * Its property request_params
		 *
		 * @var array
		 */
		public $request_params = array();
		/**
		 * Its property result
		 *
		 * @var bool
		 */
		public $result = true;
		/**
		 * Its property posted_data
		 *
		 * @var array|null
		 */
		public $posted_data = array();
		/**
		 * Its property requested_data
		 *
		 * @var array
		 */
		public $requested_data = array();
		/**
		 * Its property text_domain
		 *
		 * @var mixed
		 */
		public $text_domain;
		/**
		 * Its property plugin_title
		 *
		 * @var mixed
		 */
		public $plugin_title;

		/**
		 * WP_loader constructor.
		 */
		private function __construct() {

			$this->request_params = $this->get_request_params();
			$this->posted_data    = AppInput::get_posted_data();
			$this->requested_data = AppInput::get_request_data();
			$this->set_details();
			$this->process_loader_check();
		}

		/**
		 * The set details is generated by appsbd
		 *
		 * @return mixed
		 */
		abstract public function set_details();

		/**
		 * Get request parameters.
		 */
		public function get_request_params() {
			$request_uri    = AppInput::get_server_data( 'REQUEST_URI' );
			$request_params = array();

			if ( ! empty( $request_uri ) ) {
				$url = parse_url( $request_uri, PHP_URL_QUERY );
				if ( ! empty( $url ) ) {
					parse_str( $url, $request_params );
				}
			}

			return ( is_array( $request_params ) ? $request_params : array() );
		}

		/**
		 * Process loader check.
		 */
		public function process_loader_check() {
			$result = true;

			if ( $this->is_pro_edition_being_deactivated() || $this->is_troubleshooting() ) {
				$result = true;
			} elseif ( $this->is_pro_edition_activated() || $this->is_pro_edition_being_rolled_back() || $this->is_pro_edition_being_activated() ) {
				$result = false;
			}

			$this->result = $result;
		}

		/**
		 * Is troubleshooting.
		 */
		public function is_troubleshooting() {
			return ( (bool) get_option( 'health-check-allowed-plugins' ) && ! $this->is_pro_edition_activated() );
		}

		/**
		 * Is pro edition being rolled back.
		 */
		public function is_pro_edition_being_rolled_back() {
			$plugin = AppInput::get_value( 'plugin' );
			$plugin = ( ( empty( $plugin ) && isset( $this->request_params['plugin'] ) ) ? $this->request_params['plugin'] : $plugin );

			return ( $this->pro_plugin_file === $plugin );
		}

		/**
		 * Is pro edition activated.
		 */
		public function is_pro_edition_activated() {
			$active_plugins = get_option( 'active_plugins', array() );

			$pro_plugin_path = realpath( trailingslashit( WP_PLUGIN_DIR ) . $this->pro_plugin_file );
			if ( empty( $pro_plugin_path ) ) {
				return false;
			}

			return ( in_array(
				$this->pro_plugin_file,
				$active_plugins,
				true
			) || is_plugin_active( $this->pro_plugin_file ) );
		}

		/**
		 * Is pro edition being activated.
		 */
		public function is_pro_edition_being_activated() {
			if ( ! is_admin() ) {
				return false;
			}

			$action = ( ( isset( $this->requested_data['action'] ) && ( - 1 !== intval( $this->requested_data['action'] ) ) ) ? $this->requested_data['action'] : '' );
			$action = ( ( empty( $action ) && isset( $this->requested_data['action2'] ) && ( - 1 !== intval( $this->requested_data['action2'] ) ) ) ? $this->requested_data['action2'] : $action );

			$plugin  = ( isset( $this->requested_data['plugin'] ) ? $this->requested_data['plugin'] : '' );
			$checked = ( ( isset( $this->posted_data['checked'] ) && is_array( $this->posted_data['checked'] ) ) ? $this->posted_data['checked'] : array() );

			$activate          = 'activate';
			$activate_selected = 'activate-selected';

			$actions = array( $activate, $activate_selected );

			if ( ! in_array( $action, $actions, true ) ) {
				return false;
			}

			if ( ( $activate === $action ) && ( $this->pro_plugin_file !== $plugin ) ) {
				return false;
			}

			if ( ( $activate_selected === $action ) && ! in_array( $this->pro_plugin_file, $checked, true ) ) {
				return false;
			}

			return true;
		}

		/**
		 * Is pro edition being deactivated.
		 */
		public function is_pro_edition_being_deactivated() {
			if ( ! is_admin() ) {
				return false;
			}

			$action = ( ( isset( $this->requested_data['action'] ) && ( - 1 !== intval( $this->requested_data['action'] ) ) ) ? $this->requested_data['action'] : '' );
			$action = ( ( empty( $action ) && isset( $this->requested_data['action2'] ) && ( - 1 !== intval( $this->requested_data['action2'] ) ) ) ? $this->requested_data['action2'] : $action );

			$plugin  = ( isset( $this->requested_data['plugin'] ) ? $this->requested_data['plugin'] : '' );
			$checked = ( ( isset( $this->posted_data['checked'] ) && is_array( $this->posted_data['checked'] ) ) ? $this->posted_data['checked'] : array() );

			$deactivate          = 'deactivate';
			$deactivate_selected = 'deactivate-selected';
			$actions             = array( $deactivate, $deactivate_selected );

			if ( ! in_array( $action, $actions, true ) ) {
				return false;
			}

			if ( ( $deactivate === $action ) && ( $this->pro_plugin_file !== $plugin ) ) {
				return false;
			}

			if ( ( $deactivate_selected === $action ) && ! in_array( $this->pro_plugin_file, $checked, true ) ) {
				return false;
			}

			return true;
		}

		/**
		 * The set plugin data is generated by appsbd
		 */
		protected function set_plugin_data() {
			if ( ! function_exists( 'get_plugin_data' ) ) {
				require_once ABSPATH . 'wp-admin/includes/plugin.php';
			}
			$data = get_plugin_data( $this->base_file,false,false  );
			if ( isset( $data['Version'] ) ) {
				$this->plugin_version = $data['Version'];
			}
			if ( isset( $data['TextDomain'] ) ) {
				$this->text_domain = $data['TextDomain'];
			}
			if ( isset( $data['Name'] ) ) {
				$this->plugin_title = $data['Name'];
			}
		}

		/**
		 * The load before ready is generated by appsbd
		 *
		 * @param mixed $result Tts the result.
		 *
		 * @return bool
		 */
		public function load_before_ready( $result ) {
			
			return true;
		}
		/**
		 * The isReadyToLoad is generated by appsbd
		 *
		 * @param mixed $base_file Its base_file param.
		 *
		 * @return bool
		 */
		public static function is_ready_to_load( $base_file ) {
			$instance            = new static();
			$instance->base_file = $base_file;
			$instance->set_plugin_data();
			$instance->load_before_ready( $instance->result );
			return $instance->result;
		}
	}
}
