<?php
/**
 * Display site contents
 *
 * Call appropriate functions to display various site contents.
 *
 * @package Cambay
 * @since 1.0.0
 */

/*
 * Register hooks for displaying site items.
 *
 * 'cambay_add_markup_for()' is a wrapper function for WordPress core 'add_action()' to make
 * it slightly more redable. Following is an example,
 * cambay_add_markup_for( 'what_to_display', 'where_to_display', priority (optional), args (optional) )
 *
 * 'what_to_display' should be a function ( theme slug will be added programatically ) i.e., cambay_what_to_display()
 * 'where_to_display' should be a theme action hook ( theme slug will be added programatically ) i.e., cambay_where_to_display
 *
 * Above function will register an action hook as follows,
 * add_action( 'cambay_where_to_display', 'cambay_what_to_display', priority (optional), args (optional) )
 */

// Display site header items.
cambay_add_markup_for( 'skip_link', 'inside_header', 0 );
cambay_add_markup_for( 'header_items', 'inside_header' );
cambay_add_markup_for( 'header_image', 'inside_header' );
cambay_add_markup_for( 'home_above_content_area', 'after_header' );

// Display primary site content.
cambay_add_markup_for( 'blog_title', 'before_main_content' );
cambay_add_markup_for( 'page_header', 'inside_main_content' );
cambay_add_markup_for( 'main_loop', 'inside_main_content' );
cambay_add_markup_for( 'hentry', 'inside_entry' );

// Display secondary site content.
cambay_add_markup_for( 'post_author', 'inside_entry' );
cambay_add_markup_for( 'image_navigation', 'inside_entry' );
cambay_add_markup_for( 'comments_toggle', 'before_comments' );
cambay_add_markup_for( 'post_navigation', 'inside_main_content' );
cambay_add_markup_for( 'post_pagination', 'after_main_content' );
cambay_add_markup_for( 'sidebar', 'inside_sidebar' );
cambay_add_markup_for( 'home_below_content_area', 'after_site_content' );

// Display site footer items.
cambay_add_markup_for( 'footer_widgets', 'inside_footer' );
cambay_add_markup_for( 'footer_items', 'inside_footer' );
cambay_add_markup_for( 'scroll_to_top', 'inside_footer' );

// Include svg file at the bottom of '<body>' tag.
add_action( 'wp_footer', 'cambay_svg_icons', 9999 );

/*
 * Wrapper functions for displaying site items.
 *
 * WordPress core 'add_action()' does not allow to
 * 1. Pass variables to the called function (Predefined variables in do_action()
 * can only be passed).
 * 2. No provision for conditional check before calling a function.
 *
 * Therefore, we have to create wrapper functions. Also, these wrapper functions
 * make code more redable.
 */

/**
 * Conditionally display skip link.
 *
 * @since 1.0.0
 */
function cambay_skip_link() {

	printf(
		'<a class="skip-link screen-reader-text" href="#content">%s</a>',
		esc_html__( 'Skip to content', 'cambay' )
	);
}

/**
 * Header items wrapper markup.
 *
 * @since 1.0.0
 */
function cambay_header_items() {
	cambay_markup(
		'header-items',
		[
			'cambay_site_branding',
			'cambay_main_navigation',
			'cambay_user_action_items',
		]
	);
}

/**
 * Custom header text markup.
 *
 * @since 1.0.0
 */
function cambay_header_text() {
	$header_text = [];
	if ( is_singular( 'page' ) ) {
		$header_text[] = [ 'cambay_get_template_partial', 'template-parts/post', 'entry-title' ];

		if ( has_excerpt() ) {
			$header_text[] = [ 'cambay_markup', 'single-excerpt', [ 'the_excerpt' ] ];
		}
	} elseif ( is_home() ) {
		$header_text[] = [ 'cambay_get_template_partial', 'template-parts/header', 'site-desc' ];
	}

	if ( ! empty( $header_text ) ) {
		echo '<div class="header-text-wrapper wrapper">';
		cambay_markup( 'header-text', $header_text );
		echo '</div>';
	}
}

/**
 * Custom header image markup.
 *
 * @since 1.0.0
 */
function cambay_header_image() {
	$img = '';

	if ( is_singular( 'page' ) && has_post_thumbnail() ) {
		$img = [ 'the_post_thumbnail', 'cambay-featured' ];
	} elseif ( is_home() && get_header_image() ) {
		$img = 'the_header_image_tag';
	} else {
		return;
	}

	if ( $img ) {
		cambay_header_text();
		cambay_markup( 'header-image', [ $img ] );
	}
}

/**
 * Site branding wrapper markup.
 *
 * @since 1.0.0
 */
function cambay_site_branding() {
	$site_branding = [ 'the_custom_logo' ];

	if ( cambay_get_mod( 'cambay_display_site_title', 'none' ) ) {
		array_push(
			$site_branding,
			[ 'cambay_get_template_partial', 'template-parts/header', 'site-title' ]
		);
	}

	cambay_markup( 'site-branding', $site_branding );
}

/**
 * Main navigation markup.
 *
 * @since 1.0.0
 */
function cambay_main_navigation() {
	if ( ! cambay_get_mod( 'cambay_primary_nav', 'none' ) ) {
		return;
	}

	printf(
		'<button aria-expanded="false" class="menu-toggle"><span class="bar"><span class="screen-reader-text">%s</span></span></button>',
		esc_html__( 'Site Navigation', 'cambay' )
	);

	cambay_nav_menu(
		'site-navigation',
		esc_html__( 'Site Navigation', 'cambay' ),
		[
			'menu_id'         => 'primary-menu',
			'menu_class'      => 'nav-menu nav-menu--primary',
			'container'       => 'div',
			'container_id'    => 'menu-container',
			'container_class' => 'menu-container',
			'theme_location'  => 'primary',
		]
	);
}

/**
 * Sidebar widgets wrapper markup.
 *
 * @since 1.0.0
 */
function cambay_user_action_items() {
	cambay_markup(
		'header-widgets',
		[
			'cambay_header_search',
			'cambay_header_widgets',
		]
	);
}

/**
 * Sidebar widgets wrapper markup.
 *
 * @since 1.0.0
 */
function cambay_header_widgets() {
	if ( ! is_active_sidebar( 'header' ) ) {
		return;
	}
	printf(
		'<button class="action-toggle"><span class="bar"><span class="screen-reader-text">%s</span></span></button>',
		esc_html__( 'Show secondary sidebar', 'cambay' )
	);
	echo '<div id="header-widget-area" class="header-widget-area">';
		cambay_widgets(
			'header-widget-wrapper',
			'header-widget-wrapper',
			esc_html__( 'Header Widget Wrapper', 'cambay' ),
			'header'
		);
	echo '</div>';
}

/**
 * Sidebar widgets wrapper markup.
 *
 * @since 1.0.0
 */
function cambay_header_search() {
	if ( ! cambay_get_mod( 'cambay_header_search', 'none' ) ) {
		return;
	}

	printf(
		'<button class="search-toggle"><span class="screen-reader-text">%s</span>%s%s</button>',
		esc_html__( 'Show secondary sidebar', 'cambay' ),
		cambay_get_icon( [ 'icon' => 'search' ] ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		cambay_get_icon( [ 'icon' => 'close' ] ) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
	);
	echo '<div id="header-search-wrapper" class="header-search-wrapper">';
	echo '<div class="header-search-container">';
	get_search_form();
	echo '</div></div>';
}

/**
 * Social navigation markup.
 *
 * @since 1.0.0
 */
function cambay_social_menu() {

	if ( ! has_nav_menu( 'social' ) ) {
		return;
	}

	cambay_nav_menu(
		'social-navigation',
		esc_html__( 'Social Navigation', 'cambay' ),
		[
			'menu_id'        => 'social-menu',
			'menu_class'     => 'nav-menu nav-menu--social',
			'theme_location' => 'social',
			'depth'          => 1,
			'link_before'    => '<span class="screen-reader-text">',
			'link_after'     => '</span>' . cambay_get_icon( [ 'icon' => 'chain' ] ),
		]
	);
}

/**
 * Display Homepage widgets 1 (widget area).
 *
 * @since 1.0.0
 */
function cambay_home_above_content_area() {
	if ( ! is_front_page() || is_paged() ) {
		return;
	}

	cambay_widgets(
		'home-content-area-above',
		'home-content-area-above widgetlayer flex-wrapper',
		esc_html__( 'Homepage Above Content Widget Area', 'cambay' ),
		'home-widgetlayer-1'
	);
}

/**
 * Display Homepage widgets 2 (widget area).
 *
 * @since 1.0.0
 */
function cambay_home_below_content_area() {
	if ( ! is_front_page() || is_paged() ) {
		return;
	}

	cambay_widgets(
		'home-content-area-below',
		'home-content-area-below widgetlayer flex-wrapper',
		esc_html__( 'Homepage Below Content Widget Area', 'cambay' ),
		'home-widgetlayer-2'
	);
}

/**
 * Page Entry header items markup.
 *
 * @since 1.0.0
 */
function cambay_page_entry_header_items() {

	$page_entry_header_items = [
		[ 'cambay_get_template_partial', 'template-parts/post', 'entry-title' ],
		'cambay_entry_meta_wrapper',
	];

	if ( has_excerpt() ) {
		$page_entry_header_items[] = [ 'cambay_markup', 'single-excerpt', [ 'the_excerpt' ] ];
	}

	cambay_markup( 'page-entry-header-items', $page_entry_header_items );
}

/**
 * Include page header display template.
 *
 * @since 1.0.0
 */
function cambay_page_header() {
	cambay_get_template_partial( 'template-parts/page', 'page-header' );
}

/**
 * Include blog title display template.
 *
 * @since 1.0.0
 */
function cambay_blog_title() {
	if ( is_home() ) {
		$blog_title = cambay_get_mod( 'cambay_blog_title', 'html' );
		if ( ! $blog_title ) {
			return;
		}
		$blog_title = explode( '-', $blog_title );
		?>
		<div class="title-wrapper wrapper">
			<h2 class="blog-title">
				<?php
				if ( isset( $blog_title[0] ) ) {
					printf( '<span class="bt-1">%s</span>', esc_html( trim( $blog_title[0] ) ) );
				}
				if ( isset( $blog_title[1] ) ) {
					printf( '<span class="bt-2">%s</span>', esc_html( trim( $blog_title[1] ) ) );
				}
				?>
			</h2>
		</div>
		<?php
	}
}

/**
 * Include main loop execution template.
 *
 * @since 1.0.0
 */
function cambay_main_loop() {
	cambay_get_template_partial( 'template-parts/loop', 'main-loop' );
}

/**
 * Include comment toggle template.
 *
 * @since 1.0.0
 */
function cambay_comments_toggle() {
	$text = '';
	if ( comments_open() ) {
		if ( have_comments() ) {
			$text = esc_html__( 'Join the Conversation', 'cambay' );
		} else {
			$text = esc_html__( 'Leave a comment', 'cambay' );
		}
	} else {
		if ( have_comments() ) {
			$text = esc_html__( 'Show comments', 'cambay' );
		}
	}

	if ( $text ) {
		$toggle_text = sprintf(
			'<span class="toggle-text">%s</span>',
			$text // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		);
		printf(
			'<button class="comments-toggle">%1$s%2$s%3$s</button>',
			$toggle_text, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			cambay_get_icon( [ 'icon' => 'angle-down' ] ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			cambay_get_icon( [ 'icon' => 'angle-up' ] ) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		);
	}
}

/**
 * Entry main content wrapper markup.
 *
 * @since 1.0.0
 */
function cambay_entry_main_content() {
	cambay_markup(
		'entry-main-content',
		[
			'cambay_entry_header_wrapper',
			'cambay_entry_content_wrapper',
			'cambay_entry_footer_wrapper',
		]
	);
}

/**
 * Entry header wrapper markup.
 *
 * @since 1.0.0
 */
function cambay_entry_header_wrapper() {
	if ( ! is_singular() ) {

		cambay_markup(
			'entry-header',
			[
				[ 'cambay_get_template_partial', 'template-parts/post', 'entry-title' ],
			]
		);
	} elseif ( ! ( is_singular( [ 'page' ] ) && has_post_thumbnail() ) ) {
		cambay_entry_title_area();
	}
}

/**
 * Entry header title area wrapper markup.
 *
 * @since 1.0.0
 */
function cambay_entry_title_area() {
	cambay_markup(
		'entry-header-title-area',
		[
			'cambay_page_entry_header_items',
			[ 'cambay_get_template_partial', 'template-parts/post', 'entry-thumbnail' ],
		]
	);
}

/**
 * Conditionally display entry meta wrapper markup.
 *
 * @since 1.0.0
 */
function cambay_entry_meta_wrapper() {

	if ( ! in_array( get_post_type(), [ 'page', 'attachment' ], true ) ) {
		cambay_markup( 'entry-meta', [ 'cambay_entry_meta' ] );
	}
}

/**
 * Include entry meta display template(s).
 *
 * @since 1.0.0
 */
function cambay_entry_meta() {
	cambay_get_template_partial( 'template-parts/meta', 'meta-author' );
	printf( '<span class="meta-sep">%s</span>', esc_html__( 'on', 'cambay' ) );
	cambay_get_template_partial( 'template-parts/meta', 'meta-date' );
}

/**
 * Create featured content markup on index pages.
 *
 * @since 1.0.0
 */
function cambay_hentry() {
	if ( is_singular() ) {
		cambay_entry_main_content();
	} else {
		cambay_markup(
			'entry-index-wrapper',
			[
				'cambay_entry_featured_content',
				'cambay_entry_main_content',
			]
		);
	}
}

/**
 * Create featured content markup on index pages.
 *
 * @since 1.0.0
 */
function cambay_entry_featured_content() {
	if ( ! is_singular() && ( cambay_get_mod( 'cambay_thumbnail_placeholder', 'none' ) || has_post_thumbnail() ) ) {
		cambay_markup(
			'entry-featured-content',
			[
				[ 'cambay_get_template_partial', 'template-parts/post', 'entry-thumbnail' ],
				[ 'cambay_get_template_partial', 'template-parts/meta', 'meta-permalink' ],
				[ 'cambay_sticky_post_icon' ],
			]
		);
	}
}

/**
 * Sticky Post Icon.
 *
 * @since 1.0.0
 */
function cambay_sticky_post_icon() {
	if ( is_sticky() ) {
		cambay_icon( [ 'icon' => 'sticky' ] );
	}
}

/**
 * Entry content wrapper markup.
 *
 * @since 1.0.0
 */
function cambay_entry_content_wrapper() {
	if ( is_attachment() && wp_attachment_is_image() ) {
		cambay_get_template_partial( 'template-parts/post', 'entry-attachment' );
		return;
	}

	if ( is_singular() ) {
		cambay_markup(
			'entry-content',
			[
				[ 'cambay_get_template_partial', 'template-parts/post', 'entry-content' ],
			]
		);
	} else {
		cambay_markup( 'entry-excerpt', [ 'the_excerpt' ] );
	}
}

/**
 * Entry footer wrapper markup.
 *
 * @since 1.0.0
 */
function cambay_entry_footer_wrapper() {
	if ( ! is_singular() ) {
		cambay_entry_meta_wrapper();
		return;
	}

	$type = get_post_type();
	$meta = [];

	if ( 'post' === $type ) {
		$meta = [
			[ 'cambay_get_template_partial', 'template-parts/meta', 'meta-categories' ],
			[ 'cambay_get_template_partial', 'template-parts/meta', 'meta-tags' ],
		];
	} elseif ( 'attachment' === $type ) {
		$meta = [
			[ 'cambay_get_template_partial', 'template-parts/meta', 'meta-attachment' ],
		];
	}

	if ( $meta ) {
		cambay_markup( 'entry-footer', $meta );
	}
}

/**
 * Conditionally include post author display template.
 *
 * @since 1.0.0
 */
function cambay_post_author() {
	global $post;

	// No need to display author box on image attachment pages.
	if ( is_attachment() && wp_attachment_is_image() ) {
		return;
	}

	// Display author box on single posts, if author description is available.
	if ( ! is_single() || '' === get_the_author_meta( 'description', $post->post_author ) ) {
		return;
	}

	cambay_get_template_partial( 'template-parts/post', 'entry-author' );
}

/**
 * Display post pagination on home, archive and search pages.
 *
 * @since 1.0.0
 */
function cambay_post_pagination() {
	if ( is_singular() ) {
		return;
	}

	the_posts_pagination(
		[
			'mid_size'           => 2,
			'prev_text'          => '<span class="screen-reader-text">' . esc_html__( 'Previous', 'cambay' ) . '</span>' . cambay_get_icon( array( 'icon' => 'angle-left' ) ) . '<span class="post-pagi">' . esc_html__( 'Previous', 'cambay' ) . '</span>',
			'next_text'          => '<span class="screen-reader-text show-on-mobile">' . esc_html__( 'Next', 'cambay' ) . '</span><span class="post-pagi">' . esc_html__( 'Next', 'cambay' ) . '</span>' . cambay_get_icon( array( 'icon' => 'angle-right' ) ),
			'before_page_number' => '<span class="meta-nav screen-reader-text">' . esc_html__( 'Page', 'cambay' ) . ' </span>',
		]
	);
}

/**
 * Display post navigation on single posts.
 *
 * @since 1.0.0
 */
function cambay_post_navigation() {
	if ( ! is_singular( 'post' ) ) {
		return;
	}

	the_post_navigation(
		[
			'next_text' => '<span class="nav-text">' . esc_html__( 'Next Post ', 'cambay' ) . '</span><span class="post-title screen-reader-text">%title</span>',
			'prev_text' => '<span class="nav-text">' . esc_html__( ' Previous Post', 'cambay' ) . '</span><span class="post-title screen-reader-text">%title</span>',
		]
	);
}

/**
 * Display image navigation on image attachment pages.
 *
 * @since 1.0.0
 */
function cambay_image_navigation() {
	if ( ! ( is_attachment() && wp_attachment_is_image() ) ) {
		return;
	}
	?>
	<nav id="image-navigation" class="navigation image-navigation">
		<div class="nav-links">
			<div class="nav-previous"><?php previous_image_link( false, esc_html__( 'Previous Image', 'cambay' ) ); ?></div>
			<div class="nav-next"><?php next_image_link( false, esc_html__( 'Next Image', 'cambay' ) ); ?></div>
		</div><!-- .nav-links -->
	</nav><!-- .image-navigation -->
	<?php
}

/**
 * Display sidebar widgets.
 *
 * @since 1.0.0
 */
function cambay_sidebar() {
	if ( is_singular() ) {

		if ( is_single() && 'no-sidebar' === cambay_get_mod( 'cambay_post_sidebar', 'none' ) ) {
			return;
		}

		if ( is_page() && 'no-sidebar' === cambay_get_mod( 'cambay_page_sidebar', 'none' ) ) {
			return;
		}

		cambay_widgets(
			'sidebar-widget-area',
			'sidebar-widget-area',
			esc_html__( 'Sidebar Widget Area', 'cambay' ),
			'sidebar'
		);
	}
}

/**
 * Display footer widgets.
 *
 * @since 1.0.0
 */
function cambay_footer_widgets() {
	cambay_widgets(
		'footer-widget-area',
		'footer-widget-area widgetlayer flex-wrapper',
		esc_html__( 'Footer Widget Area', 'cambay' ),
		'home-widgetlayer-3'
	);
}

/**
 * Site footer items wrapper markup.
 *
 * @since 1.0.0
 */
function cambay_footer_items() {
	cambay_markup(
		'footer-items',
		[
			'cambay_footer_text',
			'cambay_social_menu',
		]
	);
}

/**
 * Scroll to top button markup.
 *
 * @since 1.0.0
 */
function cambay_scroll_to_top() {
	printf(
		'<button class="scrl-to-top">%1$s<span class="screen-reader-text">%2$s</span></button>',
		cambay_get_icon( [ 'icon' => 'arrow-up' ] ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		esc_html__( 'Scroll to top of the page', 'cambay' )
	);
}

/**
 * Display footer text.
 *
 * Escape footer text and replace year, title and symbol placeholders with
 * proper markup.
 *
 * @since 1.0.0
 * @return void
 */
function cambay_footer_text() {
	// Note: Footer text is escaped via `cambay_escape()`.
	$footer_text = cambay_get_mod( 'cambay_footer_text', 'html' );
	if ( '' === $footer_text ) {
		return;
	}

	$output = str_replace( '[current_year]', esc_html( date_i18n( __( 'Y', 'cambay' ) ) ), $footer_text );
	$output = str_replace( '[site_title]', get_bloginfo( 'name', 'display' ), $output );
	$output = str_replace( '[copy_symbol]', '&copy;', $output );

	printf( '<div class="footer-text">%1$s</div>', $output ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}

/**
 * Add SVG definitions to the site footer.
 *
 * @since 1.0.0
 */
function cambay_svg_icons() {
	if ( has_nav_menu( 'social' ) ) {
		include_once get_parent_theme_file_path( 'assets/images/icons-with-social.svg' );
	} else {
		include_once get_parent_theme_file_path( 'assets/images/icons.svg' );
	}
}
