<?php
/**
 * Custom functions that act independently of the theme templates.
 *
 * Eventually, some of the functionality here could be replaced by core features.
 *
 * @package fiona
 */

/**
 * Adds custom classes to the array of body classes.
 *
 * @param array $classes Classes for the body element.
 * @return array
 */
function news25_body_classes( $classes ) {
	// Adds a class of group-blog to blogs with more than 1 published author.
	if ( is_multi_author() ) {
		$classes[] = 'group-blog';
	}

	// Adds a class of hfeed to non-singular pages.
	if ( ! is_singular() ) {
		$classes[] = 'hfeed';
	}
	
	return $classes;
}
add_filter( 'body_class', 'news25_body_classes' );

if ( ! function_exists( 'wp_body_open' ) ) {
	/**
	 * Backward compatibility for wp_body_open hook.
	 *
	 * @since 1.0.0
	 */
	function wp_body_open() {
		do_action( 'wp_body_open' );
	}
}

if (!function_exists('news25_str_replace_assoc')) {

    /**
     * news25_str_replace_assoc
     * @param  array $replace
     * @param  array $subject
     * @return array
     */
    function news25_str_replace_assoc(array $replace, $subject) {
        return str_replace(array_keys($replace), array_values($replace), $subject);
    }
}

 /**
 * Add WooCommerce Cart Icon With Cart Count (https://isabelcastillo.com/woocommerce-cart-icon-count-theme-header)
 */
function news25_add_to_cart_fragment( $fragments ) {
	
    ob_start();
   $count = WC()->cart->cart_contents_count;
	$cart_url = wc_get_cart_url();
	
	if ( $count > 0 ) { ?>
		<span class="count"><?php echo esc_html( $count ); ?></span> 
	<?php 
	}
	else {	?>
		<span class="count"><?php esc_html_e('0','news-25'); ?></span> 
		<?php 
	}
    $fragments['span.count'] = ob_get_clean();
     
    return $fragments;
}
add_filter( 'woocommerce_add_to_cart_fragments', 'news25_add_to_cart_fragment' );



/**
 * Returns posts.
 *
 * @since Fiona 1.0
 */
if (!function_exists('news25_get_cat_posts')):
    function news25_get_cat_posts($number_of_posts, $category = '0')
    {
        $ins_args = array(
            'post_type' => 'post',
            'posts_per_page' => absint($number_of_posts),
            'post_status' => 'publish',
            'orderby' => 'date',
            'order' => 'DESC',
            'ignore_sticky_posts' => true
        );

        $category = isset($category) ? $category : '0';
        if (absint($category) > 0) {
            $ins_args['cat'] = absint($category);
        }

        $all_posts = new WP_Query($ins_args);

        return $all_posts;
    }
	
endif;


/*******************************************************************************
 *  Get Started Notice
 *******************************************************************************/

add_action( 'wp_ajax_news25_dismissed_notice_handler', 'news25_ajax_notice_handler' );

/**
 * AJAX handler to store the state of dismissible notices.
 */
function news25_ajax_notice_handler() {
    if ( isset( $_POST['type'] ) ) {
        // Pick up the notice "type" - passed via jQuery (the "data-notice" attribute on the notice)
        $type = sanitize_text_field( wp_unslash( $_POST['type'] ) );
        // Store it in the options table
        update_option( 'dismissed-' . $type, TRUE );
    }
}

function news25_deprecated_hook_admin_notice() {
        // Check if it's been dismissed...
        if ( ! get_option('dismissed-get_started', FALSE ) ) {
            // Added the class "notice-get-started-class" so jQuery pick it up and pass via AJAX,
            // and added "data-notice" attribute in order to track multiple / different notices
            // multiple dismissible notice states ?>
            <div class="updated notice notice-get-started-class is-dismissible" data-notice="get_started">
                <div class="news25-getting-started-notice clearfix">
                    <div class="news25-theme-screenshot">
                        <img src="<?php echo esc_url( get_stylesheet_directory_uri() ); ?>/screenshot.jpg" class="screenshot" alt="<?php esc_attr_e( 'Theme Screenshot', 'news-25' ); ?>" />
                    </div><!-- /.news25-theme-screenshot -->
                    <div class="news25-theme-notice-content">
                        <h2 class="news25-notice-h2">
                            <?php
                        printf(
                        /* translators: 1: welcome page link starting html tag, 2: welcome page link ending html tag. */
                            esc_html__( 'Welcome! Thank you for choosing %1$s!', 'news-25' ), '<strong>'. wp_get_theme()->get('Name'). '</strong>' );
                        ?>
                        </h2>

                        <p class="plugin-install-notice"><?php echo sprintf(__('Install and activate <strong>Clever Fox</strong> plugin for taking full advantage of all the features this theme has to offer.', 'news-25')) ?></p>

                        <a class="news25-btn-get-started button button-primary button-hero news25-button-padding" href="#" data-name="" data-slug=""><?php esc_html_e( 'Get started with News 25', 'news-25' ) ?></a><span class="news25-push-down">
                        <?php
                            /* translators: %1$s: Anchor link start %2$s: Anchor link end */
                            printf(
                                'or %1$sCustomize theme%2$s</a></span>',
                                '<a target="_blank" href="' . esc_url( admin_url( 'customize.php' ) ) . '">',
                                '</a>'
                            );
                        ?>
                    </div><!-- /.news25-theme-notice-content -->
                </div>
            </div>
        <?php }
}

add_action( 'admin_notices', 'news25_deprecated_hook_admin_notice' );

/*******************************************************************************
 *  Plugin Installer
 *******************************************************************************/

add_action( 'wp_ajax_install_act_plugin', 'news25_admin_install_plugin' );

function news25_admin_install_plugin() {
    /**
     * Install Plugin.
     */
    include_once ABSPATH . '/wp-admin/includes/file.php';
    include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
    include_once ABSPATH . 'wp-admin/includes/plugin-install.php';

    if ( ! file_exists( WP_PLUGIN_DIR . '/clever-fox' ) ) {
        $api = plugins_api( 'plugin_information', array(
            'slug'   => sanitize_key( wp_unslash( 'clever-fox' ) ),
            'fields' => array(
                'sections' => false,
            ),
        ) );

        $skin     = new WP_Ajax_Upgrader_Skin();
        $upgrader = new Plugin_Upgrader( $skin );
        $result   = $upgrader->install( $api->download_link );
    }

    // Activate plugin.
    if ( current_user_can( 'activate_plugin' ) ) {
        $result = activate_plugin( 'clever-fox/clever-fox.php' );
    }
}	



function logo_content() {
	if(has_custom_logo())
	{	
		the_custom_logo();
	} ?>
	<a href="<?php echo esc_url( home_url( '/' ) ); ?>">
		<h4 class="site-title d-block">
			<?php 
				echo esc_html(bloginfo('name'));
			?>
		</h4>
	</a>	

	<?php
	$description = get_bloginfo( 'description');
	if ($description) : ?>
		<p class="site-description"><?php echo $description; ?></p>
	<?php endif; 	
}

/**
 * Get registered sidebar name by sidebar ID.
 *
 * @since  1.0.0
 * @param  string $sidebar_id Sidebar ID.
 * @return string Sidebar name.
 */
function news25_get_sidebar_name_by_id( $sidebar_id = '' ) {

	if ( ! $sidebar_id ) {
		return;
	}

	global $wp_registered_sidebars;
	$sidebar_name = '';

	if ( isset( $wp_registered_sidebars[ $sidebar_id ] ) ) {
		$sidebar_name = $wp_registered_sidebars[ $sidebar_id ]['name'];
	}

	return $sidebar_name;
}


if(! function_exists('default_section_image')){
function default_section_image($height) {	
	if ( has_post_thumbnail()) : ?>
	<img  src="<?php echo esc_url(get_the_post_thumbnail_url(get_the_ID(), 'full')); ?>" class="img-fluid" alt="<?php echo esc_attr(get_the_title()); ?>">
	<?php else: 
	// URL of the default fallback image
	$default_image_url = get_template_directory_uri() . '/assets/images/pl4.png';
	
	// Build the fallback image HTML
	echo '<img style="width:100%; height:'.$height.'px" src="'.$default_image_url.'" class="img-fluid" alt="'. esc_attr(get_the_title()).'">';
		
	endif;
} }
add_action('default_section_image','default_section_image');


if(! function_exists('default_blog_image')){
function default_blog_image($width, $height) {	
	if ( has_post_thumbnail()) : ?>
		<img style="width:<?php echo esc_attr($width); ?>px; height:<?php echo esc_attr($height); ?>px" src="<?php echo esc_url(get_the_post_thumbnail_url(get_the_ID(), 'full')); ?>" class="img-fluid" alt="<?php echo esc_attr(get_the_title()); ?>">
		<?php else: 
	// URL of the default fallback image
	$default_image_url = get_template_directory_uri() . '/assets/images/pl4.png';
	
	// Build the fallback image HTML
	echo '<img style="width:'.$width.'px; height:'.$height.'px;" src="'.$default_image_url.'" class="img-fluid" alt="'. esc_attr(get_the_title()).'">';
		
	endif;
} }
add_action('default_blog_image','default_blog_image');


if( !function_exists('quick_header_search')):
function quick_header_search() { ?>	
	<div class="view-search-btn header-search-popup">
		<div class="search-box">
			<form method="get" class="search-form mb-4 mb-lg-5" action="<?php echo esc_url( home_url( '/' ) ); ?>" aria-label="<?php esc_attr_e( 'Site Search', 'news-25' ); ?>">
				<span class="screen-reader-text"><?php esc_html_e( 'Search for:', 'news-25' ); ?></span>
				<input type="search" class="search-field search-form-control header-search-field" placeholder="<?php esc_attr_e( 'Type To Search', 'news-25' ); ?>" name="s" id="popfocus" value="" autofocus>
				<button type="submit" class="search-submit"><i class="fa fa-search"></i></button>
			</form>
			<div class="breaking-news-section">
				<div class="container">
					<h6 class="fw-bold text-center mb-4 mb-lg-5"><?php esc_html_e('Or Check Our Popuplar Categories...','news-25'); ?></h6>
					<div class="container-wrapper">
						<div class="news-title">
							<i class="fa fa-bolt"></i> <?php esc_html_e('Trending Searching','news-25'); ?>
						</div>
						
						<div class="breaking-news-slider owl-carousel">
							<?php
							$tags = get_tags();
							foreach ($tags as $tag) {
							  echo '<div class="item tag"><a href="' . get_tag_link($tag->term_id) . '">#' . $tag->name . '</a></div>';
							}
							?>
						</div>
					</div>
				</div>
			</div>
			<a href="javascript:void(0)" class="close-style header-search-close"></a>
		</div>
	</div>
	<!-- Breking News SLider Here -->
	
<?php }
endif;

add_action('quick_header_search','quick_header_search');