<?php

/*
*  restore default
*/
function lucia_otpions_restore(){
  	add_option(LUCIA_TEXTDOMAIN.'_backup_'.time(),get_option(LUCIA_TEXTDOMAIN));
  	delete_option(LUCIA_TEXTDOMAIN);
	echo 'done';
	exit(0);
}
add_action( 'wp_ajax_lucia_otpions_restore', 'lucia_otpions_restore' );
add_action( 'wp_ajax_nopriv_lucia_otpions_restore', 'lucia_otpions_restore' );

/**
 * Get option
 */
function lucia_option($name){
	
	global $lucia_options, $lucia_default_options;
		
	if(function_exists('is_customize_preview') && is_customize_preview()){
		$options = get_option(LUCIA_TEXTDOMAIN);

		if( isset($options[$name]) )
			return $options[$name];
	}
	if( isset($lucia_options[$name]) )
		return $lucia_options[$name];
	elseif(isset($lucia_default_options[$name]))
		return $lucia_default_options[$name];
	else
		return '';
	}

/**
 * Get option saved in database
 */
function lucia_option_saved($name){
	
	$lucia_options = get_option(LUCIA_TEXTDOMAIN);
	
	if( isset($lucia_options[$name]) )
		return $lucia_options[$name];
	else
		return '';
	}

/**
 * Selective Refresh
 */
function lucia_register_partials( WP_Customize_Manager $wp_customize ) {
	
	  global $lucia_customizer_options;

	// Abort if selective refresh is not available.
		if ( ! isset( $wp_customize->selective_refresh ) ) {
			return;
		}

		// Bail early if we don't have any options.
		if ( empty( $lucia_customizer_options ) ) {
			return;
		}
	
	$wp_customize->selective_refresh->add_partial( 'copyright_selective', array(
		'selector' => '.copyright_selective',
		'settings' => array( 'lucia[copyright]' ),
		'render_callback' => 'lucia_copyright',
	) );
	
	$wp_customize->selective_refresh->add_partial( 'header_site_title', array(
		'selector' => '.site-name',
		'settings' => array( 'blogname' ),
		'render_callback' => 'lucia_header_site_title',
		
	) );
	
	$wp_customize->selective_refresh->add_partial( 'header_site_description', array(
		'selector' => '.site-tagline',
		'settings' => array( 'blogdescription' ),
		'render_callback' => 'lucia_header_site_descriptione',
		
	) );
	
	$wp_customize->get_section ('title_tagline')->panel = 'panel-header';
	//$wp_customize->get_section ('colors')->panel = 'panel-header';
	$wp_customize->get_section ('header_image')->panel = 'panel-header';
	
}
add_action( 'customize_register', 'lucia_register_partials' );

/**
 * Footer copyright information
 */

function lucia_copyright(){
	
	$lucia_options = get_option(LUCIA_TEXTDOMAIN);
	if( isset($lucia_options['copyright']) )
		return $lucia_options['copyright'];
		
	}

function lucia_header_site_title(){
	return get_bloginfo( 'name' );
	}

function lucia_header_site_descriptione(){
	return get_bloginfo( 'description' );
	}

function lucia_ajax_get_image_url(){
	
	$id = $_POST['id'];
	$image = $id;
	if (is_numeric($id)) {
			$image_attributes = wp_get_attachment_image_src($id, 'full');
			$image   = $image_attributes[0];
		  }
	echo esc_url( $image );
	exit(0);
	
	}
	
add_action('wp_ajax_lucia_ajax_get_image_url', 'lucia_ajax_get_image_url');
add_action('wp_ajax_nopriv_lucia_ajax_get_image_url', 'lucia_ajax_get_image_url');

/**
 * Include the TGM_Plugin_Activation class.
 */
if ( !class_exists( 'TGM_Plugin_Activation' ) ) 
	load_template( trailingslashit( get_template_directory() ) . 'inc/class-tgm-plugin-activation.php' );

add_action( 'tgmpa_register', 'lucia_theme_register_required_plugins' );

/**
 * Register the required plugins for this theme.
 *
 */
function lucia_theme_register_required_plugins() {

    $plugins = array(
		array(
			'name'     				=> __('Hoo Companion','lucia'), 
			'slug'     				=> 'hoo-companion',
			'source'   				=> '', 
			'required' 				=> false, 
			'version' 				=> '1.0.0', 
			'force_activation' 		=> false,
			'force_deactivation' 	=> false,
			'external_url' 			=> '', 
		),

		array(
			'name'     				=> __('Elementor','lucia'),
			'slug'     				=> 'elementor',
			'source'   				=> '',
			'required' 				=> false,
			'version' 				=> '1.0.0',
			'force_activation' 		=> false,
			'force_deactivation' 	=> false,
			'external_url' 			=> '',
		),
		
		array(
			'name'     				=> __('Contact Form 7','lucia'),
			'slug'     				=> 'contact-form-7',
			'source'   				=> '',
			'required' 				=> false,
			'version' 				=> '1.0.0',
			'force_activation' 		=> false,
			'force_deactivation' 	=> false,
			'external_url' 			=> '',
		),
		
//@if NODE_ENV == 'pro'
		array(
			'name'     				=> __('Elementor Pro','lucia'),
			'slug'     				=> 'elementor-pro',
			'source'   				=> esc_url('https://lqthemes.com/downloads/elementor-pro.zip'),
			'required' 				=> false,
			'version' 				=> '1.0.0',
			'force_activation' 		=> false,
			'force_deactivation' 	=> false,
			'external_url' 			=> '',
		),
//@endif

	);

    /**
     * Array of configuration settings. Amend each line as needed.
     */
    $config = array(
        'id'           => 'hoo-companion',
        'default_path' => '', 
        'menu'         => 'tgmpa-install-plugins', 
        'has_notices'  => true,
        'dismissable'  => true,
        'dismiss_msg'  => '',
        'is_automatic' => false,
        'message'      => '',
    );

    tgmpa( $plugins, $config );

}

/**
 * Convert Hex Code to RGB
 *
 */
 
function lucia_hex2rgb( $hex ) {
	if ( strpos( $hex,'rgb' ) !== FALSE ) {

		$rgb_part = strstr( $hex, '(' );
		$rgb_part = trim($rgb_part, '(' );
		$rgb_part = rtrim($rgb_part, ')' );
		$rgb_part = explode( ',', $rgb_part );

		$rgb = array($rgb_part[0], $rgb_part[1], $rgb_part[2], $rgb_part[3]);

	} elseif( $hex == 'transparent' ) {
		$rgb = array( '255', '255', '255', '0' );
	} else {

		$hex = str_replace( '#', '', $hex );
		
		
		if( strlen( $hex ) == 3 ) {
			$r = hexdec( substr( $hex, 0, 1 ) . substr( $hex, 0, 1 ) );
			$g = hexdec( substr( $hex, 1, 1 ) . substr( $hex, 1, 1 ) );
			$b = hexdec( substr( $hex, 2, 1 ) . substr( $hex, 2, 1 ) );
		} else {
			$r = hexdec( substr( $hex, 0, 2 ) );
			$g = hexdec( substr( $hex, 2, 2 ) );
			$b = hexdec( substr( $hex, 4, 2 ) );
		}
		$rgb = array( $r, $g, $b );
	}

	return $rgb;
}

/*
 * Get header widgets
 */
function lucia_get_header_widgets( $key, $output = true ){
	
	$widgets = lucia_option($key);
	$html = '';
	if(is_array($widgets) && !empty($widgets)):
		$html = "";
		foreach($widgets as $item):
			$html .= '<span class="lucia-microwidget">';
			if($item['link']!=''){
				$html .= '<a href="'.esc_url($item['link']).'" target="'.esc_attr($item['target']).'">';
			}
			if($item['icon']!=''){
				
				$item['icon'] = str_replace('fa-','',$item['icon']);
				$item['icon'] = str_replace('fa','',$item['icon']);
				$item['icon'] = 'fa fa-'.$item['icon'];
				
				$html .= '<i class="'.esc_attr($item['icon']).'"></i>&nbsp;&nbsp;';
			}
			$html .= esc_attr($item['text']);
			if($item['link']!=''){
				$html .= '</a>';
			}
			$html .= '</span>';
		endforeach;
	endif;
	if( $output == true)
		echo $html;
	else
		return $html;
	
	}
	