<?php
/**
 * Adds the Featured section and its fields to the Frontpage Settings panel.
 * 
 * @package Blogbuster
 */

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

add_action( 'customize_register', 'blogbuster_register_front_featured_options' );

if ( ! function_exists( 'blogbuster_register_front_featured_options' ) ) :

    /**
     * Register theme options for Featured section.
     * 
     * @param WP_Customize_Manager $wp_customize Object that holds the customizer data.
     * @since 1.0.0
     */
    function blogbuster_register_front_featured_options( $wp_customize ) {

        /*
         * Failsafe is safe
         */
        if ( ! isset( $wp_customize ) ) {
            return;
        }

        /**
         * Add Featured Section
         * 
         * Frontpage Settings > Featured
         * 
         * @uses $wp_customize->add_section() https://developer.wordpress.org/reference/classes/wp_customize_manager/add_section/
         * @since 1.0.0
         */
        $wp_customize->add_section( new Blogbuster_Customize_Section (
            $wp_customize, 'blogbuster_section_front_featured',
                array(
                    'priority'  => 10,
                    'panel'     => 'blogbuster_panel_frontpage',
                    'title'     => __( 'Featured', 'blogbuster' ),
                )
            )
        );

        /**
         * Toggle option for featured section.
         *
         * Frontpage Settings > Featured
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'blogbuster_front_featured_enable',
            array(
                'default'           => blogbuster_get_customizer_default( 'blogbuster_front_featured_enable' ),
                'sanitize_callback' => 'blogbuster_sanitize_checkbox'
            )
        );
        $wp_customize->add_control( new Blogbuster_Control_Toggle(
            $wp_customize, 'blogbuster_front_featured_enable',
                array(
                    'priority'      => 5,
                    'section'       => 'blogbuster_section_front_featured',
                    'settings'      => 'blogbuster_front_featured_enable',
                    'label'         => __( 'Enable Featured Section', 'blogbuster' )
                )
            )
        );

        /**
         * Text option for featured title
         *
         * Frontpage Settings > Featured
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'blogbuster_front_featured_section_title',
            array(
                'default'           => blogbuster_get_customizer_default( 'blogbuster_front_featured_section_title' ),
                'transport'         => 'postMessage',
                'sanitize_callback' => 'sanitize_text_field'
            )
        );
        $wp_customize->add_control( new Blogbuster_Customize_Control (
            $wp_customize, 'blogbuster_front_featured_section_title',
            array(
                'priority'          => 10,
                'section'           => 'blogbuster_section_front_featured',
                'settings'          => 'blogbuster_front_featured_section_title',
                'label'             => __( 'Section Title', 'blogbuster' ),
                'type'              => 'text',
                 // Required Dependency
                    'required'        => array(
                        array(
                            'control'  => 'blogbuster_front_featured_enable',
                            'value'    => true,
                            'operator' => '==',
                        ),
                    )
                )
            )
        );

        /**
         * Repeater field for front featured items
         *
         * Frontpage Settings > Featured
         * 
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'blogbuster_front_featured_items', 
            array(
                'default'           => blogbuster_get_customizer_default( 'blogbuster_front_featured_items' ),
                'sanitize_callback' => 'blogbuster_sanitize_repeater'
            )
        );
        $wp_customize->add_control( new Blogbuster_Control_Repeater(
            $wp_customize,  'blogbuster_front_featured_items',
                array(
                    'priority'        => 20,
                    'section'         => 'blogbuster_section_front_featured',
                    'settings'        => 'blogbuster_front_featured_items',
                    'label'           => __( 'Featured Items', 'blogbuster' ),
                    'blogbuster_box_label_text'       => __( 'Featured Item','blogbuster' ),
                    'blogbuster_box_add_control_text' => __( 'Add Item','blogbuster' ),
                    'blogbuster_field_limit'          => 4,
                    // Required Dependency
                    'required'        => array(
                        array(
                            'control'  => 'blogbuster_front_featured_enable',
                            'value'    => true,
                            'operator' => '==',
                        ),
                    ),
                ),
                array(
                    'item_category' => array(
                        'type'          => 'dropdown_category',   
                        'label'         => __( 'Item Category', 'blogbuster' ),
                        'description'   => __( 'Select category from list.', 'blogbuster' )
                    ),
                    'item_image'  => array(
                        'type'          => 'upload',
                        'label'         => __( 'Item Image', 'blogbuster' ),
                        'description'   => __( 'Upload Item Image.', 'blogbuster' )
                    ),
                    'item_visible'   => array(
                        'type'  => 'hidden'
                    )
                )
            )
        );

        /**
         * Upgrade field for featured items
         * 
         * Frontpage Settings > Featured
         *
         * @since 1.0.0
         */ 
        $wp_customize->add_setting( 'blogbuster_upgrade_front_featured',
            array(
                'sanitize_callback' => 'sanitize_text_field'
            )
        );
        $wp_customize->add_control( new Blogbuster_Control_Upgrade(
            $wp_customize, 'blogbuster_upgrade_front_featured',
                array(
                    'priority'      => 70,
                    'section'       => 'blogbuster_section_front_featured',
                    'settings'      => 'blogbuster_upgrade_front_featured',
                    'label'         => __( 'More features with Blogbuster Pro', 'blogbuster' ),
                    'choices'       => blogbuster_upgrade_choices( 'blogbuster_front_featured' )
                )
            )
        );

    }

endif;