about
7 years ago
fields
7 years ago
interfaces
9 years ago
storages
7 years ago
templates
8 years ago
walkers
7 years ago
autoloader.php
7 years ago
clone.php
7 years ago
core.php
8 years ago
field-registry.php
8 years ago
field.php
7 years ago
functions.php
7 years ago
loader.php
7 years ago
media-modal.php
8 years ago
meta-box-registry.php
8 years ago
meta-box.php
7 years ago
sanitizer.php
7 years ago
storage-registry.php
8 years ago
validation.php
7 years ago
wpml.php
8 years ago
sanitizer.php
65 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Sanitize field value before saving. |
| 4 | * |
| 5 | * @package Meta Box |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Sanitize class. |
| 10 | */ |
| 11 | class RWMB_Sanitizer { |
| 12 | |
| 13 | /** |
| 14 | * Built-in callbacks for some specific types. |
| 15 | * |
| 16 | * @var array |
| 17 | */ |
| 18 | protected $callbacks = array( |
| 19 | 'email' => 'sanitize_email', |
| 20 | 'file_input' => 'esc_url_raw', |
| 21 | 'oembed' => 'esc_url_raw', |
| 22 | 'url' => 'esc_url_raw', |
| 23 | ); |
| 24 | |
| 25 | /** |
| 26 | * Register hook to sanitize field value. |
| 27 | */ |
| 28 | public function init() { |
| 29 | // Built-in callback. |
| 30 | foreach ( $this->callbacks as $type => $callback ) { |
| 31 | add_filter( "rwmb_{$type}_sanitize", $callback ); |
| 32 | } |
| 33 | |
| 34 | // Custom callback. |
| 35 | $methods = array_diff( get_class_methods( __CLASS__ ), array( 'init' ) ); |
| 36 | foreach ( $methods as $method ) { |
| 37 | $type = substr( $method, 9 ); |
| 38 | add_filter( "rwmb_{$type}_sanitize", array( $this, $method ) ); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Set the value of checkbox to 1 or 0 instead of 'checked' and empty string. |
| 44 | * This prevents using default value once the checkbox has been unchecked. |
| 45 | * |
| 46 | * @link https://github.com/rilwis/meta-box/issues/6 |
| 47 | * @param string $value Checkbox value. |
| 48 | * @return int |
| 49 | */ |
| 50 | public function sanitize_checkbox( $value ) { |
| 51 | return (int) ! empty( $value ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Set the value of switch to 1 or 0 instead of 'checked' and empty string. |
| 56 | * This prevents using default value once the switch has been unchecked. |
| 57 | * |
| 58 | * @param string $value Switch value. |
| 59 | * @return int |
| 60 | */ |
| 61 | public function sanitize_switch( $value ) { |
| 62 | return (int) ! empty( $value ); |
| 63 | } |
| 64 | } |
| 65 |