<?php 
/**
 * Load admin scripts and styles
 */

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






function shrk_load_admin_scripts() {
	$screen=get_current_screen()->id;

	// Styles
	wp_enqueue_style( 'font-awesome', SHRK_FONTS_DIR_URL . '/fontawesome/css/font-awesome.min.css' );
	wp_enqueue_style( 'shrk_wp_admin_plugin_css', SHRK_ADMIN_CSS_URI . '/admin-style.css' );

	// Scripts
	wp_enqueue_script( 'shrk-admin-scripts.js', SHRK_ADMIN_JS_URI . '/admin-scripts.js', array( 'jquery', 'jquery-ui-sortable' ) );

	// Spectrum color-picker (redux handle to avoid double loading)
	wp_register_script(
		'redux-spectrum-js',
		SHRK_ADMIN_JS_URI . '/spectrum/redux-spectrum.min.js',
		array( 'jquery' ),
		'1.3.3',
		true
		);
	
	wp_register_style(
		'redux-spectrum-css',
		SHRK_ADMIN_CSS_URI . '/spectrum/redux-spectrum.min.css',
		array(),
		'1.3.3',
		'all'
		);
}
add_action( 'admin_enqueue_scripts', 'shrk_load_admin_scripts' );

/**
 * Run at theme activation
 * setup extra options
 */
function shrk_setup_theme_extra() {
	global $shrk_theme_info;

	if ( get_option( 'shrk_theme_activated_once', 'noname' ) != $shrk_theme_info['Name'] ) {
		//Run only on 1st theme activation

		// If attachments and products are found
		if ( count( get_posts( array( 'post_type' => 'attachment' ) ) ) > 0 && count( get_posts( array( 'post_type' => 'product' ) ) ) > 0  ) {
			// Add notice about regenerating thumbnails
			add_action( 'admin_notices', 'shrk_admin_notices_regenerate_thumbnails' );
		}

		do_action( 'shrk_theme_first_install' );

		// set flag for run on 1st activation 
		update_option( 'shrk_theme_activated_once', $shrk_theme_info['Name'] );
	} else {
		// Run on every theme activation
	}
}
add_action( 'after_switch_theme', 'shrk_setup_theme_extra', 12 );

function shrk_admin_notices_regenerate_thumbnails() {
	?>
	<div class="notice notice-warning is-dismissible">
	    <p><?php esc_html_e( 'You should regenerate thumbnails to ensure all products display correctly. If you haven\'t already, please install \'Regenerate Thumbnails\' plugin.', 'ux-shop' ); ?></p>
	</div>
	<?php
}

/**
 * Modify backend body classes
 */
function shrk_admin_body_classes( $classes ) {
	if ( post_type_exists( 'pagesections' ) ) {
		$classes .= ' pagesections-enabled';
	}

	return $classes;
}
add_filter( 'admin_body_class', 'shrk_admin_body_classes' );

/**
 * Abort theme activation on PHP < 5.3
 * And avoid a fatal error.
 * Show a notice instead
 * 
 * @param  string $oldtheme_name
 * @param  object $oldtheme
 * @return bool
 */
function shrk_check_php_version( $oldtheme_name, $oldtheme ) {
	global $_GET;

	if ( version_compare( PHP_VERSION, '5.3.0', '<' ) ) {
		
		add_action( 'admin_notices', 'shrk_notice_wrong_php_version' );

		if ( !empty( $oldtheme ) && !empty( $oldtheme->stylesheet )  ) {

			if ( isset( $_GET['activated'] ) ) {
				unset( $_GET['activated'] );
			}

			// Switch back to previous theme.
			switch_theme( $oldtheme->stylesheet );
			return false;
		}
	}
}
add_action( 'after_switch_theme', 'shrk_check_php_version', 10, 2 );

/**
 * Display custom admin notice
 * @return none
 */
function shrk_notice_wrong_php_version() { 
	?>
	<div class="notice notice-error is-dismissible">
	    <p><?php esc_html_e( 'Your server is using a very old PHP version. This theme requires at least PHP 5.3. Theme activation aborted.', 'ux-shop' ); ?></p>
	</div>
	<?php 
}

?>