| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDeveloper\BetterDocs\Admin\Customizer; |
| 4 |
|
| 5 |
use WP_Customize_Setting; |
| 6 |
use WPDeveloper\BetterDocs\Utils\Base; |
| 7 |
|
| 8 |
class Sanitizer extends Base { |
| 9 |
/** |
| 10 |
* Sanitize options like value. Works for Select. |
| 11 |
* |
| 12 |
* @since 1.0.0 |
| 13 |
* |
| 14 |
* @param string $input |
| 15 |
* @param WP_Customize_Setting $setting |
| 16 |
* |
| 17 |
* @return string |
| 18 |
*/ |
| 19 |
public function select( $input, $setting ) { |
| 20 |
$input = sanitize_key( $input ); |
| 21 |
$choices = $setting->manager->get_control( $setting->id )->choices; |
| 22 |
|
| 23 |
//return input if valid or return default option |
| 24 |
return ( array_key_exists( $input, $choices ) ? $input : $setting->default ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Choice |
| 29 |
* |
| 30 |
* @param mixed $input |
| 31 |
* @param mixed $setting |
| 32 |
* @return string |
| 33 |
*/ |
| 34 |
|
| 35 |
public function choices( $input, $setting ) { |
| 36 |
return $this->select( $input, $setting ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* |
| 41 |
* Sanitize checkbox values |
| 42 |
* |
| 43 |
* @since 1.0.0 |
| 44 |
*/ |
| 45 |
public function checkbox( $input ) { |
| 46 |
return $input ? '1' : false; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Sanitize integers |
| 51 |
* @since 1.0.0 |
| 52 |
* |
| 53 |
* @param int $input |
| 54 |
* |
| 55 |
* @return int |
| 56 |
*/ |
| 57 |
public function integer( $input ) { |
| 58 |
return filter_var( $input, FILTER_SANITIZE_NUMBER_INT ); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Sanitize Float |
| 63 |
* @param float $input |
| 64 |
* @since 1.0.0 |
| 65 |
* |
| 66 |
* @return float |
| 67 |
*/ |
| 68 |
public function float( $input ) { |
| 69 |
return filter_var( $input, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Sanitize checkbox values |
| 74 |
* @param mixed $color |
| 75 |
* @return string |
| 76 |
*/ |
| 77 |
public function rgba( $color ) { |
| 78 |
if ( empty( $color ) || is_array( $color ) ) { |
| 79 |
return 'rgba(0,0,0,0)'; |
| 80 |
} |
| 81 |
|
| 82 |
// If string does not start with 'rgba', then treat as hex |
| 83 |
// sanitize the hex color and finally convert hex to rgba |
| 84 |
if ( false === strpos( $color, 'rgba' ) ) { |
| 85 |
return sanitize_hex_color( $color ); |
| 86 |
} |
| 87 |
|
| 88 |
// By now we know the string is formatted as an rgba color so we need to further sanitize it. |
| 89 |
$color = str_replace( ' ', '', $color ); |
| 90 |
sscanf( $color, 'rgba(%d,%d,%d,%f)', $red, $green, $blue, $alpha ); |
| 91 |
return 'rgba(' . $red . ',' . $green . ',' . $blue . ',' . $alpha . ')'; |
| 92 |
} |
| 93 |
} |
| 94 |
|