<?php
//radio box sanitization function
function rebeccafood_sanitize_radio( $input, $setting ){
    //input must be a slug: lowercase alphanumeric characters, dashes and underscores are allowed only
    $input = sanitize_key($input);

    //get the list of possible radio box options
    $choices = $setting->manager->get_control( $setting->id )->choices;

    //return input if valid or return default option
    return ( array_key_exists( $input, $choices ) ? $input : $setting->default );
}

function rebeccafood_sanitize_checkbox( $checked ) {
	return ( isset($checked) && !empty($checked) ? 1 : 0 );
}

function rebeccafood_sanitize_number_absint( $number, $setting ) {
	// Ensure $number is an absolute integer (whole number, zero or greater).
	$number = absint( $number );

	// If the input is an absolute integer, return it; otherwise, return the default
	return ( $number ? $number : $setting->default );
}

function rebeccafood_sanitize_select( $input, $setting ) {
	// Ensure input is a slug.
	$input = sanitize_key( $input );

	// Get list of choices from the control associated with the setting.
	$choices = $setting->manager->get_control( $setting->id )->choices;

	// If the input is a valid key, return it; otherwise, return the default.
	return ( array_key_exists( $input, $choices ) ? $input : $setting->default );
}

/** Theme Customizer */
function rebeccafood_theme_customizer( $wp_customize ) {
    /**
     * Colors
     */
    $wp_customize->add_setting( 'rebeccafood_primary_color', array( 'default' => '#212121', 'sanitize_callback' => 'sanitize_hex_color' ) );
    $wp_customize->add_setting( 'rebeccafood_text_color', array( 'default' => '#363636', 'sanitize_callback' => 'sanitize_hex_color' ) );
    $wp_customize->add_setting( 'rebeccafood_accent_color', array( 'default' => '#28d631', 'sanitize_callback' => 'sanitize_hex_color' ) );
    $wp_customize->add_setting( 'rebeccafood_meta_color', array( 'default' => '#666', 'sanitize_callback' => 'sanitize_hex_color' ) );
    $wp_customize->add_setting( 'rebeccafood_boder_color', array( 'default' => '#ddd', 'sanitize_callback' => 'sanitize_hex_color' ) );

    $wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'rebeccafood_primary_color',
			array(
				'label'      => __( 'Primary Color', 'rebeccafood' ),
				'section'    => 'colors'
			)
		)
	);

    $wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'rebeccafood_text_color',
			array(
				'label'      => __( 'Text Color', 'rebeccafood' ),
				'section'    => 'colors'
			)
		)
	);

    $wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'rebeccafood_accent_color',
			array(
				'label'      => __( 'Accent Color', 'rebeccafood' ),
				'section'    => 'colors'
			)
		)
	);

	$wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'rebeccafood_meta_color',
			array(
				'label'      => __( 'Meta Color', 'rebeccafood' ),
				'section'    => 'colors'
			)
		)
	);

    $wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'rebeccafood_boder_color',
			array(
				'label'      => __( 'Boder Color', 'rebeccafood' ),
				'section'    => 'colors'
			)
		)
	);
    
    /**
     * Blog Layouts
     */
    $wp_customize->add_section( 'rebeccafood_section_blog_layouts', array(
        'title'     => __( 'Blog Layouts', 'rebeccafood' ),
        'priority'  => 30
    ) );
    
    $wp_customize->add_setting( 'rebeccafood_blog_layout', array(
        'default' => 'blog-2cols-grid',        
        'sanitize_callback' => 'rebeccafood_sanitize_select'
    ) );
    
    $wp_customize->add_setting( 'rebeccafood_blog_disable_sidebar', array(
        'default' => false,        
        'sanitize_callback' => 'rebeccafood_sanitize_checkbox'
    ) );
    
    $wp_customize->add_control( 'rebeccafood_blog_layout', array(
        'label'     => __( 'Blog Layout', 'rebeccafood' ),
        'section'   => 'rebeccafood_section_blog_layouts',
        'type'      => 'select',
        'choices'   => array(
            'blog-2cols-grid' => __( '2 Cols Grid Layout', 'rebeccafood' ),
            'blog-3cols-grid' => __( '3 Cols Grid Layout', 'rebeccafood' ),
            'blog-standard' => __( 'Standard Layout', 'rebeccafood' )
        )
	) );

    $wp_customize->add_control( 'rebeccafood_blog_disable_sidebar', array(
        'label'     => __( 'Disable Sidebar', 'rebeccafood' ),
        'section'   => 'rebeccafood_section_blog_layouts',
        'type'      => 'checkbox'
	) );
    
    /**
     * Promo Boxes
     */
    $wp_customize->add_panel( 'rebeccafood_promo_boxes_panel', array(
        'title'         => __( 'Promo Boxes', 'rebeccafood' ),
        'priority'      => 35
    ) );

    $wp_customize->add_section( 'rebeccafood_section_promo_boxes_settings', array(
        'title' => __( 'Settings', 'rebeccafood' ),
        'panel' => 'rebeccafood_promo_boxes_panel'
    ) );

    $wp_customize->add_setting( 'rebeccafood_promo_boxes_show', array(
        'default'           => false,        
        'sanitize_callback' => 'rebeccafood_sanitize_checkbox'
    ) );

    $wp_customize->add_control( new WP_Customize_Control( $wp_customize, 'rebeccafood_promo_boxes_show', array(
	   'label'     => __( 'Show ?', 'rebeccafood' ),
       'type'      => 'checkbox',
	   'section'   => 'rebeccafood_section_promo_boxes_settings'
	)));

    for ( $i = 1; $i <= 3; $i ++ ) {
        $wp_customize->add_section( "rebeccafood_section_promo_box_{$i}", array(
            'title' => __( 'Promo Box', 'rebeccafood' ) . " {$i}",
            'panel' => 'rebeccafood_promo_boxes_panel'
        ) );

        $wp_customize->add_setting( "rebeccafood_promo_box_title_{$i}", array(
            'default' => __( 'Box', 'rebeccafood' ) . " {$i}",
            'sanitize_callback' => 'sanitize_text_field'
        ) );
        
        $wp_customize->add_setting( "rebeccafood_promo_box_link_{$i}", array(
            'default' => '',
            'sanitize_callback' => 'esc_url_raw'
        ) );
        
        $wp_customize->add_setting( "rebeccafood_promo_box_image_{$i}", array(
            'default' => '',
            'sanitize_callback' => 'esc_url_raw'
        ) );
        
        $wp_customize->add_control( new WP_Customize_Control( $wp_customize, "rebeccafood_promo_box_title_{$i}", array(
    	   'label'     => __( 'Title', 'rebeccafood' ),
    	   'section'   => "rebeccafood_section_promo_box_{$i}"
    	)));
        
        $wp_customize->add_control( new WP_Customize_Control( $wp_customize, "rebeccafood_promo_box_link_{$i}", array(
    	   'label'     => __( 'Link(URL)', 'rebeccafood' ),
    	   'section'   => "rebeccafood_section_promo_box_{$i}"
    	)));
        
        $wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, "rebeccafood_promo_box_image_{$i}", array(
    	   'label'     => __( 'Background Image', 'rebeccafood' ),
    	   'section'   => "rebeccafood_section_promo_box_{$i}"
    	)));
    }

    /**
     * Footer settings
     */
    $wp_customize->add_section( 'rebeccafood_section_footer', array( 'title' => __( 'Site Footer', 'rebeccafood' ) ) );

    $wp_customize->add_setting( 'rebeccafood_footer_copyright', array( 'default' => __( 'Proudly powered by AZ-Theme.Net', 'rebeccafood' ), 'sanitize_callback' => 'wp_kses_post' ));
    $wp_customize->add_setting( 'rebeccafood_footer_bg_color', array( 'default' => '#28d631', 'sanitize_callback' => 'sanitize_hex_color' ) );
    $wp_customize->add_setting( 'rebeccafood_footer_text_color', array( 'default' => '#363636', 'sanitize_callback' => 'sanitize_hex_color' ) );
    $wp_customize->add_setting( 'rebeccafood_footer_text_link_color', array( 'default' => '#fff', 'sanitize_callback' => 'sanitize_hex_color' ) );
    $wp_customize->add_setting( 'rebeccafood_footer_icon_color', array( 'default' => '#212121', 'sanitize_callback' => 'sanitize_hex_color' ) );

    $wp_customize->add_control(
		new WP_Customize_Control(
			$wp_customize,
			'rebeccafood_footer_copyright',
			array(
				'label'      => __( 'Copyright Text', 'rebeccafood' ),
				'section'    => 'rebeccafood_section_footer',
				'type'		 => 'textarea'
			)
		)
	);

    $wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'rebeccafood_footer_bg_color',
			array(
				'label'      => __( 'Footer Background Color', 'rebeccafood' ),
				'section'    => 'rebeccafood_section_footer'
			)
		)
	);

    $wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'rebeccafood_footer_text_color',
			array(
				'label'      => __( 'Footer Text Color', 'rebeccafood' ),
				'section'    => 'rebeccafood_section_footer'
			)
		)
	);
    
    $wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'rebeccafood_footer_text_link_color',
			array(
				'label'      => __( 'Footer Text Link Color', 'rebeccafood' ),
				'section'    => 'rebeccafood_section_footer'
			)
		)
	);

    $wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'rebeccafood_footer_icon_color',
			array(
				'label'      => __( 'Footer Icon Color', 'rebeccafood' ),
				'section'    => 'rebeccafood_section_footer'
			)
		)
	);
}

add_action( 'customize_register', 'rebeccafood_theme_customizer' );

/**
 * Inline style
 */
function rebeccafood_inline_style() {
    $custom_css = '';
    /**
     * Colors
     */
    if ( get_theme_mod( 'rebeccafood_primary_color' ) ) {
        $custom_css .= '
            :root {
                --primary-color: ' . esc_attr( get_theme_mod( 'rebeccafood_primary_color' ) ) . ';
            }';
    }

    if ( get_theme_mod( 'rebeccafood_text_color' ) ) {
        $custom_css .= '
            :root{
                --text-color: ' . esc_attr( get_theme_mod( 'rebeccafood_text_color' ) ) . ';
            }';
    }

    if ( get_theme_mod( 'rebeccafood_accent_color' ) ) {
        $custom_css .= '
            :root{
                --accent-color: ' . esc_attr( get_theme_mod( 'rebeccafood_accent_color' ) ) . ';
            }';
    }

    if ( get_theme_mod( 'rebeccafood_meta_color' ) ) {
        $custom_css .= '
            :root{
                --meta-color: ' . esc_attr( get_theme_mod( 'rebeccafood_meta_color' ) ) . ';
            }';
    }

    if ( get_theme_mod( 'rebeccafood_boder_color' ) ) {
        $custom_css .= '
            :root{
                --border-color: ' . esc_attr( get_theme_mod( 'rebeccafood_boder_color' ) ) . ';
            }';
    }

    /**
     * Footer
     */
    if ( get_theme_mod( 'rebeccafood_footer_bg_color' ) ) {
        $custom_css .= '
            .credits {
                background-color: ' . esc_attr( get_theme_mod( 'rebeccafood_footer_bg_color' ) ) . ';
            }';
    }

    if ( get_theme_mod( 'rebeccafood_footer_text_color' ) ) {
        $custom_css .= '
            .copyright {
                color: ' . esc_attr( get_theme_mod( 'rebeccafood_footer_text_color' ) ) . ';
            }';
    }
    
    if ( get_theme_mod( 'rebeccafood_footer_text_link_color' ) ) {
        $custom_css .= '
            .site-footer a {
                color: ' . esc_attr( get_theme_mod( 'rebeccafood_footer_text_link_color' ) ) . ';
            }';
    }

    if ( get_theme_mod( 'rebeccafood_footer_icon_color' ) ) {
        $custom_css .= '
            .credits .social-icons a {
                color: ' . esc_attr( get_theme_mod( 'rebeccafood_footer_icon_color' ) ) . ';
            }';
    }

    wp_add_inline_style( 'rebeccafood-style', $custom_css );
}

add_action( 'wp_enqueue_scripts', 'rebeccafood_inline_style', 15 );

