<?php
/**
 * Functions which enhance the theme by hooking into WordPress
 *
 * @package Book Review Blog
 */

function book_review_blog_body_classes( $classes ) {
	global $post;

	if ( ! is_singular() ) {
		$classes[] = 'hfeed';
	}
	$classes[] = 'book-review-blog-theme-active theme-by-rswpthemes';
	$backgroundColor = get_theme_mod('background_color', 'f1f1f1');
	if ('f1f1f1' == $backgroundColor) {
		$classes[] = 'custom-background';
	}
	if (is_page()) {
		$get_page_sidebar_settings = get_theme_mod('page_sidebar', 'right');
		$sidebar_layouts = $get_page_sidebar_settings;
		$indvPageSidebarSettings = get_post_meta( $post->ID, 'book_review_blog_sidebar-settings', true );
		if ('customizer' === $indvPageSidebarSettings || empty($indvPageSidebarSettings)) {
			$sidebar_layouts = $get_page_sidebar_settings;
		}else{
			$sidebar_layouts = $indvPageSidebarSettings;
		}
		$classes[] = $sidebar_layouts . '-sidebar';
	}elseif (is_single()) {
		$get_post_sidebar_settings = get_theme_mod('post_sidebar', 'right');
		$sidebar_layouts = $get_post_sidebar_settings;
		$indvPostSidebarSettings = get_post_meta( $post->ID, 'book_review_blog_sidebar-settings', true );
		if ('customizer' === $indvPostSidebarSettings || empty($indvPostSidebarSettings)) {
			$sidebar_layouts = $get_post_sidebar_settings;
		}else{
			$sidebar_layouts = $indvPostSidebarSettings;
		}
		$classes[] = $sidebar_layouts . '-sidebar';
	}

	// Adds a class of no-sidebar when there is no sidebar present.
	if ( ! is_active_sidebar( 'right-sidebar' ) ) {
		$classes[] = 'no-sidebar';
	}

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

/**
 * Add a pingback url auto-discovery header for single posts, pages, or attachments.
 */
function book_review_blog_pingback_header() {
	if ( is_singular() && pings_open() ) {
		echo '<link rel="pingback" href="', esc_url( get_bloginfo( 'pingback_url' ) ), '">';
	}
}
add_action( 'wp_head', 'book_review_blog_pingback_header' );

/**
 * Author VCard
 */
function book_review_blog_author_vcard() {
	?>
	<div class="author-vcard">
		<div class="author-vcard__image">
			<?php
			echo get_avatar( get_the_author_meta( 'ID' ), 100, '', '', null );
			?>
		</div>
		<div class="author-vcard__about">
			<h4><?php echo esc_html( get_the_author_meta( 'nickname' ) ); ?></h4>
			<?php if(!empty(get_the_author_meta( 'description' ))): ?>
			<p><?php echo wp_kses_post( get_the_author_meta( 'description' ) ); ?></p>
			<?php endif; ?>
			<p>
			<?php
			$userpost_count = count_user_posts( get_the_author_meta( 'ID' ), 'post', false );
			if ( $userpost_count > 1 ) {
				$numberingtext = 'posts';
			} else {
				$numberingtext = 'post';
			}
			$userposts = esc_html__( 'The author has %1$s %2$s', 'book-review-blog' );
			printf( $userposts, $userpost_count, $numberingtext );
			?>
			 </p>
		</div>
	</div>
	<?php
	return;
}

/**
 * Get Current Year
 */

 function book_review_blog_currentYear(){
    return date('Y');
}

/**
 * Get theme Data
 */
function book_review_blog_author_uri(){
	$themeData = wp_get_theme();
	return $authorURI = $themeData->get( 'AuthorURI' );
}

/**
 * Limit Excerpt length
 */
function book_review_blog_post_excerpt_limit( $length ) {
	if (is_admin()) {
		return $length;
	}
	$length = get_theme_mod('post_loop_excerpt_limit', 42);
   return $length;
}
add_filter( 'excerpt_length', 'book_review_blog_post_excerpt_limit', 999 );

/**
 * has social links
 */

function book_review_blog_has_social_links() {
    $social_media_fields = array(
        'facebook'       => 'Facebook',
        'twitter'        => 'Twitter',
        'instagram'      => 'Instagram',
        'tiktok'         => 'Tiktok',
        'linkedin'       => 'Linkedin',
        'pinterest'      => 'Pinterest',
        'line'           => 'Line',
        'github'         => 'Github',
        'discord'        => 'Discord',
        'youtube'        => 'Youtube',
        'wordpress'      => 'WordPress',
        'slack'          => 'Slack',
        'apple'          => 'Apple',
        'stack-overflow' => 'Stack-overflow',
        'kickstarter'    => 'Kickstarter',
        'dribbble'       => 'Dribbble',
        'codepen'        => 'Codepen',
        'whatsapp'       => 'Whatsapp',
        'medium'         => 'Medium',
        'goodreads'      => 'Goodreads'
    );

    foreach ( $social_media_fields as $field => $label ) {
        $value = get_theme_mod( $field, '#' );
        if ( ! empty( $value ) && '#' !== $value ) {
            return true; // Return true as soon as a valid link is found
        }
    }

    return false; // No valid links found
}


/**
 * Social Links
 */
function book_review_blog_social_links(){
	$social_media_fields = array(
	    'facebook' => 'Facebook',
	    'twitter' => 'Twitter',
	    'instagram' => 'Instagram',
	    'tiktok' => 'Tiktok',
	    'linkedin' => 'Linkedin',
	    'pinterest' => 'Pinterest',
	    'line' => 'Line',
	    'github' => 'Github',
	    'discord' => 'Discord',
	    'youtube' => 'Youtube',
	    'wordpress' => 'WordPress',
	    'slack' => 'Slack',
	    'apple' => 'Apple',
	    'stack-overflow' => 'Stack-overflow',
	    'kickstarter' => 'kickstarter',
	    'dribbble' => 'Dribbble',
	    'codepen' => 'Codepen',
	    'whatsapp' => 'Whatsapp',
	    'medium' => 'Medium',
	    'goodreads' => 'Goodreads'
	);
	foreach ( $social_media_fields as $field => $label ) {
	    $value = get_theme_mod( $field, '#' );
	    if ( ! empty( $value ) && '#' != $value) {
	        echo '<a target="_blank" href="' . esc_url( $value ) . '" class="rswpthemes-icon icon-'.$field.'"></a>';
	    }
	}
}
function book_review_blog_get_books_layout(){
	$getBooksLayout = get_theme_mod('books_layout', 'product');
	return $getBooksLayout;
}

function book_review_blog_template_chooser($template){
	  global $wp_query;
	  $post_type = get_query_var('post_type');
	  if( $wp_query->is_search && $post_type == 'books-gallery' )
	  {
	    return locate_template('search-books.php');  //  redirect to archive-search.php
	  }
	  return $template;
}
add_filter('template_include', 'book_review_blog_template_chooser');

add_action('admin_menu', 'book_review_blog_personalize');
function book_review_blog_personalize(){
	remove_submenu_page( 'sb-instagram-feed', 'sb-instagram-feed-about' );
}

add_action('sbi_before_feed', 'book_review_blog_sbi_before_feed');
function book_review_blog_sbi_before_feed(){
	?>
	<div class="book-review-blog-instagram-section">
	<?php
}

add_action('sbi_after_feed', 'book_review_blog_sbi_after_feed');
function book_review_blog_sbi_after_feed(){
	?>
	</div>
	<?php
}

function book_review_blog_page_header_control(){
	$get_page_header = true;
	if (is_archive()) {
		$get_page_header = get_theme_mod('show_page_banner_on_archive_pages', true);
	}elseif(is_page()){
		$get_page_header = get_theme_mod('show_page_banner_on_pages', true);
	}
	$page_header = $get_page_header;
	$page_meta_header = true;
	if (is_page()) :
		$get_page_meta_header_settings = get_post_meta( get_the_ID(), 'book_review_blog_show-page-banner', true );
		if ('show' == $get_page_meta_header_settings) {
			$page_meta_header = true;
		}elseif('hide' == $get_page_meta_header_settings){
			$page_meta_header = false;
		}
		if ('customizer' === $get_page_meta_header_settings || NULL === $get_page_meta_header_settings) {
			$page_header = $get_page_header;
		}else{
			$page_header = $page_meta_header;
		}
	endif;

	return $page_header;
}


function book_review_blog_utm_url($medium){
    $rawUrl = sprintf(
        'https://rswpthemes.com/rs-wp-book-showcase-wordpress-plugin/?utm_source=free_theme&utm_medium=%s&utm_campaign=book_review_blog',
        rawurlencode($medium)
    );
    return $rawUrl;
}

function book_review_blog_utm_url_for_rswpbs($medium){
    $rawUrl = sprintf(
        'https://rswpthemes.com/rs-wp-book-showcase-wordpress-plugin/?utm_source=free_theme&utm_medium=%s&utm_campaign=book_review_blog',
        rawurlencode($medium)
    );
    return $rawUrl;
}

/**
 * book_review_blog_is_woocommerce_exist
 */
if ( ! function_exists( 'book_review_blog_is_woocommerce_exists' ) ) {
	function book_review_blog_is_woocommerce_exists() {
		if ( class_exists( 'woocommerce' ) && is_woocommerce() ) {
			return true;
		}
		return false;
	}
}
/**
 * book_review_blog is default page
 */
if ( ! function_exists( 'book_review_blog_is_default_page' ) ) {
	function book_review_blog_is_default_page() {
		if ( is_home() || is_archive() || is_404() || is_author() || is_category() || book_review_blog_is_woocommerce_exists() || is_search() || is_tag() ) {
			return true;
		}
		return false;
	}
}

/**
 * Fix skip link focus in IE11.
 * This does not enqueue the script because it is tiny and because it is only for IE11,
 * thus it does not warrant having an entire dedicated blocking script being loaded.
 *
 * @link https://git.io/vWdr2
 */
function book_review_blog_skip_link_focus_fix() {
	// The following is minified via `terser --compress --mangle -- js/skip-link-focus-fix.js`.
	?>
	<script>
	/(trident|msie)/i.test(navigator.userAgent)&&document.getElementById&&window.addEventListener&&window.addEventListener("hashchange",function(){var t,e=location.hash.substring(1);/^[A-z0-9_-]+$/.test(e)&&(t=document.getElementById(e))&&(/^(?:a|select|input|button|textarea)$/i.test(t.tagName)||(t.tabIndex=-1),t.focus())},!1);
	</script>
	<?php
}
add_action( 'wp_print_footer_scripts', 'book_review_blog_skip_link_focus_fix' );


/**
 * Header Template
 */
add_action('book_review_blog_before_site_content', 'book_review_blog_header_template', 10);
function book_review_blog_header_template(){
	get_template_part( 'template-parts/header/header', 'one' );
}

/**
 * Footer Template
 */
add_action('book_review_blog_after_site_content', 'book_review_blog_footer_template', 5);
function book_review_blog_footer_template(){
	$show_scroll_to_top_button = get_theme_mod('show_scroll_to_top_button', true);
	$button_extra_class = '';
	if (true == get_theme_mod('hide_button_on_mobile_device', true)) {
		$button_extra_class = ' hide-button-on-mobile';
	}

	$showFooterTop = get_theme_mod('show_footer_top_section', true);
	$showFooterBottom = get_theme_mod('show_footer_bottom_section', true);

	?>
	<footer id="colophon" class="site-footer">
		<?php
		if (true == $showFooterTop) {
			get_template_part('template-parts/footer/footer-top-area');
		}
		if (true == $showFooterBottom) {
			get_template_part('template-parts/footer/footer-bottom-area');
		}
		?>
		<!-- Footer Copyright Section -->
		<section class="site-copyright">
			<div class="container">
				<div class="row">
					<div class="col-md-12 align-self-center">
						<div class="site-info text-center">
							<div class="site-copyright-text d-inline-block">
							<?php
							echo wp_kses_post( get_theme_mod('copyright_text', __( 'Copyright <i class="rswpthemes-icon icon-copyright-regular" aria-hidden="true"></i> 2025. All rights reserved.', 'book-review-blog' )));
							?>
							</div>
						</div><!-- .site-info -->
						<?php book_review_blog_theme_by(); ?>
					</div>
				</div>
			</div>
		</section>
	</footer><!-- #colophon -->
	<?php if(true == $show_scroll_to_top_button) : ?>
	<div class="scrooltotop<?php echo esc_attr($button_extra_class);?>">
		<a href="#" class="rswpthemes-icon icon-angle-up-solid"></a>
	</div>
	<?php endif;
}