<?php

require get_template_directory() . '/inc/customizer/customizer.php';

// Custom template functions for this theme.
require get_template_directory() . '/inc/template-functions.php';

/*-----------------------------------------------------------------------------------*/
/* Sets up the content width value based on the theme's design and stylesheet.
/*-----------------------------------------------------------------------------------*/
if ( ! isset( $content_width ) ) {
	$content_width = 832;
}

/*-----------------------------------------------------------------------------------*/
/* Sets up theme defaults and registers the various WordPress features that
/* PlusBlog supports.
/*-----------------------------------------------------------------------------------*/
if ( ! function_exists( 'bloomy_theme_setup' ) ) :
function bloomy_theme_setup() {
    
	// Adds RSS feed links to <head> for posts and comments.
	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' );
	
	// Register WordPress Custom Menus
	add_theme_support( 'menus' );
	register_nav_menu( 'main-menu', esc_html__( 'Main Menu', 'bloomy' ) );
	
	// Register Post Thumbnails
	add_theme_support( 'post-thumbnails' );
	set_post_thumbnail_size( 150, 150, true );
	add_image_size( 'bloomy-slider', 1170, 546, true );
	add_image_size( 'bloomy-featured', 875, 430, true );
	add_image_size( 'bloomy-related', 265, 145, true );
	add_image_size( 'bloomy-widgetthumb-big', 276, 125, true );
	add_image_size( 'bloomy-widgetthumb', 70, 70, true );
    
    /*
	 * Enable support for custom logo.
	 *
	 */
    add_theme_support( 'custom-logo', array(
       'height'      => 96,
       'width'       => 278,
       'flex-width'  => true,
    ) );
	
	/*
	 * 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_editor_style( array( 'css/editor-style.css') );
    
	// Load Localization Files
	$lang_dir = get_template_directory() . '/languages';
	load_theme_textdomain( 'bloomy', $lang_dir );
}
endif; // bloomy_theme_setup
add_action( 'after_setup_theme', 'bloomy_theme_setup' );

/*-----------------------------------------------------------------------------------*/
/*	Add Stylesheets
/*-----------------------------------------------------------------------------------*/
function bloomy_stylesheets_scripts() {
    
	// Add Stylesheets
    //------------------------------------------------//
	
	// Default Stylesheet
	wp_enqueue_style( 'bloomy-style', get_stylesheet_uri(), array(), null, 'all' );
    
    $bloomy_settings_css = bloomy_customizer_apply_css();
    wp_add_inline_style('bloomy-style', $bloomy_settings_css);
	
    // Font Awesome Icons
    wp_enqueue_style( 'font-awesome', get_template_directory_uri() . '/css/font-awesome.min.css', array(), null, 'all' );
	
    // Responsive
    wp_enqueue_style( 'bloomy-responsive', get_template_directory_uri() . '/css/responsive.css', array(), null, 'all' );
	
	if ( is_rtl() ) {
		// Responsive
		wp_enqueue_style( 'bloomy-rtl', get_template_directory_uri() . '/rtl.css' );
	}

	// Add JavaScripts
    //------------------------------------------------//
    wp_enqueue_script( 'jquery' );
	if ( is_singular() ) wp_enqueue_script( 'comment-reply' );
	
	// Sticky Menu
	$bloomy_sticky_menu = bloomy_theme_mod('sticky_menu');
	if ( $bloomy_sticky_menu == 'enable' ) {
		wp_enqueue_script( 'bloomy-stickymenu', get_template_directory_uri() . '/js/stickymenu.js', array( 'jquery' ), null, true );
	}
	
	// Owl Carousel
    wp_enqueue_script( 'jquery-owl-carousel', get_template_directory_uri() . '/js/owl.carousel.js', array( 'jquery' ), '1.0', true );
    
	// Required jQuery Scripts
    wp_enqueue_script( 'bloomy-theme-scripts', get_template_directory_uri() . '/js/theme-scripts.js', array( 'jquery' ), null, true );
}
add_action( 'wp_enqueue_scripts', 'bloomy_stylesheets_scripts', 99 );

/*-----------------------------------------------------------------------------------*/
/*	Add Admin Scripts
/*-----------------------------------------------------------------------------------*/
function bloomy_admin_scripts( $hook ) {
    $current_screen = get_current_screen();

    if ( 'customize' != $current_screen->base ) {
        return;
    }
    
	wp_register_style( 'admin-css', get_template_directory_uri() . '/css/admin-styles.css' );
	wp_enqueue_style( 'admin-css' );
}
add_action( 'admin_enqueue_scripts', 'bloomy_admin_scripts' );

/*-----------------------------------------------------------------------------------*/
/*	Load Widgets
/*-----------------------------------------------------------------------------------*/
// Theme Functions
include("inc/widgets/widget-author-info.php"); // Author Info Widget
include("inc/widgets/widget-cat-posts.php"); // Category Posts
include("inc/widgets/widget-recent-posts.php"); // Recent Posts
include("inc/widgets/widget-social.php"); // Social Widget
	
/*-----------------------------------------------------------------------------------*/
/*	Exceprt Length
/*-----------------------------------------------------------------------------------*/
// Limit the Length of Excerpt
function bloomy_excerpt_length( $length ) {
    $length = bloomy_theme_mod( 'excerpt_length' );
    
    return is_admin() ? 50 : intval( $length );
}
add_filter( 'excerpt_length', 'bloomy_excerpt_length', 999 );

// Remove […] string
function bloomy_excerpt_more( $more ) {
	return is_admin() ? '[...]' : '';
}
add_filter('excerpt_more', 'bloomy_excerpt_more');

/*-----------------------------------------------------------------------------------*/
/*	Register Theme Widgets
/*-----------------------------------------------------------------------------------*/
function bloomy_widgets_init() {
	register_sidebar(array(
		'name'          => esc_html__('Primary Sidebar', 'bloomy'),
		'id'            => 'sidebar-1',
		'before_widget' => '<div class="widget sidebar-widget %2$s">',
		'after_widget'  => '</div>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	));
    $bloomy_footer_columns = get_theme_mod('footer_layout','4');
    for ( $i = 1; $i <= $bloomy_footer_columns; $i++ ) {
        register_sidebar( array(
            /* translators: %s: footer widget number */
            'name'          => sprintf( esc_html__( 'Footer %s','bloomy' ), $i ),
            'id'            => 'footer-' . $i,
            'description'   => esc_html__( 'This widget area appears on footer of theme.', 'bloomy' ),
            'before_widget' => '<div id="%1$s" class="widget %2$s">',
            'after_widget'  => '</div>',
            'before_title'  => '<h3 class="widget-title">',
            'after_title'   => '</h3>'
        ) );
    }
}
add_action( 'widgets_init', 'bloomy_widgets_init' );

/*-----------------------------------------------------------------------------------*/
/*	Breadcrumb
/*-----------------------------------------------------------------------------------*/
function bloomy_breadcrumb() {
	if (!is_home()) { ?>
		<span itemtype="http://schema.org/ListItem" itemscope="" itemprop="itemListElement" class="breadcrumb-item">
			<a itemprop="item" href="<?php echo esc_url( home_url() ); ?>">
	        	<span itemprop="name">
					<?php echo esc_html__( 'Home','bloomy' ); ?>
				</span>
			</a>
		</span>
		<?php if (is_category() || is_single()) { ?>
			&nbsp;&nbsp;/&nbsp;&nbsp;
            <span itemtype="http://schema.org/ListItem" itemscope="" itemprop="itemListElement" class="breadcrumb-item">
            	<span itemprop="item">
					<?php the_category(' &bull; '); ?>
            	</span>
            </span>
			<?php if (is_single()) { ?>
				&nbsp;&nbsp;/&nbsp;&nbsp;
                <span itemtype="http://schema.org/ListItem" itemscope="" itemprop="itemListElement" class="breadcrumb-item">
                	<span itemprop="item">
						<?php the_title(); ?>
                	</span>
                </span>
			<?php } ?>
		<?php } elseif (is_page()) { ?>
			&nbsp;&nbsp;/&nbsp;&nbsp;
			<?php the_title(); ?>
		<?php }
	}
}

/*-----------------------------------------------------------------------------------*/
/*	Add a pingback url auto-discovery header for singularly identifiable articles.
/*-----------------------------------------------------------------------------------*/
function bloomy_pingback_header() {
	if ( is_singular() && pings_open() ) {
		printf( '<link rel="pingback" href="%s">' . "\n", get_bloginfo( 'pingback_url' ) );
	}
}
add_action( 'wp_head', 'bloomy_pingback_header' );

/*-----------------------------------------------------------------------------------*/
/*	Custom theme mod functions
/*-----------------------------------------------------------------------------------*/
function bloomy_theme_mod( $name ) {
    global $bloomy_theme_defaults;

    return get_theme_mod( $name, $bloomy_theme_defaults[ $name ] );
}
?>