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

if ( ! defined( 'PRIME_CHARITY_TRUST_URL' ) ) {
    define( 'PRIME_CHARITY_TRUST_URL', esc_url( 'https://themeignite.com/products/charitable-trust-wordpress-theme', 'prime-charity-trust') );
}
if ( ! defined( 'PRIME_CHARITY_TRUST_FREE_DOC_URL' ) ) {
    define( 'PRIME_CHARITY_TRUST_FREE_DOC_URL', esc_url( 'https://demo.themeignite.com/documentation/prime-charity-trust-free
', 'prime-charity-trust') );
}
if ( ! defined( 'PRIME_CHARITY_TRUST_PRO_DOC_URL' ) ) {
    define( 'PRIME_CHARITY_TRUST_PRO_DOC_URL', esc_url( 'https://demo.themeignite.com/documentation/charity-pro', 'prime-charity-trust') );
}
if ( ! defined( 'PRIME_CHARITY_TRUST_DEMO_URL' ) ) {
    define( 'PRIME_CHARITY_TRUST_DEMO_URL', esc_url( 'https://demo.themeignite.com/prime-charity-trust', 'prime-charity-trust') );
}
if ( ! defined( 'PRIME_CHARITY_TRUST_REVIEW_URL' ) ) {
    define( 'PRIME_CHARITY_TRUST_REVIEW_URL', esc_url( 'https://wordpress.org/support/theme/prime-charity-trust/reviews/#new-post', 'prime-charity-trust') );
}
if ( ! defined( 'PRIME_CHARITY_TRUST_SUPPORT_URL' ) ) {
    define( 'PRIME_CHARITY_TRUST_SUPPORT_URL', esc_url( 'https://wordpress.org/support/theme/prime-charity-trust', 'prime-charity-trust') );
}
if ( ! defined( 'PRIME_CHARITY_TRUST_BUNDLE_URL' ) ) {
    define( 'PRIME_CHARITY_TRUST_BUNDLE_URL', esc_url( 'https://www.themeignite.com/products/wp-theme-bundle', 'prime-charity-trust') );
}

$prime_charity_trust_theme_data = wp_get_theme();
if( ! defined( 'PRIME_CHARITY_TRUST_THEME_VERSION' ) ) define ( 'PRIME_CHARITY_TRUST_THEME_VERSION', $prime_charity_trust_theme_data->get( 'Version' ) );
if( ! defined( 'PRIME_CHARITY_TRUST_THEME_TITLE' ) ) define ( 'PRIME_CHARITY_TRUST_THEME_TITLE', 'Prime Charity Trust' );
if( ! defined( 'PRIME_CHARITY_TRUST_THEME_NAME' ) ) define( 'PRIME_CHARITY_TRUST_THEME_NAME', $prime_charity_trust_theme_data->get( 'Name' ) );
if( ! defined( 'PRIME_CHARITY_TRUST_THEME_TEXTDOMAIN' ) ) define( 'PRIME_CHARITY_TRUST_THEME_TEXTDOMAIN', $prime_charity_trust_theme_data->get( 'TextDomain' ) );

if ( ! function_exists( 'prime_charity_trust_setup' ) ) :
/**
 * 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 prime_charity_trust_setup() {
	/*
	 * Make theme available for translation.
	 * Translations can be filed in the /languages/ directory.
	 * If you're building a theme based on Prime Charity Trust, use a find and replace
	 * to change 'prime-charity-trust' to the name of your theme in all the template files.
	 */
	load_theme_textdomain( 'prime-charity-trust', get_template_directory() . '/languages' );

	// Add default posts and comments RSS feed links to head.
	add_theme_support( 'automatic-feed-links' );

	/*
	 * Enable support for Post Formats.
	 *
	 * See: https://codex.wordpress.org/Post_Formats
	*/
	add_theme_support( 'post-formats', array('image','video','gallery','audio',) );

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

	add_theme_support( 'woocommerce' );

	/*
	 * 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' );
	
	// This theme uses wp_nav_menu() in one location.
	register_nav_menus( array(
		'primary' => esc_html__( 'Primary', 'prime-charity-trust' ),
	) );

	// Enable support for custom logo.
	add_theme_support( 'custom-logo' , array(
		'height'		=>45,	
		'width'			=>45,	
		'flex-height'	=>true,	
		'flex-width'	=>true,
	));

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

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

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

	/*
	* This theme styles the visual editor to resemble the theme style,
	* specifically font, colors, icons, and column width.
	*/
	$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
	add_editor_style( array( '/assets/css/editor-style' . $min . '.css', prime_charity_trust_fonts_url() ) );

	// Gutenberg support
	add_theme_support( 'editor-color-palette', array(
       	array(
			'name' => esc_html__( 'Tan', 'prime-charity-trust' ),
			'slug' => 'tan',
			'color' => '#E6DBAD',
       	),
       	array(
           	'name' => esc_html__( 'Yellow', 'prime-charity-trust' ),
           	'slug' => 'yellow',
           	'color' => '#FDE64B',
       	),
       	array(
           	'name' => esc_html__( 'Orange', 'prime-charity-trust' ),
           	'slug' => 'orange',
           	'color' => '#ED7014',
       	),
       	array(
           	'name' => esc_html__( 'Red', 'prime-charity-trust' ),
           	'slug' => 'red',
           	'color' => '#D0312D',
       	),
       	array(
           	'name' => esc_html__( 'Pink', 'prime-charity-trust' ),
           	'slug' => 'pink',
           	'color' => '#b565a7',
       	),
       	array(
           	'name' => esc_html__( 'Purple', 'prime-charity-trust' ),
           	'slug' => 'purple',
           	'color' => '#A32CC4',
       	),
       	array(
           	'name' => esc_html__( 'Blue', 'prime-charity-trust' ),
           	'slug' => 'blue',
           	'color' => '#3A43BA',
       	),
       	array(
           	'name' => esc_html__( 'Green', 'prime-charity-trust' ),
           	'slug' => 'green',
           	'color' => '#3BB143',
       	),
       	array(
           	'name' => esc_html__( 'Brown', 'prime-charity-trust' ),
           	'slug' => 'brown',
           	'color' => '#231709',
       	),
       	array(
           	'name' => esc_html__( 'Grey', 'prime-charity-trust' ),
           	'slug' => 'grey',
           	'color' => '#6C626D',
       	),
       	array(
           	'name' => esc_html__( 'Black', 'prime-charity-trust' ),
           	'slug' => 'black',
           	'color' => '#000000',
       	),
   	));

	add_theme_support( 'align-wide' );
	add_theme_support( 'editor-font-sizes', array(
	   	array(
	       	'name' => esc_html__( 'small', 'prime-charity-trust' ),
	       	'shortName' => esc_html__( 'S', 'prime-charity-trust' ),
	       	'size' => 12,
	       	'slug' => 'small'
	   	),
	   	array(
	       	'name' => esc_html__( 'regular', 'prime-charity-trust' ),
	       	'shortName' => esc_html__( 'M', 'prime-charity-trust' ),
	       	'size' => 16,
	       	'slug' => 'regular'
	   	),
	   	array(
	       	'name' => esc_html__( 'larger', 'prime-charity-trust' ),
	       	'shortName' => esc_html__( 'L', 'prime-charity-trust' ),
	       	'size' => 36,
	       	'slug' => 'larger'
	   	),
	   	array(
	       	'name' => esc_html__( 'huge', 'prime-charity-trust' ),
	       	'shortName' => esc_html__( 'XL', 'prime-charity-trust' ),
	       	'size' => 48,
	       	'slug' => 'huge'
	   	)
	));
	add_theme_support('editor-styles');
	add_theme_support( 'wp-block-styles' );
	add_theme_support( 'wc-product-gallery-zoom' );
    add_theme_support( 'wc-product-gallery-slider' );
    add_theme_support( 'responsive-embeds' );
}
endif;
add_action( 'after_setup_theme', 'prime_charity_trust_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 $prime_charity_trust_content_width
 */
function prime_charity_trust_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'prime_charity_trust_content_width', 640 );
}
add_action( 'after_setup_theme', 'prime_charity_trust_content_width', 0 );

/**
 * Register widget area.
 *
 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
 */
function prime_charity_trust_widgets_init() {
	register_sidebar( array(
		'name'          => esc_html__( 'Sidebar Option', 'prime-charity-trust' ),
		'id'            => 'right-sidebar',
		'description'   => '',
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	) );
	register_sidebar( array(
		'name'          => esc_html__( 'Sidebar Two', 'prime-charity-trust' ),
		'id'            => 'sidebar-2',
		'description'   => '',
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	) );
	register_sidebar( array(
		'name'          => esc_html__( 'Sidebar Three', 'prime-charity-trust' ),
		'id'            => 'sidebar-3',
		'description'   => '',
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	) );
		register_sidebar( array(
		'name'          => esc_html__( 'Footer 1', 'prime-charity-trust' ),
		'id'            => 'footer-1',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	) );
	register_sidebar( array(
		'name'          => esc_html__( 'Footer 2', 'prime-charity-trust' ),
		'id'            => 'footer-2',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	) );
	register_sidebar( array(
		'name'          => esc_html__( 'Footer 3', 'prime-charity-trust' ),
		'id'            => 'footer-3',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	) );
	register_sidebar( array(
		'name'          => esc_html__( 'Footer 4', 'prime-charity-trust' ),
		'id'            => 'footer-4',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	) );
}
add_action( 'widgets_init', 'prime_charity_trust_widgets_init' );

/**
 * Register custom fonts.
 */
function prime_charity_trust_fonts_url() {
	$font_family   = array(
		'Quicksand:wght@300;400;500;600;700',
		'Mulish:ital,wght@0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;0,1000;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900;1,1000'
	);
	
	$fonts_url = add_query_arg( array(
		'family' => implode( '&family=', $font_family ),
		'display' => 'swap',
	), 'https://fonts.googleapis.com/css2' );

	$prime_charity_trust_contents = wptt_get_webfont_url( esc_url_raw( $fonts_url ) );
	return $prime_charity_trust_contents;
}

/**
 * Enqueue scripts and styles.
 */
function prime_charity_trust_scripts() {
	$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
	$fonts_url = prime_charity_trust_fonts_url();
	$primary_color = prime_charity_trust_get_option( 'primary_color' );
	if ( ! empty( $fonts_url ) ) {
		wp_enqueue_style( 'prime-charity-trust-google-fonts', $fonts_url, array(), null );
	}
	
	wp_enqueue_style( 'bootstrap-style', get_template_directory_uri().'/assets/css/bootstrap.css' );

	wp_enqueue_style( 'fontawesome-all', get_template_directory_uri() . '/assets/css/all' . $min . '.css', '', '4.7.0' );

	wp_enqueue_style( 'owl.carousel-style', get_template_directory_uri().'/assets/css/owl.carousel.css' );

	wp_enqueue_style( 'slick-theme-css', get_template_directory_uri() .'/assets/css/slick-theme' . $min . '.css', '', 'v2.2.0');

	wp_enqueue_style( 'slick-css', get_template_directory_uri() .'/assets/css/slick' . $min . '.css', '', 'v1.8.0');

	wp_enqueue_style( 'prime-charity-trust-blocks', get_template_directory_uri() . '/assets/css/blocks' . $min . '.css' );
	
	wp_enqueue_style( 'prime-charity-trust-style', get_stylesheet_uri() );

	wp_style_add_data('prime-charity-trust-style', 'rtl', 'replace');

	require get_parent_theme_file_path( '/inc/css_custom.php' );
	wp_add_inline_style( 'prime-charity-trust-style',$prime_charity_trust_custom_css );
	
	wp_enqueue_script( 'jquery-slick', get_template_directory_uri() . '/assets/js/slick' . $min . '.js', array('jquery'), '2017417', true );
	
	wp_enqueue_script( 'prime-charity-trust-navigation', get_template_directory_uri() . '/assets/js/navigation' . $min . '.js', array(), '20151215', true );

	wp_enqueue_script( 'custom-js', get_template_directory_uri(). '/assets/js/custom.js', array('jquery') ,'',true);

	wp_enqueue_script( 'owl.carousel-js', get_template_directory_uri(). '/assets/js/owl.carousel.js', array('jquery') ,'',true);

	wp_enqueue_script( 'prime-charity-trust-skip-link-focus-fix', get_template_directory_uri() . '/assets/js/skip-link-focus-fix' . $min . '.js', array(), '20151215', true );
	
	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}
}
add_action( 'wp_enqueue_scripts', 'prime_charity_trust_scripts' );

if( ! function_exists( 'prime_charity_trust_admin_scripts' ) ) :
/**
 * Addmin scripts
*/
function prime_charity_trust_admin_scripts() {
	wp_enqueue_style( 'prime-charity-trust-admin-style',get_template_directory_uri().'/assets/css/admin.css', PRIME_CHARITY_TRUST_THEME_VERSION, 'screen' );
}
endif;
add_action( 'admin_enqueue_scripts', 'prime_charity_trust_admin_scripts' );

/**
 * Enqueue editor styles for Gutenberg
 *
 * @since Prime Charity Trust 1.0.0
 */
function prime_charity_trust_block_editor_styles() {
	$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
	// Block styles.
	wp_enqueue_style( 'prime-charity-trust-block-editor-style', get_theme_file_uri( '/assets/css/editor-blocks' . $min . '.css' ) );
	// Add custom fonts.
	wp_enqueue_style( 'prime-charity-trust-fonts', prime_charity_trust_fonts_url(), array(), null );
}
add_action( 'enqueue_block_editor_assets', 'prime_charity_trust_block_editor_styles' );

// Change number or products per row to 3
add_filter('loop_shop_columns', 'prime_charity_trust_loop_columns');
	if (!function_exists('prime_charity_trust_loop_columns')) {
		function prime_charity_trust_loop_columns() {
		return 3; // 3 products per row
	}
}

//Change number of products that are displayed per page (shop page)
add_filter( 'loop_shop_per_page', 'prime_charity_trust_products_per_page' );
function prime_charity_trust_products_per_page( $cols ) {
  	return  9;
}

/**
 * AJAX handler to dismiss Whizzie notice
 */
if ( ! function_exists( 'prime_charity_trust_dismiss_whizzie_notice' ) ) {
    function prime_charity_trust_dismiss_whizzie_notice() {

        update_user_meta(
            get_current_user_id(),
            'prime_charity_trust_whizzie_dismissed',
            true
        );

        wp_die();
    }
}
add_action(
    'wp_ajax_prime_charity_trust_dismiss_whizzie_notice',
    'prime_charity_trust_dismiss_whizzie_notice'
);

/**
 * Check if Whizzie notice is dismissed
 */
if ( ! function_exists( 'prime_charity_trust_is_whizzie_dismissed' ) ) {
    function prime_charity_trust_is_whizzie_dismissed() {

        return (bool) get_user_meta(
            get_current_user_id(),
            'prime_charity_trust_whizzie_dismissed',
            true
        );

    }
}

/**
 * Reset Whizzie notice when theme is activated
 */
add_action( 'after_switch_theme', function () {

    $users = get_users( array(
        'fields' => 'ID',
    ) );

    foreach ( $users as $user_id ) {
        delete_user_meta( $user_id, 'prime_charity_trust_whizzie_dismissed' );
    }

});

/**
 * Display the admin notice unless dismissed.
 */
function prime_charity_trust_dashboard_notice() {
    // Check if the notice is dismissed
    $dismissed = get_user_meta( get_current_user_id(), 'prime_charity_trust_dismissable_notice', true );

    // Display the notice only if not dismissed
    if ( ! $dismissed ) {

        // Get the theme name dynamically
        $theme_name = wp_get_theme()->get( 'Name' );
        ?>
        <div class="updated notice notice-success is-dismissible notice-get-started-class" data-notice="get-start">
            <div class="notice-details">
                <div class="notice-content">
                    <h2><?php /* translators: %s: Theme name */
					printf( esc_html__( 'Thanks you for installing %s.', 'prime-charity-trust' ), '<strong>Prime Charity Trust</strong>' );?></h2>
                    <p><?php echo esc_html('Your journey to a powerful and stylish website begins here. Let’s get everything set up in just a few clicks!', 'prime-charity-trust'); ?></p>
                    <div class="notice-btns">
                        <a class="button button-primary getstart"
                           href="<?php echo esc_url(admin_url('themes.php?page=prime-charity-trust')); ?>"><?php esc_html_e('Getting Started', 'prime-charity-trust') ?></a>
                       	<a class="button button-primary premium" target="_blank" href="<?php echo esc_url(PRIME_CHARITY_TRUST_URL); ?>"><?php esc_html_e('Go To Premium', 'prime-charity-trust') ?></a>
						<a class="button button-primary demo" target="_blank" href="<?php echo esc_url(PRIME_CHARITY_TRUST_DEMO_URL); ?>"><?php esc_html_e('View Demo', 'prime-charity-trust') ?></a>
                    </div>
                </div>
                <div class="notice-img">
                    <a href="<?php echo esc_url( PRIME_CHARITY_TRUST_BUNDLE_URL ); ?>" target="_blank"><img src="<?php echo esc_url( get_template_directory_uri() . '/assets/images/notice.png' ); ?>"></a>
                </div>
            </div>
        </div>
        <?php
    }
}

// Hook to display the notice
add_action( 'admin_notices', 'prime_charity_trust_dashboard_notice' );


/**
 * AJAX handler to dismiss the notice.
 */
function prime_charity_trust_dismissable_notice() {
    // Set user meta to indicate the notice is dismissed
    update_user_meta(get_current_user_id(), 'prime_charity_trust_dismissable_notice', true);
    die();
}

// Hook for the AJAX action
add_action('wp_ajax_prime_charity_trust_dismissable_notice', 'prime_charity_trust_dismissable_notice');

/**
 * Clear dismissed notice state when switching themes.
 */
function prime_charity_trust_switch_theme() {
    // Clear the dismissed notice state when switching themes
    delete_user_meta(get_current_user_id(), 'prime_charity_trust_dismissable_notice');
}

// Hook for switching themes
add_action('after_switch_theme', 'prime_charity_trust_switch_theme');

/**
 *  Webfonts 
 */
require get_template_directory() . '/inc/wptt-webfont-loader.php';

function prime_charity_trust_template_setup() {

/**
 * Load init.
 */
require_once get_template_directory() . '/inc/init.php';

/**
 * Pro Link.
 */
require_once get_template_directory() . '/inc/premium-link/class-button-link.php';

/**
 * Getting Started
*/
require get_template_directory() . '/inc/getting-started/getting-started.php';

/**
 * Setup Wizard
 */
if ( wp_get_theme()->get('Name') === PRIME_CHARITY_TRUST_THEME_NAME ) {
    require get_theme_file_path() . '/theme-wizard/config.php';

// Hook to display the notice
add_action( 'admin_notices', 'prime_charity_trust_dashboard_notice' );
}

}
add_action('after_setup_theme', 'prime_charity_trust_template_setup');


/**
 * Set the number of products displayed per row on WooCommerce archive pages.
 *
 * @param int $columns Number of columns per row.
 * @return int Number of columns to display per row.
 */
function customize_woocommerce_archive_per_row($columns) {
    // Set the desired number of columns per row.
    $columns = prime_charity_trust_get_option( 'prime_charity_trust_archive_product_per_row' ); // You can change this to your preferred number.

    return $columns;
}

// Add a filter to modify the number of columns per row on WooCommerce archive pages.
add_filter('loop_shop_columns', 'customize_woocommerce_archive_per_row');

/**
 * Set the number of products displayed per page on WooCommerce archive pages.
 *
 * @param int $posts_per_page Number of products per page.
 * @return int Number of products to display per page.
 */
function customize_woocommerce_archive_per_page($posts_per_page) {
    // Set the desired number of products per page.
    $posts_per_page = prime_charity_trust_get_option( 'prime_charity_trust_archive_product_per_page' ); // You can change this to your preferred number.

    return $posts_per_page;
}

// Add a filter to modify the number of products displayed per page on WooCommerce archive pages.
add_filter('loop_shop_per_page', 'customize_woocommerce_archive_per_page');
function prime_charity_trust_customizer_css() {
    $prime_charity_trust_sidebar_text_align = get_theme_mod('sidebar_text_align', 'left');
    ?>
    <style type="text/css">
        .widget-area {
            text-align: <?php echo esc_attr($prime_charity_trust_sidebar_text_align); ?>;
        }
    </style>
    <?php
}
add_action('wp_head', 'prime_charity_trust_customizer_css');

function prime_charity_trust_menu_customizer_css() {
    $prime_charity_trust_menu_font_weight = get_theme_mod('prime_charity_trust_menu_font_weight', '400');
    $prime_charity_trust_menu_text_transform = get_theme_mod('prime_charity_trust_menu_text_transform', 'capitalize');

    $prime_charity_trust_custom_css = "
        .main-navigation ul li a {
            font-weight: " . esc_html($prime_charity_trust_menu_font_weight) . ";
            text-transform: " . esc_html($prime_charity_trust_menu_text_transform) . ";
        }
    ";

    wp_add_inline_style('prime-charity-trust-style', $prime_charity_trust_custom_css);
}
add_action('wp_enqueue_scripts', 'prime_charity_trust_menu_customizer_css');


function prime_charity_trust_custom_blog_banner_title() {
    if (is_404()) {
        echo '<h1 class="entry-title">Page Not Found</h1>';
    } elseif (is_search()) {
        echo '<h1 class="entry-title">Search Results for: ' . get_search_query() . '</h1>';
    } elseif (is_home() && !is_front_page()) {
        echo '<h1 class="entry-title">Blogs</h1>';
    } elseif (function_exists('is_shop') && is_shop()) {
        echo '<h1 class="entry-title">Shop</h1>';
    } elseif (is_page_template('template-homepage.php')) {
    } elseif (is_page()) {
        the_title('<h1 class="entry-title">', '</h1>');
    } elseif (is_single()) {
        the_title('<h1 class="entry-title">', '</h1>');
    } elseif (is_archive()) {
        the_archive_title('<h1 class="entry-title">', '</h1>');
    } else {
        the_archive_title('<h1 class="entry-title">', '</h1>');
    }
	prime_charity_trust_the_breadcrumb();
}

function prime_charity_trust_the_breadcrumb() {
    echo '<div class="breadcrumb justify-content-center align-items-center mt-5">';

    if (!is_home()) {
        echo '<a class="home-main align-self-center" href="' . esc_url(home_url()) . '">';
        bloginfo('name');
        echo "</a> >> ";

        if (is_category() || is_single()) {
            the_category(' >> ');
            if (is_single()) {
                echo ' >> <span class="current-breadcrumb">' . esc_html(get_the_title()) . '</span>';
            }
        } elseif (is_page()) {
            echo '<span class="current-breadcrumb">' . esc_html(get_the_title()) . '</span>';
        }
    }

    echo '</div>';
}

function prime_charity_trust_custom_header_image() {
    echo '<div class="single-header-img">';

    if (!is_front_page() || is_home()) {
        if (is_single() || is_page() || (function_exists('is_shop') && is_shop()) || is_archive() || is_search() || is_404() || is_home()) {
            if (!is_page_template('template-homepage.php')) {
                echo '<div class="post-thumbnail">';
                if (function_exists('is_shop') && (is_shop() || function_exists('is_product') && is_product())) {
                    $prime_charity_trust_default_image_url = get_template_directory_uri() . '/assets/images/default-header.jpg'; 
                    echo '<img src="' . esc_url($prime_charity_trust_default_image_url) . '" alt="Default Image" itemprop="image">';
                } else {
                    if (has_post_thumbnail()) {
                        (is_active_sidebar('right-sidebar')) ? the_post_thumbnail('prime-charity-trust-with-sidebar', array('itemprop' => 'image')) : the_post_thumbnail('prime-charity-trust-without-sidebar', array('itemprop' => 'image'));
                    } else {
                        $prime_charity_trust_default_image_url = get_template_directory_uri() . '/assets/images/default-header.jpg'; 
                        echo '<img src="' . esc_url($prime_charity_trust_default_image_url) . '" alt="Default Image" itemprop="image">';
                    }
                }
                echo '</div>';
                echo '<div class="single-header-heading">';
                prime_charity_trust_custom_blog_banner_title();
                echo '</div>';
            }
        }
    }

    echo '</div>';
}

function prime_charity_trust_hide_single_header_img() {
    ?>
    <script type="text/javascript">
        document.addEventListener('DOMContentLoaded', function() {
            if (document.body.classList.contains('page-template-template-home')) {
                var headerImg = document.querySelector('.single-header-img');
                if (headerImg) {
                    headerImg.style.display = 'none';
                }
            }
        });
    </script>
    <?php
}
add_action('wp_footer', 'prime_charity_trust_hide_single_header_img');


function prime_charity_trust_body_classess( $classes ) {
    // Get the customizer setting value
    $hide_post_meta = get_theme_mod( 'prime_charity_trust_single_post_meta_setting', true );

    // Check if the setting is set to hide
    if ( !$hide_post_meta ) {
        $classes[] = 'hide-post-meta';
    }

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

function prime_charity_trust_enqueue_google_fontss() {
    $prime_charity_trust_heading_font_family = get_theme_mod('prime_charity_trust_heading_font_family', '');
    $prime_charity_trust_body_font_family = get_theme_mod('prime_charity_trust_body_font_family', '');

    // Google Fonts URL builder
    $google_fonts = array(
        'arial'          => '',
        'verdana'        => '',
        'helvetica'      => '',
        'times-new-roman'=> '',
        'georgia'        => '',
        'courier-new'    => '',
        'trebuchet-ms'   => '',
        'tahoma'         => '',
        'palatino'       => '',
        'garamond'       => '',
        'impact'         => '',
        'comic-sans-ms'  => '',
        'lucida-sans'    => '',
        'arial-black'    => '',
        'gill-sans'      => '',
        'segoe-ui'       => '',
        'open-sans'      => 'Open+Sans:wght@400;700',
        'roboto'         => 'Roboto:wght@400;700',
        'lato'           => 'Lato:wght@400;700',
        'montserrat'     => 'Montserrat:wght@400;700',
        'libre-baskerville' => 'Libre+Baskerville:wght@400;700'
    );

    $prime_charity_trust_google_fonts_url = '';

    if (!empty($google_fonts[$prime_charity_trust_heading_font_family]) || !empty($google_fonts[$prime_charity_trust_body_font_family])) {
        $fonts = array();

        if (!empty($google_fonts[$prime_charity_trust_heading_font_family])) {
            $fonts[] = $google_fonts[$prime_charity_trust_heading_font_family];
        }

        if (!empty($google_fonts[$prime_charity_trust_body_font_family])) {
            $fonts[] = $google_fonts[$prime_charity_trust_body_font_family];
        }

        // Build Google Fonts URL
        $prime_charity_trust_google_fonts_url = add_query_arg(
            'family',
            implode('|', $fonts),
            'https://fonts.googleapis.com/css2'
        );
    }

    if ($prime_charity_trust_google_fonts_url) {
        wp_enqueue_style('prime-charity-trust-google-fonts', $prime_charity_trust_google_fonts_url, false);
    }
}
add_action('wp_enqueue_scripts', 'prime_charity_trust_enqueue_google_fontss');


function prime_charity_trust_apply_typography() {
    $prime_charity_trust_heading_font_family = get_theme_mod('prime_charity_trust_heading_font_family');
    $prime_charity_trust_body_font_family = get_theme_mod('prime_charity_trust_body_font_family');

    $prime_charity_trust_custom_css = '';

    if ($prime_charity_trust_body_font_family) {
        $prime_charity_trust_custom_css .= "body, a, a:active, a:hover { font-family: " . esc_html($prime_charity_trust_body_font_family) . " !important; }";
    }

    if ($prime_charity_trust_heading_font_family) {
        $prime_charity_trust_custom_css .= "h1, h2, h3, h4, h5, h6 { font-family: " . esc_html($prime_charity_trust_heading_font_family) . " !important; }";
    }

    if (!empty($prime_charity_trust_custom_css)) {
        wp_add_inline_style('prime-charity-trust-style', $prime_charity_trust_custom_css);
    }
}
add_action('wp_enqueue_scripts', 'prime_charity_trust_apply_typography');

function prime_charity_trust_body_sidebar_classes( $classes ) {
    $prime_charity_trust_sidebar_position = get_theme_mod( 'prime_charity_trust_sidebar_position', 'right' );

    if ( $prime_charity_trust_sidebar_position === 'left' ) {
        $classes[] = 'sidebar-left';
    } elseif ( $prime_charity_trust_sidebar_position === 'right' ) {
        $classes[] = 'sidebar-right';
    } elseif ( $prime_charity_trust_sidebar_position === 'no-sidebar' ) {
        $classes[] = 'no-sidebar';
    }

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