<?php
/**
 * Theme settings
 *
 * @package wpstream-theme
 */

// Allow HTML in term (category, tag) descriptions END.
// Remove the default filters that sanitize and remove HTML from term descriptions

add_filter( 'pre_user_description', 'wp_filter_post_kses' ); // Adds a filter that applies post KSES filtering for user descriptions

/**
 * Register block styles
 *
 * This function is currently a placeholder for future implementation of block styles.
 * Block styles allow you to define custom styles for specific blocks in the block editor.
 *
 * @package wpstream-theme
 */
function wpstream_register_block_styles() {
    // Placeholder for registering block styles
}
add_action( 'init', 'wpstream_register_block_styles' );

/**
 * Register block patterns
 *
 * This function is currently a placeholder for future implementation of block patterns.
 * Block patterns are predefined block layouts that users can insert into their content.
 *
 * @package wpstream-theme
 */
function wpstream_register_block_patterns() {
    // Placeholder for registering block patterns
}
add_action( 'init', 'wpstream_register_block_patterns' );

/**
 * Register editor styles
 *
 * This function is currently a placeholder for future implementation of editor styles.
 * Editor styles ensure that the block editor displays content in a way that matches the theme's front-end styles.
 *
 * @package wpstream-theme
 */
function wpstream_add_editor_styles() {
    // Placeholder for adding editor styles
}
add_action( 'admin_init', 'wpstream_add_editor_styles' );

/**
 * Hook after #primary.
 *
 * Defines a custom hook that allows developers to add content or functionality 
 * after the primary content area in the theme.
 */
function wpstream_bs_after_primary() {
    do_action( 'bs_after_primary' );
}

// Disable Gutenberg blocks in widgets (WordPress 5.8).
// Disables the block editor from managing widgets in the Gutenberg plugin.
add_filter( 'gutenberg_use_widgets_block_editor', '__return_false' );
// Disables the block editor from managing widgets in the WordPress core.
add_filter( 'use_widgets_block_editor', '__return_false' );

// Process shortcodes in text widgets.
// Allows shortcodes to be processed and rendered when used in text widgets.
add_filter( 'widget_text', 'do_shortcode' );


// Register custom navigation walker and navigation menus.
add_action( 'after_setup_theme', 'wpstream_register_navwalker', 20 );
if ( ! function_exists( 'wpstream_register_navwalker' ) ) {
    /**
     * Register NavWalker
     *
     * Includes the custom navigation walker class for Bootstrap 5 and registers multiple navigation menus.
     */
    function wpstream_register_navwalker() {
	    require_once get_theme_file_path( '/inc/class-wpstream-bootstrap-5-wp-nav-menu-walker.php' ); // Include the custom nav walker class
        register_nav_menu( 'main-menu', 'Main menu' ); // Register the main menu
        register_nav_menu( 'footer-menu', 'Footer menu' ); // Register the footer menu
        register_nav_menu( 'main-menu2', 'Header Type 2nd Menu' ); // Register the second header menu
        register_nav_menu( 'main-menu3', 'Header Type 3rd Menu' ); // Register the third header menu
    }
}

// Register custom comment list functionality.
add_action( 'after_setup_theme', 'wpstream_register_comment_list' );
if ( ! function_exists( 'wpstream_register_comment_list' ) ) {
    /**
     * Register Comment List
     *
     * Includes the custom comment list template for displaying comments.
     */
    function wpstream_register_comment_list() {
        require_once get_theme_file_path( 'inc/comment-list.php' ); // Include the custom comment list template
    }
}

// Theme setup function to initialize various theme supports and configurations.
add_action( 'after_setup_theme', 'wpstream_theme_setup', 11 );
if ( ! function_exists( 'wpstream_theme_setup' ) ) {
    /**
     * Theme Setup
     *
     * Sets up theme defaults and registers support for various WordPress features.
     */
    function wpstream_theme_setup() {
        load_theme_textdomain( 'hello-wpstream', get_template_directory() . '/languages' ); // Load theme textdomain for translations
        add_theme_support( 'automatic-feed-links' ); // Add default posts and comments RSS feed links to head
        add_theme_support( 'title-tag' ); // Let WordPress manage the document title
        add_theme_support( 'post-thumbnails' ); // Enable support for Post Thumbnails
        add_theme_support( 'site-icon' ); // Enable support for site icons
        add_theme_support( 
            'html5', 
            array( 
                'comment-form', 
                'comment-list', 
                'gallery', 
                'caption' 
            ) 
        ); // Enable support for HTML5 markup
        add_theme_support( 'customize-selective-refresh-widgets' ); // Enable support for selective refresh for widgets in the Customizer
        
        // Enable additional theme supports
        $args = array();
        add_theme_support( 'wp-block-styles' ); // Enable support for block styles
        add_theme_support( 'responsive-embeds' ); // Enable support for responsive embeds
        add_theme_support( 'custom-logo', $args ); // Enable support for custom logo
        add_theme_support( 'custom-header', $args ); // Enable support for custom header
        add_theme_support( 'custom-background', $args ); // Enable support for custom background
        add_theme_support( 'align-wide' ); // Enable support for wide alignment

        wpstream_theme_image_size(); // Initialize custom image sizes
        wpstream_create_helper_content(); // Create helper content pages
    }
}

/**
 * Create Helper Content
 *
 * Creates necessary pages for the theme if they do not already exist.
 */
function wpstream_create_helper_content() {
    update_option( 'wpstream_theme_page_creation', '' ); // Reset the page creation option
    if ( get_option( 'wpstream_theme_page_creation' ) !== 'yes' ) {
        $page_creation = array(
            array(
                'name'     => 'Dashboard',
                'template' => 'wpstream-theme-dashboard.php',
            ),
        );

        foreach ( $page_creation as $key => $template ) {
            if ( trim( wstream_get_template_link( $template['template'], 1 ) ) === trim( home_url( '/' ) ) ) {
                $my_post = array(
                    'post_title'  => $template['name'],
                    'post_type'   => 'page',
                    'post_status' => 'publish',
                );

                $new_id = wp_insert_post( $my_post ); // Insert the new page
                update_post_meta( $new_id, '_wp_page_template', $template['template'] ); // Set the page template

                if ( 'wpstream-theme-dashboard.php' === $template['template'] ) {
                    update_option( 'woocommerce_myaccount_page_id', $new_id ); // Update the WooCommerce My Account page ID
                }
            }
        }

        update_option( 'wpstream_theme_page_creation', 'yes' ); // Mark the pages as created
    }
}

// Override single product template with custom template.
add_filter( 'template_include', 'custom_single_product_template_include', 50, 1 );
/**
 * Override Single Product Template
 *
 * Overrides the default single product template with a custom template.
 *
 * @param string $template The default template file path.
 * @return string The template file path to be used.
 */
function custom_single_product_template_include( $template ) {
    if ( is_singular( 'product' ) ) {
        $template = get_stylesheet_directory() . '/woocommerce/single-product.php'; // Set the custom single product template
    }
    return $template;
}

// Initialize custom image sizes.
add_action( 'after_setup_theme', 'wpstream_theme_image_size' );
if ( ! function_exists( 'wpstream_theme_image_size' ) ) {
    /**
     * Image Sizes
     *
     * Registers custom image sizes used by the theme.
     */
    function wpstream_theme_image_size() {
        set_post_thumbnail_size( 250, 220, true ); // Set the default post thumbnail size
        add_image_size( 'wpstream_user_image', 96, 96, true ); // Add custom image size for user images
        add_image_size( 'wpstream_featured_blog_image', 1024, 600, true ); // Add custom image size for featured blog images
        add_image_size( 'wpstream_featured_image_tower', 555, 390, true ); // Add custom image size for featured image towers
        add_image_size( 'wpstream_featured_unit_cards', 326, 183, true ); // Add custom image size for featured unit cards
        add_image_size( 'wpstream_bundle_unit_cards_image', 316, 449, true ); // Add custom image size for bundle unit cards
        add_image_size( 'wpstream_featured_shortcodes', 750, 350, true ); // Add custom image size for featured shortcodes
        add_image_size( 'wpstream_categories_image', 600, 600, true ); // Add custom image size for category images
    }
}



if ( ! function_exists( 'wpstream_theme_widgets_init' ) ) {
	/**
	 * Registers various widget areas (sidebars) in the theme.
	 * This function will create multiple widget areas such as header areas, sidebars, and footers.
	 */
	function wpstream_theme_widgets_init() {
		// Register a widget area for the header, between search form and user menu.
		register_sidebar(
			array(
				'name'          => esc_html__( 'Header Area', 'hello-wpstream' ),
				'id'            => 'top-nav',
				'description'   => esc_html__( 'Widgets will be added between search form and user menu', 'hello-wpstream' ),
				'before_widget' => '<div class="top-nav-widget ms-2">',
				'after_widget'  => '</div>',
				'before_title'  => '<div class="widget-title d-none">',
				'after_title'   => '</div>',
			)
		);

		// Register a second widget area for the header, between main menu and search form.
		// Moves to off-canvas on screens smaller than the lg breakpoint.
		register_sidebar(
			array(
				'name'          => esc_html__( 'Header Area 2', 'hello-wpstream' ),
				'id'            => 'top-nav-2',
				'description'   => esc_html__( 'Widgets will be added between main menu and search form', 'hello-wpstream' ),
				'before_widget' => '<div class="top-nav-widget-2 d-lg-flex align-items-lg-center mt-2 mt-lg-0 ms-lg-2">',
				'after_widget'  => '</div>',
				'before_title'  => '<div class="widget-title d-none">',
				'after_title'   => '</div>',
			)
		);

		// Register the main sidebar widget area.
		register_sidebar(
			array(
				'name'          => esc_html__( 'Sidebar', 'hello-wpstream' ),
				'id'            => 'sidebar-1',
				'description'   => esc_html__( 'Add widgets here.', 'hello-wpstream' ),
				'before_widget' => '<section id="%1$s" class="%2$s widget mb-30">',
				'after_widget'  => '</section>',
				'before_title'  => '<h2 class="widget-title">',
				'after_title'   => '</h2>',
			)
		);

		// Register the sidebar widget area for VOD (Video On Demand).
		register_sidebar(
			array(
				'name'          => esc_html__( 'Sidebar VOD', 'hello-wpstream' ),
				'id'            => 'sidebar-vod',
				'description'   => esc_html__( 'Add widgets here.', 'hello-wpstream' ),
				'before_widget' => '<section id="%1$s" class="%2$s widget mb-30">',
				'after_widget'  => '</section>',
				'before_title'  => '<h2 class="widget-title">',
				'after_title'   => '</h2>',
			)
		);

		// Register the sidebar widget area for live events.
		register_sidebar(
			array(
				'name'          => esc_html__( 'Sidebar Live Events', 'hello-wpstream' ),
				'id'            => 'sidebar-live',
				'description'   => esc_html__( 'Add widgets here.', 'hello-wpstream' ),
				'before_widget' => '<section id="%1$s" class="%2$s widget mb-30">',
				'after_widget'  => '</section>',
				'before_title'  => '<h2 class="widget-title">',
				'after_title'   => '</h2>',
			)
		);

		// Register the sidebar widget area for WooCommerce products.
		register_sidebar(
			array(
				'name'          => esc_html__( 'Sidebar WooCommerce Products', 'hello-wpstream' ),
				'id'            => 'sidebar-products',
				'description'   => esc_html__( 'Add widgets here.', 'hello-wpstream' ),
				'before_widget' => '<section id="%1$s" class="%2$s widget mb-30">',
				'after_widget'  => '</section>',
				'before_title'  => '<h2 class="widget-title">',
				'after_title'   => '</h2>',
			)
		);

		// Register the first footer widget area.
		register_sidebar(
			array(
				'name'          => esc_html__( 'Footer 1', 'hello-wpstream' ),
				'id'            => 'footer-1',
				'description'   => esc_html__( 'Add widgets here.', 'hello-wpstream' ),
				'before_widget' => '<div class="footer_widget mb-4">',
				'after_widget'  => '</div>',
				'before_title'  => '<h2 class="widget-title h5">',
				'after_title'   => '</h2>',
			)
		);

		// Register the second footer widget area.
		register_sidebar(
			array(
				'name'          => esc_html__( 'Footer 2', 'hello-wpstream' ),
				'id'            => 'footer-2',
				'description'   => esc_html__( 'Add widgets here.', 'hello-wpstream' ),
				'before_widget' => '<div class="footer_widget mb-4">',
				'after_widget'  => '</div>',
				'before_title'  => '<h2 class="widget-title h5">',
				'after_title'   => '</h2>',
			)
		);

		// Register the third footer widget area.
		register_sidebar(
			array(
				'name'          => esc_html__( 'Footer 3', 'hello-wpstream' ),
				'id'            => 'footer-3',
				'description'   => esc_html__( 'Add widgets here.', 'hello-wpstream' ),
				'before_widget' => '<div class="footer_widget mb-4">',
				'after_widget'  => '</div>',
				'before_title'  => '<h2 class="widget-title h5">',
				'after_title'   => '</h2>',
			)
		);

		// Register the fourth footer widget area.
		register_sidebar(
			array(
				'name'          => esc_html__( 'Footer 4', 'hello-wpstream' ),
				'id'            => 'footer-4',
				'description'   => esc_html__( 'Add widgets here.', 'hello-wpstream' ),
				'before_widget' => '<div class="footer_widget mb-4">',
				'after_widget'  => '</div>',
				'before_title'  => '<h2 class="widget-title h5">',
				'after_title'   => '</h2>',
			)
		);

		// Register the fifth footer widget area.
		register_sidebar(
			array(
				'name'          => esc_html__( 'Footer 5', 'hello-wpstream' ),
				'id'            => 'footer-5',
				'description'   => esc_html__( 'Add widgets here.', 'hello-wpstream' ),
				'before_widget' => '<div class="footer_widget mb-4">',
				'after_widget'  => '</div>',
				'before_title'  => '<h2 class="widget-title h5">',
				'after_title'   => '</h2>',
			)
		);

		// Register a widget area for footer information.
		register_sidebar(
			array(
				'name'          => esc_html__( 'Footer Info', 'hello-wpstream' ),
				'id'            => 'footer-info',
				'description'   => esc_html__( 'Add widgets here.', 'hello-wpstream' ),
				'before_widget' => '<div class="footer_widget">',
				'after_widget'  => '</div>',
				'before_title'  => '<div class="widget-title d-none">',
				'after_title'   => '</div>',
			)
		);
	}

	// Hook the function into the 'widgets_init' action.
	add_action( 'widgets_init', 'wpstream_theme_widgets_init' );
}


/**
 * Modify the Font Awesome CSS tag to preload it with all media types.
 *
 * @param string $tag The original HTML tag for Font Awesome CSS.
 * @return string Modified HTML tag for Font Awesome CSS.
 */
function wpstream_theme_fa_preload( $tag ) {
	// Use regular expression to add a preload attribute to the Font Awesome CSS link tag.
	$tag = preg_replace( "/id='fontawesome-css'/", "id='fontawesome-css' online=\"if(media!='all')media='all'\"", $tag );
	return $tag;
}

if ( ! function_exists( 'wpstream_theme_redux_config' ) ) {
	/**
	 * Set up the Redux framework for theme options.
	 */
	function wpstream_theme_redux_config() {
		if ( class_exists( 'ReduxFramework' ) ) {
			// Include the theme options files if ReduxFramework class exists.
			require_once get_theme_file_path( '/framework/options/theme-options.php' );
			require_once get_theme_file_path( '/framework/options/main.php' );
		}
	}
}
// Hook the Redux setup function into the 'after_setup_theme' action with priority 20.
add_action( 'after_setup_theme', 'wpstream_theme_redux_config', 20 );

/**
 * Remove specific wpstream filters.
 */
add_action( 'init', 'wpstream_remove_wpstream_filter' );

function wpstream_remove_wpstream_filter() {
	global $wpstream_plugin;

	if ( class_exists( 'Wpstream_Player' ) ) {
		// Instantiate the Wpstream_Player class if it exists.
		$pstream_player = new Wpstream_Player( $wpstream_plugin->main );
		// Remove filters applied by wpstream.
		remove_filter( 'the_content', 'wpstream_filter_the_title' );
		remove_filter( 'woocommerce_before_single_product', array( $pstream_player, 'wpstream_user_logged_in_product_already_bought' ) );
	}
}

add_action( 'after_setup_theme', 'wpstream_theme_content_width', 0 );
/**
 * Set the content width for the theme.
 */
function wpstream_theme_content_width() {
	// Set the global content width to 640 pixels.
	$GLOBALS['content_width'] = apply_filters( 'wpstream_theme_content_width', 640 );
}

add_filter( 'woocommerce_show_admin_notice', function( $show, $notice ) {
	if ( 'template_files' === $notice ) {
		return false; // Disable only the notices about the outdated WooCommerce templates
	}
	return $show;
}, 10, 2 );

