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

if ( ! defined( 'FRESH_BAKERS_SHOP_URL' ) ) {
    define( 'FRESH_BAKERS_SHOP_URL', esc_url( 'https://www.themeignite.com/products/bakery-wordpress-theme', 'fresh-bakers-shop') );
}

$fresh_bakers_shop_theme_data = wp_get_theme();
if( ! defined( 'FRESH_BAKERS_SHOP_THEME_VERSION' ) ) define ( 'FRESH_BAKERS_SHOP_THEME_VERSION', $fresh_bakers_shop_theme_data->get( 'Version' ) );
if( ! defined( 'FRESH_BAKERS_SHOP_THEME_NAME' ) ) define( 'FRESH_BAKERS_SHOP_THEME_NAME', $fresh_bakers_shop_theme_data->get( 'Name' ) );

if ( ! function_exists( 'fresh_bakers_shop_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 fresh_bakers_shop_setup() {

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

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

	// This theme uses wp_nav_menu() in one location.
	register_nav_menus( array(
		'primary' => esc_html__( 'Primary', 'fresh-bakers-shop' ),
	) );

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

	/*
	 * Enable support for Post Formats.
	 * See https://developer.wordpress.org/themes/functionality/post-formats/
	 */
	add_theme_support( 'post-formats', array(
		'aside',
		'image',
		'video',
		'quote',
		'link',
		'gallery',
        'status',
        'audio', 
        'chat'
	) );

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

	/* Custom Logo */
    add_theme_support( 'custom-logo', array(
    	'header-text' => array( 'site-title', 'site-description' ),
    ) );

	add_theme_support( 'woocommerce' );

    load_theme_textdomain( 'fresh-bakers-shop', get_template_directory() . '/languages' );

    /**
	 * Custom template tags for this theme.
	 */
	require get_template_directory() . '/inc/template-tags.php';

	/**
	 * Custom functions that act independently of the theme templates.
	 */
	require get_template_directory() . '/inc/extra.php';

	/**
	 * Customizer additions.
	 */
	require get_template_directory() . '/inc/customizer.php';

	/**
	 * Social Links Widget
	 */
	require get_template_directory() . '/inc/widget-social-links.php';

	/**
	 * Info Theme
	 */
	require get_template_directory() . '/inc/info.php';

	/**
	 * Info Theme
	 */
	require get_template_directory() . '/inc/sanitization.php';

	/**
	 * Load TGM.
	 */
	require get_template_directory() . '/inc/tgm/tgm.php';

}
endif;
add_action( 'after_setup_theme', 'fresh_bakers_shop_setup' );

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

/**
 * 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 $fresh_bakers_shop_content_width
 */
function fresh_bakers_shop_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'fresh_bakers_shop_content_width', 780 );
}
add_action( 'after_setup_theme', 'fresh_bakers_shop_content_width', 0 );


/**
 * Register widget area.
 *
 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
 */
function fresh_bakers_shop_widgets_init() {
	register_sidebar( array(
		'name'          => esc_html__( 'Sidebar Option', 'fresh-bakers-shop' ),
		'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', 'fresh-bakers-shop' ),
		'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', 'fresh-bakers-shop' ),
		'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 One', 'fresh-bakers-shop' ),
		'id'            => 'footer-one',
		'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 Two', 'fresh-bakers-shop' ),
		'id'            => 'footer-two',
		'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 Three', 'fresh-bakers-shop' ),
		'id'            => 'footer-three',
		'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 Four', 'fresh-bakers-shop' ),
		'id'            => 'footer-four',
		'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__( 'Shop Sidebar', 'fresh-bakers-shop' ),
		'id'            => 'woocommerce-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__( 'Single Product Sidebar', 'fresh-bakers-shop' ),
		'id'            => 'woocommerce-single-sidebar',
		'description'   => '',
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	) );

}
add_action( 'widgets_init', 'fresh_bakers_shop_widgets_init' );

// Change number of products per row to 3
add_filter('loop_shop_columns', 'fresh_bakers_shop_loop_columns');
if (!function_exists('fresh_bakers_shop_loop_columns')) {
    function fresh_bakers_shop_loop_columns() {
        $fresh_bakers_shop_colm = 3;
        return $fresh_bakers_shop_colm;
    }
}

if( ! function_exists( 'fresh_bakers_shop_scripts' ) ) :

/**
 * Enqueue scripts and styles.
 */
function fresh_bakers_shop_scripts() {

	// Use minified libraries if SCRIPT_DEBUG is false
    $fresh_bakers_shop_build  = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '/build' : '';
    $fresh_bakers_shop_suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';

    wp_enqueue_style( 'bootstrap-style', get_template_directory_uri().'/css/build/bootstrap.css' );

    wp_enqueue_style( 'owl.carousel', get_template_directory_uri() . '/css/build/owl.carousel.css' );

    wp_enqueue_style( 'fontawesome-all', esc_url(get_template_directory_uri()).'/css/all.min.css');

	wp_enqueue_style( 'fresh-bakers-shop-style', get_stylesheet_uri(), array(), FRESH_BAKERS_SHOP_THEME_VERSION );

	require get_parent_theme_file_path( '/inc/css_custom.php' );
	wp_add_inline_style( 'fresh-bakers-shop-style',$fresh_bakers_shop_custom_css );

	wp_style_add_data('fresh-bakers-shop-basic-style', 'rtl', 'replace');
	
  	wp_enqueue_script( 'fresh-bakers-shop-all', get_template_directory_uri() . '/js' . $fresh_bakers_shop_build . '/all' . $fresh_bakers_shop_suffix . '.js', array( 'jquery' ), '6.1.1', true );
  	wp_enqueue_script( 'fresh-bakers-shop-v4-shims', get_template_directory_uri() . '/js' . $fresh_bakers_shop_build . '/v4-shims' . $fresh_bakers_shop_suffix . '.js', array( 'jquery' ), '6.1.1', true );
  	wp_enqueue_script( 'fresh-bakers-shop-modal-accessibility', get_template_directory_uri() . '/js' . $fresh_bakers_shop_build . '/modal-accessibility' . $fresh_bakers_shop_suffix . '.js', array( 'jquery' ), FRESH_BAKERS_SHOP_THEME_VERSION, true );
	wp_enqueue_script( 'owl.carousel', get_template_directory_uri() . '/js/build/owl.carousel.js', array('jquery'), '2.6.0', true );
	wp_enqueue_script( 'fresh-bakers-shop-js', get_template_directory_uri() . '/js/build/custom.js', array('jquery'), FRESH_BAKERS_SHOP_THEME_VERSION, true );
	// Wow script.
	wp_enqueue_script( 'wow-jquery', get_template_directory_uri() . '/js/build/wow.js', array('jquery'),'' ,true );
	// Animate CSS
	wp_enqueue_style( 'animate-style', get_template_directory_uri() . '/css/build/animate.css' );

	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}
}
endif;
add_action( 'wp_enqueue_scripts', 'fresh_bakers_shop_scripts' );

if( ! function_exists( 'fresh_bakers_shop_admin_scripts' ) ) :
/**
 * Addmin scripts
*/
function fresh_bakers_shop_admin_scripts() {
	wp_enqueue_style( 'fresh-bakers-shop-admin-style',get_template_directory_uri().'/inc/css/admin.css', FRESH_BAKERS_SHOP_THEME_VERSION, 'screen' );
}
endif;
add_action( 'admin_enqueue_scripts', 'fresh_bakers_shop_admin_scripts' );

function fresh_bakers_shop_customize_enque_js(){
	wp_enqueue_script( 'customizer', get_template_directory_uri() . '/inc/js/customizer.js', array('jquery'), '2.6.0', true );
}
add_action( 'customize_controls_enqueue_scripts', 'fresh_bakers_shop_customize_enque_js', 0 );


if( ! function_exists( 'fresh_bakers_shop_block_editor_styles' ) ) :
/**
 * Enqueue editor styles for Gutenberg
 */
function fresh_bakers_shop_block_editor_styles() {
	// Use minified libraries if SCRIPT_DEBUG is false
	$fresh_bakers_shop_build  = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '/build' : '';
	$fresh_bakers_shop_suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
	
	// Block styles.
	wp_enqueue_style( 'fresh-bakers-shop-block-editor-style', get_template_directory_uri() . '/css' . $fresh_bakers_shop_build . '/editor-block' . $fresh_bakers_shop_suffix . '.css' );
}
endif;
add_action( 'enqueue_block_editor_assets', 'fresh_bakers_shop_block_editor_styles' );

/**
 * Remove header text setting and control from the Customizer.
 */
function fresh_bakers_shop_remove_customizer_setting($wp_customize) {
    // Replace 'your_setting_id' with the actual ID or name of the setting you want to remove
    $wp_customize->remove_control('display_header_text');
    $wp_customize->remove_setting('display_header_text');
}
add_action('customize_register', 'fresh_bakers_shop_remove_customizer_setting');

function fresh_bakers_shop_custom_blog_banner_title() {
    if (is_404()) {
        echo '<h1 class="entry-title">'. esc_html__( 'Comments are closed.', 'fresh-bakers-shop' ).'</h1>';
    } elseif (is_search()) {
        echo '<h1 class="entry-title">'. esc_html__( 'Search Result For.', 'fresh-bakers-shop' ).' ' . get_search_query() . '</h1>';
    } elseif (is_home() && !is_front_page()) {
        echo '<h1 class="entry-title">'. esc_html__( 'Blogs', 'fresh-bakers-shop' ).'</h1>';
    } elseif (function_exists('is_shop') && is_shop()) {
        echo '<h1 class="entry-title">'. esc_html__( 'Shop', 'fresh-bakers-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>');
    }
	fresh_bakers_shop_the_breadcrumb();
}

function fresh_bakers_shop_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 fresh_bakers_shop_enqueue_google_fontss() {
    $fresh_bakers_shop_heading_font_family = get_theme_mod('fresh_bakers_shop_heading_font_family', '');
    $fresh_bakers_shop_body_font_family = get_theme_mod('fresh_bakers_shop_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',
        'Inter'         => 'Inter:wght@400;700',
        'Lato'           => 'Lato:wght@400;700',
        'Montserrat'     => 'Montserrat:wght@400;700',
        'Libre Baskerville' => 'Libre+Baskerville:wght@400;700'
    );

    $fresh_bakers_shop_google_fonts_url = '';

    if (!empty($google_fonts[$fresh_bakers_shop_heading_font_family]) || !empty($google_fonts[$fresh_bakers_shop_body_font_family])) {
        $fonts = array();

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

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

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

    if ($fresh_bakers_shop_google_fonts_url) {
        wp_enqueue_style('fresh-bakers-shop-google-fonts', $fresh_bakers_shop_google_fonts_url, false);
    }
}
add_action('wp_enqueue_scripts', 'fresh_bakers_shop_enqueue_google_fontss');


/*-----------------------Typography Function---------------------------------------*/

function fresh_bakers_shop_apply_typography() {
    $fresh_bakers_shop_heading_font_family = get_theme_mod('fresh_bakers_shop_heading_font_family');
    $fresh_bakers_shop_body_font_family = get_theme_mod('fresh_bakers_shop_body_font_family');

    $fresh_bakers_shop_custom_css = '';

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

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

    if (!empty($fresh_bakers_shop_custom_css)) {
        wp_add_inline_style('fresh-bakers-shop-style', $fresh_bakers_shop_custom_css);
    }
}
add_action('wp_enqueue_scripts', 'fresh_bakers_shop_apply_typography');


/*-----------------------Menu Typography Start---------------------------------------*/

function fresh_bakers_shop_menu_customizer_css() {
    $fresh_bakers_shop_menu_font_weight = get_theme_mod('fresh_bakers_shop_menu_font_weight', '500');
    $fresh_bakers_shop_menu_text_transform = get_theme_mod('fresh_bakers_shop_menu_text_transform', 'capitalize');

    $fresh_bakers_shop_custom_css = "
        .main-navigation ul li a {
            font-weight: " . esc_html($fresh_bakers_shop_menu_font_weight) . ";
            text-transform: " . esc_html($fresh_bakers_shop_menu_text_transform) . ";
        }
    ";

    wp_add_inline_style('fresh-bakers-shop-style', $fresh_bakers_shop_custom_css);
}
add_action('wp_enqueue_scripts', 'fresh_bakers_shop_menu_customizer_css');

/*-----------------------Menu Typography End---------------------------------------*/