<?php
/**
 * Plugin Name:       SimpleStats – Friendly Private Website Analytics
 * Description:       Powerful analytics solution. WordPress Connector plugin for SimpleStats.
 * Version:           1.0.0
 * Author:            zkwarta
 * Requires at least: 5.6
 * Requires PHP:      7.2
 * License:           GPL v3
 * License URI:       https://www.gnu.org/licenses/gpl-3.0.html
 * Text Domain:       simplestats-friendly-website-analytics
 * Domain Path:       /languages
 *
 * @package SimpleStats
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

class SimpleStats {

	/**
	 * Hold the instance.
	 *
	 * @var self
	 */
	private static $instance;

	/**
	 * Set plugin version to check any conditional script.
	 *
	 * @var string
	 */
	public $version = '1.0.0';

	/**
	 * Set instance of assets class.
	 *
	 * @var SimpleStats_Admin_Assets
	 */
	public $admin_assets;

	/**
	 * Get instance of class.
	 *
	 * @return self
	 */
	public static function get_instance() {
		if ( ! self::$instance ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * Constructor function of the class.
	 */
	private function __construct() {
		$this->define_constants();
		$this->require_files();
		$this->hooks();
	}

	/**
	 * Register necessary constants.
	 *
	 * @return void
	 */
	private function define_constants() {
		if ( ! defined( 'SIMPLESTATS_PLUGIN_VERSION' ) ) {
			define( 'SIMPLESTATS_PLUGIN_VERSION', $this->version );
		}

		if ( ! defined( 'SIMPLESTATS_PLUGIN_URL' ) ) {
			define( 'SIMPLESTATS_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
		}

		if ( ! defined( 'SIMPLESTATS_PLUGIN_PATH' ) ) {
			define( 'SIMPLESTATS_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
		}
	}

	/**
	 * Includes necessary files/classes.
	 *
	 * @return void
	 */
	private function require_files() {
		require_once SIMPLESTATS_PLUGIN_PATH . 'includes/helpers.php';
		require_once SIMPLESTATS_PLUGIN_PATH . 'includes/notifications/notifications-loader.php';
		require_once SIMPLESTATS_PLUGIN_PATH . 'includes/admin-menu.php';
		require_once SIMPLESTATS_PLUGIN_PATH . 'includes/admin-assets.php';
		require_once SIMPLESTATS_PLUGIN_PATH . 'includes/admin-ajax.php';
		require_once SIMPLESTATS_PLUGIN_PATH . 'includes/tracking.php';
		require_once SIMPLESTATS_PLUGIN_PATH . 'includes/report.php';
		require_once SIMPLESTATS_PLUGIN_PATH . 'includes/admin-bar.php';
		require_once SIMPLESTATS_PLUGIN_PATH . 'includes/dashboard-widget.php';

		$this->admin_assets = SimpleStats_Admin_Assets::get_instance();

		SimpleStats_Admin_Menu::get_instance();
		SimpleStats_Admin_Ajax::get_instance();
		SimpleStats_Tracking::get_instance();
		SimpleStats_Admin_Bar::get_instance();
		SimpleStats_Dashboard_Widget::get_instance();
	}

	/**
	 * Run necessary hooks.
	 *
	 * @return void
	 */
	private function hooks() {
		register_activation_hook( __FILE__, array( $this, 'set_activation_redirect' ) );

		add_action( 'admin_init', array( $this, 'activation_redirect' ) );
	}

	/**
	 * Set activation redirect transient + seed installation id on plugin activation.
	 */
	public function set_activation_redirect() {
		simplestats_get_installation_id();
		set_transient( 'simplestats_activation_redirect', true, 60 );
	}

	/**
	 * Redirect to the auth page once, right after activation, if not connected.
	 *
	 * `admin_init` also fires on admin-ajax.php and during bulk plugin
	 * activation, so both are excluded: an AJAX request landing inside the
	 * 60-second window would otherwise burn the transient and receive a redirect
	 * body instead of its JSON, and bulk-activating plugins would dump the user
	 * on our screen instead of back on plugins.php.
	 */
	public function activation_redirect() {
		if ( wp_doing_ajax() || wp_doing_cron() || is_network_admin() ) {
			return;
		}

		// Bulk activation — never hijack it.
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		if ( isset( $_GET['activate-multi'] ) ) {
			delete_transient( 'simplestats_activation_redirect' );
			return;
		}

		if ( ! get_transient( 'simplestats_activation_redirect' ) ) {
			return;
		}

		delete_transient( 'simplestats_activation_redirect' );

		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		if ( ! simplestats_get_connection() ) {
			wp_safe_redirect( admin_url( 'admin.php?page=simplestats_authentication#/sign-up' ) );
			exit;
		}
	}

}

/**
 * Initialize the plugin.
 *
 * @return SimpleStats
 */
function simplestats() {
	return SimpleStats::get_instance();
}

simplestats();
