<?php
/**
 * BlazeBlog functions and definitions
 *
 * Set up the theme and provides some helper functions, which are used in the
 * theme as custom template tags. Others are attached to action and filter
 * hooks in WordPress to change core functionality.
 *
 * When using a child theme you can override certain functions (those wrapped
 * in a function_exists() call) by defining them first in your child theme's
 * functions.php file. The child theme's functions.php file is included before
 * the parent theme's file, so the child theme functions would be used.
 *
 * @link https://codex.wordpress.org/Theme_Development
 * @link https://codex.wordpress.org/Child_Themes
 *
 * Functions that are not pluggable (not wrapped in function_exists()) are
 * instead attached to a filter or action hook.
 *
 * For more information on hooks, actions, and filters,
 * {@link https://codex.wordpress.org/Plugin_API}
 *
 * @package WordPress
 * @subpackage blazeblog
 * @since BlazeBlog 1.0
 */

/**
 * Custom bootstrap based nav menu walker for this theme.
 */
require_once( 'includes/wp_bootstrap_navwalker.php' );

if ( ! function_exists( 'blazeblog_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.
     *
     * Create your own blazeblog_setup() function to override in a child theme.
     *
     * @since BlazeBlog 1.0
     */
    function blazeblog_setup() {
        /*
         * Make theme available for translation.
         * Translations can be filed in the /languages/ directory.
         * If you're building a theme based on Twenty Sixteen, use a find and replace
         * to change 'blazeblog' to the name of your theme in all the template files
         */
        load_theme_textdomain( 'blazeblog', get_template_directory() . '/languages' );

        // 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 http://codex.wordpress.org/Function_Reference/add_theme_support#Post_Thumbnails
         */
        add_theme_support( 'post-thumbnails' );
        set_post_thumbnail_size( 1200, 9999 );

        // This theme uses wp_nav_menu() in two locations.
        register_nav_menus( array(
            'primary' => __( 'Primary Menu', 'blazeblog' ),
            'social'  => __( 'Social Links Menu', 'blazeblog' ),
        ) );

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

        // Add theme support for Custom Logo.
        add_theme_support( 'custom-logo', array(
            'width'       => 150,
            'height'      => 40,
            'flex-width'  => true,
        ) );

        /*
         * Enable support for Post Formats.
         *
         * See: https://codex.wordpress.org/Post_Formats
         */
        add_theme_support( 'post-formats', array(
            'aside',
            'image',
            'video',
            'quote',
            'link',
            'gallery',
            'status',
            'audio',
            'chat',
        ) );

        /*
         * This theme styles the visual editor to resemble the theme style,
         * specifically font, colors, icons, and column width.
         */
        add_editor_style( array( 'assets/css/editor-style.css', blazeblog_fonts_url() ) );
    }
endif; // blazeblog_setup
add_action( 'after_setup_theme', 'blazeblog_setup' );

/**
 * Sets 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
 *
 * @since BlazeBlog 1.0
 */
function blazeblog_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'blazeblog_content_width', 840 );
}
add_action( 'after_setup_theme', 'blazeblog_content_width', 0 );

/**
 * Gets the custom logo img.
 *
 * @since BlazeBlog 1.0
 */
function blazeblog_get_custom_logo_img() {
    $custom_logo_id = get_theme_mod( 'custom_logo' );
    $logo = '';
    if($custom_logo_id) {
        $image = wp_get_attachment_image_src( $custom_logo_id , 'full' );
        $logo = sprintf('<img class="site-logo" src="%1$s" alt="%2$s %3$s" />', $image[0], get_bloginfo( 'name', 'display' ), get_bloginfo( 'description', 'display' )  );
    }
    echo $logo;
}

/**
 * Gets the icon logo.
 *
 * @since BlazeBlog 1.0
 */
function blazeblog_get_icon_logo() {
    $icon = '';
    if(!get_theme_mod( 'custom_logo', false )) {
        if( get_theme_mod( 'blazeblog_icon_logo', false ) ) {
            $icon = sprintf('<span class="site-icon-logo %1$s"></span>', get_theme_mod( 'blazeblog_icon_logo' ));
        }
    }
    echo $icon;
}

/**
 * Registers a widget area.
 *
 * @link https://developer.wordpress.org/reference/functions/register_sidebar/
 *
 * @since BlazeBlog 1.0
 */
function blazeblog_widgets_init() {
	register_sidebar( array(
		'name'          => __( 'Sidebar', 'blazeblog' ),
		'id'            => 'sidebar-1',
		'description'   => __( 'Add widgets here to appear in your sidebar.', 'blazeblog' ),
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	) );

	register_sidebar( array(
		'name'          => __( 'Content Bottom 1', 'blazeblog' ),
		'id'            => 'sidebar-2',
		'description'   => __( 'Appears at the bottom of the content on posts and pages.', 'blazeblog' ),
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	) );

	register_sidebar( array(
		'name'          => __( 'Content Bottom 2', 'blazeblog' ),
		'id'            => 'sidebar-3',
		'description'   => __( 'Appears at the bottom of the content on posts and pages.', 'blazeblog' ),
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	) );

	register_sidebar( array(
		'name'          => __( 'Footer', 'blazeblog' ),
		'id'            => 'sidebar-4',
		'description'   => __( 'Appears in the footer area of pages.', 'blazeblog' ),
		'before_widget' => '<div class="col-md-4 col-sm-6 col-xs-12 widget-masonry"><section id="%1$s" class="widget col-sm-12 %2$s">',
		'after_widget'  => '</section></div>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	) );
}
add_action( 'widgets_init', 'blazeblog_widgets_init' );

if ( ! function_exists( 'blazeblog_fonts_url' ) ) :
    /**
     * Register Google fonts for BlazeBlog.
     *
     * Create your own blazeblog_fonts_url() function to override in a child theme.
     *
     * @since BlazeBlog 1.0
     *
     * @return string Google fonts URL for the theme.
     */
    function blazeblog_fonts_url() {
        $fonts_url = '';
        $fonts     = array();
        $subsets   = 'latin,latin-ext';

        /* translators: If there are characters in your language that are not supported by Merriweather, translate this to 'off'. Do not translate into your own language. */
        if ( 'off' !== _x( 'on', 'Merriweather font: on or off', 'blazeblog' ) ) {
            $fonts[] = 'Merriweather:400,700,900,400italic,700italic,900italic';
        }
        $fonts[] = 'Open Sans:400,700,900,400italic,700italic,900italic';
        /* translators: If there are characters in your language that are not supported by Montserrat, translate this to 'off'. Do not translate into your own language. */
        if ( 'off' !== _x( 'on', 'Montserrat font: on or off', 'blazeblog' ) ) {
            $fonts[] = 'Montserrat:400,700';
        }

        /* translators: If there are characters in your language that are not supported by Inconsolata, translate this to 'off'. Do not translate into your own language. */
        if ( 'off' !== _x( 'on', 'Inconsolata font: on or off', 'blazeblog' ) ) {
            $fonts[] = 'Inconsolata:400';
        }

        if ( $fonts ) {
            $fonts_url = add_query_arg( array(
                'family' => urlencode( implode( '|', $fonts ) ),
                'subset' => urlencode( $subsets ),
            ), 'https://fonts.googleapis.com/css' );
        }

        return $fonts_url;
    }
endif;

/**
 * Handles JavaScript detection.
 *
 * Adds a `js` class to the root `<html>` element when JavaScript is detected.
 *
 * @since BlazeBlog 1.0
 */
function blazeblog_javascript_detection() {
	echo "<script>(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);</script>\n";
}
add_action( 'wp_head', 'blazeblog_javascript_detection', 0 );

/**
 * Enqueues scripts and styles.
 *
 * @since BlazeBlog 1.0
 */
function blazeblog_scripts() {
	// Add custom fonts, used in the main stylesheet.
	wp_enqueue_style( 'ggfx-fonts', blazeblog_fonts_url(), array(), null );

	// Add Genericons, used in the main stylesheet.
	wp_enqueue_style( 'genericons', get_template_directory_uri() . '/assets/genericons/genericons.css', array(), '3.4.1' );
    wp_enqueue_style( 'bootstrap', get_template_directory_uri() . '/assets/css/bootstrap.min.css', array(), '3.3.6' );
    if(is_rtl()){
        wp_enqueue_style( 'bootstrap-rtl', get_template_directory_uri() . '/assets/css/rtl/bootstrap-rtl.min.css', array(), '3.3.6' );
    }
    wp_enqueue_style( 'font-awesome', get_template_directory_uri() . '/assets/css/font-awesome.min.css', array(), '3.3.6' );

	// Theme stylesheet.
	wp_enqueue_style( 'blaze-core-style', get_stylesheet_uri() );
    wp_enqueue_style( 'blaze-style', get_template_directory_uri() . '/assets/css/style.css', array(), '1.0.0' );
    if(is_rtl()){
        wp_enqueue_style( 'bootstrap-rtl', get_template_directory_uri() . '/rtl.css', array(), '1.0.0' );
    }
	// Load the html5 shiv.
	wp_enqueue_script( 'blaze-html5', get_template_directory_uri() . '/assets/js/html5.js', array(), '3.7.3' );
	wp_script_add_data( 'blaze-html5', 'conditional', 'lt IE 9' );

    wp_enqueue_script( 'bootstrap', get_template_directory_uri() . '/assets/js/bootstrap.min.js', array( 'jquery' ), '3.3.6', true );
	wp_enqueue_script( 'blaze-skip-link-focus-fix', get_template_directory_uri() . '/assets/js/skip-link-focus-fix.js', array(), '20151112', true );

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

	// Adds Masonry to handle vertical alignment of footer widgets.
	if ( is_active_sidebar( 'sidebar-4' ) )
		wp_enqueue_script( 'jquery-masonry' );

	wp_enqueue_script( 'blaze-script', get_template_directory_uri() . '/assets/js/functions.js', array( 'jquery' ), '20151204', true );

	wp_localize_script( 'blaze-script', 'screenReaderText', array(
		'expand'   => __( 'expand child menu', 'blazeblog' ),
		'collapse' => __( 'collapse child menu', 'blazeblog' ),
	) );
}
add_action( 'wp_enqueue_scripts', 'blazeblog_scripts' );

/**
 * Adds custom classes to the array of body classes.
 *
 * @since BlazeBlog 1.0
 *
 * @param array $classes Classes for the body element.
 * @return array (Maybe) filtered body classes.
 */
function blazeblog_body_classes( $classes ) {
	// Adds a class of custom-background-image to sites with a custom background image.
	if ( get_background_image() ) {
		$classes[] = 'custom-background-image';
	}

	// Adds a class of group-blog to sites with more than 1 published author.
	if ( is_multi_author() ) {
		$classes[] = 'group-blog';
	}

	// Adds a class of no-sidebar to sites without active sidebar.
	if ( ! is_active_sidebar( 'sidebar-1' ) ) {
		$classes[] = 'no-sidebar';
	}

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

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

/**
 * Converts a HEX value to RGB.
 *
 * @since BlazeBlog 1.0
 *
 * @param string $color The original color, in 3- or 6-digit hexadecimal form.
 * @return array Array containing RGB (red, green, and blue) values for the given
 *               HEX code, empty array otherwise.
 */
function blazeblog_hex2rgb( $color ) {
	$color = trim( $color, '#' );

	if ( strlen( $color ) === 3 ) {
		$r = hexdec( substr( $color, 0, 1 ).substr( $color, 0, 1 ) );
		$g = hexdec( substr( $color, 1, 1 ).substr( $color, 1, 1 ) );
		$b = hexdec( substr( $color, 2, 1 ).substr( $color, 2, 1 ) );
	} else if ( strlen( $color ) === 6 ) {
		$r = hexdec( substr( $color, 0, 2 ) );
		$g = hexdec( substr( $color, 2, 2 ) );
		$b = hexdec( substr( $color, 4, 2 ) );
	} else {
		return array();
	}

	return array( 'red' => $r, 'green' => $g, 'blue' => $b );
}

/**
 * Custom template tags for this theme.
 */
require_once( 'includes/theme-functions.php' );

/**
 * SASS compiler class
 */
require_once("includes/scssphp/wp.scss.inc.php");

/**
 * Customizer additions.
 */
require_once( 'includes/customizer.php' );

/**
 * Modifies tag cloud widget arguments to have all tags in the widget same font size.
 *
 * @since BlazeBlog 1.0
 *
 * @param array $args Arguments for tag cloud widget.
 * @return array A new modified arguments.
 */
function blazeblog_widget_tag_cloud_args( $args ) {
	$args['largest'] = 12;
	$args['smallest'] = 12;
	$args['unit'] = 'px';
	return $args;
}
add_filter( 'widget_tag_cloud_args', 'blazeblog_widget_tag_cloud_args' );

// get theme option value by option name
if( !function_exists('blazeblog_theme_option') ) {
    function blazeblog_theme_option( $option_name ) {
        global $blazeblog_options_var;
        $blazeblog_options = get_option($blazeblog_options_var);

        if ( isset( $blazeblog_options[$option_name] ) )
            return $blazeblog_options[$option_name];
        else
            return false;
    }
}

add_filter('embed_oembed_html', 'blazeblog_embed_oembed_html', 99, 4);
function blazeblog_embed_oembed_html($html, $url, $attr, $post_id) {
    return '<div class="responsive-wrapper">' . $html . '</div>';
}

add_filter( 'img_caption_shortcode', 'blazeblog_img_caption_shortcode', 10, 3 );
function blazeblog_img_caption_shortcode( $empty, $attr, $content ){
	$attr = shortcode_atts( array(
		'id'      => '',
		'align'   => 'alignnone',
		'width'   => '',
		'caption' => ''
	), $attr );

	if ( 1 > (int) $attr['width'] || empty( $attr['caption'] ) ) {
		return '';
	}

	if ( $attr['id'] ) {
		$attr['id'] = 'id="' . esc_attr( $attr['id'] ) . '" ';
	}

	$output  = '<div ' . $attr['id'] . 'class="wp-caption ' . esc_attr( $attr['align'] ) . '" style="max-width: ' . ( (int) $attr['width'] ) . 'px;">';
	$output .=  do_shortcode( $content );
	$output .=  '<p class="wp-caption-text">' . $attr['caption'] . '</p>';
	$output .=  '</div>';
    return $output;
}