<?php
/**
 * Adore Themes Customizer
 *
 * @package Nostalgic Blog
 *
 * Instagram Section
 */

$wp_customize->add_section(
	'nostalgic_blog_instagram_section',
	array(
		'title' => esc_html__( 'Instagram Section', 'nostalgic-blog' ),
		'panel' => 'nostalgic_blog_frontpage_panel',
	)
);

// Instagram Section section enable settings.
$wp_customize->add_setting(
	'nostalgic_blog_instagram_section_enable',
	array(
		'default'           => false,
		'sanitize_callback' => 'nostalgic_blog_sanitize_checkbox',
	)
);

$wp_customize->add_control(
	new Nostalgic_Blog_Toggle_Checkbox_Custom_control(
		$wp_customize,
		'nostalgic_blog_instagram_section_enable',
		array(
			'label'    => esc_html__( 'Enable Instagram Section', 'nostalgic-blog' ),
			'type'     => 'checkbox',
			'settings' => 'nostalgic_blog_instagram_section_enable',
			'section'  => 'nostalgic_blog_instagram_section',
		)
	)
);

// Instagram text label settings.
$wp_customize->add_setting(
	'nostalgic_blog_instagram_text_label',
	array(
		'default'           => __( 'Follow Us on', 'nostalgic-blog' ),
		'sanitize_callback' => 'sanitize_text_field',
	)
);

$wp_customize->add_control(
	'nostalgic_blog_instagram_text_label',
	array(
		'label'           => esc_html__( 'Instagram Text Label', 'nostalgic-blog' ),
		'section'         => 'nostalgic_blog_instagram_section',
		'active_callback' => 'nostalgic_blog_if_instagram_enabled',
	)
);

// Abort if selective refresh is not available.
if ( isset( $wp_customize->selective_refresh ) ) {
	$wp_customize->selective_refresh->add_partial(
		'nostalgic_blog_instagram_text_label',
		array(
			'selector'            => '.instagram-section .instagram-head h3',
			'settings'            => 'nostalgic_blog_instagram_text_label',
			'container_inclusive' => false,
			'fallback_refresh'    => true,
			'render_callback'     => 'nostalgic_blog_instagram_text_label_text_partial',
		)
	);
}

// Instagram Username settings.
$wp_customize->add_setting(
	'nostalgic_blog_instagram_username',
	array(
		'default'           => __( 'Instagram', 'nostalgic-blog' ),
		'sanitize_callback' => 'sanitize_text_field',
	)
);

$wp_customize->add_control(
	'nostalgic_blog_instagram_username',
	array(
		'label'           => esc_html__( 'Instagram Username', 'nostalgic-blog' ),
		'section'         => 'nostalgic_blog_instagram_section',
		'active_callback' => 'nostalgic_blog_if_instagram_enabled',
	)
);

// Instagram Section - User Url.
$wp_customize->add_setting(
	'nostalgic_blog_instagram_user_link',
	array(
		'default'           => '#',
		'sanitize_callback' => 'esc_url_raw',
	)
);

$wp_customize->add_control(
	'nostalgic_blog_instagram_user_link',
	array(
		'label'           => esc_html__( 'Instagram User Link', 'nostalgic-blog' ),
		'section'         => 'nostalgic_blog_instagram_section',
		'settings'        => 'nostalgic_blog_instagram_user_link',
		'type'            => 'url',
		'active_callback' => 'nostalgic_blog_if_instagram_enabled',
	)
);

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

	// Instagram image.
	$wp_customize->add_setting(
		'nostalgic_blog_instagram_image_' . $i,
		array(
			'default'           => '',
			'sanitize_callback' => 'nostalgic_blog_sanitize_image',
		)
	);

	$wp_customize->add_control(
		new WP_Customize_Image_Control(
			$wp_customize,
			'nostalgic_blog_instagram_image_' . $i,
			array(
				'label'           => sprintf( esc_html__( 'Image %d', 'nostalgic-blog' ), $i ),
				'section'         => 'nostalgic_blog_instagram_section',
				'settings'        => 'nostalgic_blog_instagram_image_' . $i,
				'active_callback' => 'nostalgic_blog_if_instagram_enabled',
			)
		)
	);

}

/*========================Active Callback==============================*/
function nostalgic_blog_if_instagram_enabled( $control ) {
	return $control->manager->get_setting( 'nostalgic_blog_instagram_section_enable' )->value();
}

/*========================Partial Refresh==============================*/
if ( ! function_exists( 'nostalgic_blog_instagram_text_label_text_partial' ) ) :
	// Title.
	function nostalgic_blog_instagram_text_label_text_partial() {
		return esc_html( get_theme_mod( 'nostalgic_blog_instagram_text_label' ) );
	}
endif;

/*========================Number Range Validation==============================*/
if ( ! function_exists( 'nostalgic_blog_validation_instagram_count' ) ) :
	function nostalgic_blog_validation_instagram_count( $validity, $value ) {
		$value = intval( $value );
		if ( empty( $value ) || ! is_numeric( $value ) ) {
			$validity->add( 'required', esc_html__( 'You must supply a valid number.', 'nostalgic-blog' ) );
		} elseif ( $value < 1 ) {
			$validity->add( 'min_no_of_posts', esc_html__( 'Minimum no of posts is 1', 'nostalgic-blog' ) );
		} elseif ( $value > 7 ) {
			$validity->add( 'max_no_of_posts', esc_html__( 'Maximum no of posts is 7', 'nostalgic-blog' ) );
		}
		return $validity;
	}
endif;
