<?php
/**
 * Equable Lite functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 * @package Equable_Lite
 */

// Declare latest theme version
$GLOBALS['equable_lite_theme_version'] = '1.0.10'; 
 
if ( ! function_exists( 'equable_lite_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.
	 */
	function equable_lite_setup() {
		/*
		 * Make theme available for translation.
		 * Translations can be filed in the /languages/ directory.
		 * If you're building a theme based on Equable Lite, use a find and replace to change 'equable-lite' to the name of your theme in all the template files.
		 */
		load_theme_textdomain( 'equable-lite', 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 https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
		 */
		add_theme_support( 'post-thumbnails' );
	
		// This theme uses wp_nav_menu() in one location.
		register_nav_menus( array(
			'top' => esc_html__( 'Top Menu', 'equable-lite' ),
			'social' => esc_html__( 'Social Menu', 'equable-lite' ),
			'footer' => esc_html__( 'Footer Menu', 'equable-lite' ),
		) );

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

		// Set up the WordPress core custom background feature.
		add_theme_support( 'custom-background', apply_filters( 'equable_lite_custom_background_args', array(
			'default-color' => 'ffffff',
			'default-repeat'         => 'no-repeat',
			'default-attachment'     => 'fixed',
			'default-size'         => 'cover',
			'default-image' => esc_url(get_template_directory_uri()) . '/images/default-bg.jpg',
		) ) );

		// Add theme support for selective refresh for widgets.
		add_theme_support( 'customize-selective-refresh-widgets' );

		/**
		 * Add support for core custom logo.
		 * @link https://codex.wordpress.org/Theme_Logo
		 */
		add_theme_support( 'custom-logo', array(
			'width'       => 450,
			'height'      => 125,
			'flex-width'  => true,
			'flex-height' => true,
		) );	

		// Set the default content width.
		$GLOBALS['content_width'] = 1140;
	
	}
endif;
add_action( 'after_setup_theme', 'equable_lite_setup' );

/**
 * Set 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
 */

	function equable_lite_content_width() {
		$content_width = $GLOBALS['content_width'];
	 
		// Check if is single post and there is no sidebar.
		if ( is_active_sidebar( 'pageleft'  ) || is_active_sidebar( 'blogleft' ) || is_active_sidebar( 'blogright' ) || is_active_sidebar( 'blogright' ) ) {
			$content_width = 855;
		}
		
	  $GLOBALS['content_width'] = apply_filters( 'equable_lite_content_width', $content_width );
	}
	add_action( 'template_redirect', 'equable_lite_content_width', 0 );

	//	Move the Continue Reading link outside of the paragraph
	if ( ! function_exists( 'equable_lite_move_more_link' ) ) :
		function equable_lite_move_more_link($link) {
				$link = '<p class="more-link-wrap">'.$link.'</p>';
				return $link;
			}
		add_filter('the_content_more_link', 'equable_lite_move_more_link');
	endif;
	
// Custom excerpt size
	function equable_lite_custom_excerpt_length( $length ) { 
	$equable_lite_excerpt_size = esc_attr(get_theme_mod( 'equable_lite_excerpt_size', '55' ));
	if ( is_admin() ) :
		return 55;		
	else: 	
		return $equable_lite_excerpt_size;
	endif;
	}
	add_filter( 'excerpt_length', 'equable_lite_custom_excerpt_length', 999 );

//	Add Read More to the excerpts
	function equable_lite_excerpt_more($more) {
		return '&hellip;';
	}
	add_filter('excerpt_more', 'equable_lite_excerpt_more', 21 );

// show or hide the read more link
	if( esc_attr(get_theme_mod( 'equable_lite_show_excerpt_readmore', true ) ) ) {	
		function equable_lite_excerpt_more_link( $excerpt ) {
		$post = get_post();
		$excerpt .= '<p><a class="more-link" href="'. esc_url(get_permalink($post->ID)) . '">' . esc_html__( 'Continue reading', 'equable-lite' ) . '</a></p>';
		return $excerpt;
		}
	add_filter( 'the_excerpt', 'equable_lite_excerpt_more_link', 21 );	
	}	

/*
 *  Remove the Inline CSS for the WP Gallery
 *  Thanks to http://wpcrux.com/blog/wordpress-default-gallery-css/
 */
	add_filter( 'use_default_gallery_style', '__return_false' );
	
/**
 * Handles JavaScript detection. Adds a `js` class to the root `<html>` element when JavaScript is detected.
 * @since Equable Lite 1.0.0
 */
	function equable_lite_javascript_detection() {
		echo "<script>(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);</script>\n";
	}
	add_action( 'wp_head', 'equable_lite_javascript_detection', 0 );


/* Display custom color CSS
 * Special thanks to Twenty Seventeen theme
 */

	function equable_lite_colors_css_wrap() {
		if ( 'custom' !== get_theme_mod( 'colorscheme' ) && ! is_customize_preview() ) {
			return;
		}
		require_once( get_parent_theme_file_path( '/inc/color-patterns.php' ) );
		$hue = absint( get_theme_mod( 'colorscheme_hue', 3 ) );
	?>
		<style type="text/css" id="custom-theme-colors" <?php if ( is_customize_preview() ) { echo 'data-hue="' . esc_attr( $hue ) . '"'; } ?>>
			<?php echo esc_html(equable_lite_custom_colors_css()); ?>
		</style>
	<?php }
	add_action( 'wp_head', 'equable_lite_colors_css_wrap' );



/**
 * Register custom fonts. families:
 */
function equable_lite_fonts_url() {
	$fonts_url = '';

	/*
	 * Translators: If there are characters in your language that are not
	 * supported by Cinzel, translate this to 'off'. Do not translate
	 * into your own language.
	 */
	$cinzel = _x( 'on', 'Cinzel font: on or off', 'equable-lite' );

	if ( 'off' !== $cinzel ) {
		$font_families = array();

		$font_families[] = 'Cinzel:400,700';

		$query_args = array(
			'family' => urlencode( implode( '|', $font_families ) ),
			'subset' => urlencode( 'latin,latin-ext' ),
		);

		$fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
	}

	return esc_url_raw( $fonts_url );
}


/**
 * Add preconnect for Google Fonts.
 * @param array  $urls  URLs to print for resource hints.
 * @param string $relation_type  The relation type the URLs are printed.
 * @return array $urls  URLs to print for resource hints.
 */
function equable_lite_resource_hints( $urls, $relation_type ) {
	if ( wp_style_is( 'equable-lite-fonts', 'queue' ) && 'preconnect' === $relation_type ) {
		$urls[] = array(
			'href' => 'https://fonts.gstatic.com',
			'crossorigin',
		);
	}

	return $urls;
}
add_filter( 'wp_resource_hints', 'equable_lite_resource_hints', 10, 2 );

	
// Enqueue scripts and styles.
function equable_lite_scripts() {
	
	global $equable_lite_theme_version;

	// Font Awesome
	wp_enqueue_style( 'font-awesome', get_template_directory_uri() . '/css/font-awesome.css', '', '4.7.0' );
	
	// Load the theme stylesheet
	wp_enqueue_style( 'equable-lite-fonts', equable_lite_fonts_url(), array(), null );
	wp_enqueue_style( 'equable-lite-style', get_stylesheet_uri() );
	

	// Load the dark colorscheme.
	if ( 'dark' === get_theme_mod( 'colorscheme', 'default' ) || is_customize_preview() ) {
		wp_enqueue_style( 'equable-lite-colors-dark', get_theme_file_uri( '/css/colors-dark.css' ), array( 'equable-lite-style' ), $equable_lite_theme_version  );
	}
	
	// Load the skip-link-focus-fix script
	wp_enqueue_script( 'equable-lite-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20151215', true );

	// Menu and accessibility functionality
	wp_enqueue_script( 'equable-lite-script', get_template_directory_uri() . '/js/navigation.js', array( 'jquery' ), '20151215', true );
	wp_localize_script( 'equable-lite-script', 'equablelitescreenReaderText', array(
		'expand'   => __( 'expand child menu', 'equable-lite' ),
		'collapse' => __( 'collapse child menu', 'equable-lite' ),
	) );
	
	// Load comments
	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}
	
}
add_action( 'wp_enqueue_scripts', 'equable_lite_scripts' );

// Add editor style.
function equable_lite_add_editor_styles() {
	add_editor_style( array(
		'css/equable-lite-editor.css',
		equable_lite_fonts_url(),
		get_template_directory_uri() . '/css/font-awesome.css',
	) );
}
add_action( 'init', 'equable_lite_add_editor_styles' );


// Custom template tags for this theme.
require get_parent_theme_file_path( '/inc/template-tags.php' );

// Functions which enhance the theme by hooking into WordPress.
require get_parent_theme_file_path( '/inc/template-functions.php' );

// Customizer additions.
require get_parent_theme_file_path( '/inc/customizer.php' );

// Sidebars
require get_parent_theme_file_path( '/inc/sidebars.php' );

// Inline Styles
require get_parent_theme_file_path( '/inc/inline-styles.php' );

// Theme Info Page
require get_parent_theme_file_path( '/inc/theme-info/equable-lite-info.php' );

// Load Jetpack compatibility file.
if ( defined( 'JETPACK__VERSION' ) ) {
	require get_template_directory() . '/inc/jetpack.php';
}

