<?php

function relic_cats()
{
  $args = array('parent' => 0);
  $categories = get_categories($args);
  $category = array();
  $i = 0;
  foreach($categories as $categorys){
      if($i==0){
          $default = $categorys->slug;
          $i++;
      }
      $category[$categorys->term_id] = $categorys->name;
  }
  return $category;
}
function relic_theme_customizer( $wp_customize ) {
	$relic_options = get_option('relic_theme_options');
	$wp_customize->add_panel(
    'general',
    array(
        'title' => __( 'General', 'relic' ),
        'description' => __('styling options','relic'),
        'priority' => 20, 
    )  );
  $wp_customize->get_section('title_tagline')->panel = 'general';
  $wp_customize->get_section('static_front_page')->panel = 'general';
  $wp_customize->get_section('header_image')->panel = 'general';
  $wp_customize->get_section('title_tagline')->title = __('Header & Logo','relic');

 $wp_customize->add_section(
    'TopHeaderSocialLinks',
    array(
      'title' => __('Top Header Social Accounts', 'relic'),
      'priority' => 120,
      'description' => __( 'In first input box, you need to add FONT AWESOME shortcode which you can find ' ,  'relic').'<a target="_blank" href="'.esc_url('https://fortawesome.github.io/Font-Awesome/icons/').'">'.__('here' ,  'relic').'</a>'.__(' and in second input box, you need to add your social media profile URL.', 'relic').'<br />'.__(' Enter the URL of your social accounts. Leave it empty to hide the icon.' ,  'relic'),
      'panel' => 'general'
    )
  );

$TopHeaderSocialIconDefault = array(
  array('url'=>$relic_options['fburl'],'icon'=>'fa-facebook'),
  array('url'=>$relic_options['twitter'],'icon'=>'fa-twitter'),
  array('url'=>$relic_options['gplus'],'icon'=>'fa-google-plus'),
  array('url'=>$relic_options['linkedin'],'icon'=>'fa-linkedin'),
  array('url'=>$relic_options['pinterest'],'icon'=>'fa-pinterest'),
  array('url'=>$relic_options['rss'],'icon'=>'fa-rss'),
  );

$TopHeaderSocialIcon = array();
  for($i=1;$i <= 6;$i++):  
    $TopHeaderSocialIcon[] =  array( 'slug'=>sprintf('TopHeaderSocialIcon%d',$i),   
      'default' => $TopHeaderSocialIconDefault[$i-1]['icon'],   
      'label' => esc_html__( 'Social Account ', 'relic') .$i,   
      'priority' => sprintf('%d',$i) );  
  endfor;
  foreach($TopHeaderSocialIcon as $TopHeaderSocialIcons){
    $wp_customize->add_setting(
      $TopHeaderSocialIcons['slug'],
      array( 
       'default' => $TopHeaderSocialIcons['default'],       
        'capability'     => 'edit_theme_options',
        'type' => 'theme_mod',
        'sanitize_callback' => 'sanitize_text_field',
      )
    );
    $wp_customize->add_control(
      $TopHeaderSocialIcons['slug'],
      array(
        'type'  => 'text',
        'section' => 'TopHeaderSocialLinks',
        'input_attrs' => array( 'placeholder' => esc_attr__('Enter Icon','relic') ),
        'label'      =>   $TopHeaderSocialIcons['label'],
        'priority' => $TopHeaderSocialIcons['priority']
      )
    );
  }
  $TopHeaderSocialIconLink = array();
  for($i=1;$i <= 6;$i++):  
    $TopHeaderSocialIconLink[] =  array( 'slug'=>sprintf('TopHeaderSocialIconLink%d',$i),   
      'default' => $TopHeaderSocialIconDefault[$i-1]['url'],   
      'label' => esc_html__( 'Social Link ', 'relic' ) .$i,
      'priority' => sprintf('%d',$i) );  
  endfor;
  foreach($TopHeaderSocialIconLink as $TopHeaderSocialIconLinks){
    $wp_customize->add_setting(
      $TopHeaderSocialIconLinks['slug'],
      array(
        'default' => $TopHeaderSocialIconLinks['default'],
        'capability'     => 'edit_theme_options',
        'type' => 'theme_mod',
        'sanitize_callback' => 'esc_url_raw',
      )
    );
    $wp_customize->add_control(
      $TopHeaderSocialIconLinks['slug'],
      array(
        'type'  => 'text',
        'section' => 'TopHeaderSocialLinks',
        'priority' => $TopHeaderSocialIconLinks['priority'],
        'input_attrs' => array( 'placeholder' => esc_html__('Enter URL','relic')),
      )
    );
  }

    /* sections */
    $wp_customize->add_section( 'relic_basic_section' , array(
    'title'       => __( 'Basic Settings', 'relic' ),
    'priority'    => 30,
    'panel' => 'general'
	) );
	$wp_customize->add_panel( 'home_id', array(
		'capability'     => 'edit_theme_options',
		'theme_supports' => '',
		'title'          => __('Front Page Settings','relic'),
		'description'    => '',
		'priority'    => 30,
	) );
	
	$wp_customize->add_section( 'frontpage_slider_section' ,
     array(
        'title'       => __( 'Banner Slider', 'relic' ),
        'priority'    => 30,
        'capability'     => 'edit_theme_options', 
        'panel' => 'home_id'   
    )
  );


	$wp_customize->add_section( 'relic_first_section' , array(
		'title'       => __( 'First Section', 'relic' ),
		'priority'    => 30,
		'panel'  => 'home_id',
	) );	
	

	$wp_customize->add_section( 'relic_blog_section' , array(
		'title'       => __( 'Second Section', 'relic' ),
		'priority'    => 30,
		'panel'  => 'home_id',
	) );
	/* basic section */
	
	
	// copyright
	$wp_customize->add_setting( 'copyright_url_setting', array(
		'default'        => isset($relic_options['footertext'])?$relic_options['footertext']:'',
		'sanitize_callback' => 'relic_sanitize_html',
	) );
	$wp_customize->add_control( 'copyright_url_setting', array(
		'label'   => __('Copyright text','relic'),
		'section' => 'relic_basic_section',
		'type'    => 'text'
	) );

  $wp_customize->add_setting( 'topheader_email', array(
    'default'        => isset($relic_options['email'])?$relic_options['email']:'',
    'sanitize_callback' => 'relic_sanitize_html',
  ) );
  $wp_customize->add_control( 'topheader_email', array(
    'label'   => __('Top Header Email','relic'),
    'section' => 'relic_basic_section',
    'type'    => 'text'
  ) );

  $wp_customize->add_setting( 'topheader_phone', array(
    'default'        => isset($relic_options['phone'])?$relic_options['phone']:'',
    'sanitize_callback' => 'relic_sanitize_html',
  ) );
  $wp_customize->add_control( 'topheader_phone', array(
    'label'   => __('Top Header Phone','relic'),
    'section' => 'relic_basic_section',
    'type'    => 'text'
  ) );

	//titlebar section	
	
 for($i=1;$i <= 5;$i++):  

    $wp_customize->add_setting(
        'relic_homepage_sliderimage'.$i.'_image',
        array(
            'default' => '',
            'capability'     => 'edit_theme_options',
            'sanitize_callback' => 'absint',
        )
    );
    $wp_customize->add_control( new WP_Customize_Cropped_Image_Control( $wp_customize, 'relic_homepage_sliderimage'.$i.'_image', array(
        'section'     => 'frontpage_slider_section',
        'label'       => __( 'Upload Slider Image ' ,'relic').$i.__( ' (Recommended size 1024*400)' ,'relic'),
        'flex_width'  => true,
        'flex_height' => true,
        'width'       => 1024,
        'height'      => 400,   
        'default-image' => '',
    ) ) );

      
    $wp_customize->add_setting( 'relic_homepage_sliderimage'.$i.'_link',
        array(
            'default' => '',
            'capability'     => 'edit_theme_options',
            'sanitize_callback' => 'esc_url_raw',
            'priority' => 20, 
        )
    );
    $wp_customize->add_control( 'relic_homepage_sliderimage'.$i.'_link',
        array(
            'section' => 'frontpage_slider_section',                
            'label'   => __('Enter Slider Link ','relic').$i,
            'type'    => 'text',
            'input_attrs' => array( 'placeholder' => esc_html__('Enter Slider URL','relic')),
        )
    );
 endfor;
	

	
    // Featured Section  
 $wp_customize->add_setting( 'relic_homepage_first_section_title_head',
      array(
          'default' => $relic_options['home-title'],
          'capability'     => 'edit_theme_options',
          'sanitize_callback' => 'sanitize_text_field',
          'priority' => 20, 
      )
  );
  $wp_customize->add_control( 'relic_homepage_first_section_title_head',
      array(
          'section' => 'relic_first_section',
          'type'    => 'text',
          'input_attrs' => array( 'placeholder' => esc_html__('Enter title Heading','relic')),
      )
  );
    for($i=1;$i <= 3;$i++):
    $wp_customize->add_setting( 'relic_homepage_first_section'.$i.'_img',array(
    'sanitize_callback' => 'esc_url_raw',
    )
  );
    $wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'relic_homepage_first_section'.$i.'_img', array(
      'label'    => __( 'Tab (Recommended size 380 x 270)', 'relic' ).$i,
      'section'  => 'relic_first_section',
      'settings' => 'relic_homepage_first_section'.$i.'_img',
    ) 
  ) );
  $wp_customize->add_setting( 'relic_homepage_first_section'.$i.'_title',
      array(
          'default' => $relic_options['section-title-'.$i],
          'capability'     => 'edit_theme_options',
          'sanitize_callback' => 'sanitize_text_field',
          'priority' => 20, 
      )
  );
  $wp_customize->add_control( 'relic_homepage_first_section'.$i.'_title',
      array(
          'section' => 'relic_first_section',
          'type'    => 'text',
          'input_attrs' => array( 'placeholder' => esc_html__('Enter title','relic')),
      )
  );

  $wp_customize->add_setting( 'relic_homepage_first_section'.$i.'_desc',
      array( 
          'default' => $relic_options['section-content-'.$i],     
          'capability'     => 'edit_theme_options',
          'sanitize_callback' => 'wp_kses_post',
          'priority' => 20, 
      )
  );
  $wp_customize->add_control( 'relic_homepage_first_section'.$i.'_desc',
      array(
          'section' => 'relic_first_section',   
          'type'    => 'textarea',
          'input_attrs' => array( 'placeholder' => esc_html__('Enter Description','relic')),
      )
  );  
endfor;
  //Blog Section
	$wp_customize->add_setting( 'relic_blog_title', array(
		'default'        => '',
		'sanitize_callback' => 'relic_sanitize_text',
	) );
    $wp_customize->add_control( 'relic_blog_title', array(
		'label'   => __('Blog Title','relic'),
        'section' => 'relic_blog_section',
        'type'    => 'text'
    ) );    
	
	$wp_customize->add_setting( 'relic_blogcategory', array(
		'default'        => 'Uncategorized',
		'sanitize_callback' => 'esc_attr',
				
	) );
    $wp_customize->add_control( 'relic_blogcategory', array(
		'label'   => __('Select Category','relic'),
        'section' => 'relic_blog_section',
        'type'    => 'select',
        'choices' => relic_cats(),
    ) );  
	
}
add_action( 'customize_register', 'relic_theme_customizer' );
function relic_sanitize_url( $relic_url ) {
	return esc_url_raw( $relic_url );
}
function relic_sanitize_html( $relic_html ) {
	return wp_filter_post_kses( $relic_html );
}
function relic_sanitize_number_absint( $relic_number ) {
	// Ensure $number is an absolute integer (whole number, zero or greater).
	$number = absint( $relic_number );
	// If the input is an absolute integer, return it; otherwise, return the default
	return ( $relic_number ? $relic_number : '' );
}
function relic_sanitize_text( $relic_input ) {
    return wp_kses_post( force_balance_tags( $relic_input ) );
}

function relic_custom_css()
{
	$custom_css = '';
  wp_enqueue_style('relic-style-css', get_stylesheet_uri(), array());
	$relic_breadcrumbs_image_bg=get_theme_mod('relic_breadcrumbs_image_bg');
	if (!empty($relic_breadcrumbs_image_bg) ){
		$relic_breadcrumbs_image_bg = esc_url(get_theme_mod('relic_breadcrumbs_image_bg'));
		$custom_css .=" .background-section { background-image :url('".$relic_breadcrumbs_image_bg."');
		background-position: center;} ";
	}	

	$relic_options = get_option('relic_theme_options');
	//if (get_theme_mod ( 'relic_topsec_img',$relic_options['headertop-bg'])!='') 
	{
		$custom_css .=".header_bg { background :url('".get_theme_mod ( 'relic_topsec_img',$relic_options['headertop-bg'])."'); } ";
	}


	wp_add_inline_style('relic-style-css',$custom_css);
}