<?php
/**
 * Archive Page and it's fields inside Innerpage Settings panel.
 * 
 * @package Blogbuster
 */

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

add_action( 'customize_register', 'blogbuster_register_archive_options' );

if ( ! function_exists( 'blogbuster_register_archive_options' ) ) :

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

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

        /**
         * Archive Section
         * 
         * Innerpage Settings > Archive Pages
         * 
         * @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_page_archive',
                array(
                    'priority'  => 5,
                    'panel'     => 'blogbuster_panel_innerpage',
                    'title'     => __( 'Archive Pages', 'blogbuster' ),
                )
            )
        );

        /**
         * Select option for archive page style
         *
         * Innerpage Settings > Archive Pages
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'blogbuster_archive_page_style',
            array(
                'default'           => blogbuster_get_customizer_default( 'blogbuster_archive_page_style' ),
                'sanitize_callback' => 'blogbuster_sanitize_select'
            )
        );
        $wp_customize->add_control( 'blogbuster_archive_page_style',
            array(
                'priority'          => 10,
                'section'           => 'blogbuster_section_page_archive',
                'settings'          => 'blogbuster_archive_page_style',
                'label'             => __( 'Archive Page Style', 'blogbuster' ),
                'type'              => 'select',
                'choices'           => blogbuster_archive_page_style_choices()
            )
        );

        /**
         * Toggle option for archive title prefix.
         *
         * Innerpage Settings > Archive Pages
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'blogbuster_archive_title_prefix_enable',
            array(
                'default'           => blogbuster_get_customizer_default( 'blogbuster_archive_title_prefix_enable' ),
                'sanitize_callback' => 'blogbuster_sanitize_checkbox'
            )
        );
        $wp_customize->add_control( new Blogbuster_Control_Toggle(
            $wp_customize, 'blogbuster_archive_title_prefix_enable',
                array(
                    'priority'      => 15,
                    'section'       => 'blogbuster_section_page_archive',
                    'settings'      => 'blogbuster_archive_title_prefix_enable',
                    'label'         => __( 'Enable archive page title prefix.', 'blogbuster' )
                )
            )
        );

        /**
         * Toggle option for archive post read more.
         *
         * Innerpage Settings > Archive Pages
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'blogbuster_archive_post_readmore_enable',
            array(
                'default'           => blogbuster_get_customizer_default( 'blogbuster_archive_post_readmore_enable' ),
                'sanitize_callback' => 'blogbuster_sanitize_checkbox'
            )
        );
        $wp_customize->add_control( new Blogbuster_Control_Toggle(
            $wp_customize, 'blogbuster_archive_post_readmore_enable',
                array(
                    'priority'      => 25,
                    'section'       => 'blogbuster_section_page_archive',
                    'settings'      => 'blogbuster_archive_post_readmore_enable',
                    'label'         => __( 'Enable Read More Button', 'blogbuster' )
                )
            )
        );

        /**
         * Radio buttonset field for archive readmore type
         *
         * Innerpage Settings > Archive Pages
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'blogbuster_archive_read_more_type',
            array(
                'default'           => blogbuster_get_customizer_default( 'blogbuster_archive_read_more_type' ),
                'sanitize_callback' => 'blogbuster_sanitize_select',
            )
        );
        $wp_customize->add_control( new Blogbuster_Control_Buttonset(
            $wp_customize, 'blogbuster_archive_read_more_type',
                array(
                    'priority'      => 30,
                    'section'       => 'blogbuster_section_page_archive',
                    'settings'      => 'blogbuster_archive_read_more_type',
                    'label'         => __( 'Site Mode', 'blogbuster' ),
                    'choices'       => blogbuster_archive_read_more_type_choices(),
                    // Required Dependency
                    'required'          => array(
                        array(
                            'control'  => 'blogbuster_archive_post_readmore_enable',
                            'value'    => true,
                            'operator' => '==',
                        ),
                    ),
                )
            )
        );

        /**
         * Radio icons field for archive read more icon
         *
         * Innerpage Settings > Archive Pages
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'blogbuster_archive_read_more_icon',
            array(
                'default'           => blogbuster_get_customizer_default( 'blogbuster_archive_read_more_icon' ),
                'sanitize_callback' => 'sanitize_text_field',
            )
        );
        $wp_customize->add_control( new Blogbuster_Control_Radio_Icons(
            $wp_customize, 'blogbuster_archive_read_more_icon',
                array(
                    'priority'          => 35,
                    'section'           => 'blogbuster_section_page_archive',
                    'settings'          => 'blogbuster_archive_read_more_icon',
                    'label'             => __( 'Read More Icon', 'blogbuster' ),
                    'description'       => __( 'Choose required icon from available lists.', 'blogbuster' ),
                    'choices'           => blogbuster_archive_read_more_icon_choices(),
                    // Required Dependency
                    'required'          => array(
                        array(
                            'control'  => 'blogbuster_archive_post_readmore_enable',
                            'value'    => true,
                            'operator' => '==',
                        ),
                        array(
                            'control'  => 'blogbuster_archive_read_more_type',
                            'value'    => 'icon',
                            'operator' => '==',
                        ),
                    ),
                )
            )
        );

        /**
         * Text optin for archive read more button
         *
         * Innerpage Settings > Archive Pages
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'blogbuster_archive_post_read_more',
            array(
                'default'           => blogbuster_get_customizer_default( 'blogbuster_archive_post_read_more' ),
                'sanitize_callback' => 'sanitize_text_field'
            )
        );
        $wp_customize->add_control( new Blogbuster_Customize_Control(
            $wp_customize, 'blogbuster_archive_post_read_more',
                array(
                    'priority'          => 40,
                    'section'           => 'blogbuster_section_page_archive',
                    'settings'          => 'blogbuster_archive_post_read_more',
                    'label'             => __( 'Read More Text', 'blogbuster' ),
                    'type'              => 'text',
                    // Required Dependency
                    'required'          => array(
                        array(
                            'control'  => 'blogbuster_archive_post_readmore_enable',
                            'value'    => true,
                            'operator' => '==',
                        ),
                        array(
                            'control'  => 'blogbuster_archive_read_more_type',
                            'value'    => 'text',
                            'operator' => '==',
                        ),
                    ),
                )
            )
        );

        /**
         * Upgrade field for archive pages
         * 
         * Innerpage Settings > Archive Pages
         *
         * @since 1.0.0
         */ 
        $wp_customize->add_setting( 'blogbuster_upgrade_archive',
            array(
                'sanitize_callback' => 'sanitize_text_field'
            )
        );
        $wp_customize->add_control( new Blogbuster_Control_Upgrade(
            $wp_customize, 'blogbuster_upgrade_archive',
                array(
                    'priority'      => 100,
                    'section'       => 'blogbuster_section_page_archive',
                    'settings'      => 'blogbuster_upgrade_archive',
                    'label'         => __( 'More Features with Blogbuster Pro', 'blogbuster' ),
                    'choices'       => blogbuster_upgrade_choices( 'blogbuster_archive' )
                )
            )
        );

    }

endif;