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

if ( ! function_exists( 'tr_affreview_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 tr_affreview_lite_setup() {
		/*
		 * Make theme available for translation.
		 * Translations can be filed in the /languages/ directory.
		 * If you're building a theme based on Affreview, use a find and replace
		 * to change 'tr-affreview-lite' to the name of your theme in all the template files.
		 */
		load_theme_textdomain( 'tr-affreview-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' );
        
		//add image size for post thumbnail
		add_image_size('tr-photography-lite-thumbnail', 1200, 450, true);

		// This theme uses wp_nav_menu() in one location.
		register_nav_menus( array(
			'primarymenu' => esc_html__( 'Primary', 'tr-affreview-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( 'tr_affreview_lite_custom_background_args', array(
			'default-color' => 'ffffff',
			'default-image' => '',
		) ) );

		// 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(
			'height'      => 80,
			'width'       => 250,
			'flex-width'  => true,
			'flex-height' => true,
		) );
		
		//Add editor style 
		add_editor_style( 'css/custom-editor-style.css' );


	}
endif;
add_action( 'after_setup_theme', 'tr_affreview_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 tr_affreview_lite_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'tr_affreview_lite_content_width', 640 );
}
add_action( 'after_setup_theme', 'tr_affreview_lite_content_width', 0 );

/**
 * Register widget area.
 *
 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
 */
function tr_affreview_lite_widgets_init() {
	register_sidebar( array(
		'name'          => esc_html__( 'Sidebar', 'tr-affreview-lite' ),
		'id'            => 'right-sidebar',
		'description'   => esc_html__( 'Add widgets here.', 'tr-affreview-lite' ),
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	) );

	register_sidebar( array(
		'name'          => esc_html__( 'Footer 1', 'tr-affreview-lite' ),
		'id'            => 'footer-1',
		'description'   => esc_html__( 'Add widgets here.', 'tr-affreview-lite' ),
		'before_widget' => '<div id="%1$s" class="widget %2$s footer-top-content">',
		'after_widget'  => '</div>',
		'before_title'  => '<h2>',
		'after_title'   => '</h2>',
	) );
	register_sidebar( array(
		'name'          => esc_html__( 'Footer 2', 'tr-affreview-lite' ),
		'id'            => 'footer-2',
		'description'   => esc_html__( 'Add widgets here.', 'tr-affreview-lite' ),
		'before_widget' => '<div id="%1$s" class="widget %2$s footer-top-content">',
		'after_widget'  => '</div>',
		'before_title'  => '<h2>',
		'after_title'   => '</h2>',
	) );

	register_sidebar( array(
		'name'          => esc_html__( 'Footer 3', 'tr-affreview-lite' ),
		'id'            => 'footer-3',
		'description'   => esc_html__( 'Add widgets here.', 'tr-affreview-lite' ),
		'before_widget' => '<div id="%1$s" class="widget %2$s footer-top-content">',
		'after_widget'  => '</div>',
		'before_title'  => '<h2>',
		'after_title'   => '</h2>',
	) );
 
}
add_action( 'widgets_init', 'tr_affreview_lite_widgets_init' );

/**
 * Enqueue scripts and styles.
 */
function tr_affreview_lite_scripts() {


   //bootstrap css
   wp_enqueue_style( 'bootstrap', get_template_directory_uri().'/css/bootstrap.css',
	'3.3.7');

   //font-awesome
   wp_enqueue_style( 'font-awesome', get_template_directory_uri().'/css/font-awesome.css',
	'4.4.0');	
  
   //mystyle css
   wp_enqueue_style( 'tr-affreview-lite-template', get_template_directory_uri().'/css/style.css');
 
 	//theme style
   wp_enqueue_style( 'tr-affreview-lite-style', get_stylesheet_uri() );

   //responsive css
   wp_enqueue_style( 'tr-affreview-lite-responsive', get_template_directory_uri().'/css/responsive.css');

	//wp-jquery
	wp_enqueue_script('jquery');
 
 	// bootstrap jQuery
   wp_enqueue_script( 'bootstrap-jQuery', get_template_directory_uri() . '/js/bootstrap.js', array(), '3.3.7', true );

	wp_enqueue_script( 'tr-affreview-lite-navigation', get_template_directory_uri() . '/js/navigation.js', array(), true );

	wp_enqueue_script( 'tr-affreview-lite-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), true );

	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}
}
add_action( 'wp_enqueue_scripts', 'tr_affreview_lite_scripts' );

//googlefonts
function tr_affreview_lite_load_fonts(){

	wp_register_style('google-fonts-Baloo-Roboto','https://fonts.googleapis.com/css2?family=Baloo+2:wght@400;500;600;700&family=Poppins:wght@400;500;600;700');
	wp_enqueue_style('google-fonts-Baloo-Roboto');
}
add_action('wp_print_styles','tr_affreview_lite_load_fonts');


//customize default comment 
function tr_affreview_lite_comment_form_defaults($defaults){
   $defaults['title_reply']=__('Submit a Comment','tr-affreview-lite');
   $defaults['comment_notes_before']=__(' ','tr-affreview-lite');
   return $defaults;
   }
add_filter('comment_form_defaults','tr_affreview_lite_comment_form_defaults');

// comment field to bottom
function tr_affreview_lite_move_comment_field_to_bottom( $fields ) {
	$comment_field = $fields['comment'];
	unset( $fields['comment'] );
	$fields['comment'] = $comment_field;
	return $fields;
}
add_filter( 'comment_form_fields', 'tr_affreview_lite_move_comment_field_to_bottom' );

/**
 * Implement the Custom Header feature.
 */
require get_template_directory() . '/inc/custom-header.php';

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

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

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

// Register Custom Navigation Walker

require get_template_directory() . '/inc/wp-bootstrap-navwalker.php';

// Register Custom comment Walker

require get_template_directory() . '/inc/class-wp-bootstrap-comment-walker.php';

// Custom css
require_once('inc/custom-css.php');

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


//pagination next- previous add class	 
add_filter('next_posts_link_attributes', 'tr_affreview_lite_posts_link_attributes_1');
add_filter('previous_posts_link_attributes', 'tr_affreview_lite_posts_link_attributes_2');

function tr_affreview_lite_posts_link_attributes_1() {
    return 'class="next"';
}
function tr_affreview_lite_posts_link_attributes_2() {
    return 'class="previous"';
}

//remove hash link from read more button
function tr_affreview_lite_remove_hash_link($link){
	$link = preg_replace('|#more-[0-9]+|','',$link);
	return $link;
}
add_filter('the_content_more_link','tr_affreview_lite_remove_hash_link');

function tr_affreview_lite_styling_read_more(){
	$style = '<a class="btn" href="'.esc_url( get_permalink() ).'" role="button">' . __('Read More', 'tr-affreview-lite') . '</a>';
	return $style;
}
add_filter('the_content_more_link','tr_affreview_lite_styling_read_more');	

// for custom widget latest post
 function tr_affreview_lite_custom_excerpt_length( $length ) {
 	if ( is_admin() ) {
        return $length;
    }
    return 15;
}
add_filter( 'excerpt_length', 'tr_affreview_lite_custom_excerpt_length', 999 );

