<?php
/**
 * Functions which enhance the theme by hooking into WordPress
 *
 * @package construc
 */
/**
 * Adds custom classes to the array of body classes.
 *
 * @param array $classes Classes for the body element.
 * @return array
 */
function construc_body_classes( $classes ) {
	// Adds a class of hfeed to non-singular pages.
	if ( ! is_singular() ) {
		$classes[] = 'hfeed';
	}
	$classes[] = 'preloader-wrapper';
	$get_border_box_shadow = get_theme_mod( 'border_box_shadow_show_hide', true );

	if (false == $get_border_box_shadow) {
		$classes[] = 'border_and_box_shadow_hide';
	}

	// Adds a class of no-sidebar when there is no sidebar present.
	if ( ! is_active_sidebar( 'sidebar-1' ) ) {
		$classes[] = 'no-sidebar';
	}
	return $classes;
}
add_filter( 'body_class', 'construc_body_classes' );
/**
 * Add a pingback url auto-discovery header for single posts, pages, or attachments.
 */
function construc_pingback_header() {
	if ( is_singular() && pings_open() ) {
		echo '<link rel="pingback" href="', esc_url( get_bloginfo( 'pingback_url' ) ), '">';
	}
}
add_action( 'wp_head', 'construc_pingback_header' );


if ( ! function_exists( 'construc_comment_list' ) ) :
	/**
	 * Template for comments and pingbacks.
	 *
	 * Used as a callback by wp_list_comments() for displaying the comments.
	 *
	 * @since Shape 1.0
	 */
	function construc_comment_list( $comment, $args, $depth ) {

		extract( $args, EXTR_SKIP );

		if ( 'div' == $args['style'] ) {
			$tag = 'div';
			$add_below = 'comment';
		} else {
			$tag = 'li';
			$add_below = 'div-comment';
		}
		?>
  <<?php echo esc_attr( $tag ); ?> <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?> id="comment-<?php comment_ID(); ?>">
		<?php if ( 'div' == $args['style'] ) : ?>
  <div id="div-comment-<?php comment_ID(); ?>" class="comment-body review-list">
	<?php endif; ?>
	<div class="single-comment">
		<div class="commenter-image">
			<?php
			if ( 0 != $args['avatar_size'] ) {
				echo get_avatar( $comment, $args['avatar_size'] );}
			?>
		</div>
		<div class="commnenter-details">
		<?php if ( $comment->comment_approved == '0' ) : ?>
			<em><?php esc_html_e( 'Your comment is awaiting moderation.', 'construc' ); ?></em>
			<br />
		<?php endif; ?>
			<h4><?php printf( wp_kses_post( '%s', 'construc' ), sprintf( '%s', get_comment_author_link() ) ); ?></h4>
			<div class="comment-time">
				<p>
					<time datetime="<?php comment_time( 'c' ); ?>">
						<?php
							/* translators: 1: comment date, 2: comment time */
							printf( wp_kses_post( '%1$s at %2$s', 'construc' ), get_comment_date( '', $comment ), get_comment_time() );
						?>
					</time>
				</p>
			</div>
				<?php comment_text(); ?>
				<?php
				comment_reply_link(
					array_merge(
						$args,
						array(
							'depth'     => $depth,
							'max_depth' => $args['max_depth'],
							'before'    => '<div class="reply">',
							'after'     => '</div>',
						)
					)
				);
				?>
		</div>
	</div>
		<?php if ( 'div' == $args['style'] ) : ?>
  </div>
			<?php
  endif;

	}
endif; // ends check for construc_comment_list()

/**
 * Excerpt Length
 */

function construc_get_excerpt($limit, $source = null){
    if($source == "content" ? ($excerpt = get_the_content()) : ($excerpt = get_the_excerpt()));
    $excerpt = preg_replace(" (\[.*?\])",'',$excerpt);
    $excerpt = strip_shortcodes($excerpt);
    $excerpt = strip_tags($excerpt);
    $excerpt = substr($excerpt, 0, $limit);
    $excerpt = substr($excerpt, 0, strripos($excerpt, " "));
    $excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt));
    return $excerpt;
}
/**
 * Footer Credit
 */
if (!function_exists('construc_footer_credit')) {
	function construc_footer_credit(){
		return '<strong>'.__(' Theme:&nbsp;', 'construc').'</strong><a href="'.esc_url('https://smarterthemes.com/themes/wordpress-theme/best-wordpress-themes-for-blogs-and-e-commerce/').'"><span> '.__( 'construc', 'construc' ).'</span></a>';
	}
}
/**
 * construc Breadcrumbs
 */
if (!function_exists('construc_bredcrumbs')) {
	function construc_bredcrumbs(){
		?>
		<nav>
            <div class="breadcrumb">
                <?php
                if ( function_exists('yoast_breadcrumb') ) {
				  yoast_breadcrumb( '<p id="breadcrumbs">','</p>' );
				}
                ?>
            </div>
        </nav>
		<?php
	}
}
/**
 * Author VCard
 */
function construc_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 get_the_author_meta( 'nickname' ); ?></h4>
			<p><?php echo get_the_author_meta( 'description' ); ?></p>
			<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 user has only %1$s %2$s', 'construc' );
			printf( $userposts , $userpost_count, $numberingtext );
			 ?></p>
		</div>
	</div>
	<?php
	return;
}


/**
 * Adding Getting Started Page in admin menu
 */
function construc_admin_notice() {
    global $pagenow;
    $theme_args      = wp_get_theme();
    $meta            = get_option( 'construc-update-notice' );
    $name            = $theme_args->__get( 'Name' );
    $current_screen  = get_current_screen();


        if ( is_network_admin() ) {
            return;
        }

        if ( ! current_user_can( 'manage_options' ) ) {
            return;
        } ?>
        <div class="smarterthemes welcome-message notice notice-info">
            <div class="notice-wrapper">
                <div class="notice-text">
                    <h3><?php printf( __( 'Thank you for installing %1$s Theme.', 'construc' ), esc_html( $name ) ); ?></h3>
                    <p><?php esc_html_e( 'Use the button below to turn on advanced blogging and AI auto posting features.', 'construc'  ) ; ?></p>
                    <p>
                        <a href="<?php echo esc_url( 'https://www.quickwrite.ai/blog-special/' );?>" class="button button-primary" style="text-decoration: none;" target="_blank">
                            <?php esc_html_e( 'Get started with AutoPosting', 'construc' ); ?>
                        </a>
                    </p>
                    <p class="dismiss-link"><strong><a href="?construc-update-notice=1"><?php esc_html_e( 'Dismiss','construc' ); ?></a></strong></p>
                </div>
            </div>
        </div>
    <?php
}

add_action( 'admin_notices', 'construc_admin_notice' );

/**
 * construc Page title
 */
if (!function_exists('construc_banner_title_control')) {
	function construc_banner_title_control(){
		echo '<div class="title-breadcrumb-inner">';
		$page_main_title = single_post_title('', false);
		if (is_search()) :
			$page_main_title = sprintf( esc_html__( 'Search Results for: %s', 'construc' ), '<span>' . get_search_query() . '</span>' );
		elseif (is_home()) :
			$page_main_title = get_theme_mod( 'blog_header_custom_title', __( 'Build Blog For Free', 'construc' ));
		endif;
		if (is_author()) :
			wp_kses_post(construc_author_vcard());
		elseif (is_page('support-forum')):
			?>
			<h1 class="page-title"><?php single_post_title( '');?></h1>
			<?php
		elseif (is_tag()) :
			?>
			<h1 class="page-title"><?php single_tag_title();?></h1>
			<?php
		elseif (is_category()) :
			?>
			<h1 class="page-title"><?php single_cat_title();?></h1>
			<?php
		elseif (class_exists('woocommerce') && is_product_category()) :
			?>
				<h1 class="page-title"><?php single_cat_title('');?></h1>
			<?php
		elseif (class_exists('woocommerce') && is_product_tag()) :
			?>
			<h1 class="page-title"><?php single_tag_title();?></h1>
			<?php
		elseif(class_exists('woocommerce') && is_shop()):
			?>
			<h1 class="page-title"><?php woocommerce_page_title();?></h1>
			<?php
		elseif(class_exists('woocommerce') && is_product()):
			?>
			<h1 class="page-title"><?php single_post_title();?></h1>
			<?php
		elseif (is_page()):
			?>
			<h1 class="page-title"><?php single_post_title(''); ?></h1>
			<?php
		elseif (is_archive()) :
			the_archive_title( '<h1 class="page-title">', '</h1>' );
			the_archive_description( '<div class="archive-description">', '</div>' );
		else :
			?>
	        <h1 class="entry-title"><?php echo $page_main_title; ?></h1>
	       <?php 
   		endif;
    echo '</div>';
	}
}


function construc_hide_default_banner_from(){
	if(is_page_template('frontpage.php')){
		$banner_show = false;
	}elseif(is_page_template('blankpage.php')){
		$banner_show = false;
	}elseif(class_exists('woocommerce') && is_product()){
		$banner_show = false;
	}elseif(is_home()){
		$banner_show = false;
	}elseif(is_front_page()){
		$banner_show = false;
	}elseif(is_single()){
		$banner_show = false;
	}else{
		$banner_show = true;
	}
	return $banner_show;
}

/**
 * Default Banner
 * Customize From customizer API
 */
if (!function_exists('construc_default_page_banner')) {
	function construc_default_page_banner(){
		$thumb_attr = ' style="background-image: url('.esc_url(get_header_image()).')"';
		$extraclass = '';
		if (has_header_image()) {
			$extraclass = ' page_header_with_thumnail';
		}
		if (is_page()) {
			if (has_post_thumbnail(get_queried_object_id())) {
				$thumb_attr = ' style="background-image: url('.esc_url(get_the_post_thumbnail_url(get_queried_object_id())).')"';
				$extraclass = ' page_header_with_thumnail';
			}else{
				$thumb_attr = '';
				$extraclass = '';
			}
		}
		if (true === construc_hide_default_banner_from()) :
			?>
			 <div class="page-banner-area<?php echo esc_attr($extraclass);?>"<?php echo $thumb_attr;?>>
			    <div class="container">
			        <div class="row">
			            <div class="col-md-12 text-center">
			                <div class="page-banner-text">
								<?php
								construc_banner_title_control(); 
	  							construc_bredcrumbs();
								?>
			                </div>
			            </div>
			        </div>
			    </div>
			</div>
			<?php
		endif;
	}
}

/**
 * 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 construc_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', 'construc_skip_link_focus_fix' );
