<?php
/**
 * Custom functions that act independently of the theme templates.
 *
 * Eventually, some of the functionality here could be replaced by core features.
 *
 * @package official_core
 */

/**
 * Adds custom classes to the array of body classes.
 *
 * @param array $classes Classes for the body element.
 * @return array
 */
function official_core_body_classes( $classes ) {
	// Adds a class of group-blog to blogs with more than 1 published author.
	if ( is_multi_author() ) {
		$classes[] = 'group-blog';
	}

	// Adds a class of hfeed to non-singular pages.
	if ( ! is_singular() ) {
		$classes[] = 'hfeed';
	}

	return $classes;
}
add_filter( 'body_class', 'official_core_body_classes' );


/**
 * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
 */
function officialcore_customize_preview_js() {
	wp_enqueue_script( 'officialcore_customizer', get_template_directory_uri() . 'assets/js/customizer.js', array( 'customize-preview' ), '20151215', true );
}
add_action( 'customize_preview_init', 'officialcore_customize_preview_js' );


/**
 * Check if WP Less plugin is enabled and then compile Less file with custom color variables
 */
if (class_exists('WPLessPlugin')){

	$less = WPLessPlugin::getInstance();

	$officialcore_colors_primary      = get_theme_mod('officialcore_colors_primary');
	$officialcore_colors_secondary    = get_theme_mod('officialcore_colors_secondary');

	// add less ariables from theme options to parse into main theme less
	$less->setVariables(array(
      'less-brand-primary' => esc_html($officialcore_colors_primary),
      'less-brand-secondary' => esc_html($officialcore_colors_secondary)
	));

  /* Make Less output minified */
	$less->getCompiler()->setFormatter('compressed');

}


/*
* Determine main column size when sidebar 'left' and 'right' are active
*/
if (!function_exists('officialcore_columns_size')) {

	function officialcore_columns_size()
	{
		if ( is_active_sidebar('officialcore_sidebar_left_widgets_area') && is_active_sidebar('officialcore_sidebar_right_widgets_area') ) {
			// if both sidebar actived.
			$officialcore_columns_size = 6;

		} elseif ( is_active_sidebar('officialcore_sidebar_left_widgets_area') || is_active_sidebar('officialcore_sidebar_right_widgets_area') ) {
			// if only one sidebar actived.
			$officialcore_columns_size = 9;

		} else {
			// if no sidebar actived.
			$officialcore_columns_size = 12;
		}

		return $officialcore_columns_size;
	}
}

/*
* Maintenance mode
*/
if (!function_exists('official_maintenance_mode')) {
	function official_maintenance_mode() {

		global $pagenow;
		$maintenance_setting = get_theme_mod('officialcore_maintenance_maintenance');
	  $maintenance_setting = $maintenance_setting == 'true'? true: false;
		$url = filter_input(INPUT_SERVER, 'REQUEST_URI');

		if (
			$pagenow !== 'wp-login.php'
			&& ! current_user_can( 'manage_options' )
			&& ! is_admin()
			&& $maintenance_setting == true
			|| (strpos($url,'official-maintenance') == true )
			)
		{

			//header( 'HTTP/1.1 Service Unavailable', true, 503 );
			//header( 'Content-Type: text/html; charset=utf-8' );

			require_once get_template_directory() . '/maintenance.php';

			die();
		}
	}
}

add_action( 'wp_loaded', 'official_maintenance_mode' );

/*
* Editor styles custom CSS
* See https://codex.wordpress.org/Editor_Style
*/
function officialcore_editor_style() {
	add_editor_style( 'assets/css/custom-editor-style.css' );
}

add_action( 'admin_init', 'officialcore_editor_style' );
