<?php
/**
 * @package Magazinex
 */

/*
 * Prevent switching to Magazinex on old versions of WordPress.
 *-----------------------------------------------------------------------*/
function magazinex_back_switch_theme() {
	switch_theme( WP_DEFAULT_THEME, WP_DEFAULT_THEME );
	unset( $_GET['activated'] );
	add_action( 'admin_notices', 'magazinex_back_upgrade_notice' );
}
add_action( 'after_switch_theme', 'magazinex_back_switch_theme' );


/*
 * Add message for unsuccessful theme switch.
 *-----------------------------------------------------------------------*/
function magazinex_back_upgrade_notice() {
	$message = sprintf( esc_html__( 'Magazinex requires at least WordPress version 4.3. You are running version %s. Please upgrade and try again.', 'magazinex-lite' ), $GLOBALS['wp_version'] );
	printf( '<div class="error"><p>%s</p></div>', $message );
}


/*
 * Prevent the Customizer from being loaded on WordPress versions prior to 4.3.
 *-----------------------------------------------------------------------*/
function magazinex_back_customize() {
	wp_die( sprintf( esc_html__( 'Magazinex requires at least WordPress version 4.3. You are running version %s. Please upgrade and try again.', 'magazinex-lite' ), $GLOBALS['wp_version'] ), '', array(
		'back_link' => true,
	) );
}
add_action( 'load-customize.php', 'magazinex_back_customize' );


/*
 * Prevent the Theme Preview from being loaded on WordPress versions prior to 4.3.
 *-----------------------------------------------------------------------*/
function magazinex_back_preview() {
	if ( isset( $_GET['preview'] ) ) {
		wp_die( sprintf( esc_html__( 'Magazinex requires at least WordPress version 4.3. You are running version %s. Please upgrade and try again.', 'magazinex-lite' ), $GLOBALS['wp_version'] ) );
	}
}
add_action( 'template_redirect', 'magazinex_back_preview' );
