<?php
/**
 * Furniture Decor functions and definitions.
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package furniture_decor
 */
if ( ! defined( 'FURNITURE_DECOR_URL' ) ) {
    define( 'FURNITURE_DECOR_URL', esc_url( 'https://www.themeignite.com/products/decor-wordpress-theme', 'furniture-decor') );
}
if ( ! defined( 'FURNITURE_DECOR_FREE_DOC_URL' ) ) {
    define( 'FURNITURE_DECOR_FREE_DOC_URL', esc_url( 'https://demo.themeignite.com/documentation/furniture-decor-free', 'furniture-decor') );
}
if ( ! defined( 'FURNITURE_DECOR_PRO_DOC_URL' ) ) {
    define( 'FURNITURE_DECOR_PRO_DOC_URL', esc_url( 'https://demo.themeignite.com/documentation/furniture-pro/', 'furniture-decor') );
}
if ( ! defined( 'FURNITURE_DECOR_DEMO_URL' ) ) {
    define( 'FURNITURE_DECOR_DEMO_URL', esc_url( 'https://demo.themeignite.com/furniture-decor/', 'furniture-decor') );
}
if ( ! defined( 'FURNITURE_DECOR_REVIEW_URL' ) ) {
    define( 'FURNITURE_DECOR_REVIEW_URL', esc_url( 'https://wordpress.org/support/theme/furniture-decor/reviews/#new-post', 'furniture-decor') );
}
if ( ! defined( 'FURNITURE_DECOR_SUPPORT_URL' ) ) {
    define( 'FURNITURE_DECOR_SUPPORT_URL', esc_url( 'https://wordpress.org/support/theme/furniture-decor', 'furniture-decor') );
}
if ( ! defined( 'FURNITURE_DECOR_BUNDLE_URL' ) ) {
    define( 'FURNITURE_DECOR_BUNDLE_URL', esc_url( 'https://www.themeignite.com/products/wp-theme-bundle', 'furniture-decor') );
}

$furniture_decor_theme_data = wp_get_theme();
if( ! defined( 'FURNITURE_DECOR_THEME_VERSION' ) ) define ( 'FURNITURE_DECOR_THEME_VERSION', $furniture_decor_theme_data->get( 'Version' ) );
if( ! defined( 'FURNITURE_DECOR_THEME_NAME' ) ) define( 'FURNITURE_DECOR_THEME_NAME', $furniture_decor_theme_data->get( 'Name' ) );

if ( ! function_exists( 'furniture_decor_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 furniture_decor_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', 'furniture-decor' ),
	) );

	/*
	 * 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( 'furniture_decor_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( 'furniture-decor', 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';

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

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

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

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

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

    /**
     * setup wizard
     */
    require get_parent_theme_file_path( '/theme-wizard/config.php' );

}
endif;
add_action( 'after_setup_theme', 'furniture_decor_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 furniture_decor_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'furniture_decor_content_width', 780 );
}
add_action( 'after_setup_theme', 'furniture_decor_content_width', 0 );


/**
 * Register widget area.
 *
 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
 */
function furniture_decor_widgets_init() {
	register_sidebar( array(
		'name'          => esc_html__( 'Sidebar Option', 'furniture-decor' ),
		'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', 'furniture-decor' ),
		'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', 'furniture-decor' ),
		'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', 'furniture-decor' ),
		'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', 'furniture-decor' ),
		'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', 'furniture-decor' ),
		'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', 'furniture-decor' ),
		'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>',
	) );

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

if( ! function_exists( 'furniture_decor_scripts' ) ) :
/**
 * Enqueue scripts and styles.
 */
function furniture_decor_scripts() {

	// Use minified libraries if SCRIPT_DEBUG is false
    $furniture_decor_build  = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '/build' : '';
    $furniture_decor_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( 'furniture-decor-style', get_stylesheet_uri(), array(), FURNITURE_DECOR_THEME_VERSION );

	wp_style_add_data('furniture-decor-style', 'rtl', 'replace');

	require get_parent_theme_file_path( '/inc/css_custom.php' );
	wp_add_inline_style( 'furniture-decor-style',$furniture_decor_custom_css );
	
  	wp_enqueue_script( 'all', get_template_directory_uri() . '/js' . $furniture_decor_build . '/all' . $furniture_decor_suffix . '.js', array( 'jquery' ), '6.1.1', true );
  	wp_enqueue_script( 'v4-shims', get_template_directory_uri() . '/js' . $furniture_decor_build . '/v4-shims' . $furniture_decor_suffix . '.js', array( 'jquery' ), '6.1.1', true );
  	wp_enqueue_script( 'furniture-decor-modal-accessibility', get_template_directory_uri() . '/js' . $furniture_decor_build . '/modal-accessibility' . $furniture_decor_suffix . '.js', array( 'jquery' ), FURNITURE_DECOR_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( 'furniture-decor-js', get_template_directory_uri() . '/js/build/custom.js', array('jquery'), FURNITURE_DECOR_THEME_VERSION, true );

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

if( ! function_exists( 'furniture_decor_admin_scripts' ) ) :
/**
 * Addmin scripts
*/
function furniture_decor_admin_scripts() {
	wp_enqueue_style( 'furniture-decor-admin-style',get_template_directory_uri().'/inc/css/admin.css', FURNITURE_DECOR_THEME_VERSION, 'screen' );
}
endif;
add_action( 'admin_enqueue_scripts', 'furniture_decor_admin_scripts' );

function furniture_decor_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', 'furniture_decor_customize_enque_js', 0 );


if( ! function_exists( 'furniture_decor_block_editor_styles' ) ) :
/**
 * Enqueue editor styles for Gutenberg
 */
function furniture_decor_block_editor_styles() {
	// Use minified libraries if SCRIPT_DEBUG is false
	$furniture_decor_build  = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '/build' : '';
	$furniture_decor_suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
	
	// Block styles.
	wp_enqueue_style( 'furniture-decor-block-editor-style', get_template_directory_uri() . '/css' . $furniture_decor_build . '/editor-block' . $furniture_decor_suffix . '.css' );
}
endif;
add_action( 'enqueue_block_editor_assets', 'furniture_decor_block_editor_styles' );

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

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

        wp_die();
    }
}
add_action(
    'wp_ajax_furniture_decor_dismiss_whizzie_notice',
    'furniture_decor_dismiss_whizzie_notice'
);


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

        return (bool) get_user_meta(
            get_current_user_id(),
            'furniture_decor_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, 'furniture_decor_whizzie_dismissed' );
    }

});

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

    // Display the notice only if not dismissed
    if (!$dismissed) {
        ?>
        <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.', 'furniture-decor' ), '<strong>Furniture Decor</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!', 'furniture-decor'); ?></p>
                    <div class="notice-btns">
                        <a class="button button-primary getstart"
                           href="<?php echo esc_url(admin_url('themes.php?page=furniture-decor')); ?>"><?php esc_html_e('Getting Started', 'furniture-decor') ?></a>
                       	<a class="button button-primary premium" target="_blank" href="<?php echo esc_url(FURNITURE_DECOR_URL); ?>"><?php esc_html_e('Go To Premium', 'furniture-decor') ?></a>
						<a class="button button-primary demo" target="_blank" href="<?php echo esc_url(FURNITURE_DECOR_DEMO_URL); ?>"><?php esc_html_e('View Demo', 'furniture-decor') ?></a>
                    </div>
                </div>
                <div class="notice-img">
                    <a href="<?php echo esc_url( FURNITURE_DECOR_BUNDLE_URL ); ?>" target="_blank"><img src="<?php echo esc_url( get_template_directory_uri() . '/images/notice.png' ); ?>"></a>
                </div>
            </div>
        </div>
        <?php
    }
}

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

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

// Hook for the AJAX action
add_action('wp_ajax_furniture_decor_dismissable_notice', 'furniture_decor_dismissable_notice');

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

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

/**
 * Remove header text setting and control from the Customizer.
 */
function furniture_decor_remove_customizer_setting($wp_customize) {
    $wp_customize->remove_control('display_header_text');
    $wp_customize->remove_setting('display_header_text');
}
add_action('customize_register', 'furniture_decor_remove_customizer_setting');

function furniture_decor_menu_customizer_css() {
    ?>
    <style type="text/css">
        .main-navigation ul li a {
            font-weight: <?php echo esc_html( get_theme_mod( 'menu_font_weight', '500' ) ); ?>;
            text-transform: <?php echo esc_html( get_theme_mod( 'menu_text_transform', 'capitalize' ) ); ?>;
        }
    </style>
    <?php
}
add_action('wp_head', 'furniture_decor_menu_customizer_css');

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

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

    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 furniture_decor_body_classess( $classes ) {
    $hide_post_meta = get_theme_mod( 'furniture_decor_single_post_meta_setting', true );
    if ( !$hide_post_meta ) {
        $classes[] = 'hide-post-meta';
    }
    return $classes;
}
add_filter( 'body_class', 'furniture_decor_body_classess' );

function furniture_decor_wc_display_sidebar() {
    if ( is_shop() || is_product_category() || is_product() ) { 
        echo '<div id="primary" class="content-area ' . ( get_theme_mod( 'furniture_decor_sidebar_position', 'right' ) === 'no-sidebar' ? 'full-width' : '' ) . '">';
        echo '<main id="main" class="site-main">';
        woocommerce_content();
        echo '</main><!-- #main -->';
        echo '</div><!-- #primary -->';

        furniture_decor_wc_sidebar_cb();
    } else {
        echo 'Not a WooCommerce page';
    }
}

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

function furniture_decor_enqueue_google_fontss() {
    $furniture_decor_heading_font_family = get_theme_mod('furniture_decor_heading_font_family', '');
    $furniture_decor_body_font_family = get_theme_mod('furniture_decor_body_font_family', '');

    $google_fonts = array(
        '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'
    );

    $fonts = array();
    if (!empty($google_fonts[$furniture_decor_heading_font_family])) {
        $fonts[] = $google_fonts[$furniture_decor_heading_font_family];
    }
    if (!empty($google_fonts[$furniture_decor_body_font_family])) {
        $fonts[] = $google_fonts[$furniture_decor_body_font_family];
    }

    if (!empty($fonts)) {
        $furniture_decor_google_fonts_url = add_query_arg(
            'family',
            implode('|', $fonts),
            'https://fonts.googleapis.com/css2'
        );

        wp_enqueue_style('furniture-decor-google-fonts', $furniture_decor_google_fonts_url, false);
    }
}
add_action('wp_enqueue_scripts', 'furniture_decor_enqueue_google_fontss');



function furniture_decor_apply_typography() {
    $furniture_decor_heading_font_family = get_theme_mod('furniture_decor_heading_font_family');
    $furniture_decor_body_font_family = get_theme_mod('furniture_decor_body_font_family');

    $furniture_decor_custom_css = '';

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

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

    if (!empty($furniture_decor_custom_css)) {
        wp_add_inline_style('furniture-decor-style', $furniture_decor_custom_css);
    }
}
add_action('wp_enqueue_scripts', 'furniture_decor_apply_typography');