<?php
/**
 * benqu functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package benqu
 */

/**
* Define Dir
*/
define( 'BENQU_THEME_DRI', get_template_directory() );
define( 'BENQU_THEME_URI', get_template_directory_uri() );
define( 'BENQU_CSS_PATH', BENQU_THEME_URI . '/assets/css' );
define( 'BENQU_JS_PATH', BENQU_THEME_URI . '/assets/js' );
define( 'BENQU_ICON_PATH', BENQU_THEME_URI . '/assets/fonts/fontawesome/css' );
define( 'BENQU_IMG_PATH', BENQU_THEME_URI . '/assets/images' );

/**
 * Sets up theme defaults and registers support for various WordPress features.
 *
 * Note that this function is hooked into the after_setup_theme hook, which
 * runs before the init hook. The init hook is too late for some features, such
 * as indicating support for post thumbnails.
 */
function benqu_setup() {
	/*
		* Make theme available for translation.
		* Translations can be filed in the /languages/ directory.
		* If you're building a theme based on benqu, use a find and replace
		* to change 'benqu' to the name of your theme in all the template files.
		*/
	load_theme_textdomain( 'benqu', get_template_directory() . '/languages' );

	

	// Add default posts and comments RSS feed links to head.
	add_theme_support( 'automatic-feed-links' );
	remove_theme_support( 'widgets-block-editor' );
	add_theme_support( 'woocommerce' );
	add_theme_support( 'wc-product-gallery-zoom' );
    add_theme_support( 'wc-product-gallery-lightbox' );
    add_theme_support( 'wc-product-gallery-slider' );

	// add support for post formats
	add_theme_support('post-formats', [
		'standard', 'image', 'video', 'gallery'
	]);
	/*
		* Let WordPress manage the document title.
		* By adding theme support, we declare that this theme does not use a
		* hard-coded <title> tag in the document head, and expect WordPress to
		* provide it for us.
		*/
	add_theme_support( 'title-tag' );

	/*
		* Enable support for Post Thumbnails on posts and pages.
		*
		* @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
		*/
	add_theme_support( 'post-thumbnails' );
	
	
	add_image_size( 'benqu-img-size1', 100, 100, true );
	add_image_size( 'benqu-img-size2', 305, 350, true );
	add_image_size( 'benqu-img-size3', 371, 450, true );
	add_image_size( 'benqu-img-size4', 1296, 700, true );
	add_image_size( 'benqu-img-size5', 960, 520, true );

	// This theme uses wp_nav_menu() in one location.
	register_nav_menus(
		array(
			'main_menu' => esc_html__( 'Main Menu', 'benqu' ),
			'footer-nav' => esc_html__( 'Footer Menu', 'benqu' ),
		)
	);

	/*
		* Switch default core markup for search form, comment form, and comments
		* to output valid HTML5.
		*/
	add_theme_support(
		'html5',
		array(
			'search-form',
			'comment-form',
			'comment-list',
			'gallery',
			'caption',
			'style',
			'script',
		)
	);

	// Set up the WordPress core custom background feature.
	add_theme_support(
		'custom-background',
		apply_filters(
			'benqu_custom_background_args',
			array(
				'default-color' => 'ffffff',
				'default-image' => '',
			)
		)
	);

	// Add theme support for selective refresh for widgets.
	add_theme_support( 'customize-selective-refresh-widgets' );

	/**
	 * Add support for core custom logo.
	 *
	 * @link https://codex.wordpress.org/Theme_Logo
	 */
	add_theme_support(
		'custom-logo',
		array(
			'height'      => 250,
			'width'       => 250,
			'flex-width'  => true,
			'flex-height' => true,
		)
	);
}
add_action( 'after_setup_theme', 'benqu_setup' );

/**
 * Set the content width in pixels, based on the theme's design and stylesheet.
 *
 * Priority 0 to make it available to lower priority callbacks.
 *
 * @global int $content_width
 */
function benqu_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'benqu_content_width', 640 );
}
add_action( 'after_setup_theme', 'benqu_content_width', 0 );

/**
 * Register widget area.
 *
 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
 */
function benqu_widgets_init() {
	register_sidebar(
		array(
			'name'          => esc_html__( 'Sidebar', 'benqu' ),
			'id'            => 'sidebar-1',
			'description'   => esc_html__( 'Add widgets here.', 'benqu' ),
			'before_widget' => '<section id="%1$s" class="widget %2$s">',
			'after_widget'  => '</section>',
			'before_title'  => '<div class="benqu-section-title-wrap text-left"><h2 class="widget-title">',
			'after_title'   => '</h2><span></span></div>',
		)
	);
	register_sidebar(
		array(
			'name'          => esc_html__( 'Footer 1', 'benqu' ),
			'id'            => 'footer-1',
			'description'   => esc_html__( 'Add Footer widgets here.', 'benqu' ),
			'before_widget' => '<div id="%1$s" class="widget %2$s pfy-footer-widget-item">',
			'after_widget'  => '</div>',
			'before_title'  => '<h4 class="footer-title">',
			'after_title'   => '</h4>',
		)
	);
	register_sidebar(
		array(
			'name'          => esc_html__( 'Footer 2', 'benqu' ),
			'id'            => 'footer-2',
			'description'   => esc_html__( 'Add Footer widgets here.', 'benqu' ),
			'before_widget' => '<div id="%1$s" class="widget %2$s mrb pfy-footer-widget-item">',
			'after_widget'  => '</div>',
			'before_title'  => '<h4 class="footer-title">',
			'after_title'   => '</h4>',
		)
	);
	register_sidebar(
		array(
			'name'          => esc_html__( 'Footer 3', 'benqu' ),
			'id'            => 'footer-3',
			'description'   => esc_html__( 'Add Footer widgets here.', 'benqu' ),
			'before_widget' => '<div id="%1$s" class="widget %2$s mrb pfy-footer-widget-item">',
			'after_widget'  => '</div>',
			'before_title'  => '<h4 class="footer-title">',
			'after_title'   => '</h4>',
		)
	);
	register_sidebar(
		array(
			'name'          => esc_html__( 'Footer 4', 'benqu' ),
			'id'            => 'footer-4',
			'description'   => esc_html__( 'Add Footer widgets here.', 'benqu' ),
			'before_widget' => '<div id="%1$s" class="widget %2$s mrb pfy-footer-widget-item">',
			'after_widget'  => '</div>',
			'before_title'  => '<h4 class="footer-title">',
			'after_title'   => '</h4>',
		)
	);
	register_sidebar(
		array(
			'name'          => esc_html__( 'Footer 5', 'benqu' ),
			'id'            => 'footer-5',
			'description'   => esc_html__( 'Add Footer widgets here.', 'benqu' ),
			'before_widget' => '<div id="%1$s" class="widget %2$s mrb pfy-footer-widget-item">',
			'after_widget'  => '</div>',
		)
	);
	
	register_sidebar(
		array(
			'name'          => esc_html__( 'Side Nav Widget', 'benqu' ),
			'id'            => 'sidebar-nav',
			'description'   => esc_html__( 'Add widgets here.', 'benqu' ),
			'before_widget' => '<section id="%1$s" class="widget %2$s">',
			'after_widget'  => '</section>',
		)
	);
}
add_action( 'widgets_init', 'benqu_widgets_init' );
/**
 * Load Google Fonts.
 */
 
if ( ! function_exists( 'benqu_fonts_url' ) ) :
	/**
	 * Register Google fonts for Blessing.
	 */

   function benqu_fonts_url() {
	$fonts_url = '';
	$font_families     = array();
	$subsets   = 'latin';

	/* translators: If there are characters in your language that are not supported by DM Sans, translate this to 'off'. Do not translate into your own language. */
	if ( 'off' !== _x( 'on', 'DM Sans: on or off', 'benqu' ) ) {
		$font_families[] = 'DM Sans:400,400i,500,500i,700,700i';
	}
	
	if ( 'off' !== _x( 'on', 'Encode Sans Condensed: on or off', 'benqu' ) ) {
		$font_families[] = 'Encode Sans Condensed:100,200,300,400,500,600,700,800,900';
	}

	if ( 'off' !== _x( 'on', 'Lora: on or off', 'benqu' ) ) {
		$font_families[] = 'Lora:400,400i,500,500i,600,600i,700,700i';
	}

	if ( $font_families ) {
		$fonts_url = add_query_arg( array(
			'family' => urlencode( implode( '|', $font_families ) ),
			'subset' => urlencode( $subsets ),
		), 'https://fonts.googleapis.com/css' );
	}
	return esc_url_raw( $fonts_url );
}
endif;

/**
 * Enqueue scripts and styles.
 */
function benqu_scripts() { 
// Add custom fonts, used in the taffees stylesheet.
	wp_enqueue_style( 'benqu-custom-fonts', benqu_fonts_url(), array(), null );

	wp_enqueue_style( 'bootstrap', BENQU_CSS_PATH . '/bootstrap.min.css' );
	wp_enqueue_style( 'font-awesome-all', BENQU_ICON_PATH . '/all.min.css' );
	wp_enqueue_style( 'sidebarnav', BENQU_CSS_PATH . '/sidebarnav.min.css' );
	wp_enqueue_style( 'animation', BENQU_CSS_PATH . '/animation.css' );
	wp_enqueue_style( 'owl-carousel', BENQU_CSS_PATH . '/owl.carousel.min.css' );
	wp_enqueue_style( 'slick-slider', BENQU_CSS_PATH . '/slick.css' );
	wp_enqueue_style( 'magnific-popup', BENQU_CSS_PATH . '/magnific-popup.css' );
	wp_enqueue_style( 'metis-menu', BENQU_CSS_PATH . '/metis-menu.css' );
	wp_enqueue_style( 'benqu-post', BENQU_CSS_PATH . '/post-style.css' );
	wp_enqueue_style( 'benqu-main', BENQU_CSS_PATH . '/benqu-style.css' );
	if(class_exists('woocommerce')){
		wp_enqueue_style( 'benqu-woocommerce', BENQU_CSS_PATH . '/woocommerce.css' );
	}
	if(is_rtl()){
		wp_enqueue_style( 'benqu-rtl', BENQU_CSS_PATH . '/rtl.css' );
	}
	wp_enqueue_style( 'benqu-responsive', BENQU_CSS_PATH . '/responsive.css' );
	wp_enqueue_style( 'benqu-style', get_stylesheet_uri(), array(), '1.0' );

	wp_enqueue_script('masonry');
	wp_enqueue_script( 'popper', BENQU_JS_PATH . '/popper.min.js', array('jquery'), '2.9.2', true );
	wp_enqueue_script( 'bootstrap', BENQU_JS_PATH . '/bootstrap.min.js', array('jquery'), '5.1.3', true );
	wp_enqueue_script( 'owl-carousel', BENQU_JS_PATH . '/owl.carousel.min.js', array('jquery'), '1.0.0', true );
	wp_enqueue_script( 'slick', BENQU_JS_PATH . '/slick.min.js', array('jquery'), '1.0.0', true );
	wp_enqueue_script( 'metisMenu', BENQU_JS_PATH . '/metisMenu.min.js', array('jquery'), '1.0.0', true );
	wp_enqueue_script( 'sidebarnav', BENQU_JS_PATH . '/sidebarnav.min.js', array('jquery'), '1.0.0', true );
	wp_enqueue_script( 'resize-sensor', BENQU_JS_PATH . '/resize-sensor.min.js', array('jquery'), '1.0.0', true );
	wp_enqueue_script( 'theia-sticky-sidebar', BENQU_JS_PATH . '/theia-sticky-sidebar.min.js', array('jquery'), '1.0.0', true );
	wp_enqueue_script( 'wow', BENQU_JS_PATH . '/wow.min.js', array('jquery'), '1.0.0', true );
	wp_enqueue_script( 'jquery-magnific-popup', BENQU_JS_PATH . '/jquery.magnific-popup.min.js', array('jquery'), '1.1.0', true );
	wp_enqueue_script( 'benqu-custom', BENQU_JS_PATH . '/scripts.js', array('jquery'), '1.0.0', true );

	wp_localize_script( 'benqu-custom', 'benqu_ajax', array(
		'ajax_url' => admin_url( 'admin-ajax.php' ),
		'post_scroll_limit' => 10,
		'nonce' => wp_create_nonce( 'benqu-nonce' ),
		'is_rtl' => is_rtl()
	) );
	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}
}
add_action( 'wp_enqueue_scripts', 'benqu_scripts' );

/**
 * Implement the Custom Header feature.
 */
require BENQU_THEME_DRI . '/inc/custom-header.php';

/**
 * Custom template tags for this theme.
 */
require BENQU_THEME_DRI . '/inc/template-tags.php';

/**
 * Functions which enhance the theme by hooking into WordPress.
 */
require BENQU_THEME_DRI . '/inc/template-functions.php';

/**
 * Customizer additions.
 */
require BENQU_THEME_DRI . '/inc/customizer.php';

/**
 * Theme Custom Functions
 */
require BENQU_THEME_DRI . '/inc/benqu-functions.php';

/**
 * Required Plugin Activation
 */
require BENQU_THEME_DRI . '/libs/plugin-activation.php';

/**
 * OCDI Import Functions
 */
require BENQU_THEME_DRI . '/libs/ocdi/functions.php';

/**
 * Custom Style
 */
require BENQU_THEME_DRI . '/inc/custom-style.php';

/**
 * Load Jetpack compatibility file.
 */
if ( defined( 'JETPACK__VERSION' ) ) {
	require BENQU_THEME_DRI . '/inc/jetpack.php';
}

/**
 * Implement the Custom Header feature.
 */
if ( file_exists( BENQU_THEME_DRI.'/inc/cs-framework-functions.php' ) ) {
	require_once  BENQU_THEME_DRI.'/inc/cs-framework-functions.php';
}

/**
 *  benqu Navwalker
 */
require BENQU_THEME_DRI . '/inc/class-wp-benqu-navwalker.php';


function benqu_single_scroll_post() {
	$current_post = isset( $_POST['current_post'] ) ? absint( $_POST['current_post'] ) : 1;
	$cat_ids = isset( $_POST['cat_ids'] ) ? explode( ',', $_POST['cat_ids'] ) : [];

	$authore_info_show = cs_get_option('authore_info_show');
	$single_post_comment_disable = cs_get_option('single_post_comment_disable');
	$blog_tags = cs_get_option('blog_tags');
	$blog_top_share = cs_get_option('blog_top_share');
	$blog_top_share = is_null($blog_top_share) ? true : $blog_top_share;
	$blog_inner_share = cs_get_option('blog_inner_share');
	$blog_inner_share = is_null($blog_inner_share) ? true : $blog_inner_share;
	$blog_share = cs_get_option('blog_share');
	$blog_nav = cs_get_option('blog_nav');
	$authore_show = cs_get_option('authore_show');
	$view_count_disable = cs_get_option('view_count_disable');
	$post_comment_disable = cs_get_option('post_comment_disable');
	$post_date_disable = cs_get_option('post_date_disable');

	ob_start(); 

	$args = [
		'post_type' => 'post',
		'post_status' => 'publish',
		'posts_per_page' => 1, 
		'category__in' => $cat_ids, 
		'offset' => $current_post, 
		'ignore_sticky_posts' => 1,
	];

	$query = new WP_Query( $args );

	global $withcomments; 
	$withcomments = true; 

	while ( $query->have_posts() ) {
		$query->the_post();	?>
		
			<div class="bnq__post_wap_loop ajax_top_space">
					<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
							<header class="entry-header">
									<?php benqu_category_badge();?>
									<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
									<div class="pfy-single-post-meta d-flex align-items-center">
											<ul>
													<?php if($authore_show != false):?>
														<li><span class="auth_by"><?php esc_html_e( 'BY', 'benqu' ); ?></span> <?php the_author()?></li>
													<?php endif;?>
													<?php if($post_date_disable == true):?>
														<li><i class="fal fa-calendar-alt"></i> <?php echo esc_html(get_the_time( get_option('date_format')));?></li>
													<?php endif;?>
													<?php if($post_comment_disable == true):?>
															<li>
																	<i class="far fa-comments"></i> <?php echo esc_attr(get_comments_number());?> 
																	<?php
																			if(get_comments_number() == 1){
																					esc_html_e( 'Comment', 'benqu' );
																			}else{
																					esc_html_e( 'Comments', 'benqu' );
																			}						 
																	?>
															</li>
													<?php endif;?>
													<?php if(function_exists('benqu_get_views') && $view_count_disable == true): ?>
															<li><i class="fal fa-eye"></i> <?php echo esc_attr(benqu_get_views()); ?> <?php esc_html_e( 'Views', 'benqu' );?></li>
													<?php endif;?>
											</ul>
											<div class="bnq__social-top">
													<?php if(function_exists('benqu_post_share_two') && $blog_top_share == true){ benqu_post_share_two(); } ?>
											</div>
									</div>
							</header><!-- .entry-header -->
							<div class="pfy-single-post-thumb">
									<?php the_post_thumbnail( 'full', ['class' => 'img-fluid'] );?>
							</div>
							<div class="bnq-single-con <?php if( !function_exists('benqu_post_share_two') || $blog_inner_share == false){echo esc_attr("no-social");} ?>">
									<?php if(function_exists('benqu_post_share_two') && $blog_inner_share == true): ?>
										<div class="bnq-inner-social">
											<?php benqu_post_share_two(); ?>
										</div>
									<?php endif; ?>
									<div class="entry-content">
											<?php
											the_content(
													sprintf(
															wp_kses(
																	/* translators: %s: Name of current post. Only visible to screen readers */
																	__( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'benqu' ),
																	array(
																			'span' => array(
																					'class' => array(),
																			),
																	)
															),
															wp_kses_post( get_the_title() )
													)
											);

											wp_link_pages(
													array(
															'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'benqu' ),
															'after'  => '</div>',
													)
											);
											?>
									</div><!-- .entry-content -->
							</div>                    
							
							<?php if(true == $blog_tags || function_exists('benqu_post_share')):?>
									<footer class="entry-footer">
											<div class="row align-items-center">
													<div class="col-lg-6 col-6">                                
															<?php if(true == $blog_tags){ benqu_entry_footer(); }?>
													</div>
													<div class="col-lg-6 col-6 text-end">
															<?php 
																	if(function_exists('benqu_post_share') && $blog_share != false){
																			benqu_post_share();
																	} 
															?>
													</div>
											</div>                        
									</footer><!-- .entry-footer -->
							<?php endif;?>
					</article><!-- #post-<?php the_ID(); ?> -->

					<?php
							if(function_exists('benqu_single_post_pagination') && true == $blog_nav){
									benqu_single_post_pagination();
							}
							
							if(function_exists('benqu_theme_author_box') && true == $authore_info_show){
									benqu_theme_author_box();
							}

							// If comments are open or we have at least one comment, load up the comment template.
							if($single_post_comment_disable == true ){                            
									if ( comments_open() || get_comments_number()) :
											comments_template();
									endif;
							}
					?>
			</div>
	<?php 
	}
	

	$post_data = ob_get_clean(); 

	wp_send_json_success( $post_data );
}
add_action( 'wp_ajax_benqu_single_scroll_post', 'benqu_single_scroll_post' ); 
add_action( 'wp_ajax_nopriv_benqu_single_scroll_post', 'benqu_single_scroll_post' );