<?php
$cloudpress_repeater_path = trailingslashit( CLOUDPRESS_THEME_DIR ) . '/free/inc/customizer-repeater/functions.php';
if ( file_exists( $cloudpress_repeater_path ) ) {
require_once( $cloudpress_repeater_path );
}
/**
 * Customize for taxonomy with dropdown, extend the WP customizer
 */

if ( ! class_exists( 'WP_Customize_Control' ) )
	return NULL;

function cloudpress_sections_settings( $wp_customize ){
	
$selective_refresh = isset( $wp_customize->selective_refresh ) ? 'postMessage' : 'refresh';	

/* Sections Settings */
	$wp_customize->add_panel( 'section_settings', array(
		'priority'       => 126,
		'capability'     => 'edit_theme_options',
		'title'      => esc_html__('Homepage Section Settings','cloudpress'),
	) );


	// Custom Control Button
	class Cloudpress_Editor_Customize_Control extends WP_Customize_Control {
	    public $type = 'new_menu';

	    public function render_content() {

	        $template_file = 'template-business.php';

	        $pages = get_posts(array(
	            'post_type'  => 'page',
	            'meta_key'   => '_wp_page_template',
	            'meta_value' => $template_file,
	            'posts_per_page' => 1,
	        ));

	        if ( !empty($pages) ) {
	            $page_id = $pages[0]->ID;
	            $edit_link = admin_url('post.php?post=' . $page_id . '&action=edit');
	        } else {
	            $edit_link = admin_url('edit.php?post_type=page');
	        }
	        ?>
	        <div class="cloudpress-pro-features-customizer">
	            <p>Use this button to insert Gutenberg blocks into the Business Template page.</p>
	            <a href="<?php echo esc_url($edit_link); ?>" class="cloudpress-pro-button button-primary">
	                <?php esc_html_e('Page Editor Section', 'cloudpress'); ?>
	            </a>
	        </div>
	        <?php
	    }
	}

   	$wp_customize->add_section( 'home_page_editor_section' , array(
	    'title'    => esc_html__('Gutenberg Editor Settings', 'cloudpress'),
	    'panel'    => 'section_settings',
	    'priority' => 2,
	));

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

	$wp_customize->add_control( 
	    new Cloudpress_Editor_Customize_Control( 
	        $wp_customize, 
	        'edit_homepage_button_setting', 
	        array(
	            'section' => 'home_page_editor_section',
	            'setting' => 'edit_homepage_button_setting',
	        )
	    )
	);

	
	//Latest News Section
	$wp_customize->add_section('cloudpress_latest_news_section',array(
			'title' => esc_html__('Latest News Settings','cloudpress'),
			'panel' => 'section_settings',
			'priority'       => 8,
			));
		
			
	// Enable news section
	$wp_customize->add_setting( 'latest_news_section_enable' , array( 'default' => 'on',  'sanitize_callback' => 'cloudpress_sanitize_radio',) );
			$wp_customize->add_control(	'latest_news_section_enable' , array(
					'label'    => esc_html__( 'Enable Home News Section', 'cloudpress' ),
					'section'  => 'cloudpress_latest_news_section',
					'type'     => 'radio',
					'choices' => array(
						'on'=>esc_html__('ON', 'cloudpress'),
						'off'=>esc_html__('OFF', 'cloudpress')
					)
			));

		// News section title
		$wp_customize->add_setting( 'home_news_section_title',array(
		'capability'     => 'edit_theme_options',
		'default' => esc_html__('Vitae Lacinia','cloudpress'),
		'sanitize_callback' => 'cloudpress_sanitize_text',
		));	
		$wp_customize->add_control( 'home_news_section_title',array(
		'label'   => esc_html__('Title','cloudpress'),
		'section' => 'cloudpress_latest_news_section',
		'type' => 'text',
		));	
		
		//News section discription
		$wp_customize->add_setting( 'home_news_section_discription',array(
		'default'=> esc_html__('Cras Vitae Placerat','cloudpress'),
		'sanitize_callback' => 'cloudpress_sanitize_text',
		));	
		$wp_customize->add_control( 'home_news_section_discription',array(
		'label'   => esc_html__('From Our Blog','cloudpress'),
		'section' => 'cloudpress_latest_news_section',
		'type' => 'textarea',
		));	
		
		
		// enable / disable meta section 
		$wp_customize->add_setting(
			'blog_meta_section_enable',
			array('capability'  => 'edit_theme_options',
			'default' => true,
			'sanitize_callback' => 'cloudpress_sanitize_checkbox',
			
			));
		$wp_customize->add_control(
			'blog_meta_section_enable',
			array(
				'type' => 'checkbox',
				'label' => esc_html__('Enable post meta values, like author name, date, comments, etc.','cloudpress'),
				'section' => 'cloudpress_latest_news_section',
			)
		);		
}
add_action( 'customize_register', 'cloudpress_sections_settings' );

/**
 * Add selective refresh for Front page section section controls.
 */
function cloudpress_register_home_section_partials( $wp_customize ){
	//News
	$wp_customize->selective_refresh->add_partial( 'home_news_section_title', array(
		'selector'            => '.section-module.blog .section-subtitle',
		'settings'            => 'home_news_section_title',
		'render_callback'  => 'cloudpress_home_news_section_title_render_callback',
	
	) );
	
	$wp_customize->selective_refresh->add_partial( 'home_news_section_discription', array(
		'selector'            => '.section-module.blog .section-title',
		'settings'            => 'home_news_section_discription',
		'render_callback'  => 'cloudpress_home_news_section_discription_render_callback',
	
	) );
}
add_action( 'customize_register', 'cloudpress_register_home_section_partials' );

function cloudpress_home_news_section_title_render_callback() {
	return get_theme_mod( 'home_news_section_title' );
}

function cloudpress_home_news_section_discription_render_callback() {
	return get_theme_mod( 'home_news_section_discription' );
}

function cloudpress_sanitize_radio( $input, $setting ){
     //input must be a slug: lowercase alphanumeric characters, dashes and underscores are allowed only
    $input = sanitize_key($input);
 
     //get the list of possible radio box options 
     $choices = $setting->manager->get_control( $setting->id )->choices;
                             
     //return input if valid or return default option
     return ( array_key_exists( $input, $choices ) ? $input : $setting->default );                          
}