Base.php
267 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class Tribe__Validator__Base |
| 5 | * |
| 6 | * Provides methods to validate values. |
| 7 | */ |
| 8 | class Tribe__Validator__Base implements Tribe__Validator__Interface { |
| 9 | |
| 10 | /** |
| 11 | * @param mixed $value |
| 12 | * |
| 13 | * @return bool |
| 14 | */ |
| 15 | public function is_not_null( $value ) { |
| 16 | return null !== $value; |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * @param mixed $value |
| 21 | * |
| 22 | * @return bool |
| 23 | */ |
| 24 | public function is_null( $value ) { |
| 25 | return null === $value; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * @param mixed $value |
| 30 | * |
| 31 | * @return bool |
| 32 | */ |
| 33 | public function is_string( $value ) { |
| 34 | return ! empty( $value ) && is_string( $value ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @param mixed $value |
| 39 | * |
| 40 | * @return bool |
| 41 | */ |
| 42 | public function is_string_or_empty( $value ) { |
| 43 | if ( empty( $value ) ) { |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | return $this->is_string( $value ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Whether the value is a timestamp or a string parseable by the strtotime function or not. |
| 52 | * |
| 53 | * @param mixed $value |
| 54 | * |
| 55 | * @return bool |
| 56 | */ |
| 57 | public function is_time( $value ) { |
| 58 | return is_numeric( $value ) || ( is_string( $value ) && strtotime( $value ) ); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Whether the value corresponds to an existing user ID or not. |
| 63 | * |
| 64 | * @param mixed $value |
| 65 | * |
| 66 | * @return bool |
| 67 | */ |
| 68 | public function is_user_id( $value ) { |
| 69 | return is_numeric( $value ) && (bool) get_user_by( 'ID', $value ); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Whether the value is a positive integer or not. |
| 74 | * |
| 75 | * @param mixed $value |
| 76 | * |
| 77 | * @return bool |
| 78 | */ |
| 79 | public function is_positive_int( $value ) { |
| 80 | return is_numeric( $value ) && (int) $value == $value && (int) $value > 0; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Whether the value is a list of positive integers only or not. |
| 85 | * |
| 86 | * @since 4.7.19 |
| 87 | * |
| 88 | * @param array|string|int $list |
| 89 | * @param string $sep |
| 90 | * |
| 91 | * @return bool |
| 92 | */ |
| 93 | public function is_positive_int_list( $list, $sep = ',' ) { |
| 94 | $sep = is_string( $sep ) ? $sep : ','; |
| 95 | $list = Tribe__Utils__Array::list_to_array( $list, $sep ); |
| 96 | |
| 97 | $valid = array_filter( $list, [ $this, 'is_positive_int' ] ); |
| 98 | |
| 99 | return ! empty( $valid ) && count( $valid ) === count( $list ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Trims a string. |
| 104 | * |
| 105 | * Differently from the trim method it will not use the second argument. |
| 106 | * |
| 107 | * @param string $value |
| 108 | * |
| 109 | * @return string |
| 110 | */ |
| 111 | public function trim( $value ) { |
| 112 | return is_string( $value ) ? trim( $value ) : $value; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Whether the value(s) all map to existing post tags. |
| 117 | * |
| 118 | * @param mixed $tag |
| 119 | * |
| 120 | * @return bool |
| 121 | */ |
| 122 | public function is_post_tag( $tag ) { |
| 123 | return $this->is_term_of_taxonomy( $tag, 'post_tag' ); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Whether the term exists and is a term of the specified taxonomy. |
| 128 | * |
| 129 | * @param mixed $term Either a single term `term_id` or `slug` or an array of |
| 130 | * `term_id`s and `slug`s |
| 131 | * @param string $taxonomy |
| 132 | * |
| 133 | * @return bool |
| 134 | */ |
| 135 | public function is_term_of_taxonomy( $term, $taxonomy ) { |
| 136 | $terms = Tribe__Utils__Array::list_to_array( $term, ',' ); |
| 137 | |
| 138 | if ( empty( $terms ) ) { |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | foreach ( $terms as $t ) { |
| 143 | if ( ! term_exists( $t, $taxonomy ) ) { |
| 144 | return false; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | return true; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Whether the provided value points to an existing attachment ID or an existing image URL. |
| 153 | * |
| 154 | * @param int|string $image |
| 155 | * |
| 156 | * @return mixed |
| 157 | */ |
| 158 | public function is_image( $image ) { |
| 159 | if ( $this->is_numeric( $image ) ) { |
| 160 | return wp_attachment_is_image( $image ); |
| 161 | } |
| 162 | |
| 163 | if ( is_string( $image ) ) { |
| 164 | $response = wp_remote_head( $image ); |
| 165 | |
| 166 | if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | $content_type = wp_remote_retrieve_header( $response, 'content-type' ); |
| 171 | |
| 172 | if ( empty( $content_type ) || 0 !== strpos( $content_type, 'image' ) ) { |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | $allowed_mime_types = get_allowed_mime_types(); |
| 177 | |
| 178 | return ( in_array( $content_type, $allowed_mime_types ) ); |
| 179 | } |
| 180 | |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Whether the provided value points to an existing attachment ID, an existing image URL, or is empty. |
| 186 | * |
| 187 | * @param int|string $image |
| 188 | * |
| 189 | * @return mixed |
| 190 | */ |
| 191 | public function is_image_or_empty( $image ) { |
| 192 | if ( empty( $image ) ) { |
| 193 | return true; |
| 194 | } |
| 195 | |
| 196 | return $this->is_image( $image ); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * @param mixed $value |
| 201 | * |
| 202 | * @return bool |
| 203 | */ |
| 204 | public function is_numeric( $value ) { |
| 205 | return is_numeric( $value ); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Whether a string represents a valid array or not. |
| 210 | * |
| 211 | * Valid means that the string looks like a URL, not that the URL is online and reachable. |
| 212 | * |
| 213 | * @param string $input |
| 214 | * |
| 215 | * @return bool |
| 216 | */ |
| 217 | public function is_url( $input ) { |
| 218 | return (bool) filter_var( $input, FILTER_VALIDATE_URL ); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Whether a string represents a valid array or not. |
| 223 | * |
| 224 | * Valid means that the string looks like a URL, not that the URL is online and reachable. |
| 225 | * |
| 226 | * @param string $input |
| 227 | * |
| 228 | * @return bool |
| 229 | */ |
| 230 | public function is_url_or_empty( $input ) { |
| 231 | if ( empty( $input ) ) { |
| 232 | return true; |
| 233 | } |
| 234 | |
| 235 | return $this->is_url( $input ); |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Whether a string represents a valid and registered post status or not. |
| 240 | * |
| 241 | * @param string $post_status |
| 242 | * |
| 243 | * @return bool |
| 244 | */ |
| 245 | public function is_post_status( $post_status ) { |
| 246 | $post_stati = get_post_stati(); |
| 247 | if ( empty( $post_stati ) ) { |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | return in_array( $post_status, $post_stati ); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Converts a string, a CSV list to an array. |
| 256 | * |
| 257 | * @since 4.7.19 |
| 258 | * |
| 259 | * @param string|array $list |
| 260 | * |
| 261 | * @return array |
| 262 | */ |
| 263 | public function list_to_array( $list ) { |
| 264 | return Tribe__Utils__Array::list_to_array( $list ); |
| 265 | } |
| 266 | } |
| 267 |