<?php
/**
 * Handmade Craft functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package handmade_craft
 */

if ( ! defined( 'HANDMADE_CRAFT_VERSION' ) ) {
	define( 'HANDMADE_CRAFT_VERSION', '1.0' );
}

$handmade_craft_theme_data = wp_get_theme();

if( ! defined( 'HANDMADE_CRAFT_THEME_VERSION' ) ) define ( 'HANDMADE_CRAFT_THEME_VERSION', $handmade_craft_theme_data->get( 'Version' ) );
if( ! defined( 'HANDMADE_CRAFT_THEME_NAME' ) ) define( 'HANDMADE_CRAFT_THEME_NAME', $handmade_craft_theme_data->get( 'Name' ) );

if ( ! function_exists( 'handmade_craft_setup' ) ) :
	
	function handmade_craft_setup() {
		
		load_theme_textdomain( 'handmade-craft', get_template_directory() . '/languages' );

		add_theme_support( 'woocommerce' );

		add_theme_support( 'automatic-feed-links' );
		
		add_theme_support( 'title-tag' );

		add_theme_support( 'post-thumbnails' );

		register_nav_menus(
			array(
				'primary' => esc_html__( 'Primary', 'handmade-craft' ),
			)
		);

		add_theme_support(
			'html5',
			array(
				'search-form',
				'comment-form',
				'comment-list',
				'gallery',
				'caption',
				'style',
				'script',
				'woocommerce',
			)
		);

		add_theme_support( 'post-formats', array(
			'image',
			'video',
			'gallery',
			'audio', 
		) );

		add_theme_support(
			'custom-background',
			apply_filters(
				'handmade_craft_custom_background_args',
				array(
					'default-color' => 'ffffff',
					'default-image' => '',
				)
			)
		);

		add_theme_support( 'customize-selective-refresh-widgets' );

		add_theme_support(
			'custom-logo',
			array(
				'height'      => 250,
				'width'       => 250,
				'flex-width'  => true,
				'flex-height' => true,
			)
		);

		add_theme_support( 'align-wide' );

		add_theme_support( 'responsive-embeds' );

		/*  Demo Import */
		require get_parent_theme_file_path( '/theme-wizard/config.php' );

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

	}
endif;
add_action( 'after_setup_theme', 'handmade_craft_setup' );

function handmade_craft_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'handmade_craft_content_width', 640 );
}
add_action( 'after_setup_theme', 'handmade_craft_content_width', 0 );

function handmade_craft_widgets_init() {
	register_sidebar(
		array(
			'name'          => esc_html__( 'Sidebar', 'handmade-craft' ),
			'id'            => 'handmade-craft-sidebar-1',
			'description'   => esc_html__( 'Add widgets here.', 'handmade-craft' ),
			'before_widget' => '<section id="%1$s" class="widget %2$s">',
			'after_widget'  => '</section>',
			'before_title'  => '<h2 class="widget-title"><span>',
			'after_title'   => '</span></h2>',
		)
	);

	// Regsiter 4 footer widgets.
	$handmade_craft_footer_widget_column = get_theme_mod('handmade_craft_footer_widget_column','4');
	for ($handmade_craft_i=1; $handmade_craft_i<=$handmade_craft_footer_widget_column; $handmade_craft_i++) {
		register_sidebar( array(
			'name' => __( 'Footer  ', 'handmade-craft' )  . $handmade_craft_i,
			'id' => 'footer-widget-' . $handmade_craft_i,
			'description' => __( 'The Footer Widget Area', 'handmade-craft' )  . $handmade_craft_i,
			'before_widget' => '<aside id="%1$s" class="widget %2$s">',
			'after_widget' => '</aside>',
			'before_title' => '<div class="widget-header"><h4 class="widget-title">',
			'after_title' => '</h4></div>',
		) );
	}
}
add_action( 'widgets_init', 'handmade_craft_widgets_init' );

/**
 * Enqueue scripts and styles.
 */
function handmade_craft_scripts() {
	// Append .min if SCRIPT_DEBUG is false.
	$min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';

	// Fontawesome style.
	wp_enqueue_style( 'font-awesome-css', esc_url(get_template_directory_uri())."/resource/css/fontawesome-all.css" );

	// Main style.
	wp_enqueue_style( 'handmade-craft-style', get_template_directory_uri() . '/style.css', array(), HANDMADE_CRAFT_VERSION );

	// RTL style.
	wp_style_add_data('handmade-craft-style', 'rtl', 'replace');

	if (get_theme_mod('handmade_craft_enable_animation', true) == true){
		// Animate CSS
		wp_enqueue_style( 'animate-style', get_template_directory_uri() . '/resource/css/animate.css' );
		// Wow script.
		wp_enqueue_script( 'wow-jquery', get_template_directory_uri() . '/resource/js/wow.js', array('jquery'),'' ,true );
	}

	wp_enqueue_style( 'owl.carousel-css', esc_url(get_template_directory_uri())."/resource/css/owl.carousel.css" );

	// Navigation script.
	wp_enqueue_script( 'handmade-craft-navigation-script', get_template_directory_uri() . '/resource/js/navigation.js', array(), HANDMADE_CRAFT_VERSION, true );

	// Custom script.
	wp_enqueue_script( 'handmade-craft-custom-script', get_template_directory_uri() . '/resource/js/custom.js', array( 'jquery' ), HANDMADE_CRAFT_VERSION, true );

	wp_enqueue_script( 'owl.carousel-js', esc_url(get_template_directory_uri()). '/resource/js/owl.carousel.js', array('jquery') );

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

	// Include the file.
	require_once get_theme_file_path( 'theme-library/function-files/wptt-webfont-loader.php' );

	wp_enqueue_style(
		'Marcellus',
		Handmade_Craft_wptt_get_webfont_url( 'https://fonts.googleapis.com/css2?family=Marcellus&display=swap' ),
		array(),
		'1.0'
	);

	wp_enqueue_style(
		'Poppins',
		Handmade_Craft_wptt_get_webfont_url( 'https://fonts.googleapis.com/css2?family=Poppins:wght@100;200;300;400;500;600;700;800;900&display=swap' ),
		array(),
		'1.0'
	);

	wp_enqueue_style(
		'Jost',
		Handmade_Craft_wptt_get_webfont_url( 'https://fonts.googleapis.com/css2?family=Jost:wght@100;200;300;400;500;600;700;800;900&display=swap' ),
		array(),
		'1.0'
	);

	wp_enqueue_style(
		'Satisfy',
		Handmade_Craft_wptt_get_webfont_url( 'https://fonts.googleapis.com/css2?family=Satisfy&display=swap' ),
		array(),
		'1.0'
	);
}
add_action( 'wp_enqueue_scripts', 'handmade_craft_scripts' );

//Change number of products per page 
add_filter( 'loop_shop_per_page', 'handmade_craft_products_per_page' );
function handmade_craft_products_per_page( $cols ) {
  	return  get_theme_mod( 'handmade_craft_products_per_page',9);
}

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

/**
 * Include wptt webfont loader.
 */
require_once get_theme_file_path( 'theme-library/function-files/wptt-webfont-loader.php' );

/**
 * Implement the Custom Header feature.
 */
require get_template_directory() . '/theme-library/custom-header.php';

/**
 * Custom template tags for this theme.
 */
require get_template_directory() . '/theme-library/function-files/template-tags.php';

/**
 * Functions which enhance the theme by hooking into WordPress.
 */
require get_template_directory() . '/theme-library/function-files/template-functions.php';

/**
 * Customizer additions.
 */
require get_template_directory() . '/theme-library/customizer.php';

/**
 * Google Fonts
 */
require get_template_directory() . '/theme-library/function-files/google-fonts.php';

/**
 * Dynamic CSS
 */
require get_template_directory() . '/theme-library/dynamic-css.php';

/**
 * Breadcrumb
 */
require get_template_directory() . '/theme-library/function-files/class-breadcrumb-trail.php';

/**
 * Customizer Settings Functions
*/
require get_template_directory() . '/theme-library/function-files/customizer-settings-functions.php';

/**
 * Getting Started
*/
require get_template_directory() . '/theme-library/getting-started/getting-started.php';

function handmade_craft_links_setup() {
	if ( ! defined( 'HANDMADE_CRAFT_PREMIUM_PAGE' ) ) {
	define('HANDMADE_CRAFT_PREMIUM_PAGE',__('https://asterthemes.com/products/handmade-wordpress-theme','handmade-craft'));
	}
	if ( ! defined( 'HANDMADE_CRAFT_THEME_PAGE' ) ) {
		define('HANDMADE_CRAFT_THEME_PAGE',__('https://asterthemes.com/products/handmade-craft','handmade-craft'));
	}
	if ( ! defined( 'HANDMADE_CRAFT_SUPPORT' ) ) {
	define('HANDMADE_CRAFT_SUPPORT',__('https://wordpress.org/support/theme/handmade-craft/','handmade-craft'));
	}
	if ( ! defined( 'HANDMADE_CRAFT_REVIEW' ) ) {
	define('HANDMADE_CRAFT_REVIEW',__('https://wordpress.org/support/theme/handmade-craft/reviews/','handmade-craft'));
	}
	if ( ! defined( 'HANDMADE_CRAFT_PRO_DEMO' ) ) {
	define('HANDMADE_CRAFT_PRO_DEMO',__('https://demo.asterthemes.com/handmade-craft/','handmade-craft'));
	}
	if ( ! defined( 'HANDMADE_CRAFT_THEME_DOCUMENTATION' ) ) {
	define('HANDMADE_CRAFT_THEME_DOCUMENTATION',__('https://demo.asterthemes.com/docs/handmade-craft-free/','handmade-craft'));
	}
	if ( ! defined( 'HANDMADE_CRAFT_PREMIUM_DOCUMENTATION' ) ) {
	define('HANDMADE_CRAFT_PREMIUM_DOCUMENTATION',__('https://demo.asterthemes.com/docs/handmade-craft-pro/','handmade-craft'));
	}
	if ( ! defined( 'HANDMADE_CRAFT_BUNDLE_PAGE' ) ) {
		define('HANDMADE_CRAFT_BUNDLE_PAGE',__('https://asterthemes.com/products/wp-theme-bundle','handmade-craft'));
	}
}
add_action('after_setup_theme', 'handmade_craft_links_setup');

// Enqueue Customizer live preview script
function handmade_craft_customizer_live_preview() {
    wp_enqueue_script(
        'handmade-craft-customizer',
        get_template_directory_uri() . '/resource/js/customizer.js',
        array('jquery', 'customize-preview'),
        '',
        true
    );
}
add_action('customize_preview_init', 'handmade_craft_customizer_live_preview');

// Featured Image Dimension
function handmade_craft_blog_post_featured_image_dimension(){
	if(get_theme_mod('handmade_craft_blog_post_featured_image_dimension') == 'custom' ) {
		return true;
	}
	return false;
}
add_filter( 'woocommerce_enable_setup_wizard', '__return_false' );

add_filter( 'woocommerce_loop_add_to_cart_link', 'custom_add_to_cart_icon', 10, 2 );
function custom_add_to_cart_icon( $button, $product ) {
    $icon = ' <i class="fas fa-arrow-right"></i>';
    return str_replace( '</a>', $icon . '</a>', $button );
}

// Reset all theme settings
function handmade_craft_reset_demo_import_settings() {
    if (current_user_can('manage_options')) {
        // List only demo import-related settings to reset
		remove_theme_mod('handmade_craft_enable_topbar');
		remove_theme_mod('handmade_craft_location_view_button_text');
		remove_theme_mod('handmade_craft_location_view_button_url');
		remove_theme_mod('handmade_craft_order_view_button_text');
		remove_theme_mod('handmade_craft_order_view_button_url');
		remove_theme_mod('handmade_craft_enable_header_search_section');
		remove_theme_mod('handmade_craft_enable_banner_section');
		remove_theme_mod('handmade_craft_banner_section_heading');
		remove_theme_mod('handmade_craft_banner_section_sub_heading');
		remove_theme_mod('handmade_craft_banner_section_content');
		remove_theme_mod('handmade_craft_banner_button_label_one');
		remove_theme_mod('handmade_craft_banner_button_link_one');
		remove_theme_mod('handmade_craft_banner_button_label_two');
		remove_theme_mod('handmade_craft_banner_button_link_two');
		remove_theme_mod('handmade_craft_banner_right_title');
		remove_theme_mod('handmade_craft_banner_image_title');
		remove_theme_mod('handmade_craft_enable_product_section');
		remove_theme_mod('handmade_craft_trending_product_heading');
		remove_theme_mod('handmade_craft_product_view_button_text');
		remove_theme_mod('handmade_craft_product_view_button_url');

        wp_send_json_success(array('message' => __('Demo import settings have been reset.', 'handmade-craft')));
    } else {
        wp_send_json_error(array('message' => __('Permission denied.', 'handmade-craft')));
    }
}
add_action('wp_ajax_handmade_craft_reset_demo_import_settings', 'handmade_craft_reset_demo_import_settings'); 