<?php
/**
 * Filters hook for the theme
 *
 * @package Case-Themes
 */

/* Custom Classs - Body */
function breeza_body_classes( $classes ) {   

	$classes[] = '';
	if (class_exists('ReduxFramework')) {
		$classes[] = ' pxl-redux-page';

		$footer_fixed = breeza()->get_theme_opt('footer_fixed');
		$p_footer_fixed = breeza()->get_page_opt('p_footer_fixed');

		if($p_footer_fixed != false && $p_footer_fixed != 'inherit') {
			$footer_fixed = $p_footer_fixed;
		}

		if(isset($footer_fixed) && $footer_fixed == 'on') {
			$classes[] = ' pxl-footer-fixed';
		}

		$pxl_body_typography = breeza()->get_theme_opt('pxl_body_typography');
		if($pxl_body_typography != 'google-font') {
			$classes[] = ' body-'.$pxl_body_typography.' ';
		}

		$pxl_heading_typography = breeza()->get_theme_opt('pxl_heading_typography');
		if($pxl_heading_typography != 'google-font') {
			$classes[] = ' heading-'.$pxl_heading_typography.' ';
		}

		$theme_default = breeza()->get_theme_opt('theme_default');
		if(isset($theme_default['font-family']) && $theme_default['font-family'] == false && $pxl_body_typography == 'google-font') {
			$classes[] = ' pxl-font-default';
		}

		$header_layout = breeza()->get_opt('header_layout');
		if(isset($header_layout) && $header_layout) {
			$post_header = get_post($header_layout);
			$header_type = get_post_meta( $post_header->ID, 'header_type', true );
			if(isset($header_type)) {
				$classes[] = ' bd-'.$header_type.'';
			}
		}

		$shop_layout = breeza()->get_theme_opt('shop_layout', 'grid');
		if(isset($_GET['shop-layout'])) {
			$shop_layout = $_GET['shop-layout'];
		}
		$classes[] = ' woocommerce-layout-'.$shop_layout;

		$body_custom_class = breeza()->get_page_opt('body_custom_class');
		if(!empty($body_custom_class)) {
			$classes[] = $body_custom_class;
		}
	}
	return $classes;
}
add_filter( 'body_class', 'breeza_body_classes' );

/* Post Type Support */
function breeza_add_cpt_support() {
	$cpt_support = get_option( 'elementor_cpt_support' );
	
	if( ! $cpt_support ) {
		$cpt_support = [ 'page', 'post', 'case', 'service', 'footer', 'pxl-template' ];
		update_option( 'elementor_cpt_support', $cpt_support );
	}
	
	else if( ! in_array( 'case', $cpt_support ) ) {
		$cpt_support[] = 'case';
		update_option( 'elementor_cpt_support', $cpt_support );
	}

	else if( ! in_array( 'service', $cpt_support ) ) {
		$cpt_support[] = 'service';
		update_option( 'elementor_cpt_support', $cpt_support );
	}

	else if( ! in_array( 'footer', $cpt_support ) ) {
		$cpt_support[] = 'footer';
		update_option( 'elementor_cpt_support', $cpt_support );
	}

	else if( ! in_array( 'pxl-template', $cpt_support ) ) {
		$cpt_support[] = 'pxl-template';
		update_option( 'elementor_cpt_support', $cpt_support );
	}

}
add_action( 'after_switch_theme', 'breeza_add_cpt_support');

/* Post Type Support Elementor*/
add_filter( 'pxl_add_cpt_support', 'seaside_add_cpt_support' );
function seaside_add_cpt_support($cpt_support) { 	
	$cpt_support[] = 'pxl-slider';
	return $cpt_support;
}

add_filter( 'pxl_support_default_cpt', 'breeza_support_default_cpt' );
function breeza_support_default_cpt($postypes){
	return $postypes; // pxl-template
}

add_filter( 'pxl_extra_post_types', 'breeza_add_posttype' );
function breeza_add_posttype( $postypes ) {
	$case_display = breeza()->get_theme_opt('case_display', true);
	$case_slug = breeza()->get_theme_opt('case_slug', 'case');
	$case_name = breeza()->get_theme_opt('case_name', 'Case');
	$service_display = breeza()->get_theme_opt('service_display', true);
	$service_slug = breeza()->get_theme_opt('service_slug', 'service');
	$service_name = breeza()->get_theme_opt('service_name', 'Services');
	if($service_display) {
		$service_status = true;
	} else {
		$service_status = false;
	}
	if($case_display) {
		$case_status = true;
	} else {
		$case_status = false;
	}

	$postypes['case'] = array(
		'status' => $case_status,
		'item_name'  => $case_name,
		'items_name' => $case_name,
		'args'       => array(
			'rewrite'             => array(
				'slug'       => $case_slug,
			),
		),
	);

	$postypes['service'] = array(
		'status' => $service_status,
		'item_name'  => $service_name,
		'items_name' => $service_name,
		'args'       => array(			
			'rewrite'             => array(
				'slug'       => $service_slug,
			),
		),
	);

	$postypes['pxl-slider'] = [
		'status'     => true,
		'item_name'  => esc_html__('Slider Builder', 'breeza'),
		'items_name' => esc_html__('Slider Builder', 'breeza'),
		'args'       => array(
			'supports'           => array(
				'title',
				'editor',
				'thumbnail',
			),
			'public'             => true,
			'publicly_queryable' => true,
			'show_in_nav_menus'   => false
		),
		'labels'     => array()
	]; 
	
	return $postypes;
}

/* Custom Archive Post Type Link */
function breeza_custom_archive_service_link() {
	if( is_post_type_archive( 'service' ) ) {
		$archive_service_link = breeza()->get_theme_opt('archive_service_link');
		wp_redirect( get_permalink($archive_service_link), 301 );
		exit();
	}
}
add_action( 'template_redirect', 'breeza_custom_archive_service_link' );

add_filter( 'pxl_extra_taxonomies', 'breeza_add_tax' );
function breeza_add_tax( $taxonomies ) {
	$taxonomies['service-category'] = array(
		'status'     => true,
		'post_type'  => array( 'service' ),
		'taxonomy'   => 'Service Categories',
		'taxonomies' => 'Service Categories',
		'args'       => array(
			'rewrite'             => array(
				'slug'       => 'service-category'
			),
		),
		'labels'     => array()
	);
	$taxonomies['case-category'] = array(
		'status'     => true,
		'post_type'  => array( 'case' ),
		'taxonomy'   => 'Case Categories',
		'taxonomies' => 'Case Categories',
		'args'       => array(
			'rewrite'             => array(
				'slug'       => 'case-category'
			),
		),
		'labels'     => array()
	);
	
	return $taxonomies;
}

function breeza_custom_archive_case_link() {
	if( is_post_type_archive( 'case' ) ) {
		$archive_case_link = breeza()->get_theme_opt('archive_case_link');
		wp_redirect( get_permalink($archive_case_link), 301 );
		exit();
	}
}
add_action( 'template_redirect', 'breeza_custom_archive_case_link' );


add_filter( 'pxl_theme_builder_post_types', 'breeza_theme_builder_post_type' );
function breeza_theme_builder_post_type($postypes){
	//default are header, footer, mega-menu
	$postypes[] = 'pxl-slider';
	return $postypes;
}

add_filter( 'pxl_theme_builder_layout_ids', 'breeza_theme_builder_layout_id' );
function breeza_theme_builder_layout_id($layout_ids){
	//default [], 
	$header_layout        = (int)breeza()->get_opt('header_layout');
	$header_sticky_layout = (int)breeza()->get_opt('header_sticky_layout');
	$footer_layout        = (int)breeza()->get_opt('footer_layout');
	$ptitle_layout        = (int)breeza()->get_opt('ptitle_layout');
	if( $header_layout > 0) 
		$layout_ids[] = $header_layout;
	if( $header_sticky_layout > 0) 
		$layout_ids[] = $header_sticky_layout;
	if( $footer_layout > 0) 
		$layout_ids[] = $footer_layout;
	if( $ptitle_layout > 0) 
		$layout_ids[] = $ptitle_layout;
	
	$mega_menu_id = breeza_get_mega_menu_builder_id();
	if(!empty($mega_menu_id))
		$layout_ids = array_merge($layout_ids, $mega_menu_id);

	$page_popup_id = breeza_get_page_popup_builder_id();
	if(!empty($page_popup_id))
		$layout_ids = array_merge($layout_ids, $page_popup_id);

	$slider_template = breeza_get_templates_option('slider');
	if( count($slider_template) > 0){
		foreach ($slider_template as $key => $value) {
			$layout_ids[] = $key;
		}
	}

	return $layout_ids;
}

add_filter( 'pxl_wg_get_source_id_builder', 'breeza_wg_get_source_builder' );
function breeza_wg_get_source_builder($wg_datas){
	$wg_datas['tabs'] = ['control_name' => 'tabs', 'source_name' => 'content_template'];
	return $wg_datas;
}

/* Update primary color in Editor Builder */
add_action( 'elementor/preview/enqueue_styles', 'breeza_add_editor_preview_style' );
function breeza_add_editor_preview_style(){
	wp_add_inline_style( 'editor-preview', breeza_editor_preview_inline_styles() );
}
function breeza_editor_preview_inline_styles(){
	$theme_colors = breeza_configs('theme_colors');
	ob_start();
	echo '.elementor-edit-area-active{';
	foreach ($theme_colors as $color => $value) {
		printf('--%1$s-color: %2$s;', str_replace('#', '',$color),  $value['value']);
	}
	echo '}';
	return ob_get_clean();
}

add_filter( 'get_the_archive_title', 'breeza_archive_title_remove_label' );
function breeza_archive_title_remove_label( $title ) {
	if ( is_category() ) {
		$title = single_cat_title( '', false );
	} elseif ( is_tag() ) {
		$title = single_tag_title( '', false );
	} elseif ( is_author() ) {
		$title = get_the_author();
	} elseif ( is_post_type_archive() ) {
		$title = post_type_archive_title( '', false );
	} elseif ( is_tax() ) {
		$title = single_term_title( '', false );
	} elseif ( is_home() ) {
		$title = single_post_title( '', false );
	}

	return $title;
}

add_filter( 'comment_reply_link', 'breeza_comment_reply_text' );
function breeza_comment_reply_text( $link ) {
	$link = str_replace( 'Reply', ''.esc_attr__('Reply', 'breeza').'', $link );
	return $link;
}
add_filter( 'pxl_enable_pagepopup', 'breeza_enable_pagepopup' );
function breeza_enable_pagepopup() {
	return false;
}
add_filter( 'pxl_enable_megamenu', 'breeza_enable_megamenu' );
function breeza_enable_megamenu() {
	return true;
}
add_filter( 'pxl_enable_onepage', 'breeza_enable_onepage' );
function breeza_enable_onepage() {
	return true;
}

add_filter( 'pxl_support_awesome_pro', 'breeza_support_awesome_pro' );
function breeza_support_awesome_pro() {
	return true;
}

add_filter( 'redux_pxl_iconpicker_field/get_icons', 'breeza_add_icons_to_pxl_iconpicker_field' );
function breeza_add_icons_to_pxl_iconpicker_field($icons){
	$custom_icons = []; //'Flaticon' => array(array('flaticon-marker' => 'flaticon-marker')),
	$icons = array_merge($custom_icons, $icons);
	return $icons;
}


add_filter("pxl_mega_menu/get_icons", "breeza_add_icons_to_megamenu");
function breeza_add_icons_to_megamenu($icons){
	$custom_icons = []; //'Flaticon' => array(array('flaticon-marker' => 'flaticon-marker')),
	$icons = array_merge($custom_icons, $icons);
	return $icons;
}


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


/* ------Disable Lazy loading---- */
add_filter( 'wp_lazy_loading_enabled', '__return_false' );

/* ------ Export Settings ---- */
add_filter( 'pxl_export_wp_settings', 'breeza_export_wp_settings' );
function breeza_export_wp_settings($wp_options){
	$wp_options[] = 'mc4wp_default_form_id';
	return $wp_options;
}

/* ------ Theme Info ---- */
add_filter( 'pxl_server_info', 'breeza_add_server_info');
function breeza_add_server_info($infos){
	$infos = [
		'api_url' => 'https://api.bravisthemes.com/',
		'docs_url' => 'https://doc.bravisthemes.com/breeza/',
		'plugin_url' => 'https://api.bravisthemes.com/plugins/',
		'demo_url' => 'https://demo.bravisthemes.com/breeza/',
		'support_url' => 'https://bravisthemes.ticksy.com/',
		'help_url' => 'https://doc.bravisthemes.com/',
		'email_support' => '',
		'video_url' => '#'
	];

	return $infos;
}

/* ------ Template Filter ---- */
add_filter( 'pxl_template_type_support', 'breeza_template_type_support' );
function breeza_template_type_support($type) {
	$extra_type = [
		'page-title'          => esc_html__('Page Title', 'breeza'), 
		'hidden-panel'          => esc_html__('Hidden Panel', 'breeza'), 
		'tab'          => esc_html__('Tab', 'breeza'), 
		'popup'          => esc_html__('Popup', 'breeza'),
		'page'          => esc_html__('Page', 'breeza'),
	];
	$template_type = array_merge($type,$extra_type); 
	return $template_type;
}

/* Search Result 
function breeza_search_result($query) {
	if ($query->is_search) {
		$query->set('post_type', 'post');
		$query->set('post_type', 'page');
		$query->set('post_type', 'case');
		$query->set('post_type', 'service');
	}
	return $query;
}
add_filter('pre_get_posts','breeza_search_result'); */