<?php
function bw_extend_theme_customizer() {
	if ( ! function_exists( 'Kadence\kadence' ) ) {
		return;
	}
	if ( ! class_exists( 'Kadence\Theme_Customizer' ) ) {
		return;
	}

	$all_post_types = \Kadence\kadence()->get_post_types_objects();
	foreach ( $all_post_types as $post_type_item ) {
		$post_type_name = $post_type_item->name;
		$ignore_type    = \Kadence\kadence()->get_post_types_to_ignore();
		if ( ! in_array( $post_type_name, $ignore_type, true ) && $post_type_item->has_archive ) {

			// Custom post single settings
			$custom_post_layout_settings = array(
				$post_type_name . '_navigation' => array(
					'control_type' => 'kadence_switch_control',
					'sanitize'     => 'kadence_sanitize_toggle',
					'section'      => $post_type_name . '_layout',
					'priority'     => 20,
					'default'      => false,
					'label'        => esc_html__( 'Show Post Navigation?' ),
					'transport'    => 'refresh',
				),
			);
			\Kadence\Theme_Customizer::add_settings( $custom_post_layout_settings );

			// Custom post archive settings
			$custom_post_archive_settings = array(
				$post_type_name . '_archive_entry_type' => array(
					'control_type' => 'kadence_switch_control',
					'sanitize'     => 'kadence_sanitize_toggle',
					'section'      => $post_type_name . '_archive',
					'priority'     => 13,
					'default'      => false,
					'label'        => esc_html__( 'Show Post Type' ),
					'transport'    => 'refresh',
				),
				$post_type_name . '_archive_entry_type_tax' => array(
					'control_type' => 'kadence_switch_control',
					'sanitize'     => 'kadence_sanitize_toggle',
					'section'      => $post_type_name . '_archive',
					'priority'     => 14,
					'default'      => false,
					'label'        => esc_html__( 'Include Category With Type' ),
					'transport'    => 'refresh',
					'context'      => array(
						array(
							'setting'    => $post_type_name . '_archive_entry_type',
							'operator'   => '=',
							'value'      => true,
						),
					)
				),
				$post_type_name . '_archive_entry_type_term' => array(
					'control_type' => 'kadence_switch_control',
					'sanitize'     => 'kadence_sanitize_toggle',
					'section'      => $post_type_name . '_archive',
					'priority'     => 14,
					'default'      => false,
					'label'        => esc_html__( 'Include Term With Type' ),
					'transport'    => 'refresh',
					'context'      => array(
						array(
							'setting'    => $post_type_name . '_archive_entry_type',
							'operator'   => '=',
							'value'      => true,
						),
					)
				),
			);
			\Kadence\Theme_Customizer::add_settings( $custom_post_archive_settings );
		}
	}

	// Search archive settings
	$search_result_settings = array(
		'search_archive_entry_type' => array(
			'control_type' => 'kadence_switch_control',
			'sanitize'     => 'kadence_sanitize_toggle',
			'section'      => 'search',
			'priority'     => 13,
			'default'      => false,
			'label'        => esc_html__( 'Show Post Type' ),
			'transport'    => 'refresh',
		),
		'search_archive_entry_type_tax' => array(
			'control_type' => 'kadence_switch_control',
			'sanitize'     => 'kadence_sanitize_toggle',
			'section'      => 'search',
			'priority'     => 14,
			'default'      => false,
			'label'        => esc_html__( 'Include Category With Type' ),
			'transport'    => 'refresh',
			'context'      => array(
				array(
					'setting'    => 'search_archive_entry_type',
					'operator'   => '=',
					'value'      => true,
				),
			)
		),
		'search_archive_entry_type_term' => array(
			'control_type' => 'kadence_switch_control',
			'sanitize'     => 'kadence_sanitize_toggle',
			'section'      => 'search',
			'priority'     => 14,
			'default'      => false,
			'label'        => esc_html__( 'Include Term With Type' ),
			'transport'    => 'refresh',
			'context'      => array(
				array(
					'setting'    => 'search_archive_entry_type',
					'operator'   => '=',
					'value'      => true,
				),
			)
		),
	);
	\Kadence\Theme_Customizer::add_settings( $search_result_settings );

	// Search bar settings
	$search_bar_settings = array(
		'header_search_bar_toggle' => array(
			'control_type' => 'kadence_switch_control',
			'sanitize'     => 'kadence_sanitize_toggle',
			'section'      => 'header_search_bar',
			'default'      => false,
			'label'        => esc_html__( 'Toggle Bar' ),
			'transport'    => 'refresh',
		),
		'header_mobile_search_bar_toggle' => array(
			'control_type' => 'kadence_switch_control',
			'sanitize'     => 'kadence_sanitize_toggle',
			'section'      => 'header_mobile_search_bar',
			'default'      => false,
			'label'        => esc_html__( 'Toggle Bar' ),
			'transport'    => 'refresh',
		),
	);
	\Kadence\Theme_Customizer::add_settings( $search_bar_settings );
}
add_action( 'init', 'bw_extend_theme_customizer' );

// hook template part to display archive entry type
function bw_loop_entry_type() {
	get_template_part( 'template-parts/content/entry_loop_type', get_post_type() );
}
// 10/taxonomies, 20/title, 30/meta
add_action( 'kadence_loop_entry_header', 'bw_loop_entry_type', 15 );
