<?php

/**
 * Theme Customizer
 *
 * @package Viralike
 */

/**
 * Add a colorpicker for change the main theme color
 */
function viralike_customize_color($wp_customize)
{
	// Add Settings
	$wp_customize->add_setting('viralike_color', array(
		'default' => '#453CC7',
		'sanitize_callback'  => 'esc_attr'
	));

	// Add Controls
	$wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'viralike_color', array(
		'label' => 'Theme Color',
		'section' => 'colors',
		'settings' => 'viralike_color'
	)));
}

add_action('customize_register', 'viralike_customize_color');

function viralike_customize_color_style()
{
	$themeColor = esc_html(get_theme_mod('viralike_color'));
	if (!empty($themeColor)) : ?>
		<style type="text/css">
			:root {
				--color--main: <?php echo esc_html(get_theme_mod('viralike_color')); ?>;
			}
		</style>
<?php
	endif;
}

add_action('wp_head', 'viralike_customize_color_style');

function viralike_customize_color_save()
{
	wp_enqueue_style('viralike-custom-styles', get_stylesheet_uri());
	$custom_css = viralike_customize_color_style();
	wp_add_inline_style('viralike-custom-styles', $custom_css);
}
add_action('wp_enqueue_scripts', 'viralike_customize_color_save');
