<?php
/**
 * hgwmag functions and definitions
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 * @subpackage hgwmag
 */

/**
 * #1. Table of Contents:
 * #2. Theme Support
 * #3. Register Styles
 * #4. Register Menus
 * #5. Register Sidebars
 * #6. Excerpt Customize
 * #7. Theme Options
 * #8. Required Files
*/


/*
#=========================================================================
#   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 hgwmag_theme_support() {

	// Add default posts and comments RSS feed links to head.
	add_theme_support( 'automatic-feed-links' );

	// Set content-width.
	global $content_width;
	if ( ! isset( $content_width ) ) {
		$content_width = 900;
	}

	// 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' );

	// Custom logo.
	add_theme_support(
		'custom-logo',
		array(
			'height'      => 100,
			'flex-height' => false,
			'flex-width'  => true,
		)
	);


	// 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' );

	// 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',
			'script',
			'style',
			'navigation-widgets',
		)
	);

	// Make theme available for translation.
	// Translations can be filed in the /languages/ directory.
	load_theme_textdomain( 'hgwmag', get_template_directory() . '/languages' );

	// Add support for full and wide align images.
	add_theme_support( 'align-wide' );

	// Add support for responsive embeds.
	add_theme_support( 'responsive-embeds' );

}
add_action( 'after_setup_theme', 'hgwmag_theme_support' );


/*
#=========================================================================
#   Register and Enqueue Script & Styles.
#=========================================================================
*/
function hgwmag_theme_scripts(){

		// Generate Theme Version
		$theme_version = wp_get_theme()->get( 'Version' );
		// Enqueue Styles
		wp_enqueue_style( 'hgwmag-style', get_stylesheet_uri(), array(), $theme_version );
		wp_enqueue_style( 'hgwmag-fontawesome', get_template_directory_uri() . '/assets/css/fontawesome.css', array(), null  );

		// Enqueue Scripts
		wp_enqueue_script('jquery');
		wp_enqueue_script( 'hgwmag-js', get_template_directory_uri() . '/assets/js/hgw-scripts.js', array('jquery'), null, true);
		// Enqueue Front-Page Scripts & Styles
		if ( is_front_page() ) {
			$front_page_top_section = get_theme_mod('hgwmag-show-homepage-top-section', 1 );
			$front_page_tiny_slideshow = get_theme_mod('hgwmag-show-fr-posts-column-5', 1 );
			$page_number = (get_query_var('paged')) ? get_query_var('paged') : 1;
			if ( ( $front_page_top_section == 1 || $front_page_tiny_slideshow == 1 ) && $page_number == 1 ) {
				wp_enqueue_style( 'hgwmag-slick-css', get_template_directory_uri() . '/assets/slick-carousel/slick.css', array(), null  );
				wp_enqueue_script( 'hgwmag-slick-js', get_template_directory_uri() . '/assets/slick-carousel/slick.min.js', array('jquery'), null, true);
				wp_enqueue_script( 'hgwmagtheme-slick', get_template_directory_uri() . '/assets/slick-carousel/hgw-slick.js', array('jquery'), null, true);
			}
		}
		// Enqueue Rtl Language Scripts & Styles
		if ( is_rtl() ) {
				wp_enqueue_style( 'hgwmag-rtl-style', get_template_directory_uri() . '/assets/css/rtl.css', array(), $theme_version  );
		}
		// Enqueue Singular-Page Scripts & Styles
		if ( is_singular() ) {
				wp_enqueue_script( "comment-reply" );
		}

}
add_action( 'wp_enqueue_scripts', 'hgwmag_theme_scripts' );


/*
#=========================================================================
#   Register navigation menus uses wp_nav_menu in two places.
#=========================================================================
*/
function hgwmag_menus() {

	$locations = array(
		'tiny-menu'  => __( 'Tiny Menu', 'hgwmag' ),
		'primary-menu'  => __( 'Primary Menu', 'hgwmag' ),
	);

	register_nav_menus( $locations );
}

add_action( 'init', 'hgwmag_menus' );


 /*
 #=========================================================================
 #   Register widget areas.
 #	 https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
 #=========================================================================
 */
function hgwmag_widgets_init() {

		// This is a Main Sidebar, Uses for all Pages except Singular
    register_sidebar(
      array(
        'name'          => __( 'Main Sidebar', 'hgwmag' ),
        'id'            => 'main-sidebar',
        'description'   => __( 'Visible on all pages except (Posts) and (Pages)', 'hgwmag' ),
        'before_widget' => '<div id="%1$s" class="widget %2$s">',
        'after_widget'  => '</div>',
        'before_title'  => '<h4 class="widget-title">',
        'after_title'   => '</h4>',
    )
  );

	// This is a Singular Sidebar [Note: The Main Sidebar Not Available in Singular Pages]
	register_sidebar(
		array(
			'name'          => __( 'Singular Sidebar', 'hgwmag' ),
			'id'            => 'singular-sidebar',
			'description'   => __( 'Available in (Posts) and (Pages)', 'hgwmag' ),
			'before_widget' => '<div id="%1$s" class="widget %2$s">',
			'after_widget'  => '</div>',
			'before_title'  => '<h4 class="widget-title">',
			'after_title'   => '</h4>',
		)
	);

	// This is Footer Widget
	register_sidebar(
		array(
			'name'          => __( 'Footer widgets 1', 'hgwmag' ),
			'id'            => 'footer-area1',
			'description'   => __( 'Available in footer', 'hgwmag' ),
			'before_widget' => '<div id="%1$s" class="widget %2$s">',
			'after_widget'  => '</div>',
			'before_title'  => '<h4 class="widget-title">',
			'after_title'   => '</h4>',
		)
	);
	// This is Footer Widget
	register_sidebar(
			array(
				'name'          => __( 'Footer widgets 2', 'hgwmag' ),
				'id'            => 'footer-area2',
				'description'   => __( 'Available in footer', 'hgwmag' ),
				'before_widget' => '<div id="%1$s" class="widget %2$s">',
				'after_widget'  => '</div>',
				'before_title'  => '<h4 class="widget-title">',
				'after_title'   => '</h4>',
		)
	);

}
add_action( 'widgets_init', 'hgwmag_widgets_init' );


/*
#=========================================================================
#   Customize the-excerpt()
#=========================================================================
*/

function hgwmag_exc( $charNum ){
	// strip tags to avoid breaking any html
		$string = strip_tags( get_the_excerpt() );
		$lenGth = $charNum;
		if (strlen($string) > $lenGth) {

				// truncate string
				$stringCut = substr($string, 0, $lenGth);
				$endPoint = strrpos($stringCut, ' ');

				//if the string doesn't contain any space then it will cut without word basis.
				$string = $endPoint? substr($stringCut, 0, $endPoint) : substr($stringCut, 0);
				$string .= '...';
		}
		echo esc_html( $string );
}

/*
#=========================================================================
#   Custom Theme Customizer
#   All Settings For Custom Theme Customizer
#=========================================================================
*/
function hgwmag_theme_customizer($wp_customize){
		// Load customize Callback
		require get_template_directory().'/inc/customizer/customizer-callback.php';
		// Load customize Sanitize
		require get_template_directory().'/inc/customizer/customizer-sanitize.php';
		// Load customize Controls
		require get_template_directory().'/inc/customizer/customizer-control.php';
		//  ==========================================================
		//  - Add Hgwmag Header Panel
		//  ==========================================================
		$wp_customize->add_panel(
		'hgwmag-panel',
			array(
			'priority'       => 10,
			'capability'     => 'edit_theme_options',
			'title'          => 'Hgw Magazine',
			'description'    => __('Custom Settings for Hgw Magazine Theme', 'hgwmag'),
			)
		);
		 // load
		 require get_template_directory().'/inc/customizer/header-customizer.php';
		 // load
		 require get_template_directory().'/inc/customizer/front-page-customizer.php';
		 // load
		 require get_template_directory().'/inc/customizer/singular-page-customizer.php';
		 // load
		 require get_template_directory().'/inc/customizer/social-media-customizer.php';
		 // load
		 require get_template_directory().'/inc/customizer/footer-customizer.php';
}

function hgwmag_theme_customizer_scripts() {
	 wp_enqueue_style( 'hgwmag-customizer-style', get_template_directory_uri() . '/assets/css/customizer.css' );
}
add_action( 'customize_register', 'hgwmag_theme_customizer' );
add_action( 'customize_controls_enqueue_scripts', 'hgwmag_theme_customizer_scripts',0 );


/*
#=========================================================================
#   REQUIRED FILES
#   Include required files.
#=========================================================================
*/

// Theme Page info
require get_template_directory() . '/inc/inc-theme-page-info.php';
// Header Functions & Template
require get_template_directory() . '/inc/templates/inc-header.php';
require get_template_directory() . '/inc/templates/inc-social-media.php';
// Front Page Functions & Template
require get_template_directory() . '/inc/templates/inc-front-page.php';
// Singular Functions & Template
require get_template_directory() . '/inc/templates/inc-singular-navigation.php';
// Custom tags for the theme.
require get_template_directory() . '/inc/template-tags.php';
require get_template_directory() . '/inc/template-hooks.php';
// load Classes
require get_template_directory() . '/classes/class-widget-recent-posts.php';
require get_template_directory() . '/classes/class-hgw-menu-walker.php';
