<?php

/**
 * Functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package business-directory-bikit
 * @since 1.0.0
 */

/**
 * The theme version.
 *
 * @since 1.0.0
 */
define('BIKITDB_', wp_get_theme()->get('Version'));


/**
 * Add theme support for block styles and editor style.
 *
 * @since 1.0.0
 *
 * @return void
 */
function bikitdb_setup()
{
	add_editor_style('./assets/css/style-shared.css');

	/*
	 * Load additional block styles.
	 * See details on how to add more styles in the readme.txt.
	 */
	$styled_blocks = ['button', 'quote', 'navigation', 'search'];
	foreach ($styled_blocks as $block_name) {
		$args = array(
			'handle' => "bikitdb-$block_name",
			'src'    => get_theme_file_uri("assets/css/blocks/$block_name.min.css"),
			'path'   => get_theme_file_path("assets/css/blocks/$block_name.min.css"),
		);
		// Replace the "core" prefix if you are styling blocks from plugins.
		wp_enqueue_block_style("core/$block_name", $args);
	}

	load_theme_textdomain('business-directory-bikit', get_template_directory() . '/languages');

	// Add support for WooCommerce
	add_theme_support('woocommerce');
	add_theme_support('wc-product-gallery-zoom');
	//add_theme_support('wc-product-gallery-lightbox');
	//add_theme_support('wc-product-gallery-slider');

	remove_theme_support('wc-product-gallery-zoom');
	// custom logo
	add_theme_support('custom-logo');

	// Add theme support for selective refresh for widgets.
	add_theme_support('customize-selective-refresh-widgets');
	add_theme_support("wp-block-styles");
	add_theme_support("responsive-embeds");
	add_theme_support('custom-header');
	add_theme_support("custom-background");
	add_theme_support("align-wide");

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

	// 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(
		'main_menu' => esc_html__('Primary Menu', 'business-directory-bikit'),
		'footer_links' => esc_html__('Footer Links', 'business-directory-bikit'),
		'mobile_menu' => esc_html__('Mobile Menu', 'business-directory-bikit'),
	));
}

add_action('after_setup_theme', 'bikitdb_setup');


/**
 * Enqueue the CSS files.
 *
 * @since 1.0.0
 *
 * @return void
 */
function bikitdb_styles()
{
	wp_enqueue_style(
		'bikitdb-style',
		get_stylesheet_uri(),
		[],
		BIKITDB_
	);
	wp_enqueue_style(
		'bikitdb-shared-styles',
		get_theme_file_uri('assets/css/style-shared.css'),
		[],
		BIKITDB_
	);

	$remote_url = 'https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;1,100;1,200;1,300;1,400;1,500;1,600;1,700&family=Space+Grotesk:wght@300..700&family=Dm Sans:wght@300;400;500;600;700&display=swap';
	if (function_exists('wptt_get_webfont_url')) {
		wp_enqueue_style('google-fonts',  wptt_get_webfont_url($remote_url), false);
	}

	wp_enqueue_style(
		'bootstrap-grid-5',
		get_theme_file_uri('assets/libs/bootstrap-5.3.3/css/bootstrap-grid.min.css'),
		[],
		'5.3.3'
	);
	wp_enqueue_script('bootstrap-5', get_theme_file_uri('assets/libs/bootstrap-5.3.3/js/bootstrap.min.js'), ['jquery'], '5.3.3', true);

	wp_enqueue_style(
		'fontawesome-6.5',
		get_theme_file_uri('assets/libs/fontawesome-6.7/css/all.min.css'),
		[],
		'6.7'
	);
	wp_enqueue_style(
		'business-directory-bikit',
		get_theme_file_uri('assets/css/theme.css'),
		[],
		BIKITDB_
	);
	wp_enqueue_style(
		'bikitdb-responsive',
		get_theme_file_uri('assets/css/theme-responsive.css'),
		[],
		BIKITDB_
	);

	wp_enqueue_script('business-directory-bikit', get_theme_file_uri('assets/js/theme.js'), ['jquery'], BIKITDB_, true);
}
add_action('wp_enqueue_scripts', 'bikitdb_styles');

// Filters.
require_once get_theme_file_path('inc/filters.php');

// Block variation example.
require_once get_theme_file_path('inc/register-block-variations.php');

// Block style examples.
require_once get_theme_file_path('inc/register-block-styles.php');

// Block pattern and block category examples.
require_once get_theme_file_path('inc/register-block-patterns.php');

require_once get_theme_file_path('includes/welcome.php');
require_once get_theme_file_path('includes/init.php');

// Font upload
require_once get_theme_file_path('includes/wptt-webfont-loader.php');



/**
 * Admin styles.
 *
 */

 add_action('admin_head', function () {
    echo '<style>
      .appearance_page-dashboard #setting-error-tgmpa {
        margin-left: 0;
      }
      
	  .ocdi__content-container .ocdi-content-notice--warning,
      .ocdi__content-container #setting-error-tgmpa,
      .ocdi-install-plugins-content-content .ocdi-content-notice.ocdi-content-notice--warning,
      .ocdi__file-upload-container .ocdi__file-upload-container-items .ocdi__file-upload:not(:first-child),
      .ocdi__file-upload-container .ocdi__file-upload-container-items--second-row,
      .ocdi__content-container .plugin-item.plugin-item-wpforms-lite,
      .ocdi__content-container .plugin-item.plugin-item-all-in-one-seo-pack,
      .ocdi__content-container .plugin-item.plugin-item-google-analytics-for-wordpress {
        display: none !important;
      }
      
      .button.button-primary.js-ocdi-install-plugins-before-import.ocdi-button-disabled::after {
        content: "\f113";
        font-family: dashicons;
        display: inline-block;
        line-height: 1;
        font-weight: 400;
        font-style: normal;
        speak: never;
        text-decoration: inherit;
        text-transform: none;
        text-rendering: auto;
        -webkit-animation: nexproperty-spin 2s infinite linear;
        animation: nexproperty-spin 2s infinite linear;
        margin-left: 13px;
        display: inline-block;
    }
        
    @keyframes nexproperty-spin {
        0% {
        -webkit-transform: rotate(0deg);
        transform: rotate(0deg);
        }
        100% {
        -webkit-transform: rotate(360deg);
        transform: rotate(360deg);
        }
    }
    </style>';
  });


add_filter('intermediate_image_sizes_advanced', function ($sizes) {
	if(get_option('bikitdb_install_ocdi_images_sizes_enable') == 1) {
		unset($sizes['thumb']);
		unset($sizes['thumbnail']);
		unset($sizes['medium']);
		unset($sizes['large']);
		unset($sizes['medium_large']);
		unset($sizes['big_image_size_threshold']);
		unset($sizes['post-thumbnail']);
		unset($sizes['1536x1536']);
		unset($sizes['nexproperty-footer-thumbnail']);
		unset($sizes['nexproperty-slider-thumbnail']);
		unset($sizes['nexproperty-post-thumbnail']);
		unset($sizes['woocommerce_thumbnail']);
		unset($sizes['woocommerce_single']);
		unset($sizes['woocommerce_gallery_thumbnail']);
		unset($sizes['shop_single']);
		unset($sizes['shop_thumbnail']);
	}

	return $sizes;
});



if (get_option('wdk_theme_bikit_installed') == 1) {
		
	add_filter( 'template_include', function ( $template ) {
		// Check if the template is in the /templates/ directory and is an HTML file
		$html_templates_dir = get_template_directory() . '/templates/';

		if ( strpos( $template, 'template-canvas.php' ) ) {
			return get_template_directory() . '/html-template-wrapper.php';
		}

		if ( strpos( $template, $html_templates_dir ) === 0 && pathinfo( $template, PATHINFO_EXTENSION ) === 'html' ) {
			return get_template_directory() . '/html-template-wrapper.php';
		}
		return $template;

	} );

    add_action('wp_head', function() {
        echo '<style>
            /* Inline CSS for wdk_theme_bikit_installed */
            .hidde-on-wdk_theme_bikit_installed {
                display: none;
            }
          
        </style>';
    });
}


add_action('wp_footer', function() {
    ?>
    <style>
        #wdk-scroll-top-btn {
            position: fixed;
            right: 24px;
            bottom: 32px;
            display: none;
            z-index: 9999;
            background: #405FF2;
            color: #fff;
            width: 44px;
            height: 44px;
            border-radius: 50%;
            box-shadow: 0 2px 10px rgba(64,95,242,0.18);
            border: none;
            outline: none;
            cursor: pointer;
            transition: background 0.2s;
            justify-content: center;
            align-items: center;
            font-size: 22px;
            text-align: center;
        }
        #wdk-scroll-top-btn:hover {
            background: #2E4BEF;
        }
        #wdk-scroll-top-btn svg {
            display: block;
            margin: 0 auto;
        }
    </style>
    <button id="wdk-scroll-top-btn" title="<?php esc_attr_e('Back to Top','business-directory-bikit'); ?>">
        <!-- Simple up arrow SVG -->
        <svg width="21" height="21" viewBox="0 0 20 20" fill="none">
            <path d="M10 15V5M10 5L5 10M10 5L15 10" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
        </svg>
    </button>
    <script>
    document.addEventListener('DOMContentLoaded', function () {
        var btn = document.getElementById('wdk-scroll-top-btn');
        window.addEventListener('scroll', function() {
            if (window.scrollY > 200) {
                btn.style.display = "flex";
            } else {
                btn.style.display = "none";
            }
        });
        btn.addEventListener('click', function(e) {
            e.preventDefault();
            window.scrollTo({top: 0, behavior: 'smooth'});
        });
    });
    </script>
    <?php
});


// Redirect all tag links to the Elementor blog page with ?tag=slug

/**
 * Redirect all tag and category links to Elementor Blog page
 * Format:
 *   ?tag=tag_slug
 *   ?category_name=category_slug
 */
add_action('init', function () {

	if(function_exists('wdk_page_by_title')) {
		$page_id  = wdk_page_by_title( 'Blog Grid' );
		if (!$page_id) return;
		
		$blog_page_id = $page_id->ID;

		// Replace with your real Elementor blog page ID
		$blog_page_url = get_permalink($blog_page_id);

		// ---- Tag links ----
		add_filter('tag_link', function ($url, $tag_id) use ($blog_page_url) {
			$tag = get_tag($tag_id);
			if (!$tag) return $url;
			return add_query_arg('tag', $tag->slug, $blog_page_url);
		}, 10, 2);

		// ---- Category links ----
		add_filter('category_link', function ($url, $term_id) use ($blog_page_url) {
			$cat = get_category($term_id);
			if (!$cat) return $url;
			return add_query_arg('category_name', $cat->slug, $blog_page_url);
		}, 10, 2);
	}
});



add_action('template_redirect', function() {
    if (is_search() && function_exists('wdk_page_by_title')) {
 
		$page_id  = wdk_page_by_title( 'Blog Grid' );
		if (!$page_id) return;
		
		$blog_page_id = $page_id->ID;
		
        $elementor_instance = \Elementor\Plugin::instance();

        get_header();
        echo $elementor_instance->frontend->get_builder_content_for_display($blog_page_id);
        get_footer();

        exit;
    }
});

/**
* Add Sub Menu Page to the Layers Menu Item
*/
if( ! function_exists('bikitdb_register_submenu_page') ) {
	function bikitdb_register_submenu_page(){
		add_theme_page( __( 'Business Directory Bikit Help' , 'business-directory-bikit'  ), __( 'Business Directory Bikit Help' , 'business-directory-bikit'  ), 
							'edit_theme_options', 'business-directory-bikit-dashboard', 'bikitdb_get_child_onboarding' );
	}
}
function bikitdb_get_child_onboarding(){
	require_once get_stylesheet_directory() . '/includes/theme-help.php';
}
add_action('admin_menu', 'bikitdb_register_submenu_page', 60);

/**
* Welcome Redirect
* http://docs.layerswp.com/how-to-add-help-pages-onboarding-to-layers-themes-or-extensions/
*/
add_action( 'after_setup_theme' , function (){
	if( isset($_GET["activated"]) && $pagenow = "themes.php" ) { //&& '' == get_option( 'layers_welcome' )
		update_option( 'layers_welcome' , 1);
	}
}, 20 );
