Check.php
110 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | class TCMP_Check { |
| 8 | var $data; |
| 9 | |
| 10 | public function __construct() { |
| 11 | $this->data = array_merge( $_POST, $_GET ); |
| 12 | } |
| 13 | |
| 14 | public function is( $name, $value, $ignoreCase = true ) { |
| 15 | $result = false; |
| 16 | if ( isset( $this->data[ $name ] ) ) { |
| 17 | if ( $ignoreCase ) { |
| 18 | $result = ( strtolower( $this->data[ $name ] ) == strtolower( $value ) ); |
| 19 | } else { |
| 20 | $result = ( $this->data[ $name ] == $value ); |
| 21 | } |
| 22 | } |
| 23 | return $result; |
| 24 | } |
| 25 | public function of( $name, $default = '' ) { |
| 26 | $result = $default; |
| 27 | if ( isset( $this->data[ $name ] ) ) { |
| 28 | $result = $this->data[ $name ]; |
| 29 | } |
| 30 | return $result; |
| 31 | } |
| 32 | public function nonce( $action, $nonce = '_wpnonce' ) { |
| 33 | if ( isset( $_REQUEST[ $nonce ] ) ) { |
| 34 | $nonce = $_REQUEST[ $nonce ]; |
| 35 | } |
| 36 | return wp_verify_nonce( $nonce, $action ); |
| 37 | } |
| 38 | |
| 39 | //check if is a mandatory field by checking the .txt language file |
| 40 | private function error( $name ) { |
| 41 | global $tcmp; |
| 42 | |
| 43 | $result = false; |
| 44 | $k = $tcmp->form->prefix . '.' . $name . '.check'; |
| 45 | $v = $tcmp->lang->L( $k ); |
| 46 | if ( $v != $k ) { |
| 47 | //this is a mandatory field so we give error |
| 48 | $tcmp->options->pushErrorMessage( $v ); |
| 49 | $result = true; |
| 50 | } |
| 51 | return $result; |
| 52 | } |
| 53 | |
| 54 | public function value( $name ) { |
| 55 | $result = ''; |
| 56 | if ( isset( $this->data[ $name ] ) ) { |
| 57 | $result = sanitize_text_field( $this->data[ $name ] ); |
| 58 | } |
| 59 | if ( '' == $result ) { |
| 60 | $this->error( $name ); |
| 61 | } |
| 62 | $this->data[ $name ] = $result; |
| 63 | return $result; |
| 64 | } |
| 65 | public function values( $name ) { |
| 66 | $result = array(); |
| 67 | if ( is_string( $name ) ) { |
| 68 | $name = explode( ',', $name ); |
| 69 | } |
| 70 | foreach ( $name as $v ) { |
| 71 | $result[] = $this->value( trim( $v ) ); |
| 72 | } |
| 73 | return $result; |
| 74 | } |
| 75 | public function email( $name ) { |
| 76 | $result = $this->value( $name ); |
| 77 | if ( '' != $result ) { |
| 78 | $result = sanitize_email( $result ); |
| 79 | if ( ! is_email( $result ) ) { |
| 80 | $this->error( $name ); |
| 81 | } |
| 82 | } |
| 83 | $this->data[ $name ] = $result; |
| 84 | return $result; |
| 85 | } |
| 86 | public function float( $name ) { |
| 87 | $result = $this->value( $name ); |
| 88 | if ( '' != $result && ! is_float( $result ) ) { |
| 89 | $this->error( $name ); |
| 90 | } |
| 91 | $result = floatval( $result ); |
| 92 | $this->data[ $name ] = $result; |
| 93 | return $result; |
| 94 | } |
| 95 | public function integer( $name ) { |
| 96 | $result = $this->value( $name ); |
| 97 | if ( '' != $result && ! is_int( $result ) ) { |
| 98 | $this->error( $name ); |
| 99 | } |
| 100 | $result = intval( $result ); |
| 101 | $this->data[ $name ] = $result; |
| 102 | return $result; |
| 103 | } |
| 104 | |
| 105 | public function hasErrors() { |
| 106 | global $tcmp; |
| 107 | return $tcmp->options->hasErrorMessages(); |
| 108 | } |
| 109 | } |
| 110 |