<?php
/**
 * Adds the Social Icons section and its fields to the General Settings panel.
 * 
 * @package Blogbuster
 */

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

add_action( 'customize_register', 'blogbuster_register_social_icons_options' );

if ( ! function_exists( 'blogbuster_register_social_icons_options' ) ) :

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

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

        /**
         * Add Social Icons Section
         * 
         * General Settings > Social Icons
         * 
         * @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_social_icons',
                array(
                    'priority'  => 25,
                    'panel'     => 'blogbuster_panel_general',
                    'title'     => __( 'Social Icons', 'blogbuster' ),
                )
            )
        );

        /**
         * Toggle option for social icon target.
         *
         * General Settings > Social Icons
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'blogbuster_social_icon_link_target',
            array(
                'default'           => blogbuster_get_customizer_default( 'blogbuster_social_icon_link_target' ),
                'sanitize_callback' => 'blogbuster_sanitize_checkbox'
            )
        );
        $wp_customize->add_control( new Blogbuster_Control_Toggle(
            $wp_customize, 'blogbuster_social_icon_link_target',
                array(
                    'priority'      => 10,
                    'section'       => 'blogbuster_section_social_icons',
                    'settings'      => 'blogbuster_social_icon_link_target',
                    'label'         => __( 'Open links in new tab', 'blogbuster' )
                )
            )
        );

        /**
         * Repeater field for Social Icons
         *
         * General Settings > Social Icons
         * 
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'blogbuster_social_icons', 
            array(
                'default'           => blogbuster_get_customizer_default( 'blogbuster_social_icons' ),
                'sanitize_callback' => 'blogbuster_sanitize_repeater'
            )
        );
        $wp_customize->add_control( new Blogbuster_Control_Repeater(
            $wp_customize,  'blogbuster_social_icons',
                array(
                    'priority'        => 20,
                    'section'         => 'blogbuster_section_social_icons',
                    'settings'        => 'blogbuster_social_icons',
                    'label'           => __( 'Social Icons', 'blogbuster' ),
                    'blogbuster_box_label_text'       => __( 'Social Icons','blogbuster' ),
                    'blogbuster_box_add_control_text' => __( 'Add New Icon','blogbuster' ),
                    'blogbuster_field_limit'          => 4
                ),
                array(
                    'social_icon' => array(
                        'type'          => 'social_icon',   
                        'label'         => __( 'Social Icon', 'blogbuster' ),
                        'description'   => __( 'Choose social media icon.', 'blogbuster' )
                    ),
                    'social_url'  => array(
                        'type'          => 'url',
                        'label'         => __( 'Social Link URL', 'blogbuster' ),
                        'description'   => __( 'Enter social media url.', 'blogbuster' )
                    ),
                    'item_visible'   => array(
                        'type'  => 'hidden'
                    )
                )
            )
        );

        /**
         * Upgrade field for social icon section
         * 
         * General Settings > Social Icons
         *
         * @since 1.0.0
         */ 
        $wp_customize->add_setting( 'blogbuster_upgrade_social_icons',
            array(
                'sanitize_callback' => 'sanitize_text_field'
            )
        );
        $wp_customize->add_control( new Blogbuster_Control_Upgrade(
            $wp_customize, 'blogbuster_upgrade_social_icons',
                array(
                    'priority'      => 70,
                    'section'       => 'blogbuster_section_social_icons',
                    'settings'      => 'blogbuster_upgrade_social_icons',
                    'label'         => __( 'More features with Blogbuster Pro', 'blogbuster' ),
                    'choices'       => blogbuster_upgrade_choices( 'blogbuster_social_icon' )
                )
            )
        );

    }

endif;