<?php
/**
 * Functions for updating data, used by the background updater.
 */

defined( 'ABSPATH' ) || exit;

use KaizenCoders\MagicLink\Install;

/* --------------------- 1.1.0 (Start)--------------------------- */

/**
 * Create Magic Link Table
 *
 * @since 1.1.0
 */
function kc_ml_update_110_create_tables() {
	Install::create_tables( '1.1.0' );
}

function kc_ml_update_110_update_options() {
	Install::create_options( '1.1.0' );
}

/* --------------------- 1.0.1 (End)--------------------------- */


function kc_ml_update_117_update_options() {
	Install::create_options( '1.1.7' );
}

/* --------------------- 1.0.7 (End)--------------------------- */

function kc_ml_update_119_update_options() {
	Install::create_options( '1.1.9' );

	$options_to_remove = [
		'general|settings|ip_address_check',
	];

	Install::remove_options( $options_to_remove );
}

/* --------------------- 1.0.9 (End)--------------------------- */

function kc_ml_update_124_update_options() {
	Install::create_options( '1.2.4' );
}

/* --------------------- 1.2.4 (End)--------------------------- */

function kc_ml_update_125_update_options() {
	Install::create_options( '1.2.5' );
}

/* --------------------- 1.2.5 (End)--------------------------- */

function kc_ml_update_130_update_options() {
	Install::create_options( '1.3.0' );

	// Migrate custom email_subject / email_body from general|settings to the new email|settings path.
	$settings = get_option( 'kc_ml_settings', [] );

	$old_subject = \KaizenCoders\MagicLink\Helper::get_data( $settings, 'general|settings|email_subject', '' );
	$old_body    = \KaizenCoders\MagicLink\Helper::get_data( $settings, 'general|settings|email_body', '' );

	if ( ! empty( $old_subject ) && empty( $settings['email']['settings']['email_subject'] ) ) {
		$settings['email']['settings']['email_subject'] = $old_subject;
	}
	if ( ! empty( $old_body ) && empty( $settings['email']['settings']['email_body'] ) ) {
		$settings['email']['settings']['email_body'] = $old_body;
	}

	update_option( 'kc_ml_settings', $settings );
}

/* --------------------- 1.3.0 (End)--------------------------- */


/* --------------------- 1.5.0 (Start)--------------------------- */

/**
 * Create the audit-log table introduced in 1.5.0.
 *
 * @since 1.5.0
 */
function kc_ml_update_150_create_audit_log_table() {
	Install::create_tables( '1.5.0' );
}

/* --------------------- 1.5.0 (End)--------------------------- */