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