<?php
/**
 * french_vanilla functions and definitions.
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package French_Vanilla
 */

if ( ! function_exists( 'french_vanilla_setup' ) ) :
/**
 * Sets up theme defaults and registers support for various WordPress features.
 *
 * Note that this function is hooked into the after_setup_theme hook, which
 * runs before the init hook. The init hook is too late for some features, such
 * as indicating support for post thumbnails.
 */
function french_vanilla_setup() {
	/*
	 * Make theme available for translation.
	 * Translations can be filed in the /languages/ directory.
	 * If you're building a theme based on french_vanilla, use a find and replace
	 * to change 'french_vanilla' to the name of your theme in all the template files.
	 */
	load_theme_textdomain( 'french-vanilla', get_template_directory() . '/languages' );

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

	/*
	 * Let WordPress manage the document title.
	 * By adding theme support, we declare that this theme does not use a
	 * hard-coded <title> tag in the document head, and expect WordPress to
	 * provide it for us.
	 */
	add_theme_support( 'title-tag' );
	
	/*
	 * Enable support for Post Thumbnails on posts and pages.
	 *
	 * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
	 */
	add_theme_support( 'post-thumbnails' );

	// This theme uses wp_nav_menu() in one location.
	register_nav_menus( array(
		'primary' => esc_html__( 'Primary', 'french-vanilla' ),
		'secondary' => esc_html__( 'Secondary', 'french-vanilla' ),
		'tertiary' => esc_html__( 'Tertiary', 'french-vanilla' ),
	) );

	/*
	 * Switch default core markup for search form, comment form, and comments
	 * to output valid HTML5.
	 */
	add_theme_support( 'html5', array(
		'search-form',
		'comment-form',
		'comment-list',
		'gallery',
		'caption',
	) );

	/*
	 * Enable support for Post Formats.
	 * See https://developer.wordpress.org/themes/functionality/post-formats/
	 */
	add_theme_support( 'post-formats', array(
		'aside',
		'image',
		'video',
		'quote',
		'link',
	) );

}
endif;
add_action( 'after_setup_theme', 'french_vanilla_setup' );

/**
 * Set the content width in pixels, based on the theme's design and stylesheet.
 *
 * Priority 0 to make it available to lower priority callbacks.
 *
 * @global int $content_width
 */
function french_vanilla_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'french_vanilla_content_width', 640 );
}
add_action( 'after_setup_theme', 'french_vanilla_content_width', 0 );

/**
 * Register widget area.
 *
 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
 */
function french_vanilla_widgets_init() {
	register_sidebar( array(
		'name'          => esc_html__( 'Sidebar', 'french-vanilla' ),
		'id'            => 'sidebar-1',
		'description'   => '',
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	) );
}
add_action( 'widgets_init', 'french_vanilla_widgets_init' );

function french_vanilla_add_google_fonts() {
	wp_enqueue_style( 'french-vanilla-google-fonts', 
	'http://fonts.googleapis.com/css?family=Josefin+Sans:300,400,600,700|Cormorant+Garamond|Playfair+Display|Fira+Sans+Condensed', 
	false );
}



add_action( 'wp_enqueue_scripts', 'french_vanilla_add_google_fonts' );

/**
 * Enqueue scripts and styles.
 */
function french_vanilla_scripts() {
	wp_enqueue_style( 'french-vanilla-style', get_stylesheet_uri() );

	wp_enqueue_style ("font-awesome", get_template_directory_uri() . '/fonts/font-awesome/css/font-awesome.min.css', array(), '20160805');

	if( get_theme_mod( 'layout_select' ) == 'sidebar-content' ) {
		wp_enqueue_style('french-vanilla-sidebar-content', get_template_directory_uri() . '/css/sidebar-content.css', array(), '20160204' );
	}
	
	if( get_theme_mod( 'layout_select' ) == 'content-sidebar' ) {
		wp_enqueue_style('french-vanilla-content-sidebar', get_template_directory_uri() . '/css/content-sidebar.css', array(), '2016020402' );
	}
	
	wp_enqueue_script ( 'french-vanilla-navbar', get_template_directory_uri() . '/js/navbar-script.js', array( 'jquery'), '2016020403' );
	
	wp_enqueue_script( 'french-vanilla-slider', get_template_directory_uri() . '/js/slider-script.js', array( 'jquery'), '20160205', true );
	
	wp_enqueue_script( 'french-vanilla-navigation', get_template_directory_uri() . '/js/navigation.js', array(), '20120206', true );

	wp_enqueue_script( 'french-vanilla-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20151215', true );

	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}
}
add_action( 'wp_enqueue_scripts', 'french_vanilla_scripts' );

/**
 * Custom template tags for this theme.
 */
require get_template_directory() . '/inc/template-tags.php';

/**
 * Custom functions that act independently of the theme templates.
 */
require get_template_directory() . '/inc/extras.php';

/**
 * Customizer additions.
 */
require get_template_directory() . '/inc/customizer.php';

/**
 * Load Jetpack compatibility file.
 */
require get_template_directory() . '/inc/jetpack.php';

/* Theme Options */

/*Adds individual sections, settings, and controls to the theme customizer */

function french_vanilla_customizer( $wp_customize ) {


	
	/* Layout Options */
	$wp_customize->add_section(
		'layout_options',
		array(
			'title' => esc_html__('Layout Options', 'french-vanilla'),
			'description' => esc_html__('Change the layout orientation.', 'french-vanilla'),
			'priority' => 34,
		)
	);
	$wp_customize->add_setting(
		'layout_select',
		array(
			'default' => 'content-sidebar',
			'sanitize_callback' => 'sanitize_text_field',
		)
	);
	$wp_customize->add_control(
	'layout_select',
		array (
			'label' => 'Layouts',
			'section' => 'layout_options',
			'type' => 'select',
			'choices' => array(
				'no-sidebar' => esc_html__('No Sidebar', 'french-vanilla'),
				'content-sidebar' => esc_html__('Content-Sidebar', 'french-vanilla'),
				'sidebar-content' => esc_html__('Sidebar-Content', 'french-vanilla'),
			),
		)
	);
	
	/* Footer Options */
	$wp_customize->add_section(
		'footer-options',
		array(
		'title' => esc_html__('Footer Options', 'french-vanilla'),
		'description' => esc_html__('Options for your footer.', 'french-vanilla'),
		'priority' => 35,
		)
	);
	$wp_customize->add_setting(
		'copyright_textbox',
		array(
			'default' => esc_html__(get_bloginfo(name), 'french-vanilla'),
			'sanitize_callback' => 'sanitize_text_field',
		)
	);
	$wp_customize->add_control(
		'copyright_textbox',
		array(
			'label' => esc_html__('Copyright text:', 'french-vanilla'),
			'section' => 'footer-options',
			'type'=>'text',
		)
	);
	$wp_customize->add_setting(
		'hide_copyright',
		array (
			'sanitize_callback' => 'french_vanilla_sanitize_checkbox'
		)
	);
	$wp_customize->add_control(
		'hide_copyright',
		array(
			'type' => 'checkbox',
			'label' => esc_html__('Hide copyright text', 'french-vanilla'),
			'section' => 'footer-options',
		)
	);

	/* Home Page Image */

	$wp_customize->add_section(
		'homepage-image',
		array(
		'title' => esc_html__('Homepage Image', 'french-vanilla'),
		'description' => esc_html__('Chose homepage image.', 'french-vanilla'),
		'priority' => 35,
		)
	);

	$wp_customize->add_setting(
		'display_homepage_image',
		array (
			'sanitize_callback' => 'french_vanilla_sanitize_checkbox'
		)
	);
	$wp_customize->add_control(
		'display_homepage_image',
		array(
			'type' => 'checkbox',
			'label' => esc_html__('Display Homepage Image', 'french-vanilla'),
			'section' => 'homepage-image',
		)
	);

	$wp_customize->add_setting(
		'home_page_image',
		array(
			'default' => 'content-sidebar',
			'sanitize_callback' => 'sanitize_text_field',
		)
	);

	$wp_customize->add_control(
       new WP_Customize_Image_Control(
           $wp_customize,
           'home_page_image',
           array(
               'label'      => __( 'Upload your home page image', 'french-vanilla' ),
               'section'    => 'homepage-image',
           )
       )
   );

}

add_action( 'customize_register', 'french_vanilla_customizer');


function french_vanilla_sanitize_checkbox ( $input ) {
	if ( $input == 1 ) {
		return 1;
	}
		else {
		return '';
	}
}

/**
 * Add support for Vertical Featured Images
 * 
 * Thanks & credit to John Regan for this feature!
 *
 * https://johnregan3.co/2014/01/02/adding-support-for-vertical-featured-images-in-wordpress-themes/
 */

if ( ! function_exists( 'mytheme_vertical_check' ) ) :
	function mytheme_vertical_check( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
		$image_data = wp_get_attachment_image_src( $post_thumbnail_id , 'large' );
		//Get the image width and height from the data provided by wp_get_attachment_image_src()
		$width  = $image_data[1];
		$height = $image_data[2];
		if ( $height > $width ) {
			$html = str_replace( 'attachment-', 'vertical-image attachment-', $html );
		}
		return $html;
	}
endif;
add_filter( 'post_thumbnail_html', 'mytheme_vertical_check', 10, 5 );