<?php
/**
 * Theme setup and initialization.
 *
 * @package     BrunchPro
 * @subpackage  Genesis
 * @author      Robert Neu
 * @copyright   Copyright (c) 2016, Shay Bocks
 * @license     GPL-2.0+
 * @link        http://www.shaybocks.com/brunch-pro/
 * @since       1.0.0
 */

defined( 'ABSPATH' ) || exit;

define( 'CHILD_THEME_NAME', 'Brunch Pro Theme' );
define( 'CHILD_THEME_VERSION', '2.1.1' );
define( 'CHILD_THEME_URL', 'http://shaybocks.com/brunch-pro/' );
define( 'CHILD_THEME_DEVELOPER', 'Shay Bocks' );

/**
 * Require an included theme file once.
 *
 * @since  1.0.0
 * @access public
 * @param  string $path the relative path of the file to be included.
 * @return void
 */
function brunch_pro_require_once( $path ) {
	require_once trailingslashit( get_stylesheet_directory() ) . 'includes/' . ltrim( $path );
}

add_action( 'after_setup_theme', 'brunch_pro_content_width', 0 );
/**
 * Set the content width and allow it to be filtered directly.
 *
 * @since  1.0.0
 * @access public
 * @return void
 */
function brunch_pro_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'brunch_pro_content_width', 980 );
}

add_action( 'after_setup_theme', 'brunch_pro_load_textdomain' );
/**
 * Load the child theme textdomain.
 *
 * @since  1.0.0
 * @access public
 * @return void
 */
function brunch_pro_load_textdomain() {
	load_child_theme_textdomain(
		'brunch-pro',
		trailingslashit( get_stylesheet_directory() ) . 'languages'
	);
}

add_action( 'init', 'brunch_pro_register_image_sizes', 5 );
/**
 * Register custom image sizes for the theme.
 *
 * @since  1.0.0
 * @access public
 * @return void
 */
function brunch_pro_register_image_sizes() {
	add_image_size( 'horizontal-thumbnail', 680, 450, true );
	add_image_size( 'vertical-thumbnail',   680, 900, true );
	add_image_size( 'square-thumbnail',     300, 300, true );
}

add_action( 'init', 'brunch_pro_register_layouts' );
/**
 * Register additional theme layout options.
 *
 * @since  1.0.0
 * @access public
 * @return void
 */
function brunch_pro_register_layouts() {
	genesis_register_layout( 'full-width-slim', array(
		'label' => __( 'Full Width Slim', 'brunch-pro' ),
		'img'   => trailingslashit( get_stylesheet_directory_uri() ) . 'images/layout-slim.gif',
	) );
	genesis_register_layout( 'alt-sidebar-content', array(
		'label' => __( 'Alt Sidebar/Content', 'brunch-pro' ),
		'img'   => trailingslashit( get_stylesheet_directory_uri() ) . 'images/layout-alt-sidebar-content.gif',
	) );
}

add_action( 'genesis_setup', 'brunch_pro_theme_setup', 15 );
/**
 * Theme Setup
 *
 * This setup function hooks into the Genesis Framework to allow access to all
 * of the core Genesis functions. All the child theme functionality can be found
 * in files located within the /includes/ directory.
 *
 * @since  1.0.0
 * @access public
 * @return void
 */
function brunch_pro_theme_setup() {
	add_theme_support( 'genesis-responsive-viewport' );

	add_theme_support( 'html5' );

	add_theme_support( 'custom-background' );

	add_theme_support( 'genesis-accessibility', array(
		'headings',
		'search-form',
		'skip-links',
	) );

	add_theme_support( 'custom-header', array(
	'width'           => 1400,
	'height'          => 321,
	'header-selector' => '.site-title a',
    'header-text'     => false,
    'flex-width'     => true,
    'flex-height'     => true,
	) );

	add_theme_support( 'genesis-footer-widgets', 4 );

	add_theme_support( 'genesis-after-entry-widget-area' );
}

add_action( 'genesis_setup', 'brunch_pro_includes', 15 );
/**
 * Load additional functions and helpers.
 *
 * @since  1.0.0
 * @access public
 * @return void
 */
function brunch_pro_includes() {
	brunch_pro_require_once( 'vendor/customizer-library/customizer-library.php' );
	brunch_pro_require_once( 'customize/helpers-colors.php' );
	brunch_pro_require_once( 'customize/helpers-fonts.php' );
	brunch_pro_require_once( 'customize/helpers-general.php' );
	brunch_pro_require_once( 'customize/library-filters.php' );
	brunch_pro_require_once( 'helpers-archive.php' );
	brunch_pro_require_once( 'helpers-layout.php' );
	brunch_pro_require_once( 'compatibility.php' );
	brunch_pro_require_once( 'scripts.php' );
	brunch_pro_require_once( 'template-archive.php' );
	brunch_pro_require_once( 'template-global.php' );
	brunch_pro_require_once( 'widgets-init.php' );
	if ( genesis_is_customizer() ) {
		brunch_pro_require_once( 'customize/settings-archives.php' );
		brunch_pro_require_once( 'customize/settings-colors.php' );
		brunch_pro_require_once( 'customize/settings-fonts.php' );
	}
}

add_action( 'genesis_setup', 'brunch_pro_admin_includes', 15 );
/**
 * Load admin functions and helpers.
 *
 * @since  1.0.0
 * @access public
 * @return void
 */
function brunch_pro_admin_includes() {
	if ( is_admin() ) {
		brunch_pro_require_once( 'vendor/class-tgm-plugin-activation.php' );
		brunch_pro_require_once( 'admin/functions.php' );
	}
}

//* Enqueue scripts and styles
add_action( 'wp_enqueue_scripts', 'custom_enqueue_scripts_styles' );
function custom_enqueue_scripts_styles() {

	wp_enqueue_style( 'font-awesome', '//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css' );
	
	wp_enqueue_script( 'global', get_bloginfo( 'stylesheet_directory' ) . '/js/global.js', array( 'jquery' ), '1.0.0', true );
	
	wp_enqueue_script( 'sticky-nav', get_bloginfo( 'stylesheet_directory' ) . '/js/sticky-nav.js', array( 'jquery' ), '', true );
	
}

add_filter( 'wp_nav_menu_items', 'theme_menu_extras', 10, 2 );
/**
 * Filter menu items to append a search form.
 *
 * @param string   $menu HTML string of list items.
 * @param stdClass $args Menu arguments.
 *
 * @return string Amended HTML string of list items.
 */
function theme_menu_extras( $menu, $args ) {

	if ( 'primary' !== $args->theme_location )
		return $menu;

	$menu .= '<li class="search"><a id="main-nav-search-link" class="icon-search"></a><div class="search-div">' . get_search_form( false ) . '</div></li>';
	
	return $menu;

}

//* Customize search form input button text
add_filter( 'genesis_search_button_text', 'sp_search_button_text' );
function sp_search_button_text( $text ) {
	return esc_attr( 'Go' );
}

//* Load Google Fonts
add_filter( 'brunch_pro_disable_google_fonts', '__return_true' );
add_action( 'wp_enqueue_scripts', 'dear_enqueue_syles' );
function dear_enqueue_syles() {
	wp_enqueue_style( 'brunch-pro-google-fonts', '//fonts.googleapis.com/css?family=IM+Fell+Double+Pica:400,400italic|Source+Sans+Pro:300,300italic,400,400italic,600,600italic', array(), brunch_pro_THEME_VERSION );
}

add_shortcode( 'primary_post_category', 'sk_primary_post_category_shortcode' );
/**
 * Produces the primary category link.
 *
 * Supported shortcode attributes are:
 *   after (output after link, default is empty string),
 *   before (output before link, default is 'Tagged With: '),
 *   sep (separator string between tags, default is ', ').
 *
 * Output passes through 'sk_primary_post_category_shortcode' filter before returning.
 *
 * @since 1.1.0
 *
 * @param array|string $atts Shortcode attributes. Empty string if no attributes.
 *
 * @return string Output for `primary_post_category` shortcode.
 */
function sk_primary_post_category_shortcode( $atts ) {

    $defaults = array(
        'sep'    => ', ',
        'before' => __( 'Filed Under: ', 'genesis' ),
        'after'  => '',
    );

    $atts = shortcode_atts( $defaults, $atts, 'primary_post_category' );

    $primary_cat = new WPSEO_Primary_Term( 'category', get_the_ID() );
    $primary_cat = $primary_cat->get_primary_term();
    $cat_name = get_cat_name( $primary_cat );
    $cat_link = get_category_link( $primary_cat );

    $cat = '<a href="' . $cat_link . '">' . $cat_name . '</a>';

    // output primary category link.
    $output = sprintf( '', genesis_attr( 'entry-category' ) ) . $atts['before'] . $cat . $atts['after'] . '';

    return apply_filters( 'sk_primary_post_category_shortcode', $output, $atts );

}

add_filter( 'genesis_post_meta', 'sk_post_meta_filter' );
/**
 * Customize entry meta
 *
 * @param string $post_meta Default post meta.
 *
 * @return string Modified post meta if Yoast SEO is active and if a primary category has been selected, otherwise default post meta
 *
 * @link http://my.studiopress.com/docs/shortcode-reference/#post-shortcodes
 */
function sk_post_meta_filter( $post_meta ) {

    if ( class_exists( 'WPSEO_Frontend' ) ) { // if Yoast SEO is active.
        $wpseo_primary_term = new WPSEO_Primary_Term( 'category', get_the_ID() ); // WPSEO_Primary_Term Object.
        $wpseo_primary_term = $wpseo_primary_term->get_primary_term(); // term_id.
        $term = get_term( $wpseo_primary_term ); // WP_Term Object.

        if ( $term && ! is_wp_error( $term ) ) { // if primary category term exists.
            $post_meta = '[primary_post_category before="Filed Under: "] [post_tags before="Tagged: "]';
        }
    }

    return $post_meta;

}