<?php
$debugmode          = ( isset( $_GET['debugmode'] ) && $_GET['debugmode'] == 1 );
$theme_options_data = theme_options_data();

$text_color = $custom_title = $subtitle = $bg_color = $bg_header = $class_full = $text_color_header =
$bg_image = $thim_custom_heading = $cate_top_image_src = $front_title = $title_recipe_archive = $title_recipe_single = '';

$hide_breadcrumbs = $hide_title = 0;

$cat_obj = $wp_query->get_queried_object();

if ( isset( $cat_obj->term_id ) ) {
	$cat_ID = $cat_obj->term_id;
} else {
	$cat_ID = "";
}

if ( get_post_type() == "product" ) {
	$prefix = 'thim_woo';
} elseif ( get_post_type() == 'recipe' ) {
	$prefix = 'thim_recipe';
} else {
	$prefix = 'thim_archive';
}
$text_color = $bg_color = $cate_top_image_src = '';

/*
 * Page and Archive
 */

if ( is_page() || is_single() ) {
	$prefix_inner = '_single_';

} else {
	if ( is_front_page() || is_home() ) {
		$prefix       = 'cakeart';
		$prefix_inner = '_front_page_';
		if ( isset( $theme_options_data[ $prefix . $prefix_inner . 'custom_title' ] ) && $theme_options_data[ $prefix . $prefix_inner . 'custom_title' ] <> '' ) {
			$front_title = $theme_options_data[ $prefix . $prefix_inner . 'custom_title' ];
		}
		$postid = get_the_ID();
		if ( is_home() ) {
			$postid = get_option( 'page_for_posts' );
		}
	} else {
		$prefix_inner = '_cate_';
	}
}

/*
 * Get data customize
 */
if ( isset( $theme_options_data[ $prefix . $prefix_inner . 'heading_text_color' ] ) && $theme_options_data[ $prefix . $prefix_inner . 'heading_text_color' ] <> '' ) {
	$text_color = $theme_options_data[ $prefix . $prefix_inner . 'heading_text_color' ];
}

if ( isset( $theme_options_data[ $prefix . $prefix_inner . 'heading_bg_color' ] ) && $theme_options_data[ $prefix . $prefix_inner . 'heading_bg_color' ] <> '' ) {
	$bg_color = $theme_options_data[ $prefix . $prefix_inner . 'heading_bg_color' ];
}

if ( isset( $theme_options_data[ $prefix . $prefix_inner . 'sub_title' ] ) && $theme_options_data[ $prefix . $prefix_inner . 'sub_title' ] <> '' ) {
	$subtitle = $theme_options_data[ $prefix . $prefix_inner . 'sub_title' ];
}

if ( isset( $theme_options_data['thim_recipe_single_title'] ) && $theme_options_data['thim_recipe_single_title'] <> '' ) {
	$title_recipe_single = $theme_options_data['thim_recipe_single_title'];
}

if ( isset( $theme_options_data['thim_recipe_cate_title'] ) && $theme_options_data['thim_recipe_cate_title'] <> '' ) {
	$title_recipe_archive = $theme_options_data['thim_recipe_cate_title'];
}

if ( isset( $theme_options_data[ $prefix . $prefix_inner . 'top_image' ] ) && $theme_options_data[ $prefix . $prefix_inner . 'top_image' ] <> '' ) {
	$cate_top_image     = $theme_options_data[ $prefix . $prefix_inner . 'top_image' ];
	$cate_top_image_src = $cate_top_image;

	if ( is_numeric( $cate_top_image ) ) {
		$cate_top_attachment = wp_get_attachment_image_src( $cate_top_image, 'full' );
		$cate_top_image_src  = $cate_top_attachment ? $cate_top_attachment[0] : '';
	}
}
if ( isset( $theme_options_data[ $prefix . $prefix_inner . 'hide_title' ] ) ) {
	$hide_title = $theme_options_data[ $prefix . $prefix_inner . 'hide_title' ];
}

if ( is_page() || is_single() || is_home() ) {
	$postid = get_the_ID();
	if ( is_home() ) {
		$postid = get_option( 'page_for_posts' );
	}
	$using_custom_heading = get_post_meta( $postid, 'thim_mtb_using_custom_heading', true );
	if ( $using_custom_heading ) {
		$hide_title   = get_post_meta( $postid, 'thim_mtb_hide_title_and_subtitle', true );
		$custom_title = get_post_meta( $postid, 'thim_mtb_custom_title', true );
		$subtitle     = get_post_meta( $postid, 'thim_subtitle', true );
		$text_color_1 = get_post_meta( $postid, 'thim_mtb_text_color', true );
		if ( $text_color_1 <> '' ) {
			$text_color = $text_color_1;
		}
		$bg_color_1 = get_post_meta( $postid, 'thim_mtb_bg_color', true );
		if ( $bg_color_1 <> '' ) {
			$bg_color = $bg_color_1;
		}

		$cate_top_image = get_post_meta( $postid, 'thim_mtb_top_image', true );
		if ( $cate_top_image ) {
			$post_page_top_attachment = wp_get_attachment_image_src( $cate_top_image, 'full' );
			$cate_top_image_src       = $post_page_top_attachment ? $post_page_top_attachment[0] : '';
		}
	}
} else {
	$thim_custom_heading = get_tax_meta( $cat_ID, 'thim_custom_heading', true );
	if ( $thim_custom_heading == 'custom' ) {
		$text_color_1 = get_tax_meta( $cat_ID, $prefix . '_cate_heading_text_color', true );
		$bg_color_1   = get_tax_meta( $cat_ID, $prefix . '_cate_heading_bg_color', true );
		if ( $text_color_1 != '#' ) {
			$text_color = $text_color_1;
		}
		if ( $bg_color_1 != '#' ) {
			$bg_color = $bg_color_1;
		}
		$hide_title     = get_tax_meta( $cat_ID, $prefix . '_cate_hide_title', true );
		$cate_top_image = get_tax_meta( $cat_ID, $prefix . '_top_image', true );
		if ( $cate_top_image ) {
			$cate_top_image_src = $cate_top_image['src'];
		}
	}
}

$hide_title = ( $hide_title == 'on' ) ? '1' : $hide_title;

/*
 * Css
 */
$c_css_style = $css_line = '';
$c_css_style .= ( $text_color != '' ) ? 'color: ' . $text_color . ';' : '';
$c_css_style .= ( $bg_color != '' ) ? 'background-color: ' . $bg_color . ';' : '';
$css_line    .= ( $text_color != '' ) ? 'background-color: ' . $text_color . ';' : '';

$c_css = ( $c_css_style != '' ) ? 'style="' . $c_css_style . '"' : '';

$c_css_1 = ( $bg_color != '' ) ? 'style="background-color:' . $bg_color . '"' : '';

$c_css_line = ( $css_line != '' ) ? 'style="' . $css_line . '"' : '';

$top_images = ( $cate_top_image_src != '' ) ? '<img alt="" src="' . $cate_top_image_src . '" />' : '<img alt="" src="' . THIM_URI . '/images/bg_header.jpg" />';

/*
 * Show title
 */
?>

<div class="top_site_main<?php if ( $top_images == '' ) {
	echo ' top-site-no-image';
} ?>" <?php echo ent2ncr( $c_css ); ?>>
	<?php echo ent2ncr( $top_images ); ?>
    <div class="page-title-wrapper">
		<?php if ( $hide_title != '1' ) { ?>
            <div class="banner-wrapper container article_heading">
				<?php
				if ( is_single() ) {
					$typography = 'h2';
				} else {
					$typography = 'h1';
				}
				if ( ( is_page() || is_single() ) && get_post_type() != 'product' ) {
					if ( is_single() ) {
						if ( get_post_type() == "recipe" ) {
							echo '<' . $typography . ' class="heading__secondary">';
							if ( $custom_title ) {
								echo esc_html( $custom_title );
							} elseif ( $title_recipe_single ) {
								echo esc_html( $title_recipe_single );
							} else {
								echo esc_html__( 'Recipe Detail', 'cakeart' );
							}
							echo '</' . $typography . '>';
							echo ( $subtitle != '' ) ? '<div class="banner-description"><p class="heading__primary ">' . esc_html( $subtitle ) . '</p></div>' : '';
						} else {
							echo ' <' . $typography . ' class="heading__secondary">';
							echo ( $custom_title ) ? esc_html( $custom_title ) : esc_html__( 'Blog', 'cakeart' );
							echo '</' . $typography . '>';
							echo ( $subtitle != '' ) ? '<div class="banner-description"><p class="heading__primary ">' . esc_html( $subtitle ) . '</p></div>' : '';
						}
					} else {
						echo '<' . esc_attr( $typography ) . ' class="heading__secondary">';
						$postid = get_the_ID();
						if ( is_home() ) {
							$postid = get_option( 'page_for_posts' );
						}
						echo ( $custom_title != '' ) ? esc_html( $custom_title ) : get_the_title( get_the_ID() );
						echo '</' . $typography . '>';
						echo ( $subtitle != '' ) ? '<div class="banner-description"><p class="heading__primary ">' . esc_html( $subtitle ) . '</p></div>' : '';
					}
				} elseif ( is_search() && get_post_type() == 'recipe' ) {
					echo '<' . $typography . ' class="heading__secondary">';
					$title_search = sprintf( esc_html__( 'Search Results: &ldquo;%s&rdquo;', 'cakeart' ), get_search_query() );
					echo ( $custom_title != '' ) ? esc_html( $custom_title ) : esc_html( $title_search );
					echo '</' . $typography . '>';
					echo ( $subtitle != '' ) ? '<div class="banner-description"><p class="heading__primary ">' . esc_html( $subtitle ) . '</p></div>' : '';
				} elseif ( get_post_type() == 'recipe' ) {
					echo '<' . $typography . ' class="heading__secondary recipehtt">';
					echo ( $title_recipe_archive ) ? esc_html( $title_recipe_archive ) : esc_html__( 'Recipe', 'cakeart' );
					echo '</' . $typography . '>';
					echo ( $subtitle != '' ) ? '<div class="banner-description"><p class="heading__primary ">' . esc_html( $subtitle ) . '</p></div>' : '';
				} elseif ( get_post_type() == 'product' ) {
					echo '<' . $typography . ' class="heading__secondary">';
					woocommerce_page_title();
					echo '</' . $typography . '>';
					echo ( $subtitle != '' ) ? '<div class="banner-description"><p class="heading__primary ">' . esc_html( $subtitle ) . '</p></div>' : '';
				} elseif ( is_front_page() || is_home() ) {
					echo '<' . $typography . ' class="heading__secondary">';
					if ( is_home() ) {
						echo ( $custom_title != '' ) ? esc_html( $custom_title ) : esc_html__( 'Blog', 'cakeart' );
					} else {
						echo ( $front_title != '' ) ? esc_html( $front_title ) : esc_html__( 'Blog', 'cakeart' );
					}
					echo '</' . $typography . '>';
					echo ( $subtitle != '' ) ? '<div class="banner-description"><p class="heading__primary ">' . esc_html( $subtitle ) . '</p></div>' : '';
				} else {
					echo '<' . $typography . ' class="heading__secondary">';
					thim_the_archive_title();
					echo '</' . $typography . '>';
					echo ( $subtitle != '' ) ? '<div class="banner-description"><p class="heading__primary ">' . esc_html( $subtitle ) . '</p></div>' : '';
				}
				?>
            </div>
		<?php } ?>
    </div>
</div>

