<?php

if ( ! function_exists( 'trifold_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 trifold_setup() {
	/*
	 * Make theme available for translation.
	 * Translations can be filed in the /languages/ directory.
	 * If you're building a theme based on TriFold, use a find and replace
	 * to change 'trifold' to the name of your theme in all the template files.
	 */
	load_theme_textdomain( 'trifold', get_template_directory() . '/languages' );

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

	/*
	 * Enable support for Custom Logo
	 * @link https://codex.wordpress.org/Theme_Logo
	 */
	add_theme_support( 'custom-logo', array(
		'flex-height' => true,
		'flex-width'  => true
	) );

	/*
	 * 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 Menu', 'trifold' ),
	) );

	/*
	 * 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(
		'gallery',
		'video',
		'quote',
		'link',
	) );

	// This theme styles the visual editor with editor-style.css to match the theme style.
	add_editor_style();

	/**
	 * Plugin Support
	 */
	// Yoast SEO breadcrumbs
	add_theme_support( 'yoast-seo-breadcrumbs' );
}
endif; // trifold_setup
add_action( 'after_setup_theme', 'trifold_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 trifold_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'trifold_content_width', 1140 );
}
add_action( 'after_setup_theme', 'trifold_content_width', 0 );

if ( ! function_exists( 'trifold_dynamic_content_width' ) ) :
/**
 * Set the content width based on current layout
 *
 * @since  1.0.0.
 *
 * @return void
 */
function trifold_dynamic_content_width() {
	global $content_width;

	$new_width = $content_width;
	$dynamic_content = get_theme_mod( 'layout_content', trifold_get_default( 'layout_content' ) );

	// No sidebars
	if ( $dynamic_content == 'one_col' ) {
		$new_width = 1140;
	}
	// has sidebars
	else {
		$new_width = 860;
	}

	/**
	 * Filter to modify the $content_width variable.
	 *
	 * @since 1.0.0
	 *
	 * @param int     $new_width    The new content width.
	 * @param bool    $dynamic_layout    True if the current view has a left or right sidebar.
	 */
	$content_width = apply_filters( 'trifold_dynamic_content_width', $new_width, $dynamic_content );
}
endif;

add_action( 'template_redirect', 'trifold_dynamic_content_width' );

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

	$columns = get_theme_mod( 'footer-widget-columns', trifold_get_default( 'footer-widget-columns' ) );
	if ( is_numeric( $columns ) != 0 ) {
		for ( $i = 1; $i <= intval( $columns ) ; $i++ ) {
			register_sidebar( array( 'name' => sprintf( esc_html__( 'Footer %d', 'trifold' ), $i), 'id' => sprintf('footer-%d', $i), 'description' => sprintf( esc_html__('Widgetized Footer Region %d.', 'trifold'), $i), 'before_widget' => '<div id="%1$s" class="widget %2$s">', 'after_widget' => '</div>', 'before_title' => '<h4>', 'after_title' => '</h4>' ));
		}
	}
}
add_action( 'widgets_init', 'trifold_widgets_init' );

/**
 * Enqueue scripts and styles.
 */
function trifold_scripts() {

	$suffix = trifold_min_suffix();

	$directory_scripts = '/assets/js/dist/';	
	if ( ! $suffix ) {
		$directory_scripts = '/assets/js/src/';
	}

	wp_enqueue_style( 'trifold-bootstrap', get_template_directory_uri() . '/assets/css/bootstrap' . $suffix . '.css', NULL, NULL );
	wp_enqueue_style( 'trifold-style', get_template_directory_uri() . '/style' . $suffix . '.css', NULL, NULL );
	wp_enqueue_style( 'trifold-font-icons', get_template_directory_uri() . '/assets/css/font-icons.css', NULL, NULL );
	wp_enqueue_style( 'trifold-animate', get_template_directory_uri() . '/assets/css/animate.min.css', NULL, NULL );

	wp_enqueue_script( 'trifold-skip-link-focus-fix', get_template_directory_uri() . '/assets/js/dist/skip-link-focus-fix.js', array(), '20130115', true );
	
	wp_register_script( 'trifold-script-plugins', get_template_directory_uri() . $directory_scripts . 'plugins' . $suffix . '.js', array( 'jquery' ), NULL, true );
	wp_enqueue_script( 'trifold-script-functions', get_template_directory_uri() . $directory_scripts . 'functions' . $suffix . '.js', array( 'trifold-script-plugins' ), NULL, true );
	
	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}
}
add_action( 'wp_enqueue_scripts', 'trifold_scripts' );

/**
 * Enqueue admin scripts
 * @param  boolean $hook
 * @return void
 */
function trifold_admin_scripts( $hook ){

	$suffix = trifold_min_suffix();

	$directory_scripts = '/assets/js/dist/admin/';	
	if ( ! $suffix ) {
		$directory_scripts = '/assets/js/src/admin/';
	}

	wp_enqueue_style( 'admin-style', get_template_directory_uri() . '/assets/css/admin.css' );


	if( 'widgets.php' == $hook ) {
		wp_enqueue_media();
	wp_enqueue_script( 'trifold-script-widgets', get_template_directory_uri() . $directory_scripts . 'widgets' . $suffix . '.js', array( 'jquery' ), NULL, true );
	}
}
add_action( 'admin_enqueue_scripts', 'trifold_admin_scripts' );