<?php
/**
 * Blogto functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 * @since Blogto 1.0.0
 */

/**
 * Blogto only works in WordPress 4.7 or later.
 */
if ( version_compare( $GLOBALS['wp_version'], '4.7-alpha', '<' ) ) {
	require get_template_directory() . '/inc/back-compat.php';
	return;
}

function blogto_scripts(){

	# Enqueue Vendor's Script & Style
	$scripts = array(
		array(
			'handler'  => 'blogto-google-fonts',
			'style'    => esc_url( '//fonts.googleapis.com/css?family=Poppins:300,400,400i,500,600,700,800,900' ),
			'absolute' => true
		),
		array(
			'handler' => 'bootstrap',
			'style'   => 'bootstrap/css/bootstrap.min.css',
			'script'  => 'bootstrap/js/bootstrap.min.js',
			'version' => '3.3.7'
		),
		array(
			'handler' => 'kfi-icons',
			'style'   => 'kf-icons/css/style.css',
			'version' => '1.0.0'
		),
		array(
			'handler' => 'owlcarousel',
			'style'   => 'OwlCarousel2-2.2.1/assets/owl.carousel.min.css',
			'script'  => 'OwlCarousel2-2.2.1/owl.carousel.min.js',
			'version' => '2.2.1'
		),
		array(
			'handler' => 'owlcarousel-theme',
			'style'   => 'OwlCarousel2-2.2.1/assets/owl.theme.default.min.css',
			'version' => '2.2.1'
		),
		array(
			'handler' => 'colorbox',
			'style'   => 'colorbox/css/colorbox.min.css',
			'script'  => 'colorbox/js/jquery.colorbox-min.js',
			'version' => '1.6.4'
		),
		array(
			'handler'  => 'blogto-style',
			'style'    => get_stylesheet_uri(),
			'absolute' => true,
		),
		array(
			'handler'    => 'blogto-script',
			'script'     => get_theme_file_uri( '/assets/js/main.min.js' ),
			'absolute'   => true,
			'prefix'     => '',
			'dependency' => array( 'jquery', 'masonry' )
		)
	);

	blogto_enqueue( $scripts );

	$locale = apply_filters( 'blogto_localize_var', array(
		'is_admin_bar_showing'        => is_admin_bar_showing() ? true : false,
		'enable_scroll_top_in_mobile' => blogto_get_option( 'enable_scroll_top_in_mobile' ) ? 1 : 0,
		'home_slider' => array(
			'autoplay' => blogto_get_option( 'slider_autoplay' ),
			'timeout'  => absint( blogto_get_option( 'slider_timeout' ) ) * 1000
		),
		'is_rtl' => is_rtl(),
		'search_placeholder'=> esc_html__( 'hit enter for search.', 'blogto' ),
		'search_default_placeholder'=> esc_html__( 'search...', 'blogto' )
	));

	wp_localize_script( 'blogto-script', 'BLOGTO', $locale );

	if ( is_singular() && comments_open() ) {
		wp_enqueue_script( 'comment-reply' );
	}
}
add_action( 'wp_enqueue_scripts', 'blogto_scripts' );

/**
* Adds a submit button in search form
* 
* @since Blogto Pro 1.0.0
* @param string $form
* @return string
*/
function blogto_modify_search_form( $form ){
	return str_replace( '</form>', '<button type="submit" class="search-button"><span class="kfi kfi-search"></span></button></form>', $form );
}
add_filter( 'get_search_form', 'blogto_modify_search_form' );

/**
* Modify some markup for comment section
*
* @since Blogto 1.0.0
* @param array $defaults
* @return array $defaults
*/
function blogto_modify_comment_form_defaults( $defaults ){

	$user = wp_get_current_user();
	$user_identity = $user->exists() ? $user->display_name : '';

	$defaults[ 'logged_in_as' ] = '<p class="logged-in-as">' . sprintf(
          /* translators: 1: edit user link, 2: accessibility text, 3: user name, 4: logout URL */
          __( '<a href="%1$s" aria-label="%2$s">Logged in as %3$s</a> <a href="%4$s">Log out?</a>', 'blogto' ),
          get_edit_user_link(),
          /* translators: %s: user name */
          esc_attr( sprintf( __( 'Logged in as %s. Edit your profile.', 'blogto' ), $user_identity ) ),
          $user_identity,
          wp_logout_url( apply_filters( 'the_permalink', get_permalink( get_the_ID() ) ) )
      ) . '</p>';
	return $defaults;
}
add_filter( 'comment_form_defaults', 'blogto_modify_comment_form_defaults',99 );

/**
 * Add a pingback url auto-discovery header for singularly identifiable articles.
 *
 * @since Blogto 1.0.0
 * @return void
 */
function blogto_pingback_header(){
	if ( is_singular() && pings_open() ) {
		printf( '<link rel="pingback" href="%s">' . "\n", get_bloginfo( 'pingback_url' ) );
	}
}
add_action( 'wp_head', 'blogto_pingback_header' );

/**
* Add a class in body when previewing customizer
*
* @since Blogto 1.0.0
* @param array $class
* @return array $class
*/
function blogto_body_class_modification( $class ){
	if( is_customize_preview() ){
		$class[] = 'keon-customizer-preview';
	}
	
	if( is_404() || ! have_posts() ){
 		$class[] = 'content-none-page';
	}

	if( ( is_front_page() && ! is_home() ) || is_search() ){

		$class[] = 'grid-col-3';
	}else if( is_home() || is_archive() ){

		$class[] = 'grid-col-2';
	}

	return $class;
}
add_filter( 'body_class', 'blogto_body_class_modification' );

if( ! function_exists( 'blogto_get_ids' ) ):
/**
* Fetches setting from customizer and converts it to an array
*
* @uses blogto_explode_string_to_int()
* @return array | false
* @since Blogto 1.0.0
*/
function blogto_get_ids( $setting ){

    $str = blogto_get_option( $setting );
    if( empty( $str ) )
    	return;

    return blogto_explode_string_to_int( $str );

}
endif;

if( !function_exists( 'blogto_section_heading' ) ):
/**
* Prints the heading section for home page
*
* @return void
* @since Blogto 1.0.0
*/
function blogto_section_heading( $args ){

	$defaults = array(
	    'divider'  => false,
	    'query'    => true,
	    'sub_title' => false
	);

	$args = wp_parse_args( $args, $defaults );

	# No need to query if already inside the query.
	if( !$args[ 'query'] ){
		set_query_var( 'args', $args );
		get_template_part( 'template-parts/page/home', 'heading' );
		return;
	}

	$id = blogto_get_option( $args[ 'id' ] );

	if( !empty( $id ) ){

		$query = new WP_Query( array(
			'p' => $id,
			'post_type' => 'page'
		));

		while( $query->have_posts() ){
			$query->the_post();
			set_query_var( 'args', $args );
			get_template_part( 'template-parts/page/home', 'heading' );
		}
		wp_reset_postdata();
	}
}
endif;

if( ! function_exists( 'blogto_inner_banner' ) ):
/**
* Includes the template for inner banner
*
* @return void
* @since Blogto 1.0.0
*/
function blogto_inner_banner(){

	$description = false;

	if( is_archive() ){

		# For all the archive Pages.
		$title       = get_the_archive_title();
		$description = get_the_archive_description();
	}else if( !is_front_page() && is_home() ){

		# For Blog Pages.
		$title = single_post_title( '', false );
	}else if( is_search() ){

		# For search Page.
		$title = esc_html__( 'Search Results for: ', 'blogto' ) . get_search_query();
	}else if( is_front_page() && is_home() ){
		# If Latest posts page
		
		$title = blogto_get_option( 'archive_page_title' );
	}else{

		# For all the single Pages.
		$title = get_the_title();
	}

	$args = array(
		'title'       => $title,
		'description' => $description
	);

	set_query_var( 'args', $args );
	get_template_part( 'template-parts/inner', 'banner' );
}
endif;

if( !function_exists( 'blogto_get_icon_by_post_format' ) ):
/**
* Gives a css class for post format icon
*
* @return string
* @since Blogto 1.0.0
*/
function blogto_get_icon_by_post_format(){
	$icons = array(
		'standard' => 'kfi-pushpin-alt',
		'sticky'  => 'kfi-pushpin-alt',
		'aside'   => 'kfi-documents-alt',
		'image'   => 'kfi-image',
		'video'   => 'kfi-arrow-triangle-right-alt2',
		'quote'   => 'kfi-quotations-alt2',
		'link'    => 'kfi-link-alt',
		'gallery' => 'kfi-images',
		'status'  => 'kfi-comment-alt',
		'audio'   => 'kfi-volume-high-alt',
		'chat'    => 'kfi-chat-alt',
	);

	$format = get_post_format();
	if( empty( $format ) ){
		$format = 'standard';
	}

	return apply_filters( 'blogto_post_format_icon', $icons[ $format ] );
}
endif;

if( !function_exists( 'blogto_has_sidebar' ) ):

/**
* Check whether the page has sidebar or not.
*
* @see https://codex.wordpress.org/Conditional_Tags
* @since Blogto 1.0.0
* @return bool Whether the page has sidebar or not.
*/
function blogto_has_sidebar(){

	if( is_page() || is_search() || is_single() ){
		return false;
	}

	return true;
}
endif;

if( !function_exists( 'blogto_is_search' ) ):
/**
* Conditional function for search page / jet pack supported
* @since Blogto 1.0.0
* @return Bool 
*/
function blogto_is_search(){

	if( ( is_search() || ( isset( $_POST[ 'action' ] ) && $_POST[ 'action' ] == 'infinite_scroll'  && isset( $_POST[ 'query_args' ][ 's' ] ))) ){
		return true;
	}

	return false;
}
endif;

function blogto_post_class_modification( $classes ){

	# Add no thumbnail class when its search page
	if( blogto_is_search() && ( 'post' !== get_post_type() && !has_post_thumbnail() ) ){
		$classes[] = 'no-thumbnail';
	}
	return $classes;
}
add_filter( 'post_class', 'blogto_post_class_modification' );

require_once get_parent_theme_file_path( '/inc/setup.php' );
require_once get_parent_theme_file_path( '/inc/template-tags.php' );
require_once get_parent_theme_file_path( '/modules/loader.php' );
require_once get_parent_theme_file_path( '/trt-customize-pro/example-1/class-customize.php' );
require_once get_parent_theme_file_path( '/modules/tgm-plugin-activation/loader.php' );

if( !function_exists( 'blogto_get_homepage_top_sections' ) ):
/**
* Returns the top section name of homepage
* @since Blogto 1.0.0
* @return array 
*/
function blogto_get_homepage_top_sections(){

	$arr = array(
		'slider',
	);

	return apply_filters( 'blogto_homepage_top_sections', $arr );
}
endif;

if( !function_exists( 'blogto_get_homepage_bottom_sections' ) ):
/**
* Returns the bottom section name of homepage
* @since Blogto 1.0.0
* @return array 
*/
function blogto_get_homepage_bottom_sections(){

	$arr = array(
		'callback',
		'blog',
		'about',
	);

	return apply_filters( 'blogto_homepage_bottom_sections', $arr );
}
endif;

/**
* Predefined demo Import file setup. 
* @link https://wordpress.org/plugins/one-click-demo-import/
* @since Blogto 1.0.0
* @return array
*/
function blogto_ocdi_import_files() {
	return array(
		array(
		  'import_file_name'             => esc_html__( 'Theme Demo Content', 'blogto' ),
		  'categories'                   => array( 'Category 1', 'Category 2' ),
		  'local_import_file'            => trailingslashit( get_template_directory() ) . 'ocdi/blogto.xml',
		  'local_import_widget_file'     => trailingslashit( get_template_directory() ) . 'ocdi/blogto.wie',
		  'local_import_customizer_file' => trailingslashit( get_template_directory() ) . 'ocdi/blogto.dat',
		  'import_preview_image_url'     => trailingslashit( get_template_directory() ) . 'screenshot.png',
		  'import_notice'                => __( 'Please waiting for a few minutes, do not close the window or refresh the page until the data is imported.', 'blogto' ),
		  'preview_url'                  => 'https://www.demo.keonthemes.com/blogto',
		),
	);
}
add_filter( 'pt-ocdi/import_files', 'blogto_ocdi_import_files' );

/**
* Change menu, front page display as in demo after completing demo import
* @link https://wordpress.org/plugins/one-click-demo-import/
* @since Blogto 1.0.0
* @return null
*/
function blogto_ocdi_after_import_setup() {

    // Assign menus to their locations.
    $primary_menu = get_term_by('name', 'Primary Menu', 'nav_menu');
    $top_header_menu = get_term_by('name', 'Top Header Menu', 'nav_menu');
    $social_menu = get_term_by('name', 'Social Menu', 'nav_menu');
    $footer_menu = get_term_by('name', 'Footer Menu', 'nav_menu');
    set_theme_mod( 'nav_menu_locations' , array( 
          'primary'    => $primary_menu->term_id,
          'topheader'  => $top_header_menu->term_id,
          'social'     => $social_menu->term_id,
          'footer'     => $footer_menu->term_id
         ) 
    );

    // Assign front page and posts page (blog page).
    $front_page_id = get_page_by_title( 'Front' );
    $blog_page_id  = get_page_by_title( 'Blog' );

    update_option( 'show_on_front', 'page' );
    update_option( 'page_on_front', $front_page_id->ID );
    update_option( 'page_for_posts', $blog_page_id->ID );

}
add_action( 'pt-ocdi/after_import', 'blogto_ocdi_after_import_setup' );

/**
* Disable branding of One Click Demo Import
* @link https://wordpress.org/plugins/one-click-demo-import/
* @since Blogto 1.0.0
* @return Bool
*/
function blogto_ocdi_branding(){
	return true;
}
add_filter( 'pt-ocdi/disable_pt_branding', 'blogto_ocdi_branding' );