<?php
/**
 * Digital functions and definitions
 *
 * @package Digital
 */

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
define( 'DIGITAL_VERSION', wp_get_theme()->get( 'Version' ) );
// Digital's includes directory.
$digital_inc_dir = 'inc';
define( 'DIGITAL_THEME_DIR', get_template_directory() );
define( 'DIGITAL_THEME_URI', get_template_directory_uri() );
// Array of files to include.
$digital_includes = array(
	'/theme-settings.php',                  // Initialize theme default settings.
	'/setup.php',                           // Theme setup and custom theme supports.
	'/widgets.php',                         // Register widget area.
	'/enqueue.php',                         // Enqueue scripts and styles.
	'/template-tags.php',                   // Custom template tags for this theme.
	'/pagination.php',                      // Custom pagination for this theme.
	'/hooks.php',                           // Custom hooks.
	'/extras.php',                          // Custom functions that act independently of the theme templates.
	'/customizer.php',                      // Customizer additions.
	'/custom-comments.php',                 // Custom Comments file.
	'/class-wp-bootstrap-navwalker.php',    // Load custom WordPress nav walker.
	'/editor.php',                          // Load Editor functions.
	'/block-editor.php',                    // Load Block Editor functions.
	'/deprecated.php',                      // Load deprecated functions.
	'/widget-popular-posts.php',            // Widget Popular Posts.
	'/widget-latest-posts.php',             // Widget Latest Posts.
	'/widget-block-posts.php',            	// Widget Block Posts.
	'/index-posts.php',             		// Index Posts.
	'/newsblocks.php',             		// News Posts.
	//'/incfile.php',             			// Functions.
	'/listpostsblock.php',             		// list posts block.
	'/blocks-tiles.php',             		// tiles posts block.
	'/pagemetalayout.php',             		// tiles posts block.
	 '/block-style.php',             		// block-style.
	 '/schema.php',             				// schema
	
);
function digital_styles_admin($hook) {
	if ( 'appearance_page_digital-info' != $hook ) {
        return;
    }
	wp_enqueue_style('digital-admin-styles',get_theme_file_uri( 'inc/core/bootstrap/css/admin-style.css' ),[], DIGITAL_VERSION );
	wp_enqueue_style('digital-bootstrap-responsive',get_theme_file_uri( 'inc/core/bootstrap/css/bootstrap-responsive.css' ),[], DIGITAL_VERSION );
}
add_action( 'admin_enqueue_scripts', 'digital_styles_admin' );

function digital_add_menu_page() {
	add_theme_page( 
		esc_html__( 'Digital', 'digital' ), 
		esc_html__( 'Digital', 'digital' ), 
		'edit_theme_options', 'digital-info', 'digital_theme_page_display','2' );
}
add_action( 'admin_menu', 'digital_add_menu_page' );
function digital_theme_page_display() {
	$theme = wp_get_theme();
	include_once 'inc/theme-info.php';
}
final class DIGITAL_Theme_Class {
	public static function digital_constants() {

		$version = DIGITAL_VERSION;

		// Theme version.
		define( 'DIGITAL_THEME_VERSION', $version );
		
		define( 'DIGITAL_JS_DIR_URI', DIGITAL_THEME_URI . '/js/' );
		define( 'DIGITAL_CSS_DIR_URI', DIGITAL_THEME_URI . '/css/' );

		// Include Paths.
		define( 'DIGITAL_INC_DIR', DIGITAL_THEME_DIR . '/inc/' );
		define( 'DIGITAL_INC_DIR_URI', DIGITAL_THEME_URI . '/inc/' );
		
	}
}
DIGITAL_Theme_Class::digital_constants();
// Load WooCommerce functions if WooCommerce is activated.
if ( class_exists( 'WooCommerce' ) ) {
	$digital_includes[] = '/woocommerce.php';
}

// Load Jetpack compatibility file if Jetpack is activiated.
if ( class_exists( 'Jetpack' ) ) {
	$digital_includes[] = '/jetpack.php';
}
// Include files.
foreach ( $digital_includes as $file ) {
	require_once get_theme_file_path( $digital_inc_dir . $file );
}

add_action( 'wp_footer', 'digital_fetch_ajax', 99 );
function digital_fetch_ajax() {
?>
    <script type="text/javascript">
    jQuery(document).ready(function ($) {
        $('#keyword').on('keyup', function () {
            var keyword = $(this).val();
            
            if (keyword.length > 2) { // Prevent unnecessary AJAX calls
                $.ajax({
                    url: '<?php echo admin_url('admin-ajax.php'); ?>',
                    type: 'POST',
                    data: {
                        action: 'data_fetch',
                        keyword: keyword
                    },
                    success: function (data) {
                        $('#datafetch').html(data);
                    }
                });
            }
        });
    });
    </script>
<?php
}
// the ajax function
add_action('wp_ajax_data_fetch' , 'digital_data_fetch');
add_action('wp_ajax_nopriv_data_fetch','digital_data_fetch');
function digital_data_fetch(){

    $the_query = new WP_Query( array( 'posts_per_page' => -1, 's' => esc_attr( $_POST['keyword'] ), 'post_type' => array('post') ) );
    if( $the_query->have_posts() ) :?>
	<div class="digital-instantsearch">
      <ul>
      <?php while( $the_query->have_posts() ): $the_query->the_post(); ?>
            <li><a href="<?php echo esc_url( get_permalink() ); ?>">
			<?php if (has_post_thumbnail()) : ?>
                        <div class="post-thumbnail">
                            <?php the_post_thumbnail(array(70, 70)); ?>
                        </div>
                    <?php endif; ?><?php the_title();?></a></li>
        <?php endwhile; ?>
     </ul>
	</div>
       <?php wp_reset_postdata();  
    endif;

    die();
}
// Block Theme Support
add_action( 'after_setup_theme', 'digital_block_template_part_support' );
function digital_block_template_part_support() {
	add_theme_support( 'block-template-parts' );
	add_theme_support( 'wp-block-styles' );
	add_theme_support( 'border' );
}
if ( is_user_logged_in() ) {   
	if ( ! function_exists( 'digital_primary_menu_fallback' ) ) {
		function digital_primary_menu_fallback() {		 
		  ?>	  
		<ul id="main-menu" class="navbar-nav flex-grow-1 pe-3">
			<li itemscope="itemscope" itemtype="https://www.schema.org/SiteNavigationElement" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-904 nav-item"><a title="Home" href="<?php echo esc_url(home_url()); ?>" class="nav-link"><?php echo esc_html('Home','digital_'); ?></a></li>
			<li itemscope="itemscope" itemtype="https://www.schema.org/SiteNavigationElement" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-905 nav-item"><a title="Contact us" href="<?php echo esc_url( admin_url( 'nav-menus.php' ) ); ?>" class="nav-link"><?php echo esc_html('Set primary menu','digital_'); ?></a></li>
		</ul>
		  <?php
		}
	}
}else { if ( ! function_exists( 'digital_primary_menu_fallback' ) ) {
			function digital_primary_menu_fallback() {		 
			?>	  
			<ul id="main-menu" class="navbar-nav flex-grow-1 pe-3">
				<li itemscope="itemscope" itemtype="https://www.schema.org/SiteNavigationElement" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-904 nav-item"><a title="Home" href="<?php echo esc_url(home_url()); ?>" class="nav-link"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-house-door-fill" viewBox="0 0 16 16"><path d="M6.5 14.5v-3.505c0-.245.25-.495.5-.495h2c.25 0 .5.25.5.5v3.5a.5.5 0 0 0 .5.5h4a.5.5 0 0 0 .5-.5v-7a.5.5 0 0 0-.146-.354L13 5.793V2.5a.5.5 0 0 0-.5-.5h-1a.5.5 0 0 0-.5.5v1.293L8.354 1.146a.5.5 0 0 0-.708 0l-6 6A.5.5 0 0 0 1.5 7.5v7a.5.5 0 0 0 .5.5h4a.5.5 0 0 0 .5-.5"/></svg> <?php echo esc_html('Home','digital_'); ?></a></li>				
			</ul>
			<?php
			}
		}
	}

function digital_home_link_to_menu($items, $args) {
    // Get the customizer setting value
    $show_home_link = get_theme_mod('digital_homeicon', true);

    // Only proceed if the option is enabled
    if ($show_home_link && $args->theme_location == 'primary') {
        // Determine if the current page is the homepage
        $current_class = is_front_page() ? ' current-menu-item' : '';
        
        // Create the Home link with the desired classes and attributes
        $homeMenuItem = '<li itemscope="itemscope" itemtype="https://www.schema.org/SiteNavigationElement" class="menu-item menu-item-type-post_type menu-item-object-page nav-item' . $current_class . '"><a href="' . esc_url(home_url('/')) . '" class="nav-link"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="19" fill="currentColor" class="bi bi-house-door-fill" viewBox="0 0 16 18"><path d="M6.5 14.5v-3.505c0-.245.25-.495.5-.495h2c.25 0 .5.25.5.5v3.5a.5.5 0 0 0 .5.5h4a.5.5 0 0 0 .5-.5v-7a.5.5 0 0 0-.146-.354L13 5.793V2.5a.5.5 0 0 0-.5-.5h-1a.5.5 0 0 0-.5.5v1.293L8.354 1.146a.5.5 0 0 0-.708 0l-6 6A.5.5 0 0 0 1.5 7.5v7a.5.5 0 0 0 .5.5h4a.5.5 0 0 0 .5-.5"></path></svg></a></li>';
        
        // Add the Home link to the beginning of the menu
        $items = $homeMenuItem . $items;
    }
    return $items;
}
add_filter('wp_nav_menu_items', 'digital_home_link_to_menu', 10, 2);


function digital_modify_tag_cloud($output) {
    $style_class = get_theme_mod('digital_tagscloud_style', 'sadasd');
    if ($style_class) {
        $output = str_replace('wp-block-tag-cloud', 'wp-block-tag-cloud ' . esc_attr($style_class), $output);
    }
    return $output;
}
add_filter('widget_tag_cloud_args', 'digital_modify_tag_cloud');
