<?php

/**
 * Panel
 *
 * @package Pb Addons
 */

define('PREMIUM_BLOCKS_PANEL_DIR', PREMIUM_BLOCKS_PATH . 'admin/includes/pb-panel/');
define('PREMIUM_BLOCKS_PANEL_URL', PREMIUM_BLOCKS_URL . 'admin/includes/pb-panel/');

if (! class_exists('Pb_Panel')) {

	/**
	 * Pb Panel
	 *
	 * @since 1.0.0
	 */
	class Pb_Panel
	{

		/**
		 * Default values
		 *
		 * @var array defaults
		 */
		private $defaults = array();

		/**
		 * Member Variable
		 *
		 * @var object instance
		 */
		private static $instance;
		private $menu_slug = 'pb_panel';
		/**
		 * Instance
		 *
		 * @return object
		 */
		public static function get_instance()
		{
			if (! isset(self::$instance)) {
				self::$instance = new self();
			}
			return self::$instance;
		}

		/**
		 *  Constructor
		 */
		public function __construct()
		{
			add_action('wp_ajax_pb-panel-update-option', array($this, 'update_option'));

			add_action('wp_ajax_pb-panel-update-settings', array($this, 'update_settings'));

			add_action('wp_ajax_pb-panel-update-global-features', array($this, 'update_global_features'));

			add_action('wp_ajax_pb-panel-update-performance-options', array($this, 'update_performance_options'));

			add_action('wp_ajax_pb-panel-integrations-options', array($this, 'update_integrations_options'));

			add_action('admin_menu', array($this, 'register_custom_menu_page'), 100);
			add_filter('pb_options', array($this, 'add_default_options'));
			add_filter('pb_settings', array($this, 'add_default_setings'));
			add_filter('pb_global_features', array($this, 'add_default_features'));
			add_filter('pb_performance_options', array($this, 'add_default_performance_options'));
			add_filter('pb_integrations_options', array($this, 'add_default_integrations_options'));
			add_action('admin_post_premium_gutenberg_rollback', array($this, 'post_premium_gutenberg_rollback_new'));
			add_action('wp_ajax_pb-mail-subscribe', array($this, 'subscribe_mail'));
			add_action('admin_enqueue_scripts', array($this, 'pb_admin_page_scripts'));
			add_filter('plugin_action_links_' . PREMIUM_BLOCKS_BASENAME, array($this, 'add_action_links'));
		}



		// Enqueue dashboard menu required assets
		// Enqueue icon for plugin in dashboard
		public function pb_admin_page_scripts()
		{
			wp_enqueue_style('pb-dashboard-css', PREMIUM_BLOCKS_URL . 'admin/assets/dashboard.css');
			wp_enqueue_script(
				'pb-dashboard-js', // Unique handle for the script
				PREMIUM_BLOCKS_URL . 'admin/assets/js/pb-dashboard.js', // Path to the JavaScript file
				array('jquery'), // Dependencies (e.g., jQuery)
				'1.0.0', // Version of the script
				true // Load the script in the footer
			);
			// // Pass data to the script (if necessary)
			// wp_localize_script('pb-dashboard-js', 'pbgData', array(
			// 	'ajaxUrl' => admin_url('admin-ajax.php')
			// ));

		}
		public function add_action_links($links)
		{
			$default_url = admin_url('page=' . $this->menu_slug);

			$mylinks = array(
				'<a href="' . $default_url . '">' . '</a>',
			);

			return array_merge($mylinks, $links);
		}
		public function subscribe_mail()
		{
			check_ajax_referer('pb-panel', 'nonce');

			$email = isset($_POST['email']) ? sanitize_text_field(wp_unslash($_POST['email'])) : '';

			$api_url = 'https://kemet.io/wp-json/mailchimp/v2/add';
			$request = add_query_arg(
				array(
					'email' => $email,
				),
				$api_url
			);

			$response = wp_remote_get(
				$request,
				array(
					'timeout'   => 60,
					'sslverify' => true,
				)
			);

			$body = wp_remote_retrieve_body($response);
			$body = json_decode($body, true);

			wp_send_json_success($body);
		}
		public function add_default_options($options)
		{
			$default_options = array(
				'accordion'        => true,
				'banner'           => true,
				'count-up'         => true,
				'dual-heading'     => true,
				'heading'          => true,
				'icon'             => true,
				'icon-box'         => true,
				'maps'             => true,
				'pricing-table'    => true,
				'section'          => true,
				'testimonials'     => true,
				'video-box'        => true,
				'fancy-text'       => true,
				'lottie'           => true,
				'Modal'            => true,
				'image-separator'  => true,
				'bullet-list'      => true,
				'person'           => true,
				'container'        => true,
				'content-switcher' => true,
				'buttons'          => true,
				'instagram-feed'   => true,
				'post-carousel'    => true,
				'post-grid'        => true,
				'image'            => true,
				'svg-draw'         => true,
				'form'             => true,
				'text'				     => true,
				'gallery'		       => true,
				"tabs"             => true,
				'off-canvas'       => true,
        		"one-page-scroll"   => true,
				'star-ratings'       => true,
				'templates'       => true,
			);

			return array_merge($default_options, $options);
		}

		public function add_default_setings($options)
		{
			$default_options = array(
				'premium-map-key'            => '',
				'premium-map-api'            => false,
        		'premium-load-map-api'       => true,
				'premium-fa-css'             => true,
				'premium-upload-json'        => false,
				'enable-post-editor-sidebar' => true,
				'enable-site-editor-sidebar' => false,
				'generate-assets-files'      => false,
				'premium-regenrate-assets'   => false,

			);

			return array_merge($default_options, $options);
		}

		/**
		 * Global Features Default Values.
		 *
		 * @param  array $options
		 * @return array
		 */
		public function add_default_features($options)
		{
			$default_options = array(
				'premium-equal-height'                  => true,
				'premium-entrance-animation'            => true,
				'premium-entrance-animation-all-blocks' => false,
				'premium-copy-paste-styles'             => true,
				'premium-display-conditions'            => true,
				'premium-templates-button'              => true
			);

			return array_merge($default_options, $options);
		}

		/**
		 * Global Performance Default Values.
		 *
		 * @param  array $options
		 * @return array
		 */
		public function add_default_performance_options($options)
		{
			$default_options = array(
				'premium-load-fonts-locally'   => false,
				'premium-preload-local-fonts'  => false,
				'premium-enable-allowed-fonts' => false,
				'premium-google-fonts'         => array(),
			);

			return array_merge($default_options, $options);
		}

		/**
		 * Global Integrations Default Values.
		 *
		 * @param  array $options
		 * @return array
		 */
		public function add_default_integrations_options($options)
		{
			$default_options = array(
				'premium-recaptcha-v2-site-key' => '',
				'premium-recaptcha-v2-secret'   => '',
				'premium-recaptcha-v3-site-key' => '',
				'premium-recaptcha-v3-secret'   => '',
				'premium-mailchimp-api-key'     => '',
				'premium-mailerlite-api-token'  => '',
			);

			return array_merge($default_options, $options);
		}

		/**
		 * update_option
		 *
		 * @return void
		 */
		public function update_option()
		{
			check_ajax_referer('pb-panel', 'nonce');

			if (!current_user_can('manage_options')) {
				wp_send_json_error(array('message' => 'Unauthorized'), 403);
			}

			$value   = isset($_POST['value']) ? json_decode(stripslashes($_POST['value']), true) : array();
			$options = apply_filters('pb_options', get_option('pb_options', array()));
			// $options = get_option( 'pb_options' );
			$options = ! is_array($options) ? array() : $options;

			if ($value) {
				$options = $value;
				update_option('pb_options', $options);

				wp_send_json_success(
					array(
						'success' => true,
						'values'  => $options,
					)
				);
			}

			wp_send_json_error();
		}

		/**
		 * Update Global Feature with ajax.
		 *
		 * @return void
		 */
		public function update_global_features()
		{
			check_ajax_referer('pb-panel', 'nonce');

			if (!current_user_can('manage_options')) {
				wp_send_json_error(array('message' => 'Unauthorized'), 403);
			}

			$value    = isset($_POST['value']) ? json_decode(stripslashes($_POST['value']), true) : array();
			$Settings = apply_filters('pb_settings', get_option('pbg_global_features', array()));
			$Settings = ! is_array($Settings) ? array() : $Settings;

			if ($value) {
				$Settings = $value;
				update_option('pbg_global_features', $Settings);

				wp_send_json_success(
					array(
						'success' => true,
						'setting' => $Settings,
					)
				);
			}

			wp_send_json_error();
		}

    public function update_settings()
    {
      check_ajax_referer('pb-panel', 'nonce');

      if (!current_user_can('manage_options')) {
        wp_send_json_error(array('message' => 'Unauthorized'), 403);
      }

      $value    = isset($_POST['value']) ? json_decode(stripslashes($_POST['value']), true) : array();
      $Settings = apply_filters('pb_settings', get_option('pbg_blocks_settings', array()));
      $Settings = ! is_array($Settings) ? array() : $Settings;

      if ($value) {
        $Settings = $value;
        update_option('pbg_blocks_settings', $Settings);

        // Clear all caches when "Regenerate Assets" button is clicked
        if (isset($Settings['premium-regenrate-assets']) && $Settings['premium-regenrate-assets'] === true) {
          $this->clear_all_caches();
        }

        wp_send_json_success(
          array(
            'success' => true,
            'setting' => $Settings,
          )
        );
      }

      wp_send_json_error();
    }

    /**
     * Clear all caches from popular caching plugins
     * Called when "Regenerate Assets" button is clicked
     *
     * @return void
     */
    protected function clear_all_caches()
    {
      error_log('🔥 PBG: Regenerate Assets - Starting cache clear');

      // Clear Breeze cache
      if (class_exists('Breeze_PurgeCache')) {
        error_log('🔥 PBG: Clearing Breeze cache');
        do_action('breeze_clear_all_cache');
        error_log('🔥 PBG: Clearing Breeze Varnish cache');
        do_action('breeze_clear_varnish');
      }

      // Clear WP Rocket cache
      if (function_exists('rocket_clean_domain')) {
        error_log('🔥 PBG: Clearing WP Rocket cache');
        rocket_clean_domain();
      }
      if (function_exists('rocket_clean_minify')) {
        error_log('🔥 PBG: Clearing WP Rocket minify cache');
        rocket_clean_minify();
      }

      // Clear W3 Total Cache
      if (function_exists('w3tc_flush_all')) {
        error_log('🔥 PBG: Clearing W3 Total Cache');
        w3tc_flush_all();
      }

      // Clear WP Super Cache
      if (function_exists('wp_cache_clear_cache')) {
        error_log('🔥 PBG: Clearing WP Super Cache');
        wp_cache_clear_cache();
      }

      // Clear LiteSpeed Cache
      if (class_exists('LiteSpeed_Cache_API') && method_exists('LiteSpeed_Cache_API', 'purge_all')) {
        error_log('🔥 PBG: Clearing LiteSpeed Cache');
        LiteSpeed_Cache_API::purge_all();
      }
      if (function_exists('litespeed_purge_all')) {
        error_log('🔥 PBG: Clearing LiteSpeed Cache');
        litespeed_purge_all();
      }

      // Clear WP Fastest Cache
      if (isset($GLOBALS['wp_fastest_cache']) && method_exists($GLOBALS['wp_fastest_cache'], 'deleteCache')) {
        error_log('🔥 PBG: Clearing WP Fastest Cache');
        $GLOBALS['wp_fastest_cache']->deleteCache(true);
      }
    }

    /**
     * Update Performance Options with ajax.
     *
     * @return void
     */
    public function update_performance_options()
		{
			check_ajax_referer('pb-panel', 'nonce');

			if (!current_user_can('manage_options')) {
				wp_send_json_error(array('message' => 'Unauthorized'), 403);
			}

			$value    = isset($_POST['value']) ? json_decode(stripslashes($_POST['value']), true) : array();
			$Settings = apply_filters('pb_performance_options', get_option('pbg_performance_options', array()));
			$Settings = ! is_array($Settings) ? array() : $Settings;

			if ($value) {
				$Settings = $value;
				update_option('pbg_performance_options', $Settings);

				wp_send_json_success(
					array(
						'success' => true,
						'setting' => $Settings,
					)
				);
			}

			wp_send_json_error();
		}

		/**
		 * Update integration options.
		 *
		 * @return void
		 */
		public function update_integrations_options()
		{
			check_ajax_referer('pb-panel', 'nonce');

			if (!current_user_can('manage_options')) {
				wp_send_json_error(array('message' => 'Unauthorized'), 403);
			}

			$value   = isset($_POST['value']) ? json_decode(stripslashes($_POST['value']), true) : array();
			$options = apply_filters('pb_integrations_options', get_option('pbg_integrations_options', array()));
			$options = ! is_array($options) ? array() : $options;

			if ($value) {
				$options = $value;
				update_option('pbg_integrations_options', $options);

				wp_send_json_success(
					array(
						'success' => true,
						'values'  => $options,
					)
				);
			}

			wp_send_json_error();
		}

		/**
		 * Add Premium Blocks panel menu
		 *
		 * @return void
		 */
		public function register_custom_menu_page()
		{
			$icon_url = "data:image/svg+xml;base64," . base64_encode( file_get_contents( PREMIUM_BLOCKS_PATH . 'admin/images/wordpress-dashboard-icon-pb.svg' ) );
			$page = add_menu_page(
				__('Premium Blocks', 'premium-blocks-for-gutenberg'),
				__('Premium Blocks', 'premium-blocks-for-gutenberg'),
				'manage_options',
				'pb_panel',
				array($this, 'render'),
				$icon_url
			);
			if (! defined('PREMIUM_ADMIN_PAGE')) {
				define('PREMIUM_ADMIN_PAGE', $page);
			}
			add_action('admin_print_styles-' . $page, array($this, 'enqueue_admin_script'));
		}

		/**
		 * Render panel html
		 *
		 * @return void
		 */
		public function render()
		{
?>
			<div id="pb-dashboard"></div>
<?php
		}

		/**
		 * Get system info
		 *
		 * @return array
		 */
		public static function get_system_info()
		{
			global $wpdb;
			$pb_versions = self::get_rollback_versions_options();

			$info = array(

				'home_url'             => home_url(),
				'version'              => get_bloginfo('version'),
				'multisite'            => is_multisite(),
				'memory_limit'         => wp_convert_hr_to_bytes(WP_MEMORY_LIMIT),
				'memory_limit_size'    => size_format(wp_convert_hr_to_bytes(WP_MEMORY_LIMIT)),
				'theme_version'        => esc_html(PREMIUM_BLOCKS_VERSION),
				'previous_version'     => esc_html(PREMIUM_BLOCKS_STABLE_VERSION),
				'wp_path'              => esc_html(ABSPATH),
				'debug'                => defined('WP_DEBUG') && WP_DEBUG,
				'lang'                 => esc_html(get_locale()),
				'server'               => isset($_SERVER['SERVER_SOFTWARE']) ? esc_html(sanitize_text_field(wp_unslash($_SERVER['SERVER_SOFTWARE']))) : '',
				'php_version'          => function_exists('phpversion') ? phpversion() : '',
				'mysql_version'        => $wpdb->db_version(),
				'max_upload'           => esc_html(size_format(wp_max_upload_size())),
				'ini_get'              => function_exists('ini_get'),
				'pb_previous_versions' => $pb_versions,
				'rollback_url_new'     => str_replace(array('&#038;', '&amp;'), '&', esc_url(add_query_arg('version', 'VERSION', wp_nonce_url(admin_url('admin-post.php?action=premium_gutenberg_rollback'), 'premium_gutenberg_rollback')))),
			);
			if (function_exists('ini_get')) {
				$php_memory_limit_bytes = wp_convert_hr_to_bytes(ini_get('memory_limit'));
				$info['php_memory_limit']      = esc_html(size_format($php_memory_limit_bytes));
				$info['php_memory_limit_bytes'] = $php_memory_limit_bytes;
				$info['post_max_size']         = esc_html(size_format(wp_convert_hr_to_bytes(ini_get('post_max_size'))));
				$info['max_execution_time']     = ini_get('max_execution_time');
				$info['max_input_vars']         = esc_html(ini_get('max_input_vars'));
				$info['suhosin']                = extension_loaded('suhosin');
			}

			$active_plugins = (array) get_option('active_plugins', array());
			$plugins        = array();
			if (is_multisite()) {
				$network_activated_plugins = array_keys(get_site_option('active_sitewide_plugins', array()));
				$active_plugins            = array_merge($active_plugins, $network_activated_plugins);
			}

			foreach ($active_plugins as $plugin) {
				$plugin_data    = get_plugin_data(WP_PLUGIN_DIR . '/' . $plugin);
				$dirname        = dirname($plugin);
				$version_string = '';
				$network_string = '';

				if (! empty($plugin_data['Name'])) {
					$plugins[$plugin] = array(
						'name'    => $plugin_data['Name'],
						'author'  => $plugin_data['Author'],
						'version' => $plugin_data['Version'],

					);

					if (! empty($plugin_data['PluginURI'])) {
						$plugins[$plugin]['PluginURI'] = $plugin_data['PluginURI'];
					}
				}
			}

			$info['active_plugins'] = $plugins;

			return $info;
		}
		/**
		 * panel_options
		 *
		 * @return array
		 */
		public static function panel_options()
		{
			$options = array(
				'accordion'        => array(
					'type'     => 'pb-button',
					'label'    => __('Accordion', 'premium-blocks-for-gutenberg'),
					'icon'     => 'accordion',
					'category' => array(
						'all',
						'content',
					),
				),
				'banner'           => array(
					'type'     => 'pb-button',
					'label'    => __('Banner', 'premium-blocks-for-gutenberg'),
					'icon'     => 'banner',
					'category' => array(
						'all',
						'marketing',
					),
				),
				'buttons'          => array(
					'type'     => 'pb-button',
					'label'    => __('Buttons', 'premium-blocks-for-gutenberg'),
					'icon'     => 'button',
					'category' => array(
						'all',
						'creative',
						'marketing',
						'content',
					),
				),
				'count-up'         => array(
					'type'     => 'pb-button',
					'label'    => __('Counter', 'premium-blocks-for-gutenberg'),
					'icon'     => 'count_up',
					'category' => array(
						'all',
						'creative',
					),
				),
				'dual-heading'     => array(
					'type'     => 'pb-button',
					'label'    => __('Dual Heading', 'premium-blocks-for-gutenberg'),
					'icon'     => 'dualHeading',
					'category' => array(
						'all',
						'creative',
					),
				),
				'heading'          => array(
					'type'     => 'pb-button',
					'label'    => __('Heading', 'premium-blocks-for-gutenberg'),
					'icon'     => 'heading',
					'category' => array(
						'all',
						'content',
					),
				),
				'icon'             => array(
					'type'     => 'pb-button',
					'label'    => __('Icon', 'premium-blocks-for-gutenberg'),
					'icon'     => 'icon',
					'category' => array(
						'all',
						'creative',
					),
				),
				'icon-box'         => array(
					'type'     => 'pb-button',
					'label'    => __('Icon Box', 'premium-blocks-for-gutenberg'),
					'icon'     => 'icon_box',
					'category' => array(
						'all',
						'creative',
					),
				),
				'maps'             => array(
					'type'     => 'pb-button',
					'label'    => __('Google Maps', 'premium-blocks-for-gutenberg'),
					'icon'     => 'maps',
					'category' => array(
						'all',
						'content',
					),
				),
				'pricing-table'    => array(
					'type'     => 'pb-button',
					'label'    => __('Pricing Table', 'premium-blocks-for-gutenberg'),
					'icon'     => 'pricingTable',
					'category' => array(
						'all',
						'marketing',
					),
				),
				'section'          => array(
					'type'     => 'pb-button',
					'label'    => __('Section', 'premium-blocks-for-gutenberg'),
					'icon'     => 'section',
					'category' => array(
						'all',
						'section',
					),
				),
				'testimonials'     => array(
					'type'     => 'pb-button',
					'label'    => __('Testimonials', 'premium-blocks-for-gutenberg'),
					'icon'     => 'testimonials',
					'category' => array(
						'all',
						'creative',
						'content',
					),
				),
				'video-box'        => array(
					'type'     => 'pb-button',
					'label'    => __('Video Box', 'premium-blocks-for-gutenberg'),
					'icon'     => 'video_box',
					'category' => array(
						'all',
						'creative',
						'marketing',
					),
				),
				'fancy-text'       => array(
					'type'     => 'pb-button',
					'label'    => __('Animated Text', 'premium-blocks-for-gutenberg'),
					'icon'     => 'fancyText',
					'category' => array(
						'all',
						'content',
						'creative',
					),
				),
				'lottie'           => array(
					'type'     => 'pb-button',
					'label'    => __('Lottie Animation', 'premium-blocks-for-gutenberg'),
					'icon'     => 'lottie',
					'category' => array(
						'all',
						'creative',
					),
				),
				'Modal'            => array(
					'type'     => 'pb-button',
					'label'    => __('Modal Box', 'premium-blocks-for-gutenberg'),
					'icon'     => 'modal',
					'category' => array(
						'all',
						'creative',
						'content',
					),
				),
				'image-separator'  => array(
					'type'     => 'pb-button',
					'label'    => __('Image Separator', 'premium-blocks-for-gutenberg'),
					'icon'     => 'image_separator',
					'category' =>
					array(
						'all',
						'content',
					),
				),
				'bullet-list'      => array(
					'type'     => 'pb-button',
					'label'    => __('Bullet List', 'premium-blocks-for-gutenberg'),
					'icon'     => 'bulletList',
					'category' =>
					array(
						'all',
						'content',
					),
				),
				'person'           => array(
					'type'     => 'pb-button',
					'label'    => __('Team Members', 'premium-blocks-for-gutenberg'),
					'icon'     => 'person',
					'category' => array(
						'all',
						'content',
					),
				),
				'container'        => array(
					'type'     => 'pb-button',
					'label'    => __('Container', 'premium-blocks-for-gutenberg'),
					'icon'     => 'container',
					'category' => array(
						'all',
						'content',
						'section',
					),
				),
				'content-switcher' => array(
					'type'     => 'pb-button',
					'label'    => __('Content Switcher', 'premium-blocks-for-gutenberg'),
					'icon'     => 'content_switcher',
					'category' => array(
						'all',
						'content',
						'creative',
					),
				),
				'instagram-feed'   => array(
					'type'     => 'pb-button',
					'label'    => __('Instagram Feed', 'premium-blocks-for-gutenberg'),
					'icon'     => 'instagram_feed_icon',
					'category' => array(
						'all',
						'creative',
						'marketing',
					),
				),
				'post-carousel'    => array(
					'type'     => 'pb-button',
					'label'    => __('Post Carousel', 'premium-blocks-for-gutenberg'),
					'icon'     => 'post_carousel',
					'category' => array(
						'all',
						'content',
					),
				),
				'post-grid'        => array(
					'type'     => 'pb-button',
					'label'    => __('Post Grid', 'premium-blocks-for-gutenberg'),
					'icon'     => 'post_grid',
					'category' => array(
						'all',
						'content',
					),
				),
				'image'            => array(
					'type'     => 'pb-button',
					'label'    => __('Image', 'premium-blocks-for-gutenberg'),
					'icon'     => 'image',
					'category' => array(
						'all',
						'content',
						'creative',

					),
				),
				'text'            => array(
					'type'     => 'pb-button',
					'label'    => __('Text', 'premium-blocks-for-gutenberg'),
					'icon'     => 'text',
					'category' => array(
						'all',
						'content',


					),
				),
				'svg-draw'         => array(
					'type'     => 'pb-button',
					'label'    => __('SVG Draw', 'premium-blocks-for-gutenberg'),
					'icon'     => 'svg_draw',
					'category' => array(
						'all',
						'creative',
					),
				),
				'form'             => array(
					'type'     => 'pb-button',
					'label'    => __('Form', 'premium-blocks-for-gutenberg'),
					'icon'     => 'form',
					'category' => array(
						'all',
						'content',
						'marketing',
					),
				),
				'gallery'          => array(
					'type'     => 'pb-button',
					'label'    => __('Gallery', 'premium-blocks-for-gutenberg'),
					'icon'     => 'gallery',
					'category' => array(
						'all',
						'creative',
					),
				),
				'tabs'             => array(
					'type'     => 'pb-button',
					'label'    => __('Tabs', 'premium-blocks-for-gutenberg'),
					'icon'     => 'tabs',
					'category' => array(
						'all',
						'creative',
					),
				),
				'off-canvas'       => array(
					'type'     => 'pb-button',
					'label'    => __('Off Canvas', 'premium-blocks-for-gutenberg'),
					'icon'     => 'off_canvas',
					'category' => array(
						'all',
						'content',
					),
				),
        'one-page-scroll'    => array(
					'type'     => 'pb-button',
					'label'    => __( 'One Page Scroll', 'premium-blocks-for-gutenberg' ),
					'icon'     => 'one_page_scroll',
					'category' => array(
						'all',
						'content',
					),
				),
				'star-ratings'       => array(
					'type'     => 'pb-button',
					'label'    => __('Star Ratings', 'premium-blocks-for-gutenberg'),
					'icon'     => 'starRatings',
					'category' => array(
						'all',
						'creative',
					),
				),
				'templates'       => array(
					'type'     => 'pb-button',
					'label'    => __('Templates', 'premium-blocks-for-gutenberg'),
					'icon'     => 'templates',
					'category' => array(
						'all',
						'content',
					),
				),
			);
			return apply_filters('pb_panel_options', $options);
		}

		/**
		 * Enqueue a script in the WordPress admin on edit.php
		 *
		 * @return void
		 */
		public function enqueue_admin_script()
		{
			wp_enqueue_style('pb-panel-css', PREMIUM_BLOCKS_PANEL_URL . 'assets/js/build/index.css', false, PREMIUM_BLOCKS_VERSION);
			wp_enqueue_script(
				'pb-panel-js',
				PREMIUM_BLOCKS_PANEL_URL . 'assets/js/build/index.js',
				array(
					'wp-i18n',
					'wp-components',
					'wp-element',
					'wp-media-utils',
					'wp-block-editor',
					'wp-data',
					'wp-core-data',
				),
				PREMIUM_BLOCKS_VERSION,
				true
			);

			wp_localize_script(
				'pb-panel-js',
				'PremiumBlocksPanelData',
				array(
					'home_slug'          => $this->menu_slug,
					'options'            => self::panel_options(),
					'values'             => apply_filters('pb_options', get_option('pb_options', array())),
					'ajaxurl'            => admin_url('admin-ajax.php'),
					'nonce'              => wp_create_nonce('pb-panel'),
					'system_info'        => self::get_system_info(),
					'images_url'         => PREMIUM_BLOCKS_PANEL_URL . 'assets/images/',
					'logo_url'           => PREMIUM_BLOCKS_URL . 'admin/images/',
					'apiData'            => apply_filters('pb_settings', get_option('pbg_blocks_settings', array())),
					'isBlockTheme'       => wp_is_block_theme(),
					'globalFeatures'     => apply_filters('pb_global_features', get_option('pbg_global_features', array())),
					'performanceOptions' => apply_filters('pb_performance_options', get_option('pbg_performance_options', array())),
					'integrations'       => apply_filters('pb_integrations_options', get_option('pbg_integrations_options', array())),
				)
			);
		}
		public function post_premium_gutenberg_rollback_new()
		{
			check_admin_referer('premium_gutenberg_rollback');
			$plugin_slug    = basename(PREMIUM_BLOCKS_FILE, '.php');
			$update_version = sanitize_text_field($_GET['version']);

			$pbg_rollback = new PBG_Rollback(
				array(
					'version'     => $update_version,
					'plugin_name' => PREMIUM_BLOCKS_BASENAME,
					'plugin_slug' => $plugin_slug,
					'package_url' => sprintf('https://downloads.wordpress.org/plugin/%s.%s.zip', $plugin_slug, $update_version),
				)
			);

			$pbg_rollback->run();

			wp_die(
				'',
				__('Rollback to Previous Version', 'premium-gutenberg'),
				array(
					'response' => 200,
				)
			);
		}

		/**
		 * Get Rollback versions.
		 *
		 * @since 1.23.0
		 * @return array
		 * @access public
		 */
		public static function get_rollback_versions()
		{
			$rollback_versions = get_transient('pb_rollback_versions_' . PREMIUM_BLOCKS_VERSION);

			if (empty($rollback_versions)) {

				$max_versions = 10;

				require_once ABSPATH . 'wp-admin/includes/plugin-install.php';

				$plugin_information = plugins_api(
					'plugin_information',
					array(
						'slug' => 'premium-blocks-for-gutenberg',
					)
				);

				if (empty($plugin_information->versions) || ! is_array($plugin_information->versions)) {
					return array();
				}

				uksort($plugin_information->versions, 'version_compare');
				$plugin_information->versions = array_reverse($plugin_information->versions, true);

				$rollback_versions = array();

				foreach ($plugin_information->versions as $version => $download_link) {

					$lowercase_version = strtolower($version);

					$is_valid_rollback_version = ! preg_match('/(trunk|beta|rc|dev)/i', $lowercase_version);

					if (! $is_valid_rollback_version) {
						continue;
					}

					$rollback_versions[] = $version;
				}

				$rollback_versions = array_slice($rollback_versions, 0, $max_versions, true);

				set_transient('pb_rollback_versions_' . PREMIUM_BLOCKS_VERSION, $rollback_versions, WEEK_IN_SECONDS);
			}

			return $rollback_versions;
		}

		public static function get_rollback_versions_options()
		{
			$rollback_versions = self::get_rollback_versions();

			$rollback_versions_options = array();

			foreach ($rollback_versions as $version) {

				$version = array(
					'label' => $version,
					'value' => $version,

				);

				$rollback_versions_options[] = $version;
			}

			return $rollback_versions_options;
		}
	}
	Pb_Panel::get_instance();
}