<?php
/**
 * Upgrade Functions
 *
 * @package     MDJM Google Calendar Sync
 * @subpackage  Upgrades
 * @copyright   Copyright (c) 2017, Mike Howard
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
 * @since       2.0
 */

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

/**
 * Perform automatic database upgrades when necessary
 *
 * @since   2.0
 * @return  void
*/
function mdjm_google_calendar_do_automatic_upgrades() {

	$did_upgrade     = false;
	$mdjm_gc_version = preg_replace( '/[^0-9.].*/', '', get_option( 'mdjm_google_calendar_version' ) );

	if ( ! $mdjm_gc_version || version_compare( $mdjm_gc_version, '2.0', '<' ) ) {
		mdjm_google_calendar_v20_upgrades();
	}

	if ( ! $mdjm_gc_version || version_compare( $mdjm_gc_version, '2.1', '<' ) ) {
		mdjm_google_calendar_v21_upgrades();
	}

	if ( version_compare( $mdjm_gc_version, MDJM_GC_VERSION, '<' ) ) {
		// Let us know that an upgrade has happened
		$did_upgrade = true;
	}

	if ( $did_upgrade ) {
		update_option( 'mdjm_google_calendar_version_upgraded_from', get_option( 'mdjm_google_calendar_version' ) );
		update_option( 'mdjm_google_calendar_version', preg_replace( '/[^0-9.].*/', '', MDJM_GC_VERSION ) );
	}

} // mdjm_google_calendar_do_automatic_upgrades
add_action( 'admin_init', 'mdjm_google_calendar_do_automatic_upgrades' );

/**
 * Upgrade routine to version 2.0
 */
function mdjm_google_calendar_v20_upgrades() {
	global $mdjm_options, $wpdb;

	ignore_user_abort( true );

	mdjm_set_time_limit( 0 );

	// Delete old options
	delete_option( 'mdjm_gcal_version' );
	delete_option( 'mdjm_gcal_v12_complete' );

	$options = get_option( 'mdjm_settings' );

	// Transition old option names stored within MDJM Settings
	$transition_options = array(
		'gcal_auth_code'     => 'google_calendar_auth_code',
		'gcal_calendar'      => 'google_calendar',
		'gcal_full_sync'     => 'google_calendar_full_sync',
		'gcal_reminder_type' => 'google_calendar_reminder_type',
		'gcal_reminder_when' => 'google_calendar_reminder_when',
		'gcal_status'        => 'google_calendar_event_status',
		'gcal_title'         => 'google_calendar_entry_title',
		'gcal_venue'         => 'google_calendar_entry_venue',
		'gcal_body'          => 'google_calendar_entry_body',
	);

	// Store MDJM settings just in case!
	set_transient( '_mdjm_google_calendar_v20_upgrade_settings', $options, WEEK_IN_SECONDS );

	foreach ( $transition_options as $old_option => $new_option ) {
		$value = $options[ $old_option ];

		if ( isset( $options[ $old_option ] ) ) {
			$options[ $new_option ] = $options[ $old_option ];
			unset( $options[ $old_option ] );
		}

		$did_update = update_option( 'mdjm_settings', $options );

		// If it updated, let's update the global variable
		if ( $did_update ) {
			$mdjm_options = $options;
		}
	}

	set_transient( 'mdjm_google_calendar_token', mdjm_get_option( 'gcal_access_token' ), HOUR_IN_SECONDS );
	mdjm_delete_option( 'gcal_access_token' );

	// Update meta keys
	$meta_keys = array(
		'_mdjm_gcal_entry'               => '_mdjm_event_google_calendar_entry',
		'_mdjm_event_gcal_reminder_type' => '_mdjm_event_google_calendar_reminder_type',
		'_mdjm_event_gcal_reminder_when' => '_mdjm_event_google_calendar_reminder_when',
	);

	foreach ( $meta_keys as $old_key => $new_key ) {
		$wpdb->update( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
			$wpdb->postmeta,
			array( 'meta_key' => $new_key ), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
			array( 'meta_key' => $old_key ) // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
		);
	}

	wp_clear_scheduled_hook( 'mdjm_gcal_full_sync' );
	wp_schedule_event( current_time( 'timestamp' ) + HOUR_IN_SECONDS, 'daily', 'mdjm_google_calendar_daily_sync' ); // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested

} // mdjm_google_calendar_v20_upgrades

/**
 * Upgrade routine to version 2.1
 */
function mdjm_google_calendar_v21_upgrades() {
	ignore_user_abort( true );

	mdjm_set_time_limit( 0 );

	$options = array(
		'google_calendar_absence_sync'          => 0,
		'google_calendar_absence_reminder_type' => '0',
		'google_calendar_absence_reminder_when' => '24',
		'google_calendar_absence_title'         => sprintf(
			/* translators: The name of the employee absent. %s: Employee Name. */
			__( 'Absence: %s', 'mdjm-google-calendar-sync' ),
			'{employee_name}'
		),
		'google_calendar_absence_body'          => sprintf( 'From: %s', '{start}' ) . PHP_EOL .
													sprintf( 'To: %s', '{end}' ) . PHP_EOL .
													'{notes}',
	);

	foreach ( $options as $option => $value ) {
		mdjm_update_option( $option, $value );
	}

	wp_clear_scheduled_hook( 'mdjm_google_calendar_daily_sync' );
} // mdjm_google_calendar_v21_upgrades
