<?php
/**
 * Classic Woocommerce functions and definitions
 *
 * @package Classic Woocommerce
 */

if ( ! function_exists( 'classic_woocommerce_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.
 */

function classic_woocommerce_setup() {
	global $classic_woocommerce_content_width;
	if ( ! isset( $classic_woocommerce_content_width ) )
		$classic_woocommerce_content_width = 680;

	add_theme_support( 'responsive-embeds' );
	add_theme_support( 'automatic-feed-links' );
	add_theme_support( 'wp-block-styles');
	add_theme_support( 'align-wide' );
	add_theme_support( 'woocommerce' );
	add_theme_support( 'post-thumbnails' );
	add_theme_support( 'title-tag' );
	add_theme_support( 'custom-logo', array(
		'height'      => 100,
		'width'       => 100,
		'flex-height' => true,
	) );
	add_theme_support( 'custom-background', array(
		'default-color' => 'ffffff'
	) );
	add_theme_support( 'html5', array(
		'search-form',
		'comment-form',
		'comment-list',
		'gallery',
		'caption',
	) );
	add_editor_style( 'editor-style.css' );
}
endif; // classic_ecommerce_setup
add_action( 'after_setup_theme', 'classic_woocommerce_setup' );

function classic_woocommerce_widgets_init() {
	register_sidebar( array(
		'name'          => __( 'Blog Sidebar', 'classic-woocommerce' ),
		'description'   => __( 'Appears on blog page sidebar', 'classic-woocommerce' ),
		'id'            => 'sidebar-1',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );

	$classic_woocommerce_widget_areas = get_theme_mod('classic_woocommerce_footer_widget_areas', '4');
	for ($classic_woocommerce_i=1; $classic_woocommerce_i<=$classic_woocommerce_widget_areas; $classic_woocommerce_i++) {
		register_sidebar( array(
			'name'          => __( 'Footer Widget ', 'classic-woocommerce' ) . $classic_woocommerce_i,
			'id'            => 'footer-' . $classic_woocommerce_i,
			'description'   => '',
			'before_widget' => '<aside id="%1$s" class="widget %2$s">',
			'after_widget'  => '</aside>',
			'before_title'  => '<h3 class="widget-title">',
			'after_title'   => '</h3>',
		) );
	}
}
add_action( 'widgets_init', 'classic_woocommerce_widgets_init' );

add_action( 'wp_enqueue_scripts', 'classic_woocommerce_enqueue_styles' );
function classic_woocommerce_enqueue_styles() {
    $parenthandle = 'classic-woocommerce-style'; // This is 'twentyfifteen-style' for the Twenty Fifteen theme.
    $theme = wp_get_theme();
    wp_enqueue_style( $parenthandle, esc_url(get_template_directory_uri()) . '/style.css',
        array(),  // if the parent theme code has a dependency, copy it to here
        $theme->parent()->get('Version')
    );
    wp_enqueue_style( 'classic-woocommerce-child-style', get_stylesheet_uri(),
        array( $parenthandle ),
        $theme->get('Version') // this only works if you have Version in the style header
    );
	require get_theme_file_path( 'inc/color-scheme/custom-color-control.php' );
	wp_add_inline_style( 'classic-woocommerce-style',$classic_woocommerce_color_scheme_css );
}

function classic_woocommerce_enqueue_customizer_css() {
    wp_enqueue_style( 'classic_woocommerce-customizer-css', get_stylesheet_directory_uri() . '/css/customize-controls.css' );
}
add_action( 'customize_controls_print_styles', 'classic_woocommerce_enqueue_customizer_css' );


function classic_woocommerce_scripts() {

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

add_action( 'customize_register', 'classic_woocommerce_customize_register', 11 );
function classic_woocommerce_customize_register() {
	global $wp_customize;
	$wp_customize->remove_section( 'classic_ecommerce_social_media_section' );

	$wp_customize->remove_setting( 'classic_ecommerce_offer_text' );
	$wp_customize->remove_control( 'classic_ecommerce_offer_text' );

	$wp_customize->remove_setting( 'classic_ecommerce_category_text' );
	$wp_customize->remove_control( 'classic_ecommerce_category_text' );

	$wp_customize->remove_setting( 'classic_ecommerce_product_category_number' );
	$wp_customize->remove_control( 'classic_ecommerce_product_category_number' );

	$wp_customize->remove_setting( 'classic_ecommerce_headerbg_color' );
	$wp_customize->remove_control( 'classic_ecommerce_headerbg_color' );

	$wp_customize->remove_setting( 'classic_ecommerce_header_settings_upgraded_features' );
	$wp_customize->remove_control( 'classic_ecommerce_header_settings_upgraded_features' );

	$wp_customize->remove_setting( 'classic_ecommerce_color_scheme_one' );
	$wp_customize->remove_control( 'classic_ecommerce_color_scheme_one' );

	$wp_customize->remove_setting( 'classic_ecommerce_color_scheme_two' );
	$wp_customize->remove_control( 'classic_ecommerce_color_scheme_two' );

	$wp_customize->remove_setting( 'classic_ecommerce_top_bar' );
	$wp_customize->remove_control( 'classic_ecommerce_top_bar' );

}

// Customizer Section
function classic_woocommerce_customizer ( $wp_customize ) {

	$wp_customize->add_setting('classic_woocommerce_phoneno',array(
		'default'	=> '',
		'sanitize_callback'	=> 'sanitize_text_field'
	));
	$wp_customize->add_control('classic_woocommerce_phoneno',array(
		'label'	=> __('Phone Number','classic-woocommerce'),
		'section'	=> 'classic_ecommerce_header_section',
		'setting'	=> 'classic_woocommerce_phoneno',
		'type'		=> 'text'
	));

	$wp_customize->add_setting('classic_woocommerce_email',array(
		'default'	=> '',
		'sanitize_callback'	=> 'sanitize_text_field'
	));

	$wp_customize->add_control('classic_woocommerce_email',array(
		'label'	=> __('Email','classic-woocommerce'),
		'section'	=> 'classic_ecommerce_header_section',
		'setting'	=> 'classic_woocommerce_email',
		'type'		=> 'text'
	));

	$wp_customize->add_setting( 'classic_woocommerce_header_settings_upgraded_features',array(
		'sanitize_callback' => 'sanitize_text_field'
	));
	$wp_customize->add_control('classic_woocommerce_header_settings_upgraded_features', array(
		'type'=> 'hidden',
		'description' => "<span class='customizer-upgraded-features'>Unlock Premium Customization Features:
		   <a target='_blank' href='". esc_url(CLASSIC_ECOMMERCE_PREMIUM_PAGE) ." '>Upgrade to Pro</a></span>",
		'section' => 'classic_ecommerce_header_section'
	));

	$wp_customize->add_setting('classic_woocommerce_color_scheme_one', array(
		'default'           => '',
		'sanitize_callback' => 'sanitize_hex_color',
	));
	$wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'classic_woocommerce_color_scheme_one', array(
		'label'    => __('Theme Color', 'classic-woocommerce'),
		'section'  => 'classic_ecommerce_global_color',
		'settings' => 'classic_woocommerce_color_scheme_one',	
	)));

	$wp_customize->add_setting('classic_woocommerce_color_scheme_two', array(
		'default'           => '',
		'sanitize_callback' => 'sanitize_hex_color',
	));
	$wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'classic_woocommerce_color_scheme_two', array(
		'label'    => __('Theme Color', 'classic-woocommerce'),
		'section'  => 'classic_ecommerce_global_color',
		'settings' => 'classic_woocommerce_color_scheme_two',	
	)));	

}
add_action( 'customize_register', 'classic_woocommerce_customizer' );

function classic_woocommerce_setup_theme() {
	// Footer Link
	define('CLASSIC_WOOCOMMERCE_FOOTER_LINK',__('https://www.theclassictemplates.com/products/free-wordpress-woocommerce-template','classic-woocommerce'));

	if ( ! defined( 'CLASSIC_ECOMMERCE_PRO_NAME' ) ) {
		define( 'CLASSIC_ECOMMERCE_PRO_NAME', __( 'About Classic Woocommerce', 'classic-woocommerce' ));
	}
	if ( ! defined( 'CLASSIC_ECOMMERCE_THEME_PAGE' ) ) {
	define('CLASSIC_ECOMMERCE_THEME_PAGE',__('https://www.theclassictemplates.com/collections/best-wordpress-templates','classic-woocommerce'));
	}
	if ( ! defined( 'CLASSIC_ECOMMERCE_SUPPORT' ) ) {
	define('CLASSIC_ECOMMERCE_SUPPORT',__('https://wordpress.org/support/theme/classic-woocommerce','classic-woocommerce'));
	}
	if ( ! defined( 'CLASSIC_ECOMMERCE_REVIEW' ) ) {
	define('CLASSIC_ECOMMERCE_REVIEW',__('https://wordpress.org/support/theme/classic-woocommerce/reviews/','classic-woocommerce'));
	}
	if ( ! defined( 'CLASSIC_ECOMMERCE_PRO_DEMO' ) ) {
	define('CLASSIC_ECOMMERCE_PRO_DEMO',__('https://live.theclassictemplates.com/demo/classic-ecommerce','classic-woocommerce'));
	}
	if ( ! defined( 'CLASSIC_ECOMMERCE_PREMIUM_PAGE' ) ) {
	define('CLASSIC_ECOMMERCE_PREMIUM_PAGE',__('https://www.theclassictemplates.com/products/wordpress-ecommerce-template','classic-woocommerce'));
	}
	if ( ! defined( 'CLASSIC_ECOMMERCE_THEME_DOCUMENTATION' ) ) {
	define('CLASSIC_ECOMMERCE_THEME_DOCUMENTATION',__('https://live.theclassictemplates.com/demo/docs/classic-woocommerce-free/','classic-woocommerce'));
	}
	if ( ! defined( 'CLASSIC_ECOMMERCE_BUNDLE_PAGE' ) ) {
		define('CLASSIC_ECOMMERCE_BUNDLE_PAGE',__('https://www.theclassictemplates.com/products/wordpress-theme-bundle','classic-woocommerce'));
	}
}
add_action( 'after_setup_theme', 'classic_woocommerce_setup_theme' );

if ( ! function_exists( 'classic_ecommerces_the_custom_logo' ) ) :
	/**
	 * Displays the optional custom logo.
	 *
	 * Does nothing if the custom logo is not available.
	 *
	 * @since classic-woocommerce
	 */
	function classic_ecommerces_the_custom_logo() {
		if ( function_exists( 'the_custom_logo' ) ) {
			// Check if custom logo is set via the customizer
			if ( has_custom_logo() ) {
				the_custom_logo();
			} else {
				// If no custom logo, display the logo from the child theme directory
				echo "<a href='" . esc_url( home_url() ) . "' rel='home'>
						<img src='" . esc_url( get_stylesheet_directory_uri() ) . "/images/Logo.png' alt='" . esc_attr__( 'Classic Woocommerce logo', 'classic-woocommerce' ) . "' />
					  </a>";
			}
		}
	}
endif;


// Redirect to a custom page after theme activation in the child theme
function classic_woocommerce_options() {
    global $pagenow;

    if ( is_admin() && 'themes.php' === $pagenow && isset( $_GET['activated'] ) && current_user_can( 'manage_options' ) ) {
        // Theme activated — future actions can go here, like setting theme mods or showing notices.
    }
}
add_action( 'admin_init', 'classic_woocommerce_options' );

add_filter( 'classic_ecommerce_demo_content_path', function( $classic_woocommerce_default_path ) {
    $classic_woocommerce_child_path = get_stylesheet_directory() . '/inc/demo-content.php';

    if ( file_exists( $classic_woocommerce_child_path ) ) {
        return $classic_woocommerce_child_path;
    }

    return $classic_woocommerce_default_path; 
});

// Load additional admin styles for the child theme
function classic_woocommerce_admin_enqueue_scripts() {
    wp_enqueue_style( 'classic-woocommerce-admin-style', get_stylesheet_directory_uri() . '/css/addon.css' );
}
add_action( 'admin_enqueue_scripts', 'classic_woocommerce_admin_enqueue_scripts' );
?>