<?php
/**
 * Wood House Construction functions and definitions
 *
 * @subpackage Wood House Construction
 * @since 1.0
 */

include get_theme_file_path( 'vendor/wptrt/autoload/src/Luzuk_Wood_House_Construction_Loader.php' );


$Luzuk_Wood_House_Construction_Loader = new \WPTRT\Autoload\Luzuk_Wood_House_Construction_Loader();

$Luzuk_Wood_House_Construction_Loader->luzuk_wood_house_construction_add( 'WPTRT\\Customize\\Section', get_theme_file_path( 'vendor/wptrt/customize-section-button/src' ) );

$Luzuk_Wood_House_Construction_Loader->luzuk_wood_house_construction_register();

function luzuk_wood_house_construction_setup() {
	
	load_theme_textdomain( 'wood-house-construction', get_template_directory() . '/languages' );
	add_theme_support( 'automatic-feed-links' );
	add_theme_support( 'title-tag' );
	add_theme_support( 'responsive-embeds' );
	add_theme_support( 'html5', array( 'comment-list', 'search-form', 'comment-form', ) );
	add_theme_support( 'woocommerce' );
	add_theme_support( 'post-thumbnails' );
	add_theme_support( 'custom-background', $defaults = array(
	    'default-color'          => '',
	    'default-image'          => '',
	    'default-repeat'         => '',
	    'default-position-x'     => '',
	    'default-attachment'     => '',
	    'wp-head-callback'       => '_custom_background_cb',
	    'admin-head-callback'    => '',
	    'admin-preview-callback' => ''
	));

	$GLOBALS['content_width'] = 525;
	register_nav_menus( array(
		'primary' => esc_html__( 'Primary Menu', 'wood-house-construction' ),
	) );

	// Add theme support for Custom Logo.
	add_theme_support( 'custom-logo', array(
		'width'       => 250,
		'height'      => 250,
		'flex-width'  => true,
	) );

	// Add theme support for selective refresh for widgets.
	add_theme_support( 'customize-selective-refresh-widgets' );

	/*
	 * This theme styles the visual editor to resemble the theme style,
	 * specifically font, colors, and column width.
 	 */
	add_editor_style( array( 'assets/css/editor-style.css' ) );

}
add_action( 'after_setup_theme', 'luzuk_wood_house_construction_setup' );

function luzuk_wood_house_construction_widgets_init() {
	register_sidebar( array(
		'name'          => esc_html__( 'Blog Sidebar', 'wood-house-construction' ),
		'id'            => 'sidebar-1',
		'description'   => esc_html__( 'Add widgets here to appear in your sidebar on blog posts and archive pages.', 'wood-house-construction' ),
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<div class="widget_container"><h2 class="widget-title">',
		'after_title'   => '</h2></div>',
	) );

	register_sidebar( array(
		'name'          => esc_html__( 'Sidebar 2', 'wood-house-construction' ),
		'id'            => 'sidebar-2',
		'description'   => esc_html__( 'Add widgets here to appear in your pages and posts', 'wood-house-construction' ),
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<div class="widget_container"><h2 class="widget-title">',
		'after_title'   => '</h2></div>',
	) );

	register_sidebar( array(
		'name'          => esc_html__( 'Sidebar 3', 'wood-house-construction' ),
		'id'            => 'sidebar-3',
		'description'   => esc_html__( 'Add widgets here to appear in your pages and posts', 'wood-house-construction' ),
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<div class="widget_container"><h2 class="widget-title">',
		'after_title'   => '</h2></div>',
	) );

	register_sidebar( array(
		'name'          => esc_html__( 'Footer 1', 'wood-house-construction' ),
		'id'            => 'footer-1',
		'description'   => esc_html__( 'Add widgets here to appear in your footer.', 'wood-house-construction' ),
		'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 2', 'wood-house-construction' ),
		'id'            => 'footer-2',
		'description'   => esc_html__( 'Add widgets here to appear in your footer.', 'wood-house-construction' ),
		'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 3', 'wood-house-construction' ),
		'id'            => 'footer-3',
		'description'   => esc_html__( 'Add widgets here to appear in your footer.', 'wood-house-construction' ),
		'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 4', 'wood-house-construction' ),
		'id'            => 'footer-4',
		'description'   => esc_html__( 'Add widgets here to appear in your footer.', 'wood-house-construction' ),
		'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', 'luzuk_wood_house_construction_widgets_init' );

function luzuk_wood_house_construction_fonts_url() {
    $font_url = '';
    $font_family = array();
    
    $font_family[] = 'Poppins:100,100i,200,200i,300,300i,400,400i,500,500i,600,600i,700,700i,800,800i,900,900i';
    $font_family[] = 'Space+Grotesk:100,100i,200,200i,300,300i,400,400i,500,500i,600,600i,700,700i,800,800i,900,900i';
    $font_family[] = 'Playfair+Display:100,100i,200,200i,300,300i,400,400i,500,500i,600,600i,700,700i,800,800i,900,900i';
    $font_family[] = 'Outfit:100,200,300,400,500,600,700,800,900'; // Added Outfit with all weights
    $font_family[] = 'Mulish:100,200,300,400,500,600,700,800,900'; // Added Mulish with all weights

    $query_args = array(
        'family' => rawurlencode(implode('|', $font_family)),
    );

    $font_url = add_query_arg($query_args, '//fonts.googleapis.com/css');
    return $font_url;
}

function luzuk_wood_house_construction_enqueue_google_fonts() {
    wp_enqueue_style('google-fonts', luzuk_wood_house_construction_fonts_url());
}
add_action('wp_enqueue_scripts', 'luzuk_wood_house_construction_enqueue_google_fonts');



//Enqueue scripts and styles.
function luzuk_wood_house_construction_scripts() {

	// Add custom fonts, used in the main stylesheet.
	wp_enqueue_style( 'wood-house-construction-fonts', luzuk_wood_house_construction_fonts_url(), array(), null );
	
	//Bootstarp 
	wp_enqueue_style( 'bootstrap-css', get_template_directory_uri().'/assets/css/bootstrap.css' );
	
	// Theme stylesheet.
	wp_enqueue_style( 'wood-house-construction-basic-style', get_stylesheet_uri() );


	//font-awesome
	wp_enqueue_style( 'font-awesome', get_template_directory_uri().'/assets/css/font-awesome.css' );
	wp_enqueue_style( 'font-awesome-css', get_template_directory_uri().'/assets/css/fontawesome-all.css' );

	wp_enqueue_style('wood-house-construction-animations', get_template_directory_uri() . '/assets/css/animations.css');


	require get_parent_theme_file_path( '/lz-custom-style.php' );
	wp_add_inline_style( 'wood-house-construction-basic-style',$luzuk_wood_house_construction_custom_style );


	wp_enqueue_script( 'wood-house-construction-script', get_theme_file_uri( '/assets/js/customscript.js' ), array( 'jquery' ) );

	wp_enqueue_script( 'wood-house-construction-index', get_theme_file_uri( '/assets/js/index.js' ), array( 'jquery' ), '2.1.2', true );

	wp_enqueue_script( 'wood-house-construction-navigation-jquery', get_theme_file_uri( '/assets/js/navigation.js' ), array( 'jquery' ), '2.1.2', true );
	wp_enqueue_script( 'bootstrap-js', get_template_directory_uri(). '/assets/js/bootstrap.js', array('jquery') );
	wp_enqueue_script( 'jquery-superfish', get_template_directory_uri(). '/assets/js/jquery.superfish.js', array('jquery') ,'',true);


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

function luzuk_wood_house_construction_front_page_template( $template ) {
	return is_home() ? '' : $template;
}
add_filter( 'frontpage_template',  'luzuk_wood_house_construction_front_page_template' );

function luzuk_wood_house_construction_sanitize_dropdown_pages( $page_id, $setting ) {
	// Ensure $input is an absolute integer.
	$page_id = absint( $page_id );
	// If $page_id is an ID of a published page, return it; otherwise, return the default.
	return ( 'publish' == get_post_status( $page_id ) ? $page_id : $setting->default );
}

function luzuk_wood_house_construction_sanitize_choices( $input, $setting ) {
    global $wp_customize; 
    $control = $wp_customize->get_control( $setting->id ); 
    if ( array_key_exists( $input, $control->choices ) ) {
        return $input;
    } else {
        return $setting->default;
    }
}

function luzuk_wood_house_construction_sanitize_checkbox( $input ) {
	return ( ( isset( $input ) && true == $input ) ? true : false );
}

function luzuk_wood_house_construction_sanitize_phone_number( $phone ) {
	return preg_replace( '/[^\d+]/', '', $phone );
}

function luzuk_wood_house_construction_sanitize_float( $input ) {
    return filter_var($input, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
}

/* Excerpt Limit Begin */
function luzuk_wood_house_construction_string_limit_words($string, $word_limit) {
	$words = explode(' ', $string, ($word_limit + 1));
	if(count($words) > $word_limit)
	array_pop($words);
	return implode(' ', $words);
}

// Change number or products per row to 3
add_filter('loop_shop_columns', 'luzuk_wood_house_construction_loop_columns');
if (!function_exists('luzuk_wood_house_construction_loop_columns')) {
	function luzuk_wood_house_construction_loop_columns() {
		return 3; // 3 products per row
	}
}

/* Breadcrumb Begin */
function luzuk_wood_house_construction_breadcrumb() {
	if (!is_home()) {
		echo '<a href="';
			echo esc_url(home_url());
		echo '">';
			bloginfo('name');
		echo "</a>";
		if (is_category() || is_single()) {
			the_category(', ');
			if (is_single()) {
				echo "<span> ";
					the_title();
				echo "</span>";
			}
		} elseif (is_page()) {
			echo "<span>";
				the_title();
			echo "</span> ";
		}
	}
}


function luzuk_wood_house_construction_banner_image( $image_url ){
    global $post;

    if( is_singular() ){
        $image_url      = get_the_post_thumbnail_url( $post->ID, 'full' );
    }
    return $image_url;
}


/**
 * WooCommerce Safe Filters (Professional Setup)
 */

if ( class_exists( 'WooCommerce' ) ) {

    /**
     * SHOP FILTERS (Price)
     */
    function luzuk_wood_house_construction_whc_shop_filters( $query ) {

        if ( is_admin() || ! $query->is_main_query() || ! is_shop() ) {
            return;
        }

        $meta_query = (array) $query->get( 'meta_query' );

        // Price Filter
        if ( ! empty( $_GET['whc_budget'] ) ) {

            $budget = sanitize_text_field( $_GET['whc_budget'] );

            switch ( $budget ) {

                case '10-25':
                    $meta_query[] = array(
                        'key'     => '_price',
                        'value'   => array( 1000000, 2500000 ),
                        'compare' => 'BETWEEN',
                        'type'    => 'NUMERIC',
                    );
                    break;

                case '25-50':
                    $meta_query[] = array(
                        'key'     => '_price',
                        'value'   => array( 2500000, 5000000 ),
                        'compare' => 'BETWEEN',
                        'type'    => 'NUMERIC',
                    );
                    break;

                case '50+':
                    $meta_query[] = array(
                        'key'     => '_price',
                        'value'   => 5000000,
                        'compare' => '>=',
                        'type'    => 'NUMERIC',
                    );
                    break;
            }
        }

        $query->set( 'meta_query', $meta_query );
    }
    add_action( 'pre_get_posts', 'luzuk_wood_house_construction_whc_shop_filters' );


    /**
     * LOCATION FILTER (Title + Content + Attribute)
     */
    function luzuk_wood_house_construction_whc_location_or_filter( $where, $query ) {
        global $wpdb;

        if ( is_admin() || ! $query->is_main_query() || ! is_shop() ) {
            return $where;
        }

        if ( empty( $_GET['whc_location'] ) ) {
            return $where;
        }

        $location = sanitize_text_field( $_GET['whc_location'] );
        $like     = '%' . $wpdb->esc_like( $location ) . '%';

        // Secure prepared query (IMPORTANT)
        $where .= $wpdb->prepare(
            " AND (
                {$wpdb->posts}.post_title LIKE %s
                OR {$wpdb->posts}.post_content LIKE %s
                OR {$wpdb->posts}.ID IN (
                    SELECT tr.object_id
                    FROM {$wpdb->term_relationships} tr
                    INNER JOIN {$wpdb->term_taxonomy} tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
                    INNER JOIN {$wpdb->terms} t ON tt.term_id = t.term_id
                    WHERE tt.taxonomy = 'pa_location'
                    AND t.name LIKE %s
                )
            )",
            $like,
            $like,
            $like
        );

        return $where;
    }
    add_filter( 'posts_where', 'luzuk_wood_house_construction_whc_location_or_filter', 10, 2 );

}

require get_parent_theme_file_path( '/inc/custom-header.php' );

require get_parent_theme_file_path( '/inc/template-tags.php' );

require get_parent_theme_file_path( '/inc/template-functions.php' );

require get_parent_theme_file_path( '/inc/customizer.php' );

require get_parent_theme_file_path( '/inc/font-awesome-icons.php' );

require get_parent_theme_file_path( '/inc/wptt-webfont-loader.php' );

// Custom page walker.
require get_template_directory() . '/classes/class-Luzuk_Wood_House_Construction-walker-page.php';

