<?php

/**
 * hardware-dealer-pro functions and definitions
 *
 * @package hardware-dealer-pro
 */

define('GET_PREMIUM_THEME', 'hardware-dealer-pro');

if (!function_exists('hardware_dealer_pro_setup')):

	/**
	 * Sets up theme defaults and registers support for various WordPress features.
	 *
	 * Note that this function is hooked into the after_setup_theme hook, which runs
	 * before the init hook. The init hook is too late for some features, such as indicating
	 * support post thumbnails.
	 */
	if (version_compare(phpversion(), '7.4', '<')) {
		die('Your server does not support PHP 7.4 or higher. Please upgrade your PHP version.');
	}
	function hardware_dealer_pro_setup()
	{
		$GLOBALS['content_width'] = apply_filters('hardware_dealer_pro_content_width', 640);
		if (!isset($content_width))
			$content_width = 640;
		load_theme_textdomain('hardware-dealer-pro', get_template_directory() . '/languages');
		add_theme_support('automatic-feed-links');
		add_theme_support('post-thumbnails');
		add_theme_support('woocommerce');
		add_theme_support('custom-header');
		add_theme_support('title-tag');
		add_theme_support('wc-product-gallery-zoom');
		add_theme_support('wc-product-gallery-lightbox');
		add_theme_support('wc-product-gallery-slider');

		add_theme_support('custom-logo', array(
			'height' => 240,
			'width' => 240,
			'flex-height' => true,
		));
		add_image_size('hardware-dealer-pro-homepage-thumb', 240, 145, true);
		register_nav_menus(
			array(
				'primary-menu' => __('Primary Menu', 'hardware-dealer-pro')
			));

		add_editor_style(array('assets/css/editor-style.css'));
	}
endif;
add_action('after_setup_theme', 'hardware_dealer_pro_setup');

/* Theme enqueue scripts */
function hardware_dealer_pro_scripts()
{

	if (!is_plugin_active('elementor/elementor.php')) {
		wp_enqueue_style('bootstrap-style', get_template_directory_uri() . '/assets/css/bootstrap.min.css');
	}
	//  if ( is_single() || is_shop() || is_checkout() || is_cart() || is_product_category() || is_search() || is_category() || is_archive()) {
	// 	 wp_enqueue_style( 'bootstrap-style', get_template_directory_uri().'/assets/css/bootstrap.min.css' );
	// }
	wp_enqueue_style('bootstrap-style', get_template_directory_uri() . '/assets/css/bootstrap.min.css');
	wp_enqueue_style('font-awesome', get_template_directory_uri() . '/assets/css/fontawesome-all.min.css');
	wp_enqueue_style('hardware-dealer-pro-basic-style', get_stylesheet_uri());
	wp_enqueue_style('animate-min-css', get_template_directory_uri() . '/assets/css/animate.min.css');
	wp_enqueue_style('owl-carousel-css', get_template_directory_uri() . '/assets/css/owl.carousel.css');
	wp_enqueue_style('slick--css', get_template_directory_uri() . '/assets/css/slick.css');
	wp_enqueue_style( 'jquery-ui-slider-css', get_template_directory_uri().'/assets/css/jquery-ui.min.css' );
	wp_style_add_data('hardware-dealer-pro-style', 'rtl', 'replace');

	if (is_home()) {
		wp_enqueue_style('homepage-animation-css', get_template_directory_uri() . '/assets/css/homepage-animation.css');
	} else {
		wp_enqueue_style('other-page-css', get_template_directory_uri() . '/assets/css/other-page.css');
	}
	wp_enqueue_style('responsive-media-css', get_template_directory_uri() . '/assets/css/responsive-media.css');
	wp_enqueue_style('header-footer-css', get_template_directory_uri() . '/assets/css/header-footer.css');

	wp_enqueue_script('bootstrapjs', get_template_directory_uri() . '/assets/js/bootstrap.min.js', array('jquery'), true);
	wp_enqueue_script('jquery', get_template_directory_uri() . '/assets/js/jquery.min.js', array('jquery'), true);
	wp_enqueue_script('typed-js', get_template_directory_uri() . '/assets/js/typed.min.js', array('jquery'), true);
	wp_enqueue_script('waypoint-js', get_template_directory_uri() . '/assets/js/noframework.waypoints.js', array('jquery'), true);
	wp_enqueue_script('jquery-waypoints-min', get_template_directory_uri() . '/assets/js/jquery.waypoints.min.js', array('jquery'), true);
	// wp_enqueue_script('hardware-dealer-pro-customscripts', get_template_directory_uri() . '/assets/js/custom.js', array('jquery'), '', true);
	wp_enqueue_script('font-awesome-js', get_template_directory_uri() . '/assets/js/fontawesome-all.min.js', array('jquery'), '', true);
	wp_enqueue_script('owl-carousel-js', get_template_directory_uri() . '/assets/js/owl.carousel.js', array('jquery'), true);
	wp_enqueue_script('slick--min-js', get_template_directory_uri() . '/assets/js/slick.min.js', array('jquery'), true);
	wp_enqueue_script('hardware-dealer-pro-admin-plugin-install-js', get_template_directory_uri() . '/assets/js/admin-plugin-install.js', false, '1.0.0');
	wp_enqueue_script('hardware-dealer-pro-scrollscripts', get_template_directory_uri() . '/assets/js/scroll.js', array('jquery'), '', true);

	
	wp_enqueue_script( 'jquery-ui-slider' );

	     global $wpdb;
	    $product_price_max_query = "SELECT MAX( CAST( $wpdb->postmeta.meta_value AS SIGNED ) ) AS product_max_price FROM $wpdb->postmeta WHERE meta_key='%s'";
	    $product_meta_price_max = $wpdb->get_row( $wpdb->prepare( $product_price_max_query, '_price' ) );

		// wp_register_script( 'wp-customscripts', get_template_directory_uri() . '/assets/js/custom.js', array('jquery'),true);
		wp_enqueue_script('wp-customscripts', get_template_directory_uri() . '/assets/js/custom.js', array('jquery'), '', true);
	    $wp_customscripts_object = array(
	      'root'					=>	rest_url(),
	      'ajaxurl'				=>	admin_url('admin-ajax.php'),
	      'nonce'					=>	wp_create_nonce( 'wp_rest' ),
	      'the_rest_url'	=>	get_rest_url(),
	      'product_max_price'  =>  $product_meta_price_max->product_max_price
	    );
	    if( class_exists( 'WooCommerce' ) ) {
	      $wp_customscripts_object['get_woocommerce_currency_symbol'] = get_woocommerce_currency_symbol();
	    }

	  	wp_localize_script( 'wp-customscripts', 'wp_customscripts_object', $wp_customscripts_object );

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

add_action('wp_head', function() {
    ?>
    <div class="spinner-loading-box">
        <div class="demo">
            <div class="loader">
                <img src="<?php echo get_template_directory_uri() . '/assets/images/loader.gif'; ?>" alt="Loading...">
            </div>
        </div>
    </div>
    <?php
});

// whizzie file


require get_theme_file_path('/whizzie/config.php');

require get_theme_file_path('/inc/widget-sidebar.php');
require get_parent_theme_file_path('/inc/customizer.php');
require get_theme_file_path( '/template-parts/product-shortcode.php' );
require get_parent_theme_file_path('custom-filter.php' );



// Breadcrumb
function hardware_dealer_pro_the_breadcrumb()
{
	if (!is_home()) {
		echo '<a href="';
		echo esc_url(home_url());
		echo '">';
		echo ' Home ';
		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 hardware_dealer_pro_string_limit_words($string, $word_limit)
{
	$words = explode(' ', $string, ($word_limit + 1));
	if (count($words) > $word_limit)
		array_pop($words);
	return implode(' ', $words);
}

/*
 * Show post Share
 */
if (!function_exists('hardware_dealer_pro_social_share')) {
	/**
	 * [hardware_dealer_pro_social_share show all the social share buttons
	 * @return [type] string
	 */
	function hardware_dealer_pro_social_share()
	{
		?>
		<?php do_action('hardware_dealer_pro_before_blog_sharing'); ?>
		<div class="share_icon row p-0 m-0 text-center">
			<p class="socila_share col-md-12 p-0 mb-0">
				<a href="https://www.facebook.com/sharer/sharer.php?u=<?php echo the_permalink(); ?>" target="_blank"
					class="me-2 hover-fx"><i class="fab fa-facebook-f"></i><span
						class="screen-reader-text"><?php echo esc_html('facebook.com', 'hardware-dealer-pro'); ?></span></a>
				<a href="https://twitter.com/share?url=<?php the_permalink(); ?>&amp;text=<?php echo the_title(); ?>"
					target="_blank" class="me-2 hover-fx"><i class="fab fa-twitter"></i><span
						class="screen-reader-text"><?php echo esc_html('twitter.com', 'hardware-dealer-pro'); ?></span></a>
				<a href="https://linkedin.com/share?url=<?php the_permalink(); ?>&amp;text=<?php echo the_title(); ?>"
					target="_blank" class="me-2 hover-fx"><i class="fab fa-linkedin-in"></i><span
						class="screen-reader-text"><?php echo esc_html('linkedin.com', 'hardware-dealer-pro'); ?></span></a>
			</p>
		</div>
		<?php
	}
}



// Remove the reviews and ratings after the product title
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_rating', 10 );

// Remove the short description from its default location
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );

// Add the short description after the product title
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 6 );

function add_custom_text_before_price( $price, $product ) {
    // Define your custom text
    $custom_text = 'Price ';
    
    // Prepend the custom text to the price HTML
    $price = $custom_text . $price;
    
    return $price;
}
add_filter( 'woocommerce_get_price_html', 'add_custom_text_before_price', 10, 2 );


// Add a custom tab for simple products
function add_custom_tab_for_simple_products( $tabs ) {
    global $product;

    if ( $product->is_type( 'simple' ) ) {
        $tabs['additional_information'] = array(
            'title'    => __( 'Additional Information', 'your-textdomain' ),
            'priority' => 15,
            'callback' => 'custom_additional_information_tab_content'
        );
    }

    return $tabs;
}
add_filter( 'woocommerce_product_tabs', 'add_custom_tab_for_simple_products' );

// Callback function to display content in the custom tab
function custom_additional_information_tab_content() {
    echo '<h2>' . __( 'Additional Information', 'your-textdomain' ) . '</h2>';
    echo '<p>' . __( 'Here is some additional information about this product.', 'your-textdomain' ) . '</p>';
}

//Remove Single product Breadcrumb
remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0 );

// Remove product meta from single product page
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40);


// Change number of related products per row to 5
add_filter('woocommerce_output_related_products_args', 'wc_related_products_args', 20);
function wc_related_products_args($args) {
    $args['posts_per_page'] = -1; // Number of related products
    $args['columns'] = 5; // Number of columns per row
    return $args;
}

// Change number of products per page in the shop
add_filter('loop_shop_per_page', 'wc_change_products_per_page', 20);
function wc_change_products_per_page($products_per_page) {
    return 12; // Number of products per page
}

// Change number of products per row to 5 on shop page
add_filter('loop_shop_columns', 'wc_change_products_per_row', 999);
function wc_change_products_per_row() {
    return 4; // 5 products per row
}

function change_product_tab_name($tabs) {
    // Check if the description tab is set
    if (isset($tabs['description'])) {
        // Change the name of the description tab
        $tabs['description']['title'] = __('Product Description', 'hardware-dealer-pro'); // 'your-text-domain' should be replaced with your theme's text domain
    }
    return $tabs;
}
add_filter('woocommerce_product_tabs', 'change_product_tab_name');
















