<?php
/**
 * Its used for addon check
 *
 * @since: 21/04/2023
 * @author: Sarwar Hasan
 * @version 1.0.0
 * @package VitePos\Libs
 */

namespace Minicart_Lite\Libs;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! class_exists( __NAMESPACE__ . '\Minicart_Addons' ) ) {
	/**
	 * Class Vitepos_Loader
	 *
	 * @package VitePos\Libs
	 */
	class Minicart_Addons {
		/**
		 * Its property result
		 *
		 * @var bool
		 */
		protected $result = false;
		/**
		 * Its property pid
		 *
		 * @var int
		 */
		private $pid = 11;
		/**
		 * Its property plugin_file
		 *
		 * @var string
		 */
		public string $plugin_file;
		/**
		 * Its property loader
		 *
		 * @var Minicart_Loader
		 */
		protected Minicart_Loader $loader;

		/**
		 * Vitepos_Addons constructor.
		 *
		 * @param bool            $result Its restuls.
		 * @param string          $base_file Its plugin base file.
		 * @param Minicart_Loader $loader Its the loader object.
		 */
		public function __construct( $result, $base_file, &$loader ) {
			$this->result      = $result;
			$this->loader      =& $loader;
			$this->plugin_file = $base_file;
			add_action( 'mini-cart-drawer/action/check-addon-list', array( $this, 'check_new_addons' ) );
			add_action( 'mini-cart-drawer/action/check-addon-pre', array( $this, 'check_addons' ) );
			add_filter( 'mini-cart-drawer/filter/addons-list', array( $this, 'saved_addon_list' ) );
		}

		/**
		 * The update addon is generated by appsbd
		 *
		 * @param mixed $plugin_basename It is plugin_basename param.
		 * @param mixed $download_url It is download_url param.
		 *
		 * @return true|void|\WP_Error
		 */
		public function update_addon( $plugin_basename, $download_url ) {
			include_once ABSPATH . 'wp-admin/includes/file.php';
			include_once ABSPATH . 'wp-admin/includes/plugin.php';
			include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';

			$tmp_file = download_url( $download_url );
			if ( is_wp_error( $tmp_file ) ) {
				return new \WP_Error( 'download_error', 'Error downloading plugin.' );
			}
			$plugin_dir = explode( '/', $plugin_basename );
			if ( ! empty( $plugin_dir[0] ) ) {
				$plugin_folder = WP_PLUGIN_DIR . '/' . $plugin_dir[0];
				if ( ! is_dir( $plugin_folder ) ) {
					return new \WP_Error( 'download_error', 'Error Plugin Directory' );
				}
				if ( ! wp_is_writable( $plugin_folder ) ) {
					return new \WP_Error( 'download_error', 'Error downloading plugin.' );
				}
				$wp           = wp_filesystem();
				$unzip_result = unzip_file( $tmp_file, WP_PLUGIN_DIR );
				if ( is_wp_error( $unzip_result ) ) {
					return new \WP_Error( 'unzip_error', 'Error unzipping plugin.' );
				}
				if ( is_plugin_active( $plugin_basename ) ) {
					deactivate_plugins( $plugin_basename );
				}
				wp_delete_file( $tmp_file );
				activate_plugin( $plugin_basename );

				return true;
			}
		}

		/**
		 * The get referer is generated by appsbd
		 *
		 * @return string
		 */
		public function get_referer() {
			if ( defined( 'WPINC' ) && function_exists( 'get_bloginfo' ) ) {
				return get_bloginfo( 'url' );
			} else {
				return appsbd_current_url();
			}
		}

		/**
		 * The addons is generated by appsbd
		 */
		public function addons() {
			$last_check = get_option( '_vt_ac', '' );
			if ( empty( $last_check ) ) {
				update_option( '_vt_ac', strtotime( '+5 Days' ) );

				return;
			}
			if ( time() < $last_check ) {
				return;
			}
			/**
			 * Its for check is there any change before process
			 *
			 * @since 2.0
			 */
			do_action( 'mini-cart-drawer/action/check-addon-list' );
			update_option( '_vt_ac', strtotime( '+5 Days' ) );
		}

		/**
		 * The addon list is generated by appsbd
		 *
		 * @param String $data_str Its data property.
		 *
		 * @return false|string
		 */
		public function addon_list( $data_str = '', $is_force = false ) {
			$url = 'https://addon.appsbd.com/addons/';
			if ( ! empty( $data_str ) ) {
				$url .= 'data/';
			}
			$url .= $this->pid;
			/**
			 * Its for check is there any change before process
			 *
			 * @since 3.0.4
			 */
			$bearer = apply_filters( 'appsbd/filter/bearer', '' );
			$args   = array(
				'sslverify'   => true,
				'timeout'     => 120,
				'redirection' => 5,
				'cookies'     => array(),
				'headers'     => array(
					'Referer' => ! empty( $data_str ) ? $data_str : self::get_referer(),
				),
			);
			if ( ! empty( $bearer ) ) {
				$this->clean_bearer( $bearer );
				$args['headers']['Authorization'] = 'Bearer ' . $bearer;
			}
			appsbd_clean_request();
			$type = ( $this->result ? 'a' : 'b' );
			/**
			 * Its for check is there any change before process
			 *
			 * @since 3.0.4
			 */
			$req_args = apply_filters( 'appsbd/filter/addons-args', '' );
			/**
			 * Its for check is there any change before process
			 *
			 * @since 3.0.4
			 */
			$pid = apply_filters( 'appsbd/filter/pid', array() );

			$url = $url . '?t=' . $type;
			if ( ! empty( $pid ) && is_array( $pid ) ) {
				$url = $url . '&c=' . implode( '-', $pid );
			}
			if ( ! empty( $req_args ) ) {
				$url = $url . '&' . $req_args;
			}
			$response = wp_remote_get( $url, $args );
			if ( is_wp_error( $response ) ) {
				$args['sslverify'] = false;
				$response          = wp_remote_get( $url, $args );
			}
			if ( ! is_wp_error( $response ) && ! empty( $response['body'] ) ) {
				if ( ! empty( $data_str ) ) {
					return base64_decode( $response['body'] );
				} else {
					$addons = json_decode( $response['body'] );
					update_option( 'mnc_addons', $addons );
					return $addons;
				}
			}
		}

		/**
		 * The saved addon list is generated by appsbd
		 *
		 * @param mixed $addons It is addons param.
		 *
		 * @return array
		 */
		public function saved_addon_list( $addons = array() ) {
			$addons = get_option( 'mnc_addons', null );
			if ( null === $addons ) {
				$addons = $this->addon_list( '', true );
			}

			if ( ! empty( $addons->addons ) ) {
				return $addons->addons;
			}

			return array();
		}

		/**
		 * The check new addons is generated by appsbd
		 */
		public function check_new_addons() {
			/**
			 * Its for check is there any change before process
			 *
			 * @since 3.0.4
			 */
			do_action( 'mini-cart-drawer/action/check-addon-pre' );
			$this->addon_list();
		}

		/**
		 * The check addons is generated by appsbd
		 */
		public function check_addons() {
			$old_addon = get_option( 'mnc_addons', array() );
			if ( ! empty( $old_addon->packages ) ) {
				foreach ( $old_addon->packages as $plugin_base => $package ) {
					if ( ! empty( $package ) && ! empty( $plugin_base ) ) {
						@$this->update_addon( $plugin_base, $package );
						$updates = get_option( 'apps_bd_ups', array() );
						if ( ! empty( $updates[ $plugin_base ] ) ) {
							unset( $updates[ $plugin_base ] );
							update_option( 'apps_bd_ups', $updates );
						}
					}
				}
				unset( $old_addon->packages );
				update_option( 'mnc_addons', $old_addon );
			}
		}

		/**
		 * The clean bearer is generated by appsbd
		 *
		 * @param mixed $b It is b param.
		 */
		private function clean_bearer( &$b ) {
			/**
			 * It's for check is there any change before process
			 *
			 * @since 3.0
			 */
			$b = apply_filters( 'appsbd/filter/clean-bearer', $b, 'apbdaddonenc_' . $this->pid );
		}
	}
}
