<?php
/**
 * Christmas Event functions and definitions
 *
 * @package Christmas Event
 */

if ( ! function_exists( 'christmas_event_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 christmas_event_setup() {
	global $christmas_event_content_width;
	if ( ! isset( $christmas_event_content_width ) )
		$christmas_event_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_events_setup
add_action( 'after_setup_theme', 'christmas_event_setup' );

function christmas_event_widgets_init() {
	register_sidebar( array(
		'name'          => __( 'Blog Sidebar', 'christmas-event' ),
		'description'   => __( 'Appears on blog page sidebar', 'christmas-event' ),
		'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>',
	) );

	$christmas_event_widget_areas = get_theme_mod('classic_events_footer_widget_areas', '4');
	for ($christmas_event_i=1; $christmas_event_i<=$christmas_event_widget_areas; $christmas_event_i++) {
		register_sidebar( array(
			'name'          => __( 'Footer Widget ', 'christmas-event' ) . $christmas_event_i,
			'id'            => 'footer-' . $christmas_event_i,
			'description'   => '',
			'before_widget' => '<aside id="%1$s" class="ftr-4-box widget-column-4 %2$s">',
			'after_widget'  => '</aside>',
			'before_title'  => '<h3 class="widget-title">',
			'after_title'   => '</h3>',
		) );
	}

}
add_action( 'widgets_init', 'christmas_event_widgets_init' );

add_action( 'wp_enqueue_scripts', 'christmas_event_enqueue_styles' );
function christmas_event_enqueue_styles() {
    $parenthandle = 'christmas-event-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( 'christmas-event-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( 'christmas-event-child-style',$christmas_event_color_scheme_css );
}

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

function christmas_event_scripts() {

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

}
add_action( 'wp_enqueue_scripts', 'christmas_event_scripts' );

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

	$wp_customize->remove_section( 'classic_events_below_banner_section' );

}

function christmas_event_setup_theme() {
	if ( ! defined( 'CLASSIC_EVENTS_PRO_NAME' ) ) {
		define( 'CLASSIC_EVENTS_PRO_NAME', __( 'About Christmas Events', 'christmas-event' ));
	}
	if ( ! defined( 'CLASSIC_EVENTS_PREMIUM_PAGE' ) ) {
	define('CLASSIC_EVENTS_PREMIUM_PAGE',__('https://www.theclassictemplates.com/products/christmas-wordpress-theme','christmas-event'));
	}
	if ( ! defined( 'CLASSIC_EVENTS_THEME_PAGE' ) ) {
	define('CLASSIC_EVENTS_THEME_PAGE',__('https://www.theclassictemplates.com/collections/best-wordpress-templates','christmas-event'));
	}
	if ( ! defined( 'CLASSIC_EVENTS_SUPPORT' ) ) {
	define('CLASSIC_EVENTS_SUPPORT',__('https://wordpress.org/support/theme/christmas-event/','christmas-event'));
	}
	if ( ! defined( 'CLASSIC_EVENTS_REVIEW' ) ) {
	define('CLASSIC_EVENTS_REVIEW',__('https://wordpress.org/support/theme/christmas-event/reviews/','christmas-event'));
	}
	if ( ! defined( 'CLASSIC_EVENTS_PRO_DEMO' ) ) {
	define('CLASSIC_EVENTS_PRO_DEMO',__('https://live.theclassictemplates.com/christmas-event-pro/','christmas-event'));
	}
	if ( ! defined( 'CLASSIC_EVENTS_THEME_DOCUMENTATION' ) ) {
	define('CLASSIC_EVENTS_THEME_DOCUMENTATION',__('https://live.theclassictemplates.com/demo/docs/christmas-event-free/','christmas-event'));
	}
}
add_action( 'after_setup_theme', 'christmas_event_setup_theme' );

// Customizer Section
function christmas_event_customizer ( $wp_customize ) {

	//Global Color
	$wp_customize->add_section('christmas_event_global_color', array(
		'title'    => __('Manage Global Color Section', 'christmas-event'),
		'priority'	=> 2,
		'panel'    => 'classic_events_panel_area',
	));

	$wp_customize->add_setting('christmas_event_first_color', array(
		'default'           => '#DC143C',
		'sanitize_callback' => 'sanitize_hex_color',
	));
	$wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'christmas_event_first_color', array(
		'label'    => __('Theme Color', 'christmas-event'),
		'section'  => 'christmas_event_global_color',
		'settings' => 'christmas_event_first_color',
	)));	

	$wp_customize->add_setting('christmas_event_second_color', array(
		'default'           => '#B5995A',
		'sanitize_callback' => 'sanitize_hex_color',
	));
	$wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'christmas_event_second_color', array(
		'label'    => __('Theme Color', 'christmas-event'),
		'section'  => 'christmas_event_global_color',
		'settings' => 'christmas_event_second_color',
	)));	

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

	$wp_customize->add_section('christmas_event_our_services_section', array(
		'title'	=> __('Manage Our Services Section','christmas-event'),
		'priority'	=> 4,
		'panel' => 'classic_events_panel_area',
	));

   	$wp_customize->add_setting('christmas_event_category_sec_text',array(
		'default'=> '',
		'sanitize_callback'	=> 'sanitize_text_field'
	));
	$wp_customize->add_control('christmas_event_category_sec_text',array(
		'label'	=> esc_html__('Add Section Sub-heading','christmas-event'),
		'section'=> 'christmas_event_our_services_section',
		'type'=> 'text'
	));

	$wp_customize->add_setting('christmas_event_category_sec_title',array(
		'default'=> '',
		'sanitize_callback'	=> 'sanitize_text_field'
	));
	$wp_customize->add_control('christmas_event_category_sec_title',array(
		'label'	=> esc_html__('Add Section Heading','christmas-event'),
		'section'=> 'christmas_event_our_services_section',
		'type'=> 'text'
	));

	$wp_customize->add_setting('christmas_event_info_btn_text',array(
		'default' => '',
		'sanitize_callback' => 'sanitize_text_field',
		'capability' => 'edit_theme_options',
	));
	$wp_customize->add_control( 'christmas_event_info_btn_text', array(
	   'settings' => 'christmas_event_info_btn_text',
	   'section'   => 'christmas_event_our_services_section',
	   'label' => __('Add Button Text', 'christmas-event'),
	   'type'      => 'text'
	));

	$wp_customize->add_setting('christmas_event_info_btn_url',array(
		'default' => '',
		'sanitize_callback' => 'esc_url_raw',
		'capability' => 'edit_theme_options',
	));
	$wp_customize->add_control( 'christmas_event_info_btn_url', array(
	   'settings' => 'christmas_event_info_btn_url',
	   'section'   => 'christmas_event_our_services_section',
	   'label' => __('Add Button URL', 'christmas-event'),
	   'type'      => 'url'
	));

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

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

// Category Slider
function christmas_event_taxonomy_add_custom_field() {
    ?>
    <div class="form-field term-image-wrap">
        <label for="cat-image"><?php echo esc_html( 'Image', 'christmas-event' ); ?></label>
        <p><a href="#" class="christmas_event_upload_image_button button button-secondary"><?php echo esc_html('Upload Image', 'christmas-event'); ?></a></p>
        <input type="text" name="category_image" id="cat-image" value="" size="40" />
    </div>
    <?php
}
add_action( 'category_add_form_fields', 'christmas_event_taxonomy_add_custom_field', 10, 2 );
 
function christmas_event_taxonomy_edit_custom_field($christmas_event_term) {
    $christmas_event_image = get_term_meta($christmas_event_term->christmas_event_term_id, 'category_image', true);
    ?>
    <tr class="form-field term-image-wrap">
        <th scope="row"><label for="category_image"><?php echo esc_html( 'Image', 'christmas-event' ); ?></label></th>
        <td>
            <p><a href="#" class="christmas_event_upload_image_button button button-secondary"><?php echo esc_html('Upload Image', 'christmas-event'); ?></a></p><br/>
            <input type="text" name="category_image" id="cat-image" value="<?php echo esc_attr($christmas_event_image); ?>" size="40" />
        </td>
    </tr>
    <?php
}
add_action( 'category_edit_form_fields', 'christmas_event_taxonomy_edit_custom_field', 10, 2 );

function christmas_event_include_script() {
    if ( ! did_action( 'wp_enqueue_media' ) ) {
        wp_enqueue_media();
    }
    wp_enqueue_script( 'christmas-event-script', get_stylesheet_directory_uri() . '/js/category.js', array('jquery'), null, false );
}
add_action( 'admin_enqueue_scripts', 'christmas_event_include_script' );

function christmas_event_save_taxonomy_custom_meta_field( $christmas_event_term_id ) {
    if ( isset( $_POST['category_image'] ) ) {
        update_term_meta($christmas_event_term_id, 'category_image', wp_unslash( $_POST['category_image']));
    }
}  
add_action( 'edited_category', 'christmas_event_save_taxonomy_custom_meta_field', 10, 2 );  
add_action( 'create_category', 'christmas_event_save_taxonomy_custom_meta_field', 10, 2 );



if ( ! function_exists( 'classic_events_the_custom_logo' ) ) :
	/**
	 * Displays the optional custom logo.
	 *
	 * Does nothing if the custom logo is not available.
	 *
	 * @since christmas-event
	 */
	function classic_events_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__( 'Christmas Event logo', 'christmas-event' ) . "' />
					  </a>";
			}
		}
	}
endif;

add_filter( 'classic_events_demo_content_path', function( $christmas_event_default_path ) {
    $christmas_event_child_path = get_stylesheet_directory() . '/inc/demo-content.php';

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

    return $christmas_event_default_path; 
});

// Load additional admin styles for the child theme
function christmas_event_admin_enqueue_scripts() {
    wp_enqueue_style( 'christmas-event-admin-style', get_theme_file_uri() . '/css/addon.css' );
}
add_action( 'admin_enqueue_scripts', 'christmas_event_admin_enqueue_scripts' );
?>