| @@ 79-124 (lines=46) @@ | ||
| 76 | return null; |
|
| 77 | } |
|
| 78 | ||
| 79 | static function validate( $data, $type = null ) { |
|
| 80 | if ( is_null( $data ) ) { |
|
| 81 | return $data; |
|
| 82 | } |
|
| 83 | switch( $type ) { |
|
| 84 | case 'bool': |
|
| 85 | return boolval( $data ); |
|
| 86 | case 'url': |
|
| 87 | return esc_url( $data ); |
|
| 88 | case 'on': |
|
| 89 | return ( 'on' == $data ? true : false ); |
|
| 90 | break; |
|
| 91 | case 'closed': |
|
| 92 | return ( 'closed' != $data ? true : false ); |
|
| 93 | case 'string': |
|
| 94 | return strval( $data ); |
|
| 95 | case 'int': |
|
| 96 | return ( is_numeric( $data ) ? intval( $data ) : 0 ); |
|
| 97 | case 'float': |
|
| 98 | return ( is_numeric( $data ) ? floatval( $data ) : 0 ); |
|
| 99 | case 'array': |
|
| 100 | return ( is_array( $data ) ? $data : array() ); |
|
| 101 | case 'rtrim-slash': |
|
| 102 | return strval( rtrim( $data, '/' ) ); |
|
| 103 | } |
|
| 104 | if ( is_string( $type ) && 'regex:' == substr( $type, 0, 6 ) ) { |
|
| 105 | return ( preg_match( substr( $type, 6 ), $data ) ? $data : null ); |
|
| 106 | } elseif ( is_array( $type ) ) { |
|
| 107 | // Is the array associative? |
|
| 108 | if ( count( array_filter( array_keys( $type ), 'is_string' ) ) ) { |
|
| 109 | foreach ( $type as $item => $check ) { |
|
| 110 | $data[ $item ] = self::validate( $data[ $item ], $check ); |
|
| 111 | } |
|
| 112 | return $data; |
|
| 113 | } else { |
|
| 114 | // check if the value exists in the array if not return the first value. |
|
| 115 | // Ex $type = array( 'open', 'closed' ); defaults to 'open' |
|
| 116 | return ( in_array( $data, $type ) ? $data: $type[0] ); |
|
| 117 | } |
|
| 118 | } |
|
| 119 | // Don't check for validity here |
|
| 120 | if ( 'no-validation' == $type ) { |
|
| 121 | return $data; |
|
| 122 | } |
|
| 123 | return null; |
|
| 124 | } |
|
| 125 | ||
| 126 | static function validate_option( $data, $type = null ) { |
|
| 127 | if ( is_null( $data ) ) { |
|
| @@ 126-171 (lines=46) @@ | ||
| 123 | return null; |
|
| 124 | } |
|
| 125 | ||
| 126 | static function validate_option( $data, $type = null ) { |
|
| 127 | if ( is_null( $data ) ) { |
|
| 128 | return $data; |
|
| 129 | } |
|
| 130 | switch( $type ) { |
|
| 131 | case 'bool': |
|
| 132 | return boolval( $data ); |
|
| 133 | case 'url': |
|
| 134 | return esc_url( $data ); |
|
| 135 | case 'on': |
|
| 136 | return ( 'on' == $data ? true : false ); |
|
| 137 | break; |
|
| 138 | case 'closed': |
|
| 139 | return ( 'closed' != $data ? true : false ); |
|
| 140 | case 'string': |
|
| 141 | return strval( $data ); |
|
| 142 | case 'int': |
|
| 143 | return ( is_numeric( $data ) ? intval( $data ) : 0 ); |
|
| 144 | case 'float': |
|
| 145 | return ( is_numeric( $data ) ? floatval( $data ) : 0 ); |
|
| 146 | case 'array': |
|
| 147 | return ( is_array( $data ) ? $data : array() ); |
|
| 148 | case 'rtrim-slash': |
|
| 149 | return strval( rtrim( $data, '/' ) ); |
|
| 150 | } |
|
| 151 | if ( is_string( $type ) && 'regex:' == substr( $type, 0, 6 ) ) { |
|
| 152 | return ( preg_match( substr( $type, 6 ), $data ) ? $data : null ); |
|
| 153 | } elseif ( is_array( $type ) ) { |
|
| 154 | // Is the array associative? |
|
| 155 | if ( count( array_filter( array_keys( $type ), 'is_string' ) ) ) { |
|
| 156 | foreach ( $type as $item => $check ) { |
|
| 157 | $data[ $item ] = self::validate( $data[ $item ], $check ); |
|
| 158 | } |
|
| 159 | return $data; |
|
| 160 | } else { |
|
| 161 | // check if the value exists in the array if not return the first value. |
|
| 162 | // Ex $type = array( 'open', 'closed' ); defaults to 'open' |
|
| 163 | return ( in_array( $data, $type ) ? $data: $type[0] ); |
|
| 164 | } |
|
| 165 | } |
|
| 166 | // Don't check for validity here |
|
| 167 | if ( 'no-validation' == $type ) { |
|
| 168 | return $data; |
|
| 169 | } |
|
| 170 | return null; |
|
| 171 | } |
|
| 172 | } |
|
| 173 | ||