<?php
/* 	SunRain Theme's Functions
	Copyright: 2012-2020, D5 Creation, www.d5creation.com
	Based on the Simplest D5 Framework for WordPress
	Since SunRain 1.0
*/
	
	require_once ( trailingslashit(get_template_directory()) . 'inc/customize.php' );
	function sunrain_about_page() { 
	add_theme_page( esc_html__('SunRain Options', 'sunrain'), esc_html__('SunRain Options', 'sunrain'), 'edit_theme_options', 'theme-about', 'sunrain_theme_about' ); 
	}
	add_action('admin_menu', 'sunrain_about_page');
	function sunrain_theme_about() {  require_once ( trailingslashit(get_template_directory()) . 'inc/theme-about.php' ); }		
	
	function sunrain_setup() {
	register_nav_menus( array( 'main-menu' => esc_html__('Main Menu', 'sunrain'), 'top-menu' => esc_html__('Top Menu', 'sunrain') ) );
	load_theme_textdomain( 'sunrain', get_template_directory() . '/languages' );
//	Set the content width based on the theme's design and stylesheet.
	global $content_width; if ( ! isset( $content_width ) ) $content_width = 584;
	add_theme_support( 'title-tag' );


// 	Tell WordPress for the Feed Link
	add_theme_support( 'title-tag' );
	add_theme_support( 'automatic-feed-links' );
	add_theme_support( 'yoast-seo-breadcrumbs' );
	add_theme_support('html5', array('search-form', 'comment-form', 'comment-list', 'gallery', 'caption', 'script', 'style', ));
	
// 	This theme uses Featured Images (also known as post thumbnails) for per-post/per-page Custom Header images
	
	add_theme_support( 'post-thumbnails' );
	set_post_thumbnail_size( 150, 150, true ); // default Post Thumbnail dimensions (cropped)
	// additional image sizes
	// delete the next line if you do not need additional image sizes
	add_image_size( 'category-thumb', 300, 9999 ); //300 pixels wide (and unlimited height)

	
// 	WordPress 3.4 Custom Background Support	
	$sunrain_custom_background = array( 'default-color' => 'ffffff', 'default-image'          => '', );
	add_theme_support( 'custom-background', $sunrain_custom_background );
	
// 	WordPress 3.4 Custom Header Support				
	$sunrain_custom_header = array(
	'default-image'          => '',
	'random-default'         => false,
	'width'                  => 300,
	'height'                 => 90,
	'flex-height'            => false,
	'flex-width'             => false,
	'default-text-color'     => '000000',
	'header-text'            => false,
	'uploads'                => true,
	'wp-head-callback' 		 => '',
	'admin-head-callback'    => '',
	'admin-preview-callback' => '',
	);
	add_theme_support( 'custom-header', $sunrain_custom_header );
	}
	add_action( 'after_setup_theme', 'sunrain_setup' );

// 	Functions for adding script
	function sunrain_enqueue_scripts() { 
	wp_enqueue_style('sunrain-style', get_stylesheet_uri(), false );
	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {  wp_enqueue_script( 'comment-reply' );  }
		
	wp_enqueue_style( 'dashicons' );
	wp_enqueue_script( 'sunrain-menu-style', get_template_directory_uri(). '/js/menu.js', array( 'jquery' ) );
	if ( sunrain_get_option('header-fixed', '1') != '0' ): wp_enqueue_script( 'sunrain-fixed-header', get_template_directory_uri(). '/js/fixedheader.js', array( 'jquery' )); endif;
	
	wp_enqueue_script( 'sunrain-html5', get_template_directory_uri().'/js/html5.js');
    wp_script_add_data( 'sunrain-html5', 'conditional', 'lt IE 9' );
	
	wp_register_style('sunrain-gfonts1', '//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800', false );
	wp_enqueue_style('sunrain-gfonts1');
	if (is_front_page()): 
	wp_enqueue_script( 'sunrain-main-slider', get_template_directory_uri() . '/js/jquery.fractionslider.min.js', array( 'jquery' ) );
	wp_enqueue_style('sunrain-main-slider-css', get_template_directory_uri(). '/css/slider.css' );
	endif;
	if ( sunrain_get_option('responsive', '0') == '1' ) : wp_enqueue_style('sunrain-responsive', get_template_directory_uri(). '/style-responsive.css' ); endif;
	}
	add_action( 'wp_enqueue_scripts', 'sunrain_enqueue_scripts' );

// 	Functions for adding script to Admin Area
	function sunrain_admin_style($hook) { 
		if ( 'appearance_page_theme-about' != $hook ) { return; }
		wp_enqueue_style( 'sunrain_admin_css', get_template_directory_uri() . '/inc/admin-style.css', false ); 
	}
	add_action( 'admin_enqueue_scripts', 'sunrain_admin_style' );

//	Enqueue Customizer stylesheet
	function sunrain_customizer_style() {
		wp_enqueue_style( 'sunrain-customizer-css', get_template_directory_uri() . '/inc/customizer-style.css', false );
	}
	add_action( 'customize_controls_print_styles', 'sunrain_customizer_style' );


// 	Functions for adding some custom code within the head tag of site
	function sunrain_custom_code() {?>
	
	<style type="text/css">
	.site-title a, 
	.site-title a:active, 
	.site-title a:hover { color: #<?php echo get_header_textcolor(); ?>; }
	</style>
			
<?php 
	
	}
	
	add_action('wp_head', 'sunrain_custom_code');
	
//	function tied to the excerpt_more filter hook.
	function sunrain_excerpt_length( $length ) {
	global $blExcerptLength;
	if ($blExcerptLength) {
    return $blExcerptLength;
	} else {
    return 50; //default value
    } }
	add_filter( 'excerpt_length', 'sunrain_excerpt_length', 999 );
	
	function sunrain_excerpt_more($more) {
    global $post;
	return '<a href="'. get_permalink($post->ID) . '" class="read-more">'.esc_html__('Read More', 'sunrain').'</a>';
	}
	add_filter('excerpt_more', 'sunrain_excerpt_more');
	
	// Content Type Showing
	function sunrain_content() {
	if (( sunrain_get_option('contype', '1') != '2' ) || is_page() || is_single() ) : the_content('<span class="read-more">'.esc_html__('Read More', 'sunrain').'</span>');
	else: the_excerpt();
	endif;	
	}
	
	function sunrain_creditline() { echo '<span class="credit">| SunRain Theme by: <a href="'. esc_url('https://d5creation.com') .'" target="_blank">D5 Creation</a> | Powered by: <a href="http://wordpress.org" target="_blank">WordPress</a></span>'; }

	//Multi-level pages menu  
	function sunrain_page_menu() {
		echo '<div id="mainmenuparent" class="mainmenu-parent"><ul id="main-menu-items-con" class="main-menu-items">'; wp_list_pages(array('sort_column'  => 'menu_order, post_title', 'title_li'  => '' )); echo '</ul></div>';
	}

//	Get our wp_nav_menu() fallback, wp_page_menu(), to show a home link
	function sunrain_page_menu_args( $args ) {
	$args['show_home'] = true;
	return $args;
	}
	add_filter( 'wp_page_menu_args', 'sunrain_page_menu_args' );
	
// 	Post Date design
	function sunrain_post_date() {
	echo '<div class="post-date"><span class="post-month"> ' . get_the_time('M') . ' </span> <span class="post-day"> ' . get_the_time('d') . ' </span> <span class="post-year"> ' . get_the_time('Y') . ' </span></div>';
	}
	
// 	Post Meta design
	function sunrain_post_meta() { ?>
	<div class="post-meta"><?php edit_post_link( esc_html__( 'Edit', 'sunrain' ), '', '', '', 'sunrainpmeta post-edit dashicons dashicons-edit' ); ?><span class="sunrainpmeta post-author dashicons dashicons-admin-users"><?php the_author_posts_link(); ?></span><?php the_tags('<span class="sunrainpmeta post-tag-icon dashicons dashicons-tag">' , ',','</span>'); ?><span class="sunrainpmeta post-category dashicons dashicons-category"><?php the_category(', '); ?></span><span class="sunrainpmeta post-comments dashicons dashicons-admin-comments"> <?php comments_popup_link(esc_html__('No Comments', 'sunrain').' &#187;', esc_html__('One Comment', 'sunrain').' &#187;', '% '.esc_html__('No Comment', 'sunrain').' &#187;'); ?> </span>
	</div> 
	
	<?php
	}
	
//	Registers the Widgets and Sidebars for the site
	function sunrain_widgets_init() {

	register_sidebar( array(
		'name' => esc_html__('Primary Sidebar','sunrain'), 
		'id' => 'sidebar-1',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget' => "</aside>",
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );

	register_sidebar( array(
		'name' => esc_html__('Secondary Sidebar','sunrain'),
		'id' => 'sidebar-2',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget' => "</aside>",
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );
	 
	register_sidebar( array(
		'name' => esc_html__('Footer Area One','sunrain'),
		'id' => 'sidebar-3',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget' => "</aside>",
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );
	    
	register_sidebar( array(
		'name' =>  esc_html__('Footer Area Two','sunrain'),
		'id' => 'sidebar-4',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget' => "</aside>",
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );

	register_sidebar( array(
		'name' => esc_html__('Footer Area Three','sunrain'),
		'id' => 'sidebar-5',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget' => "</aside>",
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );
	
	register_sidebar( array(
		'name' => esc_html__('Footer Area Four','sunrain'),
		'id' => 'sidebar-6',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget' => "</aside>",
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );
	
	}
	add_action( 'widgets_init', 'sunrain_widgets_init' );
	
	add_filter('the_title', 'sunrain_title');
	function sunrain_title($title) {
        if ( '' == $title ) {
            return esc_html__('(Untitled)', 'sunrain');
        } else {
            return $title;
        }
    }
