| 1 |
<?php |
| 2 |
// No direct access, please |
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 4 |
|
| 5 |
|
| 6 |
if ( ! function_exists( 'betterdocs_sanitize_integer' ) ) : |
| 7 |
/** |
| 8 |
* |
| 9 |
* Sanitize integers |
| 10 |
* |
| 11 |
* @since 1.0.0 |
| 12 |
*/ |
| 13 |
function betterdocs_sanitize_integer( $input ) { |
| 14 |
return filter_var( $input, FILTER_SANITIZE_NUMBER_INT ); |
| 15 |
} |
| 16 |
|
| 17 |
endif; |
| 18 |
|
| 19 |
if ( ! function_exists( 'betterdocs_sanitize_float' ) ) : |
| 20 |
/** |
| 21 |
* |
| 22 |
* Sanitize float |
| 23 |
* @since 1.0.0 |
| 24 |
* |
| 25 |
*/ |
| 26 |
function betterdocs_sanitize_float( $input ) { |
| 27 |
return filter_var( $input, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ); |
| 28 |
} |
| 29 |
|
| 30 |
endif; |
| 31 |
|
| 32 |
|
| 33 |
if ( ! function_exists( 'betterdocs_sanitize_choices' ) ) : |
| 34 |
/** |
| 35 |
* |
| 36 |
* Sanitize choices |
| 37 |
* |
| 38 |
* @since 1.0.0 |
| 39 |
*/ |
| 40 |
function betterdocs_sanitize_choices( $input, $setting ) { |
| 41 |
|
| 42 |
// Ensure input is a slug |
| 43 |
$input = sanitize_key( $input ); |
| 44 |
|
| 45 |
// Get list of choices from the control |
| 46 |
// associated with the setting |
| 47 |
$choices = $setting->manager->get_control( $setting->id )->choices; |
| 48 |
|
| 49 |
// If the input is a valid key, return it; |
| 50 |
// otherwise, return the default |
| 51 |
return ( array_key_exists( $input, $choices ) ? $input : $setting->default ); |
| 52 |
} |
| 53 |
endif; |
| 54 |
|
| 55 |
if ( ! function_exists( 'betterdocs_sanitize_checkbox' ) ) : |
| 56 |
/** |
| 57 |
* |
| 58 |
* Sanitize checkbox values |
| 59 |
* |
| 60 |
* @since 1.0.0 |
| 61 |
*/ |
| 62 |
function betterdocs_sanitize_checkbox( $input ) { |
| 63 |
if ( $input ) { |
| 64 |
$output = '1'; |
| 65 |
} else { |
| 66 |
$output = false; |
| 67 |
} |
| 68 |
return $output; |
| 69 |
} |
| 70 |
endif; |
| 71 |
|
| 72 |
if ( ! function_exists( 'betterdocs_sanitize_rgba' ) ) : |
| 73 |
/** |
| 74 |
* |
| 75 |
* Sanitize checkbox values |
| 76 |
* |
| 77 |
* @since 1.0.0 |
| 78 |
*/ |
| 79 |
function betterdocs_sanitize_rgba( $color ) { |
| 80 |
if ( empty( $color ) || is_array( $color ) ) |
| 81 |
return 'rgba(0,0,0,0)'; |
| 82 |
|
| 83 |
// If string does not start with 'rgba', then treat as hex |
| 84 |
// sanitize the hex color and finally convert hex to rgba |
| 85 |
if ( false === strpos( $color, 'rgba' ) ) { |
| 86 |
return sanitize_hex_color( $color ); |
| 87 |
} |
| 88 |
|
| 89 |
// By now we know the string is formatted as an rgba color so we need to further sanitize it. |
| 90 |
$color = str_replace( ' ', '', $color ); |
| 91 |
sscanf( $color, 'rgba(%d,%d,%d,%f)', $red, $green, $blue, $alpha ); |
| 92 |
return 'rgba('.$red.','.$green.','.$blue.','.$alpha.')'; |
| 93 |
} |
| 94 |
endif; |
| 95 |
|
| 96 |
if ( ! function_exists( 'betterdocs_sanitize_select' ) ) : |
| 97 |
/** |
| 98 |
* |
| 99 |
* Sanitize select option values |
| 100 |
* |
| 101 |
* @since 1.0.0 |
| 102 |
*/ |
| 103 |
function betterdocs_sanitize_select( $input, $setting ){ |
| 104 |
|
| 105 |
//input must be a slug: lowercase alphanumeric characters, dashes and underscores are allowed only |
| 106 |
$input = sanitize_key($input); |
| 107 |
|
| 108 |
//get the list of possible select options |
| 109 |
$choices = $setting->manager->get_control( $setting->id )->choices; |
| 110 |
|
| 111 |
//return input if valid or return default option |
| 112 |
return ( array_key_exists( $input, $choices ) ? $input : $setting->default ); |
| 113 |
|
| 114 |
} |
| 115 |
endif; |
| 116 |
|