<?php
/**
 * Adore Themes Customizer
 *
 * @package Adore News
 *
 * Subscription Section
 */

$wp_customize->add_section(
	'adore_news_subscription_section',
	array(
		'title'       => esc_html__( 'Subscription Section', 'adore-news' ),
		'description' => sprintf( esc_html__( '%1$sJetpack%2$s should be active and site should be connected to WordPress.com for this section to work.', 'adore-news' ), '<a target="_blank" href="https://wordpress.org/plugins/jetpack">', '</a>' ),
		'panel'       => 'adore_news_frontpage_panel',
		'priority'    => 50,
	)
);

// Subscription section enable settings.
$wp_customize->add_setting(
	'adore_news_subscription_section_enable',
	array(
		'default'           => false,
		'sanitize_callback' => 'adore_news_sanitize_checkbox',
	)
);
$wp_customize->add_control(
	new Adore_News_Toggle_Checkbox_Custom_control(
		$wp_customize,
		'adore_news_subscription_section_enable',
		array(
			'label'    => esc_html__( 'Enable Subscription Section', 'adore-news' ),
			'type'     => 'checkbox',
			'section'  => 'adore_news_subscription_section',
			'settings' => 'adore_news_subscription_section_enable',
		)
	)
);

// Subscription bg image.
$wp_customize->add_setting(
	'adore_news_subscription_bg_image',
	array(
		'default'           => '',
		'sanitize_callback' => 'adore_news_sanitize_image',
	)
);

$wp_customize->add_control(
	new WP_Customize_Image_Control(
		$wp_customize,
		'adore_news_subscription_bg_image',
		array(
			'label'           => esc_html__( 'Background Image', 'adore-news' ),
			'section'         => 'adore_news_subscription_section',
			'settings'        => 'adore_news_subscription_bg_image',
			'active_callback' => 'adore_news_if_subscription_enabled',
		)
	)
);

// Subscription title settings.
$wp_customize->add_setting(
	'adore_news_subscription_title',
	array(
		'default'           => __( 'Subscribe To Our Newsletter', 'adore-news' ),
		'sanitize_callback' => 'sanitize_text_field',
	)
);

$wp_customize->add_control(
	'adore_news_subscription_title',
	array(
		'label'           => esc_html__( 'Title', 'adore-news' ),
		'section'         => 'adore_news_subscription_section',
		'active_callback' => 'adore_news_if_subscription_enabled',
		'settings'        => 'adore_news_subscription_title',
	)
);

$wp_customize->selective_refresh->add_partial(
	'adore_news_subscription_title',
	array(
		'selector'            => '.newsletter-section h3.newsletter-title',
		'settings'            => 'adore_news_subscription_title',
		'container_inclusive' => false,
		'fallback_refresh'    => true,
		'render_callback'     => 'adore_news_subscription_partial_title',
	)
);

// Subscription subtitle settings.
$wp_customize->add_setting(
	'adore_news_subscription_subtitle',
	array(
		'default'           => '',
		'sanitize_callback' => 'sanitize_text_field',
	)
);

$wp_customize->add_control(
	'adore_news_subscription_subtitle',
	array(
		'label'           => esc_html__( 'Subtitle', 'adore-news' ),
		'section'         => 'adore_news_subscription_section',
		'active_callback' => 'adore_news_if_subscription_enabled',
		'settings'        => 'adore_news_subscription_subtitle',
	)
);

$wp_customize->selective_refresh->add_partial(
	'adore_news_subscription_subtitle',
	array(
		'selector'            => '.newsletter-section .text-section p',
		'settings'            => 'adore_news_subscription_subtitle',
		'container_inclusive' => false,
		'fallback_refresh'    => true,
		'render_callback'     => 'adore_news_subscription_partial_subtitle',
	)
);

// Subscription button settings.
$wp_customize->add_setting(
	'adore_news_subscription_button_label',
	array(
		'default'           => 'Subscribe Now',
		'sanitize_callback' => 'sanitize_text_field',
	)
);

$wp_customize->add_control(
	'adore_news_subscription_button_label',
	array(
		'label'           => esc_html__( 'Button Label', 'adore-news' ),
		'section'         => 'adore_news_subscription_section',
		'active_callback' => 'adore_news_if_subscription_enabled',
		'settings'        => 'adore_news_subscription_button_label',
	)
);

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

/*========================Partial Refresh==============================*/
if ( ! function_exists( 'adore_news_subscription_partial_title' ) ) :
	// Title.
	function adore_news_subscription_partial_title() {
		return esc_html( get_theme_mod( 'adore_news_subscription_title' ) );
	}
endif;
if ( ! function_exists( 'adore_news_subscription_partial_subtitle' ) ) :
	// Subtitle.
	function adore_news_subscription_partial_subtitle() {
		return esc_html( get_theme_mod( 'adore_news_subscription_subtitle' ) );
	}
endif;
