<?php
/**
 * Title: Core Initializer
 *
 * Description: Initializes the core. Adds all required files.
 *
 * Please do not edit this file. This file is part of the CyberChimps Framework and all modifications
 * should be made in a child theme.
 *
 * @category CyberChimps Framework
 * @package  Framework
 * @since    1.0
 * @author   CyberChimps
 * @license  http://www.opensource.org/licenses/gpl-license.php GPL v3.0 (or later)
 * @link     http://www.cyberchimps.com/
 */

if ( ! function_exists( 'cyberchimps_core_setup_theme' ) ) :

	/**
	 * Setup the theme
	 */
	function cyberchimps_core_setup_theme() {

		// Set directory path.
		$directory = get_template_directory();

		// Load core functions file.
		require_once( $directory . '/core/functions.php' );

		// Load santize before options-init and options core.
		require_once( $directory . '/core/options-sanitize.php' );

		// Load core hooks file.
		require_once( $directory . '/core/inc/cc-custom-background.php' );

		// Core Translations can be filed in the /inc/languages/ directory.
		load_theme_textdomain( 'cyberchimps_core', $directory . '/core/lib/languages' );

		// Add support for the Aside Post Formats.
		add_theme_support( 'post-formats', array( 'aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio', 'chat' ) );

		// Add default posts and comments RSS feed links to head.
		add_theme_support( 'automatic-feed-links' );

		// Enable support for Post Thumbnails.
		add_theme_support( 'post-thumbnails' );

		// Enable support for custom logo.
		add_theme_support(
			'custom-logo', array(
				'flex-width' => true,
				'flex-height' => true,
			)
		);

		// add theme support for backgrounds.
		$defaults = array(
			'default-color'    => apply_filters( 'default_background_color', '' ),
			'default-image'    => apply_filters( 'default_background_image', '' ),
			'wp-head-callback' => 'cyberchimps_custom_background_cb',
		);

		$defaults = apply_filters( 'cyberchimps_background_default_args', $defaults );

		add_theme_support( 'custom-background', $defaults );

		// selective refresh for widgets.
		add_theme_support( 'customize-selective-refresh-widgets' );

		// This theme uses wp_nav_menu() in one location.
		register_nav_menus(
			array(
				'primary' => __( 'Primary Menu', 'cyberchimps_core' ),
			)
		);

	}
endif; // cyberchimps_core_setup_theme.
add_action( 'after_setup_theme', 'cyberchimps_core_setup_theme' );

/**
 * Load options customizer file
 */
function cyberchimps_load_customizer() {
	require_once dirname( __FILE__ ) . '/options-customizer.php';
}
add_action( 'init', 'cyberchimps_load_customizer' );

/**
 * Register our sidebars and widgetized areas
 */
function cyberchimps_widgets_init() {

	register_sidebar(
		array(
			'name'          => __( 'Sidebar Right', 'cyberchimps_core' ),
			'id'            => 'sidebar-right',
			'before_widget' => apply_filters( 'cyberchimps_sidebar_before_widget', '<aside id="%1$s" class="widget-container %2$s">' ),
			'after_widget'  => apply_filters( 'cyberchimps_sidebar_after_widget', '</aside>' ),
			'before_title'  => apply_filters( 'cyberchimps_sidebar_before_widget_title', '<h3 class="widget-title">' ),
			'after_title'   => apply_filters( 'cyberchimps_sidebar_after_widget_title', '</h3>' ),
		)
	);
}
add_action( 'widgets_init', 'cyberchimps_widgets_init' );

/**
 * Custom background
 */
function cyberchimps_custom_background_cb() {

		$style = '';
	// $background is the saved custom image, or the default image.
	$background = get_background_image();

	// $color is the saved custom color.
	// A default has to be specified in style.css. It will not be printed here.
	$color = get_theme_mod( 'background_color' );

	// CyberChimps background image.
	$cc_background = get_theme_mod( 'cyberchimps_background' );

	if ( ! $background && ! $color && ! $cc_background ) {
		return;
	}

	if ( $background ) {
		$image = " background-image: url('$background');";
		$repeat = get_theme_mod( 'background_repeat', 'repeat' );
		if ( ! in_array( $repeat, array( 'no-repeat', 'repeat-x', 'repeat-y', 'repeat' ) ) ) {
			$repeat = 'repeat';
		}
		$repeat = " background-repeat: $repeat;";

		$position = get_theme_mod( 'background_position_x', 'left' );
		if ( ! in_array( $position, array( 'center', 'right', 'left' ) ) ) {
			$position = 'left';
		}
		$position = " background-position: top $position;";

		$attachment = get_theme_mod( 'background_attachment', 'scroll' );
		if ( ! in_array( $attachment, array( 'fixed', 'scroll' ) ) ) {
			$attachment = 'scroll';
		}
		$attachment = " background-attachment: $attachment;";

		$style = $image . $repeat . $position . $attachment;
	} elseif ( 'none' != $cc_background && ! empty( $cc_background ) ) {
		$img_url = get_template_directory_uri() . '/core/lib/images/backgrounds/' . $cc_background . '.jpg';
		$style = "background-image: url( '$img_url' );";
	} elseif ( $color ) {
		$style = "background-color: #$color;";
		$style .= 'background-image: none;';
	} ?>

	<style type="text/css">
		body {
		<?php echo trim( $style ); ?>
		}
	</style>
	
<?php
}

function cyberchimps_load_options_customizer_new( $hook ) {
	wp_enqueue_script( 'option-customizer-new', get_template_directory_uri() . '/core/lib/js/options-new.js', array( 'jquery' ), '', true );
}
add_action( 'admin_enqueue_scripts', 'cyberchimps_load_options_customizer_new' );

function cyberchimps_load_hooks() {

	// Set the path to hooks directory.
	$hooks_path = get_template_directory() . '/core/hooks/';

	require_once( $hooks_path . 'wp-head-hooks.php' );
	require_once( $hooks_path . 'header-hooks.php' );
	require_once( $hooks_path . 'footer-hooks.php' );
}

add_action( 'after_setup_theme', 'cyberchimps_load_hooks' );
