<?php
/**
 * rallypro functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package rallypro
 */

if ( ! function_exists( 'rallypro_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 rallypro_setup() {
	/*
	 * Make theme available for translation.
	 * Translations can be filed in the /languages/ directory.
	 * If you're building a theme based on rallypro, use a find and replace
	 * to change 'rallypro' to the name of your theme in all the template files.
	 */
	load_theme_textdomain( 'rallypro', 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' );

	// image size 

	add_image_size( 'rallypro_frontPageImg', 555, 286, true );

	add_image_size( 'rallypro_mainPageImg', 1140, 600, true );

	//adding custom field support globally

	add_theme_support( 'custom-fields' );

	// This theme uses wp_nav_menu() in one location.
	register_nav_menus( array(
		'primarymenu' => esc_html__( 'Primary', 'rallypro' ),
	) );

	/*
	 * 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( 'rallypro_custom_background_args', array(
		'default-color' => 'ffffff',
		'default-image' => '',
	) ) );

	// Add theme support for selective refresh for widgets.
	add_theme_support( 'customize-selective-refresh-widgets' );

}
endif;
add_action( 'after_setup_theme', 'rallypro_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 rallypro_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'rallypro_content_width', 640 );
}
add_action( 'after_setup_theme', 'rallypro_content_width', 0 );


/**
 * Register widget area.
 *
 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
 */
function rallypro_widgets_init() {

	register_sidebar( array(
		'name'          => esc_html__( 'Sidebar' , 'rallypro' ),
		'id'            => 'sidebar',
		'description'   => esc_html__( 'Add widgets here.', 'rallypro' ),
		'before_widget' => '<div id="%1$s" class="widget %2$s">',
		'after_widget'  => '</div>',
		'before_title'  => '<div class="widget-title text-center"><h4>',
		'after_title'   => '</h4></div>',
	) );

		
	register_sidebar( array(
		'name'          => esc_html__( 'Sub Footer Contact Us' , 'rallypro' ),
		'id'            => 'contact-us',
		'description'   => esc_html__( 'Add widgets here.', 'rallypro' ),
		'before_widget' => '<div id="%1$s" class="sub-footer-content footer-contact-us %2$s">',
		'after_widget'  => '</div>',
		'before_title'  => '<div class="section-heading footer-head"><h4>',
		'after_title'   => '</h4></div>',
	) );

	register_sidebar( array(
		'name'          => esc_html__( 'Sub Footer About Us' , 'rallypro' ),
		'id'            => 'about-us',
		'description'   => esc_html__( 'Add widgets here.', 'rallypro' ),
		'before_widget' => '<div id="%1$s" class="sub-footer-content %2$s">',
		'after_widget'  => '</div>',
		'before_title'  => '<div class="section-heading footer-head"><h4>',
		'after_title'   => '</h4></div>',
	) );

	register_sidebar( array(
		'name'          => esc_html__( 'Post Below' , 'rallypro' ),
		'id'            => 'post-below',
		'description'   => esc_html__( 'Add widgets here.', 'rallypro' ),
		'before_widget' => '<div id="%1$s" class="about-author %2$s">',
		'after_widget'  => '</div>',
		'before_title'  => '<h5>',
		'after_title'   => '</h5>',
	) );

	register_sidebar( array(
		'name'          => esc_html__( 'Sub Footer Follow Us' , 'rallypro' ),
		'id'            => 'follow-us',
		'description'   => esc_html__( 'Add widgets here.', 'rallypro' ),
		'before_widget' => '<div id="%1$s" class="sub-footer-content text-center footer-follow-us animated wow zoomInUp %2$s">',
		'after_widget'  => '</div>',
		'before_title'  => '<div class="section-heading footer-head"><h4>',
		'after_title'   => '</h4></div>',
	) );

	register_sidebar( array(
		'name'          => esc_html__( '404 Page Widget' , 'rallypro' ),
		'id'            => 'error-page',
		'description'   => esc_html__( 'Add widgets here.', 'rallypro' ),
		'before_widget' => '<div id="%1$s" class="%2$s">',
		'after_widget'  => '</div>',
		'before_title'  => '<div class=""><h4>',
		'after_title'   => '</h4></div>',
	) );
}
add_action( 'widgets_init', 'rallypro_widgets_init' );

/**
 * Enqueue scripts and styles.
 */
function rallypro_scripts() {
	
	//bootstrap-css

	wp_enqueue_style( 'bootstrap', get_template_directory_uri().'/css/bootstrap.min.css','1.0.1' );

	//animate-css

	wp_enqueue_style( 'animate', get_template_directory_uri().'/css/animate.min.css','1.0.2' );

	//ionicons-css

	wp_enqueue_style( 'ionicons', get_template_directory_uri().'/css/ionicons.min.css','1.0.3' );
	
	//style-css

	wp_enqueue_style( 'rallypro-style', get_stylesheet_uri() );

	//responsive-css

	wp_enqueue_style( 'responsive', get_template_directory_uri().'/css/responsive.css','1.0.4' );

	//wp-jquery

	wp_enqueue_script('jquery');

	//bootstrap-js

	wp_enqueue_script( 'bootstrap-js', get_template_directory_uri() . '/js/bootstrap.min.js', array(), '20151216', true );

	//headroom-js

	wp_enqueue_script( 'headroom-js', get_template_directory_uri() . '/js/headroom.js', array(), '20151218', true );

	//typed-js

	wp_enqueue_script( 'typed-js', get_template_directory_uri() . '/js/typed.min.js', array(), '20151219', true );

	//wow-js

	wp_enqueue_script( 'wow-js', get_template_directory_uri() . '/js/wow.min.js', array(), '20151211', true );

	//custom-js

	wp_enqueue_script( 'custom-js', get_template_directory_uri() . '/js/functions.js', array(), '20151217', true );


	wp_enqueue_script( 'rallypro-navigation', get_template_directory_uri() . '/js/navigation.js', array(), '20151215', true );

	wp_enqueue_script( 'rallypro-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20151215', true );

	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}
}
add_action( 'wp_enqueue_scripts', 'rallypro_scripts' );


// For google fonts

function rallypro_load_fonts(){
	wp_register_style( 'google-fonts-Montserrat','https://fonts.googleapis.com/css?family=Montserrat:400,500,600,700');
	wp_enqueue_style('google-fonts-Montserrat');
}
add_action('wp_print_styles', 'rallypro_load_fonts');

/**
 * Add a pingback url auto-discovery header for singularly identifiable articles.
 */

function rallypro_pingback_header() {
	if ( is_singular() && pings_open() ) {
		echo '<link rel="pingback" href="', esc_url( get_bloginfo( 'pingback_url' ) ), '">';
	}
}

add_action( 'wp_head', 'rallypro_pingback_header' );



//remove hash link from read more button

function rallypro_remove_hash_link($link){
	$link = preg_replace('|#more-[0-9]+|','',$link);
	return $link;
}
add_filter('the_content_more_link','rallypro_remove_hash_link');
function styling_read_more(){
	$style = '<a class="btn" href="'.get_permalink().'" role="button">continue reading</a>';
	return $style;
}
add_filter('the_content_more_link','styling_read_more');	


//For user contact add field

function rallypro_modify_contact_methods($profile_fields) {

	// Add new fields
	$profile_fields['twitter'] = 'Twitter URL';
	$profile_fields['instagram'] = 'Instagram URL';

	return $profile_fields;
}
add_filter('user_contactmethods', 'rallypro_modify_contact_methods');

// comment field to bottom

function rallypro_move_comment_field_to_bottom( $fields ) {
$comment_field = $fields['comment'];
unset( $fields['comment'] );
$fields['comment'] = $comment_field;
return $fields;
}

add_filter( 'comment_form_fields', 'rallypro_move_comment_field_to_bottom' );

// customize comment_form_defaults

function rallypro_comment_form_defaults( $defaults ) {
  $defaults['title_reply'] = __( 'Submit a Comment', 'rallypro' );
  $defaults['comment_notes_before'] = __( '<p>Must be required * marked fields.</p>', 'rallypro' );
  return $defaults;
}
add_filter( 'comment_form_defaults', 'rallypro_comment_form_defaults' );


//add class in next_posts_link and previous_posts_link

add_filter('next_posts_link_attributes', 'rallypro_posts_link_attributes_1');
add_filter('previous_posts_link_attributes', 'rallypro_posts_link_attributes_2');

function rallypro_posts_link_attributes_1() {
    return 'class="btn"';
}
function rallypro_posts_link_attributes_2() {
    return 'class="btn"';
}

/**
 * Registers an editor stylesheet for the theme.
 */
function rallypro_theme_add_editor_styles() {
    add_editor_style( get_template_directory().'/css/custom-editor-style.css' );
}
add_action( 'admin_init', 'rallypro_theme_add_editor_styles' );


/**
* To mute migrate console error
*/
add_action( 'wp_default_scripts', 'rallypro_jquery_migrate'  ) ;
    
	function rallypro_jquery_migrate($scripts){
    if ( ! empty( $scripts->registered['jquery'] ) ) {
        $scripts->registered['jquery']->deps = array_diff( $scripts->registered['jquery']->deps, array( 'jquery-migrate' ) );
    }
	} 

/**
 * 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';

/**
 * Additional features to allow styling of the templates.
 */
require get_template_directory() . '/inc/template-functions.php';

/**
 * Customizer additions.
 */
require get_template_directory() . '/inc/customizer.php';

/**
 * Load Jetpack compatibility file.
 */
require get_template_directory() . '/inc/jetpack.php';

// Register Custom Navigation Walker

require get_template_directory() . '/inc/rallypro-bootstrap-navwalker.php';

//Titan framework checker

require get_template_directory() . '/inc/titan-framework-checker.php';

//Titan option
if ( class_exists( 'TitanFramework' ) ){
	require get_template_directory() . '/inc/titan-options.php';
}

// Dynamic Styles which updates from customizer

require get_template_directory() . '/inc/custom-css.php';

// plugin activator class

require get_template_directory() . '/inc/plugin-activation.php';

// comment walker class 

require get_template_directory() . '/inc/rallypro-comment-walker.php';


