<?php
/**
 * Magazine Style functions and definitions
 *
 * @package Magazine Style
 */

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
define( 'MAGAZINESTYLE_VERSION', wp_get_theme()->get( 'Version' ) );
// Magazine Style's includes directory.
$magazinestyle_inc_dir = 'inc';
define( 'MAGAZINESTYLE_THEME_DIR', get_template_directory() );
define( 'MAGAZINESTYLE_THEME_URI', get_template_directory_uri() );
// Array of files to include.
$magazinestyle_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.
	//'/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 magazinestyle_styles_admin($hook) {
	if ( 'appearance_page_magazinestyle-info' != $hook ) {
        return;
    }
	wp_enqueue_style('magazinestyle-admin-styles',get_theme_file_uri( 'inc/core/bootstrap/css/admin-style.css' ),[], MAGAZINESTYLE_VERSION );
	wp_enqueue_style('magazinestyle-bootstrap-responsive',get_theme_file_uri( 'inc/core/bootstrap/css/bootstrap-responsive.css' ),[], MAGAZINESTYLE_VERSION );
}
add_action( 'admin_enqueue_scripts', 'magazinestyle_styles_admin' );

function magazinestyle_add_menu_page() {
	add_theme_page( 
		esc_html__( 'Magazine Style', 'magazine-style' ), 
		esc_html__( 'Magazine Style', 'magazine-style' ), 
		'edit_theme_options', 'magazinestyle-info', 'magazinestyle_theme_page_display','2' );
}
add_action( 'admin_menu', 'magazinestyle_add_menu_page' );
function magazinestyle_theme_page_display() {
	$theme = wp_get_theme();
	include_once 'inc/theme-info.php';
}
final class MAGAZINESTYLE_Theme_Class {
	public static function magazinestyle_constants() {
		
		$version = self::theme_version();

		// Theme version.
		define( 'MAGAZINESTYLE_THEME_VERSION', $version );
		
		define( 'MAGAZINESTYLE_JS_DIR_URI', MAGAZINESTYLE_THEME_URI . '/js/' );
		define( 'MAGAZINESTYLE_CSS_DIR_URI', MAGAZINESTYLE_THEME_URI . '/css/' );

		// Include Paths.
		define( 'MAGAZINESTYLE_INC_DIR', MAGAZINESTYLE_THEME_DIR . '/inc/' );
		define( 'MAGAZINESTYLE_INC_DIR_URI', MAGAZINESTYLE_THEME_URI . '/inc/' );
		
	}
}
new MAGAZINESTYLE_Theme_Class();
// Load WooCommerce functions if WooCommerce is activated.
if ( class_exists( 'WooCommerce' ) ) {
	$magazinestyle_includes[] = '/woocommerce.php';
}

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

add_action( 'wp_footer', 'magazinestyle_fetch_ajax', 99 );
function magazinestyle_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' , 'magazinestyle_data_fetch');
add_action('wp_ajax_nopriv_data_fetch','magazinestyle_data_fetch');
function magazinestyle_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="magazinestyle-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', 'magazinestyle_block_template_part_support' );
function magazinestyle_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( 'magazinestyle_primary_menu_fallback' ) ) {
		function magazinestyle_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','magazinestyle_'); ?></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','magazinestyle_'); ?></a></li>
		</ul>
		  <?php
		}
	}
}else { if ( ! function_exists( 'magazinestyle_primary_menu_fallback' ) ) {
	function magazinestyle_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','magazinestyle_'); ?></a></li>				
	</ul>
	<?php
	}
}
}


function magazinestyle_home_link_to_menu($items, $args) {
    // Get the customizer setting value
    $show_home_link = get_theme_mod('magazinestyle_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', 'magazinestyle_home_link_to_menu', 10, 2);


function magazinestyle_modify_tag_cloud($output) {
    $style_class = get_theme_mod('magazinestyle_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', 'magazinestyle_modify_tag_cloud');
