<?php
/**
 * Fired during plugin updates
 *
 * @link       https://wpsmartpost.com/
 * @since      2.0.2
 *
 * @package    Smart_Post_Show_Pro
 * @subpackage Smart_Post_Show_Pro/includes
 */

// don't call the file directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Fired during plugin updates.
 *
 * This class defines all code necessary to run during the plugin's updates.
 */
class Smart_Post_Show_Pro_Updates {

	/**
	 * DB updates that need to be run
	 *
	 * @var array
	 */
	private static $updates = array(
		'2.0.2' => 'updates/update-2.0.2.php',
		'2.1.3' => 'updates/update-2.1.3.php',
		'2.3.2' => 'updates/update-2.3.2.php',
		'2.4.4' => 'updates/update-2.4.4.php',
		'2.5.3' => 'updates/update-2.5.3.php',
		'3.0.0' => 'updates/update-3.0.0.php',
		'3.0.2' => 'updates/update-3.0.2.php',
		'3.2.0' => 'updates/update-3.2.0.php',
		'3.3.0' => 'updates/update-3.3.0.php',
		'3.3.2' => 'updates/update-3.3.2.php',
		'4.0.1' => 'updates/update-4.0.1.php',
	);

	/**
	 * Binding all events
	 *
	 * @since 2.0.2
	 *
	 * @return void
	 */
	public function __construct() {
		add_action( 'plugins_loaded', array( $this, 'do_updates' ) );
	}

	/**
	 * Check if need any update
	 *
	 * @since 2.0.2
	 *
	 * @return boolean
	 */
	public function is_needs_update() {
		$installed_version = get_option( 'smart_post_show_pro_version' );
		$first_version     = get_option( 'smart_post_show_pro_first_version' );
		$activation_date   = get_option( 'smart_post_show_pro_activation_date' );

		if ( false === $installed_version ) {
			update_option( 'smart_post_show_pro_version', SMART_POST_SHOW_PRO_VERSION );
			update_option( 'smart_post_show_pro_db_version', SMART_POST_SHOW_PRO_VERSION );
		}
		if ( false === $first_version ) {
			update_option( 'smart_post_show_pro_first_version', SMART_POST_SHOW_PRO_VERSION );
		}
		if ( false === $activation_date ) {
			update_option( 'smart_post_show_pro_activation_date', current_time( 'timestamp' ) );
		}

		if ( version_compare( $installed_version, SMART_POST_SHOW_PRO_VERSION, '<' ) ) {
			return true;
		}

		return false;
	}

	/**
	 * Do updates.
	 *
	 * @since 2.0.2
	 *
	 * @return void
	 */
	public function do_updates() {
		$this->perform_updates();
	}

	/**
	 * Perform all updates
	 *
	 * @since 2.0.2
	 *
	 * @return void
	 */
	public function perform_updates() {
		if ( ! $this->is_needs_update() ) {
			return;
		}

		$installed_version = get_option( 'smart_post_show_pro_version' );

		foreach ( self::$updates as $version => $path ) {
			if ( version_compare( $installed_version, $version, '<' ) ) {
				include $path;
				update_option( 'smart_post_show_pro_version', $version );
			}
		}

		update_option( 'smart_post_show_pro_version', SMART_POST_SHOW_PRO_VERSION );
	}
}
new Smart_Post_Show_Pro_Updates();
