<?php
/**
 * Adds the Preloader section and its fields to the General Settings panel.
 * 
 * @package Purenote
 */

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

add_action( 'customize_register', 'purenote_register_preloader_options' );

if ( ! function_exists( 'purenote_register_preloader_options' ) ) :

    /**
     * Register theme options for Preloader section.
     * 
     * @param WP_Customize_Manager $wp_customize Object that holds the settings.
     * @since 1.0.0
     */
    function purenote_register_preloader_options( $wp_customize ) {

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

        /**
         * Add Preloader Section
         * 
         * General Settings > Preloader
         * 
         * @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 Purenote_Customize_Section (
            $wp_customize, 'purenote_section_preloader',
                array(
                    'priority'  => 20,
                    'panel'     => 'purenote_panel_general',
                    'title'     => __( 'Preloader', 'purenote' ),
                )
            )
        );

        /**
         * Toggle option for preloader.
         *
         * General Settings > Preloader
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'purenote_preloader_enable',
            array(
                'default'           => purenote_get_customizer_default ( 'purenote_preloader_enable' ),
                'sanitize_callback' => 'purenote_sanitize_checkbox'
            )
        );
        $wp_customize->add_control( new Purenote_Control_Toggle(
            $wp_customize, 'purenote_preloader_enable',
                array(
                    'priority'  => 10,
                    'section'   => 'purenote_section_preloader',
                    'settings'  => 'purenote_preloader_enable',
                    'label'     => __( 'Enable Preloader', 'purenote' )
                )
            )
        );

        /**
         * Radio image field for preloader styles
         *
         * General Settings > Preloader
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'purenote_preloader_style',
            array(
                'default'           => purenote_get_customizer_default ( 'purenote_preloader_style' ),
                'sanitize_callback' => 'purenote_sanitize_select',
            )
        );
        $wp_customize->add_control( new Purenote_Control_Radio_Image(
            $wp_customize, 'purenote_preloader_style',
                array(
                    'priority'          => 20,
                    'section'           => 'purenote_section_preloader',
                    'settings'          => 'purenote_preloader_style',
                    'label'             => __( 'Preloader Layout', 'purenote' ),
                    'choices'           => purenote_preloader_style_choices(),
                    'input_attrs'       => array(
                        'column'    => 4,
                    ),
                    // Required Dependency
                    'required'          => array(
                        array(
                            'control'  => 'purenote_preloader_enable',
                            'value'    => true,
                            'operator' => '==',
                        ),
                    ),
                )
            )
        );

    }

endif;