<?php
/**
 * Customizer functionality for the Slider section.
 *
 * @package Hestia
 * @since Hestia 1.0
 */

/**
 * Hook controls for Slider section to Customizer.
 *
 * @since Hestia 1.0
 * @modified 1.1.30
 */
function hestia_slider_customize_register( $wp_customize ) {

	$selective_refresh = isset( $wp_customize->selective_refresh ) ? true : false;
	$wp_customize->add_section(
		'hestia_slider', array(
			'title'    => esc_html__( 'Slider', 'hestia-pro' ),
			'panel'    => 'hestia_frontpage_sections',
			'priority' => 5,
		)
	);

	if ( class_exists( 'Hestia_Customize_Control_Radio_Image' ) ) {
		$wp_customize->add_setting(
			'hestia_slider_alignment', array(
				'default'           => 'center',
				'sanitize_callback' => 'hestia_sanitize_alignment_options',
				'transport'         => $selective_refresh ? 'postMessage' : 'refresh',
			)
		);

		$wp_customize->add_control(
			new Hestia_Customize_Control_Radio_Image(
				$wp_customize, 'hestia_slider_alignment', array(
					'label'    => esc_html__( 'Layout', 'hestia-pro' ),
					'priority' => 5,
					'section'  => 'hestia_slider',
					'choices'  => array(
						'left'   => array(
							'url' => trailingslashit( get_template_directory_uri() ) . 'inc/customizer-radio-image/img/slider-layout-1.png',
						),
						'center' => array(
							'url' => trailingslashit( get_template_directory_uri() ) . 'inc/customizer-radio-image/img/slider-layout-2.png',
						),
						'right'  => array(
							'url' => trailingslashit( get_template_directory_uri() ) . 'inc/customizer-radio-image/img/slider-layout-3.png',
						),
					),
				)
			)
		);
	}

	if ( class_exists( 'Hestia_Repeater' ) ) {
		$slider_default = hestia_get_slider_default();

		$wp_customize->add_setting(
			'hestia_slider_content', array(
				'sanitize_callback' => 'hestia_repeater_sanitize',
				'default'           => json_encode( $slider_default ),
				'transport'         => $selective_refresh ? 'postMessage' : 'refresh',
			)
		);

		$wp_customize->add_control(
			new Hestia_Repeater(
				$wp_customize, 'hestia_slider_content', array(
					'label'                                => esc_html__( 'Slider Content', 'hestia-pro' ),
					'section'                              => 'hestia_slider',
					'priority'                             => 10,
					'item_name'                            => esc_html__( 'Slide', 'hestia-pro' ),
					'add_field_label'                      => esc_html__( 'Add new Slide', 'hestia-pro' ),
					'customizer_repeater_image_control'    => true,
					'customizer_repeater_title_control'    => true,
					'customizer_repeater_subtitle_control' => true,
					'customizer_repeater_text_control'     => true,
					'customizer_repeater_link_control'     => true,
					'customizer_repeater_text2_control'    => true,
					'customizer_repeater_link2_control'    => true,
					'customizer_repeater_color_control'    => true,
					'customizer_repeater_color2_control'   => true,
				)
			)
		);
	}

	/**
	 * Move video controls in header.
	 */
	$video_bg = $wp_customize->get_control( 'header_video' );
	if ( ! empty( $video_bg ) ) {
		$video_bg->section  = 'hestia_slider';
		$video_bg->priority = 50;
	}
	$external_video_bg = $wp_customize->get_control( 'external_header_video' );
	if ( ! empty( $external_video_bg ) ) {
		$external_video_bg->section  = 'hestia_slider';
		$external_video_bg->priority = 55;
	}
	$header_image = $wp_customize->get_setting( 'header_image' );
	if ( ! empty( $header_image ) ) {
		$header_image->transport = 'refresh';
	}

}

add_action( 'customize_register', 'hestia_slider_customize_register' );


add_action( 'after_setup_theme', 'hestia_get_slider_default', 11 );


/**
 * Add selective refresh for slider section controls.
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 * @since 1.1.31
 * @access public
 */
function hestia_register_slider_partials( $wp_customize ) {
	// Abort if selective refresh is not available.
	if ( ! isset( $wp_customize->selective_refresh ) ) {
		return;
	}

	$wp_customize->selective_refresh->add_partial(
		'hestia_slider_content', array(
			'selector'        => '#carousel-hestia-generic',
			'settings'        => 'hestia_slider_content',
			'render_callback' => 'hestia_slider_callback',
		)
	);

	$wp_customize->selective_refresh->add_partial(
		'hestia_slider_alignment', array(
			'selector'        => '#carousel-hestia-generic',
			'settings'        => 'hestia_slider_alignment',
			'render_callback' => 'hestia_slider_callback',
		)
	);
}
add_action( 'customize_register', 'hestia_register_slider_partials' );

/**
 * Callback function for slider content selective refresh.
 *
 * @since 1.1.31
 * @access public
 */
function hestia_slider_callback() {
	hestia_slider( true );
}

/**
 * Function to get color from accent color into repeater.
 *
 * @since 1.1.41
 * @access public
 */
function hestia_slider_update_colors() {
	$migrate = get_option( 'hestia_update_slider_colors' );
	if ( isset( $migrate ) && false == $migrate ) {
		$hestia_slider_content = get_theme_mod( 'hestia_slider_content' );
		if ( ! empty( $hestia_slider_content ) ) {
			$color_accent = get_theme_mod( 'accent_color', apply_filters( 'hestia_accent_color_default', '#e91e63' ) );
			if ( ! empty( $color_accent ) ) {
				$hestia_slider_content_decoded = json_decode( $hestia_slider_content, true );
				foreach ( $hestia_slider_content_decoded as $key => $slide ) {
					$hestia_slider_content_decoded[ $key ]['color'] = $color_accent;
				}
				set_theme_mod( 'hestia_slider_content', json_encode( $hestia_slider_content_decoded ) );
			}
		}
		update_option( 'hestia_update_slider_colors', true );
	}
}
add_action( 'after_setup_theme', 'hestia_slider_update_colors' );
