<?php
/**
 * Newsletter Settings
 *
 * @package Feminine_Lifestyle_Blog
 */

function feminine_lifestyle_blog_customize_register_general_newsletter( $wp_customize ) {
    
    /** Newsletter Settings */
    $wp_customize->add_section(
        'newsletter_settings',
        array(
            'title'    => __( 'Newsletter', 'feminine-lifestyle-blog' ),
            'priority' => 60,
            'panel'    => 'homepage_settings',
        )
    );
    
    if( feminine_lifestyle_blog_is_wpforms_lite_activated() ){
		
        /** Enable Newsletter Section */
        $wp_customize->add_setting( 
            'ed_newsletter', 
            array(
                'default'           => false,
                'sanitize_callback' => 'feminine_lifestyle_blog_sanitize_checkbox'
            ) 
        );
        
        $wp_customize->add_control(
    		new Feminine_Lifestyle_Blog_Toggle_Control( 
    			$wp_customize,
    			'ed_newsletter',
    			array(
    				'section'     => 'newsletter_settings',
    				'label'	      => __( 'Newsletter Section', 'feminine-lifestyle-blog' ),
                    'description' => __( 'Enable to show Newsletter Section', 'feminine-lifestyle-blog' ),
    			)
    		)
    	);
    
        /** Newsletter Shortcode */
        $wp_customize->add_setting(
            'newsletter_shortcode',
            array(
                'default'           => '',
                'sanitize_callback' => 'wp_kses_post',
            )
        );
        
        $wp_customize->add_control(
            'newsletter_shortcode',
            array(
                'type'        => 'text',
                'section'     => 'newsletter_settings',
                'label'       => __( 'Newsletter Shortcode', 'feminine-lifestyle-blog' ),
                'description' => __( 'Enter the WP Forms Lite Shortcode. Example: [wpforms id="195"]', 'feminine-lifestyle-blog' ),
                'active_callback' => 'feminine_lifestyle_blog_ed_newsletter'
            )
        );

        /** Newsletter Section Title */
        $wp_customize->add_setting(
            'newsletter_title',
            array(
                'default'           => __( 'Sign up now!', 'feminine-lifestyle-blog' ),
                'sanitize_callback' => 'sanitize_text_field',
                'transport'         => 'postMessage'
            )
        );
        
        $wp_customize->add_control(
            'newsletter_title',
            array(
                'type'             => 'text',
                'section'          => 'newsletter_settings',
                'label'            => __( 'Section Title', 'feminine-lifestyle-blog' ),
                'active_callback' => 'feminine_lifestyle_blog_ed_newsletter'
            )
        );

        $wp_customize->selective_refresh->add_partial( 'newsletter_title', array(
            'selector'          => '.newsletter-section h2.section-title',
            'render_callback'   => 'feminine_lifestyle_blog_get_newsletter_title',
        ) );

        /** Newsletter Section Content */
        $wp_customize->add_setting(
            'newsletter_content',
            array(
                'default'           => '',
                'sanitize_callback' => 'sanitize_text_field',
                'transport'         => 'postMessage'
            )
        );
        
        $wp_customize->add_control(
            'newsletter_content',
            array(
                'type'             => 'textarea',
                'section'          => 'newsletter_settings',
                'label'            => __( 'Section Description', 'feminine-lifestyle-blog' ),
                'active_callback' => 'feminine_lifestyle_blog_ed_newsletter'
            )
        );

        $wp_customize->selective_refresh->add_partial( 'newsletter_content', array(
            'selector'          => '.newsletter-section .section-desc',
            'render_callback'   => 'feminine_lifestyle_blog_get_newsletter_content',
        ) );

        $wp_customize->add_setting(
            'newsletter_image',
            array(
                'default'           => '',
                'sanitize_callback' => 'feminine_lifestyle_blog_sanitize_number_absint',
            )
        );
        
        $wp_customize->add_control(
            new WP_Customize_Media_Control(
                $wp_customize,
                'newsletter_image',
                array(
                    'label'           => __( 'Upload an Image', 'feminine-lifestyle-blog' ),
                    'description'     => __( 'The recommended image size for this section: 572px by 570px.', 'feminine-lifestyle-blog'),
                    'section'         => 'newsletter_settings',
                    'active_callback' => 'feminine_lifestyle_blog_ed_newsletter',
                    'mime_type'       => 'image',
                )
            )
        );

	} else {
		$wp_customize->add_setting(
			'newsletter_recommend',
			array(
				'sanitize_callback' => 'wp_kses_post',
			)
		);

		$wp_customize->add_control(
			new Feminine_Lifestyle_Blog_Plugin_Recommend_Control(
				$wp_customize,
				'newsletter_recommend',
				array(
					'section'     => 'newsletter_settings',
					'label'       => __( 'Newsletter Shortcode', 'feminine-lifestyle-blog' ),
					'capability'  => 'install_plugins',
					'plugin_slug' => 'wpforms-lite',//This is the slug of recommended plugin.
					'description' => sprintf( __( 'Please install and activate the recommended plugin %1$sWPForms Lite%2$s. After that option related with this section will be visible.', 'feminine-lifestyle-blog' ), '<strong>', '</strong>' ),
				)
			)
		);
	}    
}
add_action( 'customize_register', 'feminine_lifestyle_blog_customize_register_general_newsletter' );