<?php
namespace Actirise\Includes;

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

use Actirise\Includes\Loader;
use Actirise\Includes\Cron;
use Actirise\Includes\AbstractCore;
use Actirise\Includes\CacheRules;
use Actirise\Includes\Options;
use Actirise\Includes\ShortCode;
use ActiriseAdmin\Includes\Core as AdminCore;
use ActiriseAdmin\Includes\AdminBar;
use ActirisePublic\Includes\Core as PublicCore;
use ActirisePublic\Includes\NoPub;

/**
 * The core plugin class.
 *
 * This is used to define internationalization, admin-specific hooks, and
 * public-facing site hooks.
 *
 * Also maintains the unique identifier of this plugin as well as the current
 * version of the plugin.
 *
 * @link       https://actirise.com
 * @since      2.0.0
 * @package    actirise
 * @subpackage actirise/includes
 * @author     actirise <wordpress@actirise.com>
 */
final class Core extends AbstractCore {
	/**
	 * Plugin Cron instance.
	 *
	 * @since 3.2.0
	 * @access private
	 * @var Cron
	 */
	private $plugin_cron;

	/**
	 * NoPub instance
	 *
	 * @since    3.2.0
	 * @access   private
	 * @var      NoPub    $no_pub    NoPub instance
	 */
	private $no_pub;

	/**
	 * Define the core functionality of the plugin.
	 *
	 * Set the plugin name and the plugin version that can be used throughout the plugin.
	 * Load the dependencies, define the locale, and set the hooks for the admin area and
	 * the public-facing side of the site.
	 *
	 * @since    2.0.0
	 */
	public function __construct() {
		if ( defined( 'ACTIRISE_VERSION' ) ) {
			$this->version = ACTIRISE_VERSION;
		} else {
			$this->version = '2.0.0';
		}

		$this->plugin_name   = 'actirise';
		$this->plugin_prefix = 'actirise_';

		$this->loader = new Loader();

		Options::init();

		$this->plugin_cron = new Cron();
		$this->no_pub      = new NoPub( $this->plugin_name );

		if ( is_admin() ) {
			$this->define_admin_hooks();
		} else {
			$this->define_public_hooks();
		}

		if ( ACTIRISE_CRON === 'true' ) {
			$this->define_cron_hooks();
		}

		$this->define_cache_rules();
		$this->add_admin_bar();
		$this->define_shortcode();
	}

	/**
	 * Register all of the hooks related to the admin area functionality
	 * of the plugin.
	 *
	 * @since    2.0.0
	 * @access   private
	 * @return   void
	 */
	private function define_admin_hooks() {
		$plugin_admin = new AdminCore( $this->get_plugin_name(), $this->get_plugin_prefix(), $this->get_version(), $this->get_loader(), $this->no_pub );

		if ( ( isset( $_GET['page'] ) && $_GET['page'] === 'actirise-settings' ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
			$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts', 10, 1 );
			$plugin_admin->check_website_status();
		}

		$this->loader->add_filter( 'plugin_action_links_' . ACTIRISE_BASENAME, $plugin_admin, 'add_action_links' );

		if ( version_compare( get_bloginfo( 'version' ), '5.2', '>=' ) ) {
			$plugin_admin->site_health();
		}

		$this->loader->add_action( 'admin_menu', $plugin_admin, 'add_plugin_admin_menu' );
		$this->loader->add_action( 'admin_init', $plugin_admin, 'migrations' );
		$this->loader->add_action( 'admin_init', $plugin_admin, 'set_locale' );
		$this->loader->add_action( 'admin_notices', $plugin_admin, 'add_billing_notice' );
	}

	/**
	 * Register all of the hooks related to the public-facing functionality
	 * of the plugin.
	 *
	 * @since    2.0.0
	 * @access   private
	 * @return   void
	 */
	private function define_public_hooks() {
		$plugin_public = new PublicCore( $this->get_plugin_name(), $this->get_plugin_prefix(), $this->get_version(), $this->no_pub );

		$this->loader->add_action( 'init', $plugin_public, 'init' );
	}

	/**
	 * Register all of the hooks related to the cron functionality
	 * of the plugin.
	 *
	 * @since    2.0.0
	 * @access   private
	 * @return   void
	 */
	private function define_cron_hooks() {
		// Add cron jobs
		$this->loader->add_action( 'actirise_cron_update_adstxt', $this->plugin_cron, 'update_adstxt' );
		$this->loader->add_action( 'actirise_cron_update_presized_div', $this->plugin_cron, 'check_presized_div' );
		$this->loader->add_action( 'actirise_cron_update_fast_cmp', $this->plugin_cron, 'get_fast_cmp' );
		$this->loader->add_action( 'actirise_cron_check_adstxt', $this->plugin_cron, 'check_adstxt' );
		$this->loader->add_action( 'actirise_cron_check_adstxt_followup', $this->plugin_cron, 'check_adstxt_followup' );
		$this->loader->add_action( 'actirise_cron_check_slots', $this->plugin_cron, 'check_slots' );
		$this->loader->add_action( 'actirise_cron_event_annotation', $this->plugin_cron, 'upload_event_annotation' );
		$this->loader->add_action( 'actirise_cron_check_optimization', $this->plugin_cron, 'check_optimization' );

		// Hook for XPath validation after slots update
		$this->loader->add_action( 'actirise_validate_xpath_after_update', $this->plugin_cron, 'check_slots' );

		$this->plugin_cron->schedule();
	}

	/**
	 * Register all of the hooks related to the cache rules functionality
	 * of the plugin.
	 *
	 * @since    2.0.0
	 * @access   private
	 * @return   void
	 */
	private function define_cache_rules() {
		$plugin_cache_rules = new CacheRules();

		$this->loader->add_action( 'init', $plugin_cache_rules, 'init' );
	}

	/**
	 * Register admin bar menu.
	 *
	 * @since    2.6.8
	 * @access   private
	 * @return   void
	 */
	private function add_admin_bar() {
		$adminBar = new AdminBar( $this->get_loader() );
		$adminBar->register_hooks();
	}

	/**
	 * Register shortcode management
	 *
	 * @since 3.2.0
	 * @access private
	 * @return void
	 */
	private function define_shortcode() {
		$shortcode = new ShortCode();
		$shortcode->init();
	}



	/**
	 * Run the loader to execute all of the hooks with WordPress.
	 *
	 * @since    2.0.0
	 * @return   void
	 */
	public function run() {
		$this->loader->run();
	}
}
