<?php
/**
 * Sanitization functions.
 *
 * @package Buzz_Agency
 */


//========= Category  Sanitizations Functions ==================================//

function buzz_agency_sanitize_category_select($input){

    $buzz_agency_cat_list = buzz_agency_category_lists();
    if(array_key_exists($input,$buzz_agency_cat_list)){
        return $input;
    }
    else{
        return '';
    }
}

//SANITIZATION FUNCTIONS
function buzz_agency_sanitize_checkbox( $input ) {
    return wp_kses_post( force_balance_tags( $input ) );
}

//SANITIZATION FUNCTIONS
function buzz_agency_sanitize_text( $input ) {
    return wp_kses_post( force_balance_tags( $input ) );
}

//==================================== Integer Sanitizations Functions ==================================//
if ( ! function_exists( 'buzz_agency_integer_sanitize' ) ) :

/**
 *  Sanitize Multiple Dropdown Taxonomies.
 *  @since 1.0.0
 */
function buzz_agency_integer_sanitize( $input ) {
    // Make sure we have array.
    $input = (array) $input;

    // Sanitize each array element.
    $input = array_map( 'absint', $input );

    // Remove null elements.
    $input = array_values( array_filter( $input ) );

    return $input;
}
endif;

/**
 * Sanitize the slider value set within customizer controls.
 *
 * @param number $val     Customizer setting input number.
 * @param object $setting Setting object.
 *
 * @return int
 */
function buzz_agency_sanitize_number( $val, $setting ) {

    $input_attrs = $setting->manager->get_control( $setting->id )->input_attrs;

    if ( isset( $input_attrs ) ) {

        $input_attrs['min']  = isset( $input_attrs['min'] ) ? $input_attrs['min'] : 0;
        $input_attrs['step'] = isset( $input_attrs['step'] ) ? $input_attrs['step'] : 1;

        if ( isset( $input_attrs['max'] ) && $val > $input_attrs['max'] ) {
            $val = $input_attrs['max'];
        } elseif ( $val < $input_attrs['min'] ) {
            $val = $input_attrs['min'];
        }

        if ( $val ) {
            $val = (int) $val;
        }
    }

    return is_numeric( $val ) ? $val : $setting->default;

}


//=========================== Header Site Identity ============================================//

if ( ! function_exists( 'buzz_agency_sanitize_select' ) ) :

    /**
     * Sanitize select.
     *
     * @since 1.0.0
     *
     * @param mixed                $input The value to sanitize.
     * @param WP_Customize_Setting $setting WP_Customize_Setting instance.
     * @return mixed Sanitized value.
     */
    function buzz_agency_sanitize_select( $input, $setting ) {

        // Ensure input is a slug.
        $input = sanitize_key( $input );

        // Get list of choices from the control associated with the setting.
        $choices = $setting->manager->get_control( $setting->id )->choices;

        // If the input is a valid key, return it; otherwise, return the default.
        return ( array_key_exists( $input, $choices ) ? $input : $setting->default );

    }

endif;


//page senitizer
if ( ! function_exists( 'buzz_agency_sanitize_dropdown_pages' ) ) :

    /**
     * Sanitize dropdown pages.
     *
     * @since 1.0.0
     *
     * @param int                  $page_id Page ID.
     * @param WP_Customize_Setting $setting WP_Customize_Setting instance.
     * @return int|string Page ID if the page is published; otherwise, the setting default.
     */
    function buzz_agency_sanitize_dropdown_pages( $page_id, $setting ) {

        // Ensure $input is an absolute integer.
        $page_id = absint( $page_id );

        // If $page_id is an ID of a published page, return it; otherwise, return the default.
        return ( 'publish' === get_post_status( $page_id ) ? $page_id : $setting->default );

    }

endif;

/**
* Repeater Sanitize
*/
function buzz_agency_sanitize_repeater($input){      
    $input_decoded = json_decode( $input, true );
    
    if(!empty($input_decoded)) {
        foreach ($input_decoded as $boxes => $box ){
            foreach ($box as $key => $value){

                $input_decoded[$boxes][$key] = sanitize_text_field( $value );
            }
        }
        return json_encode($input_decoded);
    }    
    return $input;
}
/**
* Repeater Sanitize for html filter
*/
function buzz_agency_html_sanitize_repeater($input){      
    $input_decoded = json_decode( $input, true );
    if(!empty($input_decoded)) {
        foreach ($input_decoded as $boxes => $box ){
            foreach ($box as $key => $value){

                $input_decoded[$boxes][$key] = wp_kses_post( $value );
            }
        }
        return json_encode($input_decoded);
    }    
    return $input;
}
