<?php
/**
 * Plugin Name: Dokan Vendor Dashboard (Deprecated)
 * Plugin URI: https://wordpress.org/plugins/dokan-vendor-dashboard/
 * Description: DEPRECATED — This plugin is no longer maintained and will not receive further updates. An extension for vendor dashboard of Dokan
 * Version: 1.0.4
 * Author: weDevs
 * Author URI: https://wedevs.com/
 * Requires PHP: 7.4
 * WC requires at least: 8.0.0
 * WC tested up to: 9.3.3
 * License: GPL2
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: dokan-vendor-dashboard
 * Domain Path: /languages
 */

/*
 * Copyright (c) 2019 weDevs (email: info@wedevs.com). All rights reserved.
 *
 * Released under the GPL license
 * http://www.opensource.org/licenses/gpl-license.php
 *
 * This is an add-on for WordPress
 * http://wordpress.org/
 *
 * **********************************************************************
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * **********************************************************************
 */

// don't call the file directly
use WeDevs\DokanVendorDashboard\DashboardSwitcher;
use WeDevs\DokanVendorDashboard\Settings;

defined( 'ABSPATH' ) || exit;

/**
 * Vendor_Dashboard class
 *
 * @class Dokan_Vendor_Dashboard The class that holds the entire Vendor_Dashboard plugin
 *
 * @property WeDevs\DokanVendorDashboard\Assets $assets Assets class
 * @property WeDevs\DokanVendorDashboard\Menu $menu Menu class
 * @property WeDevs\DokanVendorDashboard\REST\DashboardSwitcher $dashboard_switcher_api
 * @property WeDevs\DokanVendorDashboard\REST\DashboardSettings $dashboard_settings_api
 *
 * @since      1.0.0
 * @deprecated This plugin is deprecated and no longer maintained.
 */
final class Dokan_Vendor_Dashboard {

	/**
	 * Plugin version
	 *
	 * @since 1.0.0
	 *
	 * @var string
	 */
	public $version = '1.0.4';

	/**
	 * Holds the dependency data.
	 *
	 * @since 1.0.0
	 *
	 * @var \WeDevs\DokanVendorDashboard\Dependency
	 */
	public $dependency;

	/**
	 * Instance of self.
	 *
	 * @since 1.0.0
	 *
	 * @var self
	 */
	private static $instance = null;

	/**
	 * Holds various class instances
	 *
	 * @since 1.0.0
	 *
	 * @var array
	 */
	private $container = array();

	/**
	 * Class constructor.
	 *
	 * @since 1.0.0
	 */
	public function __construct() {
		require_once __DIR__ . '/vendor/autoload.php';

		$this->define_constants();

		add_action( 'admin_notices', [ $this, 'deprecation_notice' ] );
		add_action( 'wp_ajax_dokan_vd_dismiss_deprecation_notice', [ $this, 'dismiss_deprecation_notice' ] );

		// Instanciate the dependency resolver.
		$this->dependency = new \WeDevs\DokanVendorDashboard\Dependency();

		// Initiate the plugin for localization.
		add_action( 'init', [ $this, 'setup_localization' ] );
		// Load this plugin after dokan is loaded successfully.
		add_action( 'dokan_loaded', [ $this, 'init_plugin' ] );

		register_activation_hook( __FILE__, [ $this, 'activate' ] );
	}

	/**
	 * Magic getter to bypass referencing plugin.
	 *
	 * @since 1.0.0
	 *
	 * @param $prop
	 *
	 * @return mixed
	 */
	public function __get( $prop ) {
		if ( array_key_exists( $prop, $this->container ) ) {
			return $this->container[ $prop ];
		}

		return $this->{$prop};
	}

	/**
	 * Magic isset to bypass referencing plugin.
	 *
	 * @since 1.0.0
	 *
	 * @param $prop
	 *
	 * @return mixed
	 */
	public function __isset( $prop ) {
		return isset( $this->{$prop} ) || isset( $this->container[ $prop ] );
	}

	/**
	 * Initializes the Dokan_Vendor_Dashboard() class.
	 *
	 * Checks for an existing Dokan_Vendor_Dashboard() instance
	 * and if it doesn't find one, creates it.
	 *
	 * @since 1.0.0
	 */
	public static function init() {
		if ( self::$instance === null ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * Define all vendor-dashboard plugin constant.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public function define_constants() {
		define( 'DOKAN_VENDOR_DASHBOARD_PLUGIN_VERSION', $this->version );
		define( 'DOKAN_VENDOR_DASHBOARD_FILE', __FILE__ );
		define( 'DOKAN_VENDOR_DASHBOARD_DIR', dirname( DOKAN_VENDOR_DASHBOARD_FILE ) );
		define( 'DOKAN_VENDOR_DASHBOARD_URL', plugins_url( '', DOKAN_VENDOR_DASHBOARD_FILE ) );
		define( 'DOKAN_VENDOR_DASHBOARD_TEMPLATE_PATH', DOKAN_VENDOR_DASHBOARD_DIR . '/templates/' );
		define( 'DOKAN_VENDOR_DASHBOARD_ASSETS', DOKAN_VENDOR_DASHBOARD_URL . '/assets' );
		define( 'DOKAN_VENDOR_DASHBOARD_BUILD', DOKAN_VENDOR_DASHBOARD_URL . '/build' );
	}

	/**
	 * Initiates the plugin.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public function init_plugin() {
		// Check dokan minimum required version or return.
		if ( ! $this->dependency->verify_dependencies() ) {
			return;
		}

        // Initiate Appsero tracker.
        $this->init_appsero_tracker();

        $this->init_classes_despite_seller_preference();
        $this->init_classes_if_seller_preferred();
	}

	/**
	 * Load resources for all vendors if dashboard enabled by admin.
	 *
	 * @since 1.0.2
	 *
	 * @return void
	 */
	public function init_classes_despite_seller_preference() {
		$this->container['admin_settings']         = new Settings();
		$this->container['dashboard_switcher']     = new DashboardSwitcher();
		$this->container['dashboard_switcher_api'] = new \WeDevs\DokanVendorDashboard\REST\DashboardSwitcher();
		$this->container['dashboard_settings_api'] = new \WeDevs\DokanVendorDashboard\REST\DashboardSettings();
	}

	/**
	 * Load resources for the vendors who preferred to use new dashboard.
	 *
	 * @since 1.0.2
	 * @return void
	 */
	public function init_classes_if_seller_preferred() {
		// Check if new dashboard ui is on from dokan settings.
		if ( ! Settings::should_load_vendor_dashboard_ui() ) {
			return;
		}

		$this->include_classes();
	}

	/**
	 * Include classes.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public function include_classes() {
		$this->container['assets']         = new \WeDevs\DokanVendorDashboard\Assets();
		$this->container['menu']           = new \WeDevs\DokanVendorDashboard\Menu();
		$this->container['page_templates'] = new \WeDevs\DokanVendorDashboard\PageTemplates();
		$this->container['hooks']          = new \WeDevs\DokanVendorDashboard\Hooks();
	}

	/**
	 * Initialize plugin for localization.
	 *
	 * @uses load_plugin_textdomain()
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public function setup_localization() {
		load_plugin_textdomain( 'dokan-vendor-dashboard', false, dirname( plugin_basename( DOKAN_VENDOR_DASHBOARD_FILE ) ) . '/languages/' );
	}

	/**
	 * Callback for activation hook.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public function activate() {
		$installer = new \WeDevs\DokanVendorDashboard\Installer();
		$installer->run();
	}

	/**
	 * Displays a dismissible deprecation admin notice with migration steps.
	 *
	 * @since      1.0.4
	 * @deprecated This plugin is deprecated and no longer maintained.
	 *
	 * @return void
	 */
	public function deprecation_notice() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		if ( get_user_meta( get_current_user_id(), 'dokan_vd_deprecation_notice_dismissed', true ) ) {
			return;
		}

		$nonce           = wp_create_nonce( 'dokan_vd_dismiss_deprecation_notice' );
		$is_dokan_active = function_exists( 'dokan' );
		$notice_title    = esc_html__( 'Dokan Vendor Dashboard is deprecated.', 'dokan-vendor-dashboard' );

		if ( $is_dokan_active ) {
			$notice_body = wp_kses(
				sprintf(
					/* translators: %s: navigation path inside the Dokan plugin. */
					__( 'This plugin is no longer maintained. The new and improved Vendor Dashboard is now built directly into the Dokan plugin. To access it, go to: %s.', 'dokan-vendor-dashboard' ),
					'<strong>Dokan → Settings → Appearance → Vendor Dashboard Style → ' . esc_html__( 'select "New UI"', 'dokan-vendor-dashboard' ) . '</strong>'
				),
				[ 'strong' => [] ]
			);
		} else {
			$notice_body = esc_html__( 'This plugin is no longer maintained. Please install the Dokan plugin to access the new Vendor Dashboard experience.', 'dokan-vendor-dashboard' );
		}
		?>
		<div class="notice notice-warning is-dismissible dokan-vd-deprecation-notice" data-nonce="<?php echo esc_attr( $nonce ); ?>">
			<p>
                <strong><?php echo $notice_title; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Pre-escaped above. ?></strong>
                <?php echo $notice_body; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Pre-escaped above. ?>
            </p>
		</div>
		<script>
			( function() {
				document.addEventListener( 'click', function( event ) {
					const notice = event.target.closest( '.dokan-vd-deprecation-notice' );
					if ( ! notice || ! event.target.classList.contains( 'notice-dismiss' ) ) {
						return;
					}
					const data = new FormData();
					data.append( 'action', 'dokan_vd_dismiss_deprecation_notice' );
					data.append( 'nonce', notice.dataset.nonce );
					window.fetch( ajaxurl, { method: 'POST', credentials: 'same-origin', body: data } );
				} );
			} )();
		</script>
		<?php
	}

	/**
	 * AJAX handler that persists the deprecation notice dismissal per user.
	 *
	 * @since 1.0.4
	 *
	 * @return void
	 */
	public function dismiss_deprecation_notice() {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( null, 403 );
		}

		check_ajax_referer( 'dokan_vd_dismiss_deprecation_notice', 'nonce' );

		update_user_meta( get_current_user_id(), 'dokan_vd_deprecation_notice_dismissed', 1 );

		wp_send_json_success();
	}

	/**
	 * Initiates Appsero services.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public function init_appsero_tracker() {
		if ( ! class_exists( '\Appsero\Client' ) ) {
            return;
        }

        $client   = new \Appsero\Client( 'Appsero key for vendor dashboard plugin', 'Dokan Vendor Dashboard', DOKAN_VENDOR_DASHBOARD_FILE );
        $insights = $client->insights();

		$insights->add_extra(
            function() {
                return array(
                    'dokan_vendor_dashboard_version' => DOKAN_VENDOR_DASHBOARD_PLUGIN_VERSION,
				);
            }
        );

        $insights->init();
	}
}

/**
* Load Vendor Dashboard plugin.
*
* @since 1.0.0
*
* @return Dokan_Vendor_Dashboard
*/
function dokan_vendor_dashboard() { // phpcs:ignore
	return Dokan_Vendor_Dashboard::init();
}

// Lets Go....
dokan_vendor_dashboard();
