<?php
/**
 * Call the init method when plugin is activated.
 *
 * @package tripzzy
 */

namespace Tripzzy\Core\Activation;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
use Tripzzy\Core\Bases\PostTypeBase;
use Tripzzy\Core\Bases\TaxonomyBase;
use Tripzzy\Core\PostTypes;
use Tripzzy\Core\PostMeta;
use Tripzzy\Core\Taxonomies;
use Tripzzy\Core\Payment;
use Tripzzy\Core\Installer;
use Tripzzy\Core\Helpers\MetaHelpers;
use Tripzzy\Core\Helpers\User;
use Tripzzy\Core\Helpers\Cron;

/**
 * Activator class.
 */
class Activator {

	/**
	 * The "Late Static Binding" class name
	 *
	 * @since 1.2.7
	 * @var string
	 */
	private static $called_class = '';

	/**
	 * Init Activator Hook.
	 *
	 * @note While resetting settings, do not reset some settings value assigned from thixs activation hook. like Tripzzy pages, Form fields etc.
	 *
	 * @since 1.3.4 Moved all the Create User Role, Cronjob, Seeder, Migration and other updates are moved to Installer::init();
	 * @since 1.2.0 Added PostTypes::instance, PostMeta::instance, Taxonomies::instance, Payment::instance.
	 * @since 1.0.0
	 */
	public static function init() {
		self::$called_class = get_called_class();

		PostTypes::instance(); // Initialize post type.
		PostMeta::instance();
		Taxonomies::instance();
		Payment::instance();
		PostTypeBase::init(); // Just register post type before creating custom post. So it will not throw any warning related to post type not registered.
		TaxonomyBase::init();
		Installer::init();
		do_action( 'tripzzy_after_activation', self::$called_class );
	}

	/**
	 * Update DB Version.
	 *
	 * @deprecated 1.3.4 Use Installer::update_db_version();
	 * @since 1.0.0
	 */
	public static function update_db_version() {
		$db_version = MetaHelpers::get_option( 'version' );
		MetaHelpers::update_option( 'initial_activation', 'false' );
		if ( TRIPZZY_VERSION !== $db_version ) {
			if ( empty( $db_version ) ) {
				MetaHelpers::update_option( 'used_since', TRIPZZY_VERSION );
			}
			MetaHelpers::update_option( 'version', TRIPZZY_VERSION );
		}
	}

	/**
	 * Initialize Seeder.
	 *
	 * @deprecated 1.3.4 Use Installer::seeder_init();
	 * @since 1.0.0
	 */
	public static function seeder_init() {
		foreach ( glob( sprintf( '%1$sinc/Core/Seeder/*.php', TRIPZZY_ABSPATH ) ) as $filename ) {
			$namespace  = 'Tripzzy\Core\Seeder';
			$class_name = basename( $filename, '.php' );
			if ( class_exists( $namespace . '\\' . $class_name ) && method_exists( $namespace . '\\' . $class_name, 'init' ) ) {
				call_user_func( $namespace . '\\' . $class_name . '::init' );
			}
		}
	}

	/**
	 * Initialize Migrations.
	 *
	 * @deprecated 1.3.4 Use Installer::migrations_init();
	 * @since 1.0.0
	 */
	public static function migrations_init() {
		require_once ABSPATH . 'wp-admin/includes/upgrade.php';
		foreach ( glob( sprintf( '%1$sinc/Core/Migrations/*.php', TRIPZZY_ABSPATH ) ) as $filename ) {
			$namespace  = 'Tripzzy\Core\Migrations';
			$class_name = basename( $filename, '.php' );
			if ( class_exists( $namespace . '\\' . $class_name ) && method_exists( $namespace . '\\' . $class_name, 'init' ) ) {
				call_user_func( $namespace . '\\' . $class_name . '::init' );
			}
		}
	}

	/**
	 * Data Upgrade.
	 *
	 * @deprecated 1.3.4 Use Installer::data_upgrade_init();
	 * @since 1.1.3
	 */
	public static function data_upgrade_init() {
		foreach ( glob( sprintf( '%1$sinc/Core/DataUpgrade/*.php', TRIPZZY_ABSPATH ) ) as $filename ) {
			$namespace  = 'Tripzzy\Core\DataUpgrade';
			$class_name = basename( $filename, '.php' );
			if ( class_exists( $namespace . '\\' . $class_name ) && method_exists( $namespace . '\\' . $class_name, 'init' ) ) {
				call_user_func( $namespace . '\\' . $class_name . '::init' );
			}
		}
	}
}
