<?php
/**
 * storecraft functions and definitions
 *
 * @subpackage storecraft
 * @since 1.0
 */

function storecraft_setup() {
	
	load_theme_textdomain( 'storecraft', 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', $storecraft_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'         => __( 'Primary Menu', 'storecraft' ),
		'footer-shop'     => __( 'Footer Shop Menu', 'storecraft' ),
		'footer-service'  => __( 'Footer Customer Service Menu', 'storecraft' ),
		'footer-about'    => __( 'Footer About Menu', 'storecraft' ),
	) );

	// 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', 'storecraft_setup' );

function storecraft_widgets_init() {
	register_sidebar( array(
		'name'          => __( 'Blog Sidebar', 'storecraft' ),
		'id'            => 'sidebar-1',
		'description'   => __( 'Add widgets here to appear in your sidebar on blog posts and archive pages.', 'storecraft' ),
		'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'          => __( 'Sidebar 2', 'storecraft' ),
		'id'            => 'sidebar-2',
		'description'   => __( 'Add widgets here to appear in your pages and posts', 'storecraft' ),
		'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'          => __( 'Sidebar 3', 'storecraft' ),
		'id'            => 'sidebar-3',
		'description'   => __( 'Add widgets here to appear in your pages and posts', 'storecraft' ),
		'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'          => __( 'Footer 1', 'storecraft' ),
		'id'            => 'footer-1',
		'description'   => __( 'Add widgets here to appear in your footer.', 'storecraft' ),
		'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'          => __( 'Footer 2', 'storecraft' ),
		'id'            => 'footer-2',
		'description'   => __( 'Add widgets here to appear in your footer.', 'storecraft' ),
		'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'          => __( 'Footer 3', 'storecraft' ),
		'id'            => 'footer-3',
		'description'   => __( 'Add widgets here to appear in your footer.', 'storecraft' ),
		'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'          => __( 'Footer 4', 'storecraft' ),
		'id'            => 'footer-4',
		'description'   => __( 'Add widgets here to appear in your footer.', 'storecraft' ),
		'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', 'storecraft_widgets_init' );

function storecraft_fonts_url() {
	$storecraft_font_url = 'https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&family=Jost:wght@400;500;700&display=swap';

	if ( ! function_exists( 'wptt_get_webfont_url' ) ) {
		require_once get_parent_theme_file_path( '/inc/wptt-webfont-loader.php' );
	}

	return wptt_get_webfont_url( $storecraft_font_url );
}

//Enqueue scripts and styles.
function storecraft_scripts() {
	// Add custom fonts, used in the main stylesheet. Loaded locally via WPTT.
	wp_enqueue_style( 'storecraft-fonts', storecraft_fonts_url(), array(), null );
	
	//Bootstarp 
	wp_enqueue_style( 'bootstrap-css', get_template_directory_uri().'/assets/css/bootstrap.css' );
	
	// Theme stylesheet.
	wp_enqueue_style( 'storecraft-basic-style', get_stylesheet_uri() );

	//font-awesome
	wp_enqueue_style( 'font-awesome-css', get_template_directory_uri().'/assets/css/fontawesome-all.css' );
	wp_enqueue_style( 'storecraft-animations', get_template_directory_uri() . '/assets/css/animations.css' );
	require get_parent_theme_file_path( '/huda-custom-style.php' );
	wp_add_inline_style( 'storecraft-basic-style',$storecraft_custom_style );

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

	wp_enqueue_script( 'storecraft-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);
	wp_enqueue_script( 'storecraft-header-search', get_theme_file_uri( '/assets/js/header-search.js' ), array( 'jquery' ), wp_get_theme()->get( 'Version' ), true );

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

	if ( storecraft_uses_minimal_footer() ) {
		wp_enqueue_script(
			'storecraft-slider-next-preview',
			get_theme_file_uri( '/assets/js/slider-next-preview.js' ),
			array( 'jquery', 'bootstrap-js' ),
			wp_get_theme()->get( 'Version' ),
			true
		);
	}
}
add_action( 'wp_enqueue_scripts', 'storecraft_scripts' );

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

function storecraft_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 storecraft_sanitize_choices( $input, $setting ) {
	global $wp_customize;

	if ( ! isset( $wp_customize ) || ! ( $wp_customize instanceof WP_Customize_Manager ) ) {
		return $setting->default;
	}

	$control = $wp_customize->get_control( $setting->id );
	if ( ! $control || empty( $control->choices ) || ! is_array( $control->choices ) ) {
		return $setting->default;
	}

	if ( array_key_exists( $input, $control->choices ) ) {
		return $input;
	}

	return $setting->default;
}

function storecraft_sanitize_checkbox( $input ) {
	return ( ! empty( $input ) );
}

function storecraft_sanitize_url( $url ) {
	return esc_url_raw( $url );
}

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

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

/* Excerpt Limit Begin */
function storecraft_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', 'storecraft_loop_columns');
if (!function_exists('storecraft_loop_columns')) {
	function storecraft_loop_columns() {
		return 3; // 3 products per row
	}
}

/* Breadcrumb Begin */
function storecraft_breadcrumb() {
	if ( is_front_page() ) {
		return;
	}

	echo '<a href="' . esc_url( home_url( '/' ) ) . '">' . esc_html( get_bloginfo( 'name' ) ) . '</a>';

	if ( function_exists( 'is_shop' ) && ( is_shop() || is_product_taxonomy() || is_product() ) ) {
		$shop_page_id = wc_get_page_id( 'shop' );
		if ( $shop_page_id > 0 ) {
			echo '<span class="hbs-breadcrumb-sep"> / </span><a href="' . esc_url( get_permalink( $shop_page_id ) ) . '">' . esc_html( get_the_title( $shop_page_id ) ) . '</a>';
		}

		if ( is_product() ) {
			$terms = get_the_terms( get_the_ID(), 'product_cat' );
			if ( $terms && ! is_wp_error( $terms ) ) {
				$term = $terms[0];
				echo '<span class="hbs-breadcrumb-sep"> / </span><a href="' . esc_url( get_term_link( $term ) ) . '">' . esc_html( $term->name ) . '</a>';
			}
			echo '<span class="hbs-breadcrumb-sep"> / </span><span>' . esc_html( get_the_title() ) . '</span>';
		}

		return;
	}

	if ( is_category() || is_single() ) {
		the_category( ', ' );
		if ( is_single() ) {
			echo '<span class="hbs-breadcrumb-sep"> / </span><span> ' . esc_html( get_the_title() ) . '</span>';
		}
	} elseif ( is_page() ) {
		echo '<span class="hbs-breadcrumb-sep"> / </span><span>' . esc_html( get_the_title() ) . '</span>';
	} elseif ( is_archive() ) {
		echo '<span class="hbs-breadcrumb-sep"> / </span><span>' . esc_html( wp_strip_all_tags( get_the_archive_title() ) ) . '</span>';
	} elseif ( is_search() ) {
		echo '<span class="hbs-breadcrumb-sep"> / </span><span>' . esc_html__( 'Search', 'storecraft' ) . '</span>';
	}
}


function storecraft_banner_image( $storecraft_image_url ){
    global $post;

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



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/woocommerce-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-storecraft-walker-page.php';
require get_template_directory() . '/classes/class-storecraft-walker-sidebar-menu.php';

require get_parent_theme_file_path( '/get-started/notice.php' );
