css
5 years ago
js
5 years ago
capabilities.php
7 years ago
config-validator.php
5 years ago
contact-form-functions.php
5 years ago
contact-form-template.php
6 years ago
contact-form.php
5 years ago
controller.php
7 years ago
form-tag.php
5 years ago
form-tags-manager.php
6 years ago
formatting.php
5 years ago
functions.php
5 years ago
integration.php
7 years ago
l10n.php
7 years ago
mail.php
5 years ago
pipe.php
5 years ago
rest-api.php
5 years ago
shortcodes.php
9 years ago
special-mail-tags.php
5 years ago
submission.php
5 years ago
upgrade.php
7 years ago
validation.php
7 years ago
form-tag.php
393 lines
| 1 | <?php |
| 2 | |
| 3 | class WPCF7_FormTag implements ArrayAccess { |
| 4 | |
| 5 | public $type; |
| 6 | public $basetype; |
| 7 | public $name = ''; |
| 8 | public $options = array(); |
| 9 | public $raw_values = array(); |
| 10 | public $values = array(); |
| 11 | public $pipes; |
| 12 | public $labels = array(); |
| 13 | public $attr = ''; |
| 14 | public $content = ''; |
| 15 | |
| 16 | public function __construct( $tag = array() ) { |
| 17 | if ( is_array( $tag ) |
| 18 | or $tag instanceof self ) { |
| 19 | foreach ( $tag as $key => $value ) { |
| 20 | if ( property_exists( __CLASS__, $key ) ) { |
| 21 | $this->{$key} = $value; |
| 22 | } |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | public function is_required() { |
| 28 | return ( '*' == substr( $this->type, -1 ) ); |
| 29 | } |
| 30 | |
| 31 | public function has_option( $opt ) { |
| 32 | $pattern = sprintf( '/^%s(:.+)?$/i', preg_quote( $opt, '/' ) ); |
| 33 | return (bool) preg_grep( $pattern, $this->options ); |
| 34 | } |
| 35 | |
| 36 | public function get_option( $opt, $pattern = '', $single = false ) { |
| 37 | $preset_patterns = array( |
| 38 | 'date' => '([0-9]{4}-[0-9]{2}-[0-9]{2}|today(.*))', |
| 39 | 'int' => '[0-9]+', |
| 40 | 'signed_int' => '-?[0-9]+', |
| 41 | 'class' => '[-0-9a-zA-Z_]+', |
| 42 | 'id' => '[-0-9a-zA-Z_]+', |
| 43 | ); |
| 44 | |
| 45 | if ( isset( $preset_patterns[$pattern] ) ) { |
| 46 | $pattern = $preset_patterns[$pattern]; |
| 47 | } |
| 48 | |
| 49 | if ( '' == $pattern ) { |
| 50 | $pattern = '.+'; |
| 51 | } |
| 52 | |
| 53 | $pattern = sprintf( '/^%s:%s$/i', preg_quote( $opt, '/' ), $pattern ); |
| 54 | |
| 55 | if ( $single ) { |
| 56 | $matches = $this->get_first_match_option( $pattern ); |
| 57 | |
| 58 | if ( ! $matches ) { |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | return substr( $matches[0], strlen( $opt ) + 1 ); |
| 63 | } else { |
| 64 | $matches_a = $this->get_all_match_options( $pattern ); |
| 65 | |
| 66 | if ( ! $matches_a ) { |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | $results = array(); |
| 71 | |
| 72 | foreach ( $matches_a as $matches ) { |
| 73 | $results[] = substr( $matches[0], strlen( $opt ) + 1 ); |
| 74 | } |
| 75 | |
| 76 | return $results; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | public function get_id_option() { |
| 81 | return $this->get_option( 'id', 'id', true ); |
| 82 | } |
| 83 | |
| 84 | public function get_class_option( $default = '' ) { |
| 85 | if ( is_string( $default ) ) { |
| 86 | $default = explode( ' ', $default ); |
| 87 | } |
| 88 | |
| 89 | $options = array_merge( |
| 90 | (array) $default, |
| 91 | (array) $this->get_option( 'class', 'class' ) ); |
| 92 | |
| 93 | $options = array_filter( array_unique( $options ) ); |
| 94 | |
| 95 | return implode( ' ', $options ); |
| 96 | } |
| 97 | |
| 98 | public function get_size_option( $default = '' ) { |
| 99 | $option = $this->get_option( 'size', 'int', true ); |
| 100 | |
| 101 | if ( $option ) { |
| 102 | return $option; |
| 103 | } |
| 104 | |
| 105 | $matches_a = $this->get_all_match_options( '%^([0-9]*)/[0-9]*$%' ); |
| 106 | |
| 107 | foreach ( (array) $matches_a as $matches ) { |
| 108 | if ( isset( $matches[1] ) |
| 109 | and '' !== $matches[1] ) { |
| 110 | return $matches[1]; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | return $default; |
| 115 | } |
| 116 | |
| 117 | public function get_maxlength_option( $default = '' ) { |
| 118 | $option = $this->get_option( 'maxlength', 'int', true ); |
| 119 | |
| 120 | if ( $option ) { |
| 121 | return $option; |
| 122 | } |
| 123 | |
| 124 | $matches_a = $this->get_all_match_options( |
| 125 | '%^(?:[0-9]*x?[0-9]*)?/([0-9]+)$%' ); |
| 126 | |
| 127 | foreach ( (array) $matches_a as $matches ) { |
| 128 | if ( isset( $matches[1] ) && '' !== $matches[1] ) { |
| 129 | return $matches[1]; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | return $default; |
| 134 | } |
| 135 | |
| 136 | public function get_minlength_option( $default = '' ) { |
| 137 | $option = $this->get_option( 'minlength', 'int', true ); |
| 138 | |
| 139 | if ( $option ) { |
| 140 | return $option; |
| 141 | } else { |
| 142 | return $default; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | public function get_cols_option( $default = '' ) { |
| 147 | $option = $this->get_option( 'cols', 'int', true ); |
| 148 | |
| 149 | if ( $option ) { |
| 150 | return $option; |
| 151 | } |
| 152 | |
| 153 | $matches_a = $this->get_all_match_options( |
| 154 | '%^([0-9]*)x([0-9]*)(?:/[0-9]+)?$%' ); |
| 155 | |
| 156 | foreach ( (array) $matches_a as $matches ) { |
| 157 | if ( isset( $matches[1] ) && '' !== $matches[1] ) { |
| 158 | return $matches[1]; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | return $default; |
| 163 | } |
| 164 | |
| 165 | public function get_rows_option( $default = '' ) { |
| 166 | $option = $this->get_option( 'rows', 'int', true ); |
| 167 | |
| 168 | if ( $option ) { |
| 169 | return $option; |
| 170 | } |
| 171 | |
| 172 | $matches_a = $this->get_all_match_options( |
| 173 | '%^([0-9]*)x([0-9]*)(?:/[0-9]+)?$%' ); |
| 174 | |
| 175 | foreach ( (array) $matches_a as $matches ) { |
| 176 | if ( isset( $matches[2] ) |
| 177 | and '' !== $matches[2] ) { |
| 178 | return $matches[2]; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | return $default; |
| 183 | } |
| 184 | |
| 185 | public function get_date_option( $opt ) { |
| 186 | $option = $this->get_option( $opt, 'date', true ); |
| 187 | |
| 188 | if ( preg_match( '/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $option ) ) { |
| 189 | return $option; |
| 190 | } |
| 191 | |
| 192 | if ( preg_match( '/^today(?:([+-][0-9]+)([a-z]*))?/', $option, $matches ) ) { |
| 193 | $today = wp_date( 'Y-m-d' ); |
| 194 | $number = isset( $matches[1] ) ? (int) $matches[1] : 0; |
| 195 | $unit = isset( $matches[2] ) ? $matches[2] : ''; |
| 196 | |
| 197 | if ( ! preg_match( '/^(day|month|year|week)s?$/', $unit ) ) { |
| 198 | $unit = 'days'; |
| 199 | } |
| 200 | |
| 201 | $format = sprintf( '%1$s %2$s %3$s', $today, $number, $unit ); |
| 202 | |
| 203 | return gmdate( 'Y-m-d', strtotime( $format ) ); |
| 204 | } |
| 205 | |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | public function get_default_option( $default = '', $args = '' ) { |
| 210 | $args = wp_parse_args( $args, array( |
| 211 | 'multiple' => false, |
| 212 | 'shifted' => false, |
| 213 | ) ); |
| 214 | |
| 215 | $options = (array) $this->get_option( 'default' ); |
| 216 | $values = array(); |
| 217 | |
| 218 | if ( empty( $options ) ) { |
| 219 | return $args['multiple'] ? $values : $default; |
| 220 | } |
| 221 | |
| 222 | foreach ( $options as $opt ) { |
| 223 | $opt = sanitize_key( $opt ); |
| 224 | |
| 225 | if ( 'user_' == substr( $opt, 0, 5 ) |
| 226 | and is_user_logged_in() ) { |
| 227 | $primary_props = array( 'user_login', 'user_email', 'user_url' ); |
| 228 | $opt = in_array( $opt, $primary_props ) ? $opt : substr( $opt, 5 ); |
| 229 | |
| 230 | $user = wp_get_current_user(); |
| 231 | $user_prop = $user->get( $opt ); |
| 232 | |
| 233 | if ( ! empty( $user_prop ) ) { |
| 234 | if ( $args['multiple'] ) { |
| 235 | $values[] = $user_prop; |
| 236 | } else { |
| 237 | return $user_prop; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | } elseif ( 'post_meta' == $opt and in_the_loop() ) { |
| 242 | if ( $args['multiple'] ) { |
| 243 | $values = array_merge( $values, |
| 244 | get_post_meta( get_the_ID(), $this->name ) ); |
| 245 | } else { |
| 246 | $val = (string) get_post_meta( get_the_ID(), $this->name, true ); |
| 247 | |
| 248 | if ( strlen( $val ) ) { |
| 249 | return $val; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | } elseif ( 'get' == $opt and isset( $_GET[$this->name] ) ) { |
| 254 | $vals = (array) $_GET[$this->name]; |
| 255 | $vals = array_map( 'wpcf7_sanitize_query_var', $vals ); |
| 256 | |
| 257 | if ( $args['multiple'] ) { |
| 258 | $values = array_merge( $values, $vals ); |
| 259 | } else { |
| 260 | $val = isset( $vals[0] ) ? (string) $vals[0] : ''; |
| 261 | |
| 262 | if ( strlen( $val ) ) { |
| 263 | return $val; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | } elseif ( 'post' == $opt and isset( $_POST[$this->name] ) ) { |
| 268 | $vals = (array) $_POST[$this->name]; |
| 269 | $vals = array_map( 'wpcf7_sanitize_query_var', $vals ); |
| 270 | |
| 271 | if ( $args['multiple'] ) { |
| 272 | $values = array_merge( $values, $vals ); |
| 273 | } else { |
| 274 | $val = isset( $vals[0] ) ? (string) $vals[0] : ''; |
| 275 | |
| 276 | if ( strlen( $val ) ) { |
| 277 | return $val; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | } elseif ( 'shortcode_attr' == $opt ) { |
| 282 | if ( $contact_form = WPCF7_ContactForm::get_current() ) { |
| 283 | $val = $contact_form->shortcode_attr( $this->name ); |
| 284 | |
| 285 | if ( strlen( $val ) ) { |
| 286 | if ( $args['multiple'] ) { |
| 287 | $values[] = $val; |
| 288 | } else { |
| 289 | return $val; |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | } elseif ( preg_match( '/^[0-9_]+$/', $opt ) ) { |
| 295 | $nums = explode( '_', $opt ); |
| 296 | |
| 297 | foreach ( $nums as $num ) { |
| 298 | $num = absint( $num ); |
| 299 | $num = $args['shifted'] ? $num : $num - 1; |
| 300 | |
| 301 | if ( isset( $this->values[$num] ) ) { |
| 302 | if ( $args['multiple'] ) { |
| 303 | $values[] = $this->values[$num]; |
| 304 | } else { |
| 305 | return $this->values[$num]; |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | if ( $args['multiple'] ) { |
| 313 | $values = array_unique( $values ); |
| 314 | return $values; |
| 315 | } else { |
| 316 | return $default; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | public function get_data_option( $args = '' ) { |
| 321 | $options = (array) $this->get_option( 'data' ); |
| 322 | |
| 323 | return apply_filters( 'wpcf7_form_tag_data_option', null, $options, $args ); |
| 324 | } |
| 325 | |
| 326 | public function get_limit_option( $default = MB_IN_BYTES ) { |
| 327 | $pattern = '/^limit:([1-9][0-9]*)([kKmM]?[bB])?$/'; |
| 328 | |
| 329 | $matches = $this->get_first_match_option( $pattern ); |
| 330 | |
| 331 | if ( $matches ) { |
| 332 | $size = (int) $matches[1]; |
| 333 | |
| 334 | if ( ! empty( $matches[2] ) ) { |
| 335 | $kbmb = strtolower( $matches[2] ); |
| 336 | |
| 337 | if ( 'kb' == $kbmb ) { |
| 338 | $size *= KB_IN_BYTES; |
| 339 | } elseif ( 'mb' == $kbmb ) { |
| 340 | $size *= MB_IN_BYTES; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | return $size; |
| 345 | } |
| 346 | |
| 347 | return (int) $default; |
| 348 | } |
| 349 | |
| 350 | public function get_first_match_option( $pattern ) { |
| 351 | foreach( (array) $this->options as $option ) { |
| 352 | if ( preg_match( $pattern, $option, $matches ) ) { |
| 353 | return $matches; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | return false; |
| 358 | } |
| 359 | |
| 360 | public function get_all_match_options( $pattern ) { |
| 361 | $result = array(); |
| 362 | |
| 363 | foreach( (array) $this->options as $option ) { |
| 364 | if ( preg_match( $pattern, $option, $matches ) ) { |
| 365 | $result[] = $matches; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | return $result; |
| 370 | } |
| 371 | |
| 372 | public function offsetSet( $offset, $value ) { |
| 373 | if ( property_exists( __CLASS__, $offset ) ) { |
| 374 | $this->{$offset} = $value; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | public function offsetGet( $offset ) { |
| 379 | if ( property_exists( __CLASS__, $offset ) ) { |
| 380 | return $this->{$offset}; |
| 381 | } |
| 382 | |
| 383 | return null; |
| 384 | } |
| 385 | |
| 386 | public function offsetExists( $offset ) { |
| 387 | return property_exists( __CLASS__, $offset ); |
| 388 | } |
| 389 | |
| 390 | public function offsetUnset( $offset ) { |
| 391 | } |
| 392 | } |
| 393 |