<?php
/**
 * Magenta Plus Theme Customizer
 *
 * @package Magenta Plus
 */

function magenta_plus_customize_register( $wp_customize ) {
	$wp_customize->get_setting( 'blogname' )->transport         = 'postMessage';
	$wp_customize->get_setting( 'blogdescription' )->transport  = 'postMessage';
	$wp_customize->get_setting( 'header_textcolor' )->transport = 'postMessage';

	if ( isset( $wp_customize->selective_refresh ) ) {
		$wp_customize->selective_refresh->add_partial(
			'blogname',
			array(
				'selector'        => '.site-title a',
				'render_callback' => 'magenta_plus_customize_partial_blogname',
			)
		);
		$wp_customize->selective_refresh->add_partial(
			'blogdescription',
			array(
				'selector'        => '.site-description',
				'render_callback' => 'magenta_plus_customize_partial_blogdescription',
			)
		);
	}

	/*
    * Theme Options Panel
    */
	$wp_customize->add_panel('magenta_plus_panel', array(
		'priority' => 25,
		'capability' => 'edit_theme_options',
		'title' => __('Magenta Plus Theme Options', 'magenta-plus'),
	));

	/*
	* Customizer top header section
	*/

	$wp_customize->add_setting(
		'magenta_plus_site_title_text',
		array(
			'capability'        => 'edit_theme_options',
			'transport'         => 'refresh',
			'default'           => 1,
			'sanitize_callback' => 'magenta_plus_sanitize_checkbox',
		)
	);

	$wp_customize->add_control(
		'magenta_plus_site_title_text',
		array(
			'label'       => __('Enable Title', 'magenta-plus'),
			'description' => __('Enable or Disable Title from the site', 'magenta-plus'),
			'section'     => 'title_tagline',
			'type'        => 'checkbox',
		)
	);

	$wp_customize->add_setting(
		'magenta_plus_site_tagline_text',
		array(
			'capability'        => 'edit_theme_options',
			'transport'         => 'refresh',
			'default'           => 0,
			'sanitize_callback' => 'magenta_plus_sanitize_checkbox',
		)
	);

	$wp_customize->add_control(
		'magenta_plus_site_tagline_text',
		array(
			'label'       => __('Enable Tagline', 'magenta-plus'),
			'description' => __('Enable or Disable Tagline from the site', 'magenta-plus'),
			'section'     => 'title_tagline',
			'type'        => 'checkbox',
		)
	);

		$wp_customize->add_setting(
		'magenta_plus_logo_width',
		array(
			'capability'        => 'edit_theme_options',
			'transport'         => 'refresh',
			'default'           => '150',
			'sanitize_callback' => 'sanitize_text_field',
		)
	);

	$wp_customize->add_control(
		'magenta_plus_logo_width',
		array(
			'label'       => __('Logo Width in PX', 'magenta-plus'),
			'section'     => 'title_tagline',
			'type'        => 'number',
			'input_attrs' => array(
	            'min' => 100,
	             'max' => 300,
	             'step' => 1,
	         ),
		)
	);

	/* WooCommerce custom settings */

	$wp_customize->add_section('woocommerce_custom_settings', array(
		'priority'       => 5,
		'capability'     => 'edit_theme_options',
		'theme_supports' => '',
		'title'          => __('WooCommerce Custom Settings', 'magenta-plus'),
		'panel'       => 'woocommerce',
	));

	$wp_customize->add_setting('magenta_plus_woocommerce_product_sale',array(
		'capability'        => 'edit_theme_options',
		'transport'         => 'refresh',
        'default'           => 'Right',
        'sanitize_callback' => 'magenta_plus_sanitize_choices'
    ));
    $wp_customize->add_control('magenta_plus_woocommerce_product_sale',array(
        'label'       => esc_html__( 'Woocommerce Product Sale Positions','magenta-plus' ),
        'type' => 'select',
        'section' => 'woocommerce_custom_settings',
        'choices' => array(
            'Right' => __('Right','magenta-plus'),
            'Left' => __('Left','magenta-plus'),
            'Center' => __('Center','magenta-plus')
        ),
    ) );

	/*Additional Options*/
	$wp_customize->add_section('magenta_plus_additional_section', array(
		'priority'       => 5,
		'capability'     => 'edit_theme_options',
		'theme_supports' => '',
		'title'          => __('Additional Options', 'magenta-plus'),
		'panel'       => 'magenta_plus_panel',
	));

	/*Main Slider Enable Option*/
	$wp_customize->add_setting(
		'magenta_plus_enable_preloader',
		array(
			'capability'        => 'edit_theme_options',
			'transport'         => 'refresh',
			'default'           => 0,
			'sanitize_callback' => 'magenta_plus_sanitize_checkbox',
		)
	);

	$wp_customize->add_control(
		'magenta_plus_enable_preloader',
		array(
			'label'       => __('Enable Preloader', 'magenta-plus'),
			'description' => __('Checked to show preloader', 'magenta-plus'),
			'section'     => 'magenta_plus_additional_section',
			'type'        => 'checkbox',
		)
	);
	
	/*Archive Post Options*/
	$wp_customize->add_section('magenta_plus_blog_post_section', array(
		'priority'       => 5,
		'capability'     => 'edit_theme_options',
		'theme_supports' => '',
		'title'          => __('Blog Page Options', 'magenta-plus'),
		'panel'       => 'magenta_plus_panel',
	));

	$wp_customize->add_setting('magenta_plus_enable_blog_post_image',array(
		'capability'        => 'edit_theme_options',
		'transport'         => 'refresh',
		'default'           => 1,
		'sanitize_callback' => 'magenta_plus_sanitize_checkbox',
	));
	$wp_customize->add_control('magenta_plus_enable_blog_post_image',array(
		'label'       => __('Enable Blog Post Image', 'magenta-plus'),
		'description' => __('Checked To Show Blog Post Image', 'magenta-plus'),
		'section'     => 'magenta_plus_blog_post_section',
		'type'        => 'checkbox',
	));	

	/*Top Header Options*/
	$wp_customize->add_section('magenta_plus_header_section', array(
		'priority'       => 5,
		'capability'     => 'edit_theme_options',
		'theme_supports' => '',
		'title'          => __('Top Header Options', 'magenta-plus'),
		'panel'       => 'magenta_plus_panel',
	));

	/*Top header section enable*/
	$wp_customize->add_setting(
		'magenta_plus_enable_top_header',
		array(
			'capability'        => 'edit_theme_options',
			'transport'         => 'refresh',
			'default'           => 1,
			'sanitize_callback' => 'magenta_plus_sanitize_checkbox',
		)
	);

	$wp_customize->add_control(
		'magenta_plus_enable_top_header',
		array(
			'label'       => __('Enable Top Header', 'magenta-plus'),
			'description' => __('Checked to show the top header section.', 'magenta-plus'),
			'section'     => 'magenta_plus_header_section',
			'type'        => 'checkbox',
		)
	);

	/*Phone Number*/
	$wp_customize->add_setting(
		'magenta_plus_phone_number_option',
		array(
			'capability'        => 'edit_theme_options',
			'transport'         => 'refresh',
			'default'           => '',
			'sanitize_callback' => 'sanitize_text_field',
		)
	);

	$wp_customize->add_control(
		'magenta_plus_phone_number_option',
		array(
			'label'       => __('Edit Phone Number', 'magenta-plus'),
			'section'     => 'magenta_plus_header_section',
			'type'        => 'text',
		)
	);

	/*Email Address*/
	$wp_customize->add_setting(
		'magenta_plus_email_address_option',
		array(
			'capability'        => 'edit_theme_options',
			'transport'         => 'refresh',
			'default'           => '',
			'sanitize_callback' => 'sanitize_text_field',
		)
	);

	$wp_customize->add_control(
		'magenta_plus_email_address_option',
		array(
			'label'       => __('Edit Email Address', 'magenta-plus'),
			'section'     => 'magenta_plus_header_section',
			'type'        => 'text',
		)
	);

	/*Enable Social Menu at the top*/
	$wp_customize->add_setting(
		'magenta_plus_enable_top_social_menu',
		array(
			'capability'        => 'edit_theme_options',
			'transport'         => 'refresh',
			'default'           => 1,
			'sanitize_callback' => 'magenta_plus_sanitize_checkbox',
		)
	);

	$wp_customize->add_control(
		'magenta_plus_enable_top_social_menu',
		array(
			'label'       => __('Enable Top Social Menu', 'magenta-plus'),
			'description' => __('Checked to show the top social menu. Go to Dashboard >> Appearance >> Menus >> Create New Menu >> Add Custom Link >> Add Social Menu >> Checked Social Menu >> Save Menu.', 'magenta-plus'),
			'section'     => 'magenta_plus_header_section',
			'type'        => 'checkbox',
		)
	);

	/*Header Button Text*/
	$wp_customize->add_setting(
		'magenta_plus_header_button_text',
		array(
			'capability'        => 'edit_theme_options',
			'transport'         => 'refresh',
			'default'           => '',
			'sanitize_callback' => 'sanitize_text_field',
		)
	);

	$wp_customize->add_control(
		'magenta_plus_header_button_text',
		array(
			'label'       => __('Edit Button Text ', 'magenta-plus'),
			'description' => __('Edit the header button text.', 'magenta-plus'),
			'section'     => 'magenta_plus_header_section',
			'type'        => 'text',
		)
	);

	/*Header Button URL*/
	$wp_customize->add_setting(
		'magenta_plus_header_button_link',
		array(
			'capability'        => 'edit_theme_options',
			'transport'         => 'refresh',
			'default'           => '',
			'sanitize_callback' => 'esc_url_raw',
		)
	);

	$wp_customize->add_control(
		'magenta_plus_header_button_link',
		array(
			'label'       => __('Edit Button URL ', 'magenta-plus'),
			'description' => __('Edit the header button url.', 'magenta-plus'),
			'section'     => 'magenta_plus_header_section',
			'type'        => 'url',
		)
	);

	/*
	* Customizer main slider section
	*/
	/*Main Slider Options*/
	$wp_customize->add_section('magenta_plus_slider_section', array(
		'priority'       => 5,
		'capability'     => 'edit_theme_options',
		'theme_supports' => '',
		'title'          => __('Main Slider Options', 'magenta-plus'),
		'panel'       => 'magenta_plus_panel',
	));

	/*Main Slider Enable Option*/
	$wp_customize->add_setting(
		'magenta_plus_enable_slider',
		array(
			'capability'        => 'edit_theme_options',
			'transport'         => 'refresh',
			'default'           => 1,
			'sanitize_callback' => 'magenta_plus_sanitize_checkbox',
		)
	);

	$wp_customize->add_control(
		'magenta_plus_enable_slider',
		array(
			'label'       => __('Enable Main Slider', 'magenta-plus'),
			'description' => __('Checked to show the main slider', 'magenta-plus'),
			'section'     => 'magenta_plus_slider_section',
			'type'        => 'checkbox',
		)
	);

	$wp_customize->add_setting(
		'magenta_plus_slide_number',
		array(
			'capability'        => 'edit_theme_options',
			'transport'         => 'refresh',
			'sanitize_callback' => 'sanitize_text_field',
		)
	);

	$wp_customize->add_control(
		'magenta_plus_slide_number',
		array(
			'label'       => __('Number of slide to show', 'magenta-plus'),
			'section'     => 'magenta_plus_slider_section',
			'type'        => 'number',
			'input_attrs' => array(
				'min' => 1,     
				'max' => 3,   
			)
		)
	);

	$slide_count = (int)get_theme_mod('magenta_plus_slide_number');

	for ($m=1; $m <= $slide_count; $m++) { 

		/*Main Slider Image*/
		$wp_customize->add_setting(
			'magenta_plus_slider_image'.$m,
			array(
				'capability'    => 'edit_theme_options',
		        'default'       => '',
		        'transport'     => 'postMessage',
		        'sanitize_callback' => 'esc_url_raw',
	    	)
	    );

		$wp_customize->add_control( 
			new WP_Customize_Image_Control( $wp_customize, 
				'magenta_plus_slider_image'.$m, 
				array(
			        'label' => __('Edit Slider Image ', 'magenta-plus') .$m,
			        'description' => __('Edit the slider image.', 'magenta-plus'),
			        'section' => 'magenta_plus_slider_section',
				)
			)
		);

		/*Main Slider Heading*/
		$wp_customize->add_setting(
			'magenta_plus_slider_heading'.$m,
			array(
				'capability'        => 'edit_theme_options',
				'transport'         => 'refresh',
				'default'           => '',
				'sanitize_callback' => 'sanitize_text_field',
			)
		);

		$wp_customize->add_control(
			'magenta_plus_slider_heading'.$m,
			array(
				'label'       => __('Edit Heading Text ', 'magenta-plus') .$m,
				'description' => __('Edit the slider heading text.', 'magenta-plus'),
				'section'     => 'magenta_plus_slider_section',
				'type'        => 'text',
			)
		);

		/*Main Slider Content*/
		$wp_customize->add_setting(
			'magenta_plus_slider_text'.$m,
			array(
				'capability'        => 'edit_theme_options',
				'transport'         => 'refresh',
				'default'           => '',
				'sanitize_callback' => 'sanitize_text_field',
			)
		);

		$wp_customize->add_control(
			'magenta_plus_slider_text'.$m,
			array(
				'label'       => __('Edit Content Text ', 'magenta-plus') .$m,
				'description' => __('Edit the slider content text.', 'magenta-plus'),
				'section'     => 'magenta_plus_slider_section',
				'type'        => 'text',
			)
		);

		/*Main Slider Button1 Text*/
		$wp_customize->add_setting(
			'magenta_plus_slider_button1_text'.$m,
			array(
				'capability'        => 'edit_theme_options',
				'transport'         => 'refresh',
				'default'           => '',
				'sanitize_callback' => 'sanitize_text_field',
			)
		);

		$wp_customize->add_control(
			'magenta_plus_slider_button1_text'.$m,
			array(
				'label'       => __('Edit Button #1 Text ', 'magenta-plus') .$m,
				'description' => __('Edit the slider button text.', 'magenta-plus'),
				'section'     => 'magenta_plus_slider_section',
				'type'        => 'text',
			)
		);

		/*Main Slider Button1 URL*/
		$wp_customize->add_setting(
			'magenta_plus_slider_button1_link'.$m,
			array(
				'capability'        => 'edit_theme_options',
				'transport'         => 'refresh',
				'default'           => '',
				'sanitize_callback' => 'esc_url_raw',
			)
		);

		$wp_customize->add_control(
			'magenta_plus_slider_button1_link'.$m,
			array(
				'label'       => __('Edit Button #1 URL ', 'magenta-plus') .$m,
				'description' => __('Edit the slider button url.', 'magenta-plus'),
				'section'     => 'magenta_plus_slider_section',
				'type'        => 'url',
			)
		);

		/*Main Slider Button2 Text*/
		$wp_customize->add_setting(
			'magenta_plus_slider_button2_text'.$m,
			array(
				'capability'        => 'edit_theme_options',
				'transport'         => 'refresh',
				'default'           => '',
				'sanitize_callback' => 'sanitize_text_field',
			)
		);

		$wp_customize->add_control(
			'magenta_plus_slider_button2_text'.$m,
			array(
				'label'       => __('Edit Button #2 Text ', 'magenta-plus') .$m,
				'description' => __('Edit the slider button text.', 'magenta-plus'),
				'section'     => 'magenta_plus_slider_section',
				'type'        => 'text',
			)
		);

		/*Main Slider Button2 URL*/
		$wp_customize->add_setting(
			'magenta_plus_slider_button2_link'.$m,
			array(
				'capability'        => 'edit_theme_options',
				'transport'         => 'refresh',
				'default'           => '',
				'sanitize_callback' => 'esc_url_raw',
			)
		);

		$wp_customize->add_control(
			'magenta_plus_slider_button2_link'.$m,
			array(
				'label'       => __('Edit Button #2 URL ', 'magenta-plus') .$m,
				'description' => __('Edit the slider button url.', 'magenta-plus'),
				'section'     => 'magenta_plus_slider_section',
				'type'        => 'url',
			)
		);

	}

	/*
	* Customizer main expert section
	*/
	/*Main Expert Options*/
	$wp_customize->add_section('magenta_plus_expert_section', array(
		'priority'       => 5,
		'capability'     => 'edit_theme_options',
		'theme_supports' => '',
		'title'          => __('Main Expert Options', 'magenta-plus'),
		'panel'       => 'magenta_plus_panel',
	));

	/*Main Expert Enable Option*/
	$wp_customize->add_setting(
		'magenta_plus_enable_expert',
		array(
			'capability'        => 'edit_theme_options',
			'transport'         => 'refresh',
			'default'           => 1,
			'sanitize_callback' => 'magenta_plus_sanitize_checkbox',
		)
	);

	$wp_customize->add_control(
		'magenta_plus_enable_expert',
		array(
			'label'       => __('Enable Expert Section', 'magenta-plus'),
			'description' => __('Checked to show the main expert', 'magenta-plus'),
			'section'     => 'magenta_plus_expert_section',
			'type'        => 'checkbox',
		)
	);

	/*Main Heading*/
	$wp_customize->add_setting(
		'magenta_plus_expert_heading',
		array(
			'capability'        => 'edit_theme_options',
			'transport'         => 'refresh',
			'default'           => '',
			'sanitize_callback' => 'sanitize_text_field',
		)
	);

	$wp_customize->add_control(
		'magenta_plus_expert_heading',
		array(
			'label'       => __('Edit Main Heading ', 'magenta-plus'),
			'description' => __('Edit the main section title.', 'magenta-plus'),
			'section'     => 'magenta_plus_expert_section',
			'type'        => 'text',
		)
	);

	/*Main Text*/
	$wp_customize->add_setting(
		'magenta_plus_expert_text',
		array(
			'capability'        => 'edit_theme_options',
			'transport'         => 'refresh',
			'default'           => '',
			'sanitize_callback' => 'sanitize_text_field',
		)
	);

	$wp_customize->add_control(
		'magenta_plus_expert_text',
		array(
			'label'       => __('Edit Main Heading Text', 'magenta-plus'),
			'description' => __('Edit the main section text.', 'magenta-plus'),
			'section'     => 'magenta_plus_expert_section',
			'type'        => 'text',
		)
	);

	for ($i=1; $i <= 4; $i++) { 

		/*Main Expert Image*/
		$wp_customize->add_setting(
			'magenta_plus_expert_image'.$i,
			array(
				'capability'    => 'edit_theme_options',
		        'default'       => '',
		        'transport'     => 'postMessage',
		        'sanitize_callback' => 'esc_url_raw',
	    	)
	    );

		$wp_customize->add_control( 
			new WP_Customize_Image_Control( $wp_customize, 
				'magenta_plus_expert_image'.$i, 
				array(
			        'label' => __('Edit Expert Image ', 'magenta-plus') .$i,
			        'description' => __('Edit the expert image.', 'magenta-plus'),
			        'section' => 'magenta_plus_expert_section',
				)
			)
		);

		/*Main Expert Name*/
		$wp_customize->add_setting(
			'magenta_plus_expert_box_heading'.$i,
			array(
				'capability'        => 'edit_theme_options',
				'transport'         => 'refresh',
				'default'           => '',
				'sanitize_callback' => 'sanitize_text_field',
			)
		);

		$wp_customize->add_control(
			'magenta_plus_expert_box_heading'.$i,
			array(
				'label'       => __('Edit Name ', 'magenta-plus') .$i,
				'description' => __('Edit the expert name.', 'magenta-plus'),
				'section'     => 'magenta_plus_expert_section',
				'type'        => 'text',
			)
		);

		/*Main Expert Designation*/
		$wp_customize->add_setting(
			'magenta_plus_expert_box_text'.$i,
			array(
				'capability'        => 'edit_theme_options',
				'transport'         => 'refresh',
				'default'           => '',
				'sanitize_callback' => 'sanitize_text_field',
			)
		);

		$wp_customize->add_control(
			'magenta_plus_expert_box_text'.$i,
			array(
				'label'       => __('Edit Designation ', 'magenta-plus') .$i,
				'description' => __('Edit the expert designation.', 'magenta-plus'),
				'section'     => 'magenta_plus_expert_section',
				'type'        => 'text',
			)
		);
	}

	/*
	* Customizer Footer Section
	*/
	/*Footer Options*/
	$wp_customize->add_section('magenta_plus_footer_section', array(
		'priority'       => 5,
		'capability'     => 'edit_theme_options',
		'theme_supports' => '',
		'title'          => __('Footer Options', 'magenta-plus'),
		'panel'       => 'magenta_plus_panel',
	));

	/*Footer bg image Option*/
	$wp_customize->add_setting('magenta_plus_footer_bg_image',array(
		'capability'        => 'edit_theme_options',
		'transport'         => 'refresh',
        'default'           => '',
        'sanitize_callback' => 'esc_url_raw',
    ));
    $wp_customize->add_control( new WP_Customize_Image_Control($wp_customize,'magenta_plus_footer_bg_image',array(
        'label' => __('Footer Background Image','magenta-plus'),
        'section' => 'magenta_plus_footer_section',
        'priority' => 1,
    )));

	/*Footer Social Menu Option*/
	$wp_customize->add_setting(
		'magenta_plus_footer_social_menu',
		array(
			'capability'        => 'edit_theme_options',
			'transport'         => 'refresh',
			'default'           => 1,
			'sanitize_callback' => 'magenta_plus_sanitize_checkbox',
		)
	);

	$wp_customize->add_control(
		'magenta_plus_footer_social_menu',
		array(
			'label'       => __('Enable Footer Social Menu', 'magenta-plus'),
			'description' => __('Checked to show the footer social menu. Go to Dashboard >> Appearance >> Menus >> Create New Menu >> Add Custom Link >> Add Social Menu >> Checked Social Menu >> Save Menu.', 'magenta-plus'),
			'section'     => 'magenta_plus_footer_section',
			'type'        => 'checkbox',
		)
	);	

	/*Go To Top Option*/
	$wp_customize->add_setting(
		'magenta_plus_enable_go_to_top_option',
		array(
			'capability'        => 'edit_theme_options',
			'transport'         => 'refresh',
			'default'           => 1,
			'sanitize_callback' => 'magenta_plus_sanitize_checkbox',
		)
	);

	$wp_customize->add_control(
		'magenta_plus_enable_go_to_top_option',
		array(
			'label'       => __('Enable Go To Top', 'magenta-plus'),
			'description' => __('Checked to enable Go To Top option.', 'magenta-plus'),
			'section'     => 'magenta_plus_footer_section',
			'type'        => 'checkbox',
		)
	);

	$wp_customize->add_setting('magenta_plus_go_to_top_position',array(
        'capability'        => 'edit_theme_options',
		'transport'         => 'refresh',
		'default'           => 'Right',
        'sanitize_callback' => 'magenta_plus_sanitize_choices'
    ));
    $wp_customize->add_control('magenta_plus_go_to_top_position',array(
        'type' => 'select',
        'section' => 'magenta_plus_footer_section',
        'label' => esc_html__('Go To Top Positions','magenta-plus'),
        'choices' => array(
            'Right' => __('Right','magenta-plus'),
            'Left' => __('Left','magenta-plus'),
            'Center' => __('Center','magenta-plus')
        ),
    ) );

	/*Footer Copyright Text Enable*/
	$wp_customize->add_setting(
		'magenta_plus_copyright_option',
		array(
			'capability'        => 'edit_theme_options',
			'transport'         => 'refresh',
			'default'           => '',
			'sanitize_callback' => 'sanitize_text_field',
		)
	);

	$wp_customize->add_control(
		'magenta_plus_copyright_option',
		array(
			'label'       => __('Edit Copyright Text', 'magenta-plus'),
			'description' => __('Edit the Footer Copyright Section.', 'magenta-plus'),
			'section'     => 'magenta_plus_footer_section',
			'type'        => 'text',
		)
	);
}
add_action( 'customize_register', 'magenta_plus_customize_register' );



/**
 * Render the site title for the selective refresh partial.
 *
 * @return void
 */
function magenta_plus_customize_partial_blogname() {
	bloginfo( 'name' );
}

/**
 * Render the site tagline for the selective refresh partial.
 *
 * @return void
 */
function magenta_plus_customize_partial_blogdescription() {
	bloginfo( 'description' );
}

/**
 * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
 */
function magenta_plus_customize_preview_js() {
	wp_enqueue_script( 'magenta-plus-customizer', get_template_directory_uri() . '/js/customizer.js', array( 'customize-preview' ), MAGENTA_PLUS_VERSION, true );
}
add_action( 'customize_preview_init', 'magenta_plus_customize_preview_js' );
