<?php
/**
 * Filters hook for the theme
 *
 * @package Bravisthemes
 */

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

  return $infos;
}

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

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

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

    $mouse_move_animation = kaitex()->get_theme_opt('mouse_move_animation');
    if($mouse_move_animation) {
        $classes[] = ' pxl-enable-cursor';
    }

    $bnw_image = kaitex()->get_theme_opt( 'bnw_image', false );
    if($bnw_image) {
    	$classes[] = 'pxl-bnw-image';
    }

    $site_loader = kaitex()->get_theme_opt( 'site_loader', false );
    if($site_loader) {
    	$classes[] = 'pxl-ov-hidden';
    }

    return $classes;
}
add_filter( 'body_class', 'kaitex_body_classes' );

add_filter( 'pxl_page_class', 'kaitex_page_classes' );
function kaitex_page_classes($str_cls){
	$header_layout	= (int)kaitex()->get_opt('header_layout');
	if ($header_layout > 0){
    	$header_position = get_post_meta( $header_layout, 'header_position', true );
    	if(!empty($header_position))
    		$str_cls .= ' header-pos-'.$header_position;
    }
    return $str_cls;
}

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

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

add_filter( 'pxl_extra_post_types', 'kaitex_add_posttype' );
function kaitex_add_posttype( $postypes ) {
	$portfolio_slug = kaitex()->get_theme_opt( 'portfolio_slug', 'portfolio' );
	$portfolio_name = kaitex()->get_theme_opt( 'portfolio_name', esc_html__('Portfolio', 'kaitex') );
	$postypes['portfolio'] = array(
		'status' => true,
		'item_name'  => $portfolio_name,
		'items_name' => $portfolio_name,
		'args'       => array(
			'rewrite'             => array(
                'slug'       => $portfolio_slug,
 		 	),
		),
	);

	$service_slug = kaitex()->get_theme_opt( 'service_slug', 'service' );
	$service_name = kaitex()->get_theme_opt( 'service_name', esc_html__('Services', 'kaitex') );
	$postypes['service'] = array(
		'status' => true,
		'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', 'kaitex'),
		'items_name' => esc_html__('Slider Builder', 'kaitex'),
        'args'       => array(
            'supports'           => array(
                'title',
                'editor',
            ),
            'public'             => true,
            'publicly_queryable' => true,
            'show_in_nav_menus'   => false
        ),
        'labels'     => array()
    ];

	return $postypes;
}

add_filter( 'pxl_extra_taxonomies', 'kaitex_add_tax' );
function kaitex_add_tax( $taxonomies ) {
	$portfolio_category_slug = kaitex()->get_theme_opt( 'portfolio_category_slug', 'portfolio-category' );
	$portfolio_category_name = kaitex()->get_theme_opt( 'portfolio_category_name', esc_html__('Portfolio Categories', 'kaitex') );
	$taxonomies['portfolio-category'] = array(
		'status'     => true,
		'post_type'  => array( 'portfolio' ),
		'taxonomy'   => $portfolio_category_name,
		'taxonomies' => $portfolio_category_name,
		'args'       => array(
			'rewrite'             => array(
                'slug'       => $portfolio_category_slug
 		 	),
		),
		'labels'     => array()
	);
	$taxonomies['portfolio-tag'] = array(
		'status'     => true,
		'post_type'  => array( 'portfolio' ),
		'taxonomy'   => 'Portfolio Tags',
		'taxonomies' => 'Portfolio Tags',
		'args'       => array(
			'rewrite'             => array(
                'slug'       => 'portfolio-tag'
 		 	),
		),
		'labels'     => array()
	);

	$service_category_slug = kaitex()->get_theme_opt( 'service_category_slug', 'service-category' );
	$service_category_name = kaitex()->get_theme_opt( 'service_category_name', esc_html__('Service Categories', 'kaitex') );
	$taxonomies['service-category'] = array(
		'status'     => true,
		'post_type'  => array( 'service' ),
		'taxonomy'   => $service_category_name,
		'taxonomies' => $service_category_name,
		'args'       => array(
			'rewrite'             => array(
                'slug'       => $service_category_slug
 		 	),
		),
		'labels'     => array()
	);
	$taxonomies['service-tag'] = array(
		'status'     => true,
		'post_type'  => array( 'service' ),
		'taxonomy'   => 'Service Tags',
		'taxonomies' => 'Service Tags',
		'args'       => array(
			'rewrite'             => array(
                'slug'       => 'service-tag'
 		 	),
		),
		'labels'     => array()
	);

	return $taxonomies;
}

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

function kaitex_custom_archive_portfolio_link() {
    if( is_post_type_archive( 'portfolio' ) ) {
        $archive_portfolio_link = kaitex()->get_theme_opt( 'archive_portfolio_link' );
        wp_redirect( get_permalink($archive_portfolio_link), 301 );
        exit();
    }
}
add_action( 'template_redirect', 'kaitex_custom_archive_portfolio_link' );

/* Custom 404 - Page */
function kaitex_custom_page_404() {
	$page_404 = kaitex()->get_theme_opt( 'page_404', 'default' );
	$page_404_link = kaitex()->get_theme_opt( 'page_404_link' );
    if( $page_404 == 'custom' && !empty($page_404_link) && is_404() ) {
        wp_redirect( get_permalink($page_404_link), 301 );
        exit();
    }
}
add_action( 'template_redirect', 'kaitex_custom_page_404' );

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

add_filter( 'pxl_theme_builder_layout_ids', 'kaitex_theme_builder_layout_id' );
function kaitex_theme_builder_layout_id($layout_ids){
	//default [],
	$header_layout        = (int)kaitex()->get_opt('header_layout');
	$header_sticky_layout = (int)kaitex()->get_opt('header_sticky_layout');
	$footer_layout        = (int)kaitex()->get_opt('footer_layout');
	$ptitle_layout        = (int)kaitex()->get_opt('ptitle_layout');
	$shop_layout  		  = (int)kaitex()->get_opt('shop_layout');
	$shop_single_layout   = (int)kaitex()->get_opt('shop_single_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;
	if( $shop_layout > 0)
		$layout_ids[] = $shop_layout;
	if( $shop_single_layout > 0)
		$layout_ids[] = $shop_single_layout;

	return $layout_ids;
}

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

add_filter( 'pxl_template_type_support', 'kaitex_template_type_support' );
function kaitex_template_type_support($type){
	//default ['header','footer','mega-menu']
	$extra_type = [
        'page-title'   => esc_html__('Page Title', 'kaitex'),
        'hidden-panel' => esc_html__('Hidden Panel', 'kaitex'),
        'tab'          => esc_html__('Tab', 'kaitex'),
        'popup'        => esc_html__('Popup', 'kaitex'),
        'hidden-panel' => esc_html__('Hidden Panel', 'kaitex'),
	];
	$template_type = array_merge($type,$extra_type);
	return $template_type;
}


add_filter( 'get_the_archive_title', 'kaitex_archive_title_remove_label' );
function kaitex_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', 'kaitex_comment_reply_link' );
function kaitex_comment_reply_link( $content ) {
    return preg_replace( '/comment-reply-link/', 'comment-reply-link ', $content);
}

add_filter( 'pxl_enable_megamenu', 'kaitex_enable_megamenu' );
function kaitex_enable_megamenu() {
	return true;
}
add_filter( 'pxl_enable_onepage', 'kaitex_enable_onepage' );
function kaitex_enable_onepage() {
	return true;
}

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

add_filter( 'redux_pxl_iconpicker_field/get_icons', 'kaitex_add_icons_to_pxl_iconpicker_field' );
function kaitex_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", "kaitex_add_icons_to_megamenu");
function kaitex_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', 'kaitex_comment_field_to_bottom' );
function kaitex_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' );
