<?php

/**
 * Functions file
 *
 * For some the main file in a theme. Here we display meta informaion and the
 * header information like menus and logo.
 *
 * Sets up the theme and provides some helper functions. Some helper functions
 * are used in the theme as custom template tags. Others are attached to action and
 * filter hooks in WordPress to change core functionality.
 *
 * @package WordPress
 * @subpackage Subtle
 */

// Include the TBM Framework
require_once ( 'inc/php/TBM_Main.php' );

// Register all the widgets
require_once ( 'inc/php/widgets/main.php' );

if( ! class_exists( 'TBM_Functions' ) ) :
class TBM_Functions {

	/**
	 * Call all loading functions for the theme. They will be started right after theme setup.
	 * 
	 * @since v1.0
	 */
	public function __construct() {

		// Run after instalation setup.
		$this->theme_setup();

		// Register actions using add_actions() custom function.
    	$this->add_actions();

	}

	/**
	 * Initial theme setup
	 * 
	 * Loading scripts and stylesheets. Register custom elements
	 * and functionality in the theme.
	 * 
	 * @uses get_template_directory_uri()
	 * @uses add_theme_support()
	 * @since v1.0.0
	 */
	public function theme_setup() {

		// Add after_setup_theme() for specific functions.
		// The action call is here, because it fits more just for the theme
		// setup, instead for all other actions during using of Subtle.
		add_action( 'after_setup_theme', array( $this, 'theme_setup_core' ) );

    	// Set content width for custom media information
    	if ( ! isset( $content_width ) ) $content_width = 900;

	}

	/**
	 * The core functionality that has to be registred after the theme is setted up
	 * 
	 * @since v1.0
	 */
	public function theme_setup_core() {

		// Add support for custom header images
		add_theme_support( 'custom-header' ) ;

		// Register the menus.
		register_nav_menus( array( 'primary' 	=> __( 'Main Menu', 'subtle' ) ) );

		// One of the required wordpress supports.
		add_theme_support( 'automatic-feed-links' );

		// Custom background color
		add_theme_support( "custom-background", array(
			'default-color'          => 'fff',
			'default-position-x'     => 'center',
			'default-repeat'         => 'no-repeat',
		)); 

		// Custom header support.
		add_theme_support( 'custom-header', array(
			'default-text-color' 	=> 'fff',
			'width' 				=> 1920,
			'height'	 			=> 300,
		) );

		//  Support post-thubnails as well!
		add_theme_support( 'post-thumbnails' );
		set_post_thumbnail_size( 'large', 1170, 750, true );
		set_post_thumbnail_size( 'medium', 750, 400, true );
		set_post_thumbnail_size( 'small', 250, 250, true );

		// Theme localization.
		load_theme_textdomain( 'subtle' );
		load_theme_textdomain( 'subtle' , get_template_directory() . '/languages' );

		// Editor style : Throught this, you should make the 
		// wordpress editor look like posts. (in css manner)
		add_editor_style( get_template_directory_uri() . 'editor-style.css');

		// Custom editor style
		add_editor_style( 'css/editor.css' );
	}

	/**
	 * Add actions and filters in wordpress theme. All the actions will be here.
	 * 
	 * @uses add_action()
	 * @uses add_filter()
	 * @since v1.0
	 */
	public function add_actions() {

		// Register all scripts and styles
		add_action( 'wp_enqueue_scripts', array($this, 'load_scripts_and_styles') );

		// Insert custom header css for styling the header dynamicly.
		add_action( 'wp_head', array( $this, 'custom_header_css' ) );

		// Register our Widgets
		add_action( 'widgets_init', array( $this, 'widgets_init' ) );

		// Enqueue comment-reply
		add_action( 'comment_form_before', array( $this, 'enqueue_comments_reply' ) );

		// Post title filter.
		add_filter( "wp_title", array( $this, "page_title" ) );

		// Change excerpt lenght
		add_filter( 'excerpt_length', array( $this, 'excerpt_length' ), 10 );

		// Add read more link instead of [...]
		add_filter( 'excerpt_more', array( $this, 'excerpt_more' ) );

	}

	/**
	 * Loading scripts and stylesheets for Innocence
	 * The order of initialising bootstrap css files is important
	 * for the theme responsivness work proerly.
	 * 
	 * @uses wp_enqueue_style()
	 * @since v1.0
	 */
	public function load_scripts_and_styles() {

		// Include Bootstrap css/js framework.
		wp_enqueue_style( 'bootstrap', get_template_directory_uri() . '/css/bootstrap.min.css', array() );

		// The font-awesome icons
		wp_enqueue_style( 'font-awesome' , get_template_directory_uri() . '/css/font-awesome.min.css', array() );

		// Google Source Sans Pro Font
		wp_enqueue_style( 'google-source-sans-pro' , '//fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,700,400italic', array() );

		// Get the main stylesheet for the theme.
		wp_enqueue_style( 'stylesheet', get_stylesheet_uri() );

		wp_register_script( 'bootstrap-js', get_template_directory_uri() . '/js/bootstrap.min.js' );

		wp_enqueue_script( 'bootstrap-js' );
		
	}

	/*
	 * @since v1.1
	 */
	public function enqueue_comments_reply() {
		if( get_option( 'thread_comments' ) )  {
			wp_enqueue_script( 'comment-reply' );
		}
	}

	public function widgets_init() {
		register_sidebar( array(
			'name'          => __( 'Home Page Widget Area', 'subtle' ),
			'id'            => 'sidebar-1',
			'description'   => __( 'Appears on home page', 'subtle' ),
			'before_widget' => '<aside id="%1$s" class="widget %2$s">',
			'after_widget'  => '</aside>',
			'before_title'  => '<h3 class="widget-title">',
			'after_title'   => '</h3>',
		) );

		 /**
		 * Registration for the widgets. Full list of the widgets offered by TBM framework:
		 * - RecentPosts
		 * - RecentPostsBig
		 *
		 * (choose which to include)
		 * The widgets are included by writing 	register_widget( 'TheWidgetName' );
		 */
		register_widget( 'RecentPosts' );
		register_widget( 'RecentPostsBig' );

	}

	/**
	 * Write the theme title. It doesnt return anything.
	 * The simple name comes, because its very natural when call it:
	 * Header::title();
	 * 
	 * @uses get_bloginfo()
	 * @uses wp_title()
	 * @uses is_home()
	 * @uses is_front_page();
	 * 
	 * @package Subtle
	 * @since  v1.0.0
	 */
	public function page_title( $title, $sep = ' | ' ) {
		global $page, $paged;

		if ( is_feed() )
			return $title;

		$site_description = get_bloginfo( 'description' );

		$filtered_title = $title . get_bloginfo( 'name' );
		$filtered_title .= ( ! empty( $site_description ) && ( is_home() || is_front_page() ) ) ? $sep . $site_description: '';
		$filtered_title .= ( 2 <= $paged || 2 <= $page ) ? $sep . sprintf( __( 'Page %s', 'subtle' ), max( $paged, $page ) ) : '';

		return $filtered_title;
	}

	/**
	 * Change the default valued for length of the post excerpt.
	 * @param  	int $length The length of desired excerpt. (for all pages and all calls)
	 * @return 	int         Hardcoded value of the excerpt length
	 * @since 	v1.0.0
	 */
	public function excerpt_length( $length ) {
		return $length;
	}

	/**
	 * Change the default valued for after the post excerpt.
	 * @param  	string $more Not used vcariable, wanted from WP
	 * @return 	string       Link for the post.
	 * @since 	v1.0.0
	 */
	public function excerpt_more( $more ) {
		return ' <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">Read More</a>';
	}

	/**
	 * Add custom style for the header. 
	 * It look quite hard to read, but the main logic is to setup
	 * custom styling when there is custom header image set.
	 * 
	 * @return void
	 * @since v1.5.4
	 */
	function custom_header_css() {
		$header_image = get_header_image();

		$output = '<style type="text/css">';

		// These are the styles when there is custom image
		if ( get_header_image() ) {
			$output .= "#site-branding {";
			$output .= "background: url('{$header_image}') center no-repeat !important;";
			$output .= "height: ". get_custom_header()->height . "px !important; }";

			$output .= "#site-header {position: relative;margin-bottom: 50px;}";
			$output .= "#primary-navigation a:hover, #primary-navigation .current_page_item {background: black !important}";
			$output .= "#primary-navigation .container {background: rgba(0, 0, 0, 0.2);}";
			$output .= "#primary-navigation { top: 150px; width: 100%; position: absolute; background-color: transparent !important; }";
		}

		$output .= "#site-branding .site-title a,";
		$output .= "#site-branding .site-description {";

		// Check if the site title text should be displayed.
		if( get_header_textcolor() == 'blank' ) {
			$output .= 'visibility: hidden;'; 
		}
		else {
			$output .= 'color: #' . get_header_textcolor() . ' !important';
		}

		// Close the title and desc styles.
		$output .= "}";

		// Close the style tag
		$output .= '</style>';

		// Print the final styles.
		echo $output;
	}

}

$TBM_Functions = new TBM_Functions;
endif;