block-editor
3 years ago
css
3 years ago
js
3 years ago
swv
3 years ago
capabilities.php
7 years ago
config-validator.php
3 years ago
contact-form-functions.php
3 years ago
contact-form-template.php
3 years ago
contact-form.php
3 years ago
controller.php
3 years ago
file.php
3 years ago
form-tag.php
3 years ago
form-tags-manager.php
3 years ago
formatting.php
3 years ago
functions.php
3 years ago
html-formatter.php
3 years ago
integration.php
3 years ago
l10n.php
3 years ago
mail.php
3 years ago
pipe.php
4 years ago
pocket-holder.php
3 years ago
rest-api.php
4 years ago
shortcodes.php
3 years ago
special-mail-tags.php
3 years ago
submission.php
3 years ago
upgrade.php
7 years ago
validation-functions.php
3 years ago
validation.php
3 years ago
form-tag.php
566 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * A form-tag. |
| 5 | * |
| 6 | * @link https://contactform7.com/tag-syntax/#form_tag |
| 7 | */ |
| 8 | class WPCF7_FormTag implements ArrayAccess { |
| 9 | |
| 10 | public $type; |
| 11 | public $basetype; |
| 12 | public $raw_name = ''; |
| 13 | public $name = ''; |
| 14 | public $options = array(); |
| 15 | public $raw_values = array(); |
| 16 | public $values = array(); |
| 17 | public $pipes; |
| 18 | public $labels = array(); |
| 19 | public $attr = ''; |
| 20 | public $content = ''; |
| 21 | |
| 22 | public function __construct( $tag = array() ) { |
| 23 | if ( is_array( $tag ) |
| 24 | or $tag instanceof self ) { |
| 25 | foreach ( $tag as $key => $value ) { |
| 26 | if ( property_exists( __CLASS__, $key ) ) { |
| 27 | $this->{$key} = $value; |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | |
| 34 | /** |
| 35 | * Returns true if the type has a trailing asterisk. |
| 36 | */ |
| 37 | public function is_required() { |
| 38 | return ( '*' === substr( $this->type, -1 ) ); |
| 39 | } |
| 40 | |
| 41 | |
| 42 | /** |
| 43 | * Returns true if the form-tag has a specified option. |
| 44 | */ |
| 45 | public function has_option( $option_name ) { |
| 46 | $pattern = sprintf( '/^%s(:.+)?$/i', preg_quote( $option_name, '/' ) ); |
| 47 | return (bool) preg_grep( $pattern, $this->options ); |
| 48 | } |
| 49 | |
| 50 | |
| 51 | /** |
| 52 | * Retrieves option values with the specified option name. |
| 53 | * |
| 54 | * @param string $option_name Option name. |
| 55 | * @param string $pattern Optional. A regular expression pattern or one of |
| 56 | * the keys of preset patterns. If specified, only options |
| 57 | * whose value part matches this pattern will be returned. |
| 58 | * @param bool $single Optional. If true, only the first matching option |
| 59 | * will be returned. Default false. |
| 60 | * @return string|array|bool The option value or an array of option values. |
| 61 | * False if there is no option matches the pattern. |
| 62 | */ |
| 63 | public function get_option( $option_name, $pattern = '', $single = false ) { |
| 64 | $preset_patterns = array( |
| 65 | 'date' => '[0-9]{4}-[0-9]{2}-[0-9]{2}', |
| 66 | 'int' => '[0-9]+', |
| 67 | 'signed_int' => '[-]?[0-9]+', |
| 68 | 'num' => '(?:[0-9]+|(?:[0-9]+)?[.][0-9]+)', |
| 69 | 'signed_num' => '[-]?(?:[0-9]+|(?:[0-9]+)?[.][0-9]+)', |
| 70 | 'class' => '[-0-9a-zA-Z_]+', |
| 71 | 'id' => '[-0-9a-zA-Z_]+', |
| 72 | ); |
| 73 | |
| 74 | if ( isset( $preset_patterns[$pattern] ) ) { |
| 75 | $pattern = $preset_patterns[$pattern]; |
| 76 | } |
| 77 | |
| 78 | if ( '' == $pattern ) { |
| 79 | $pattern = '.+'; |
| 80 | } |
| 81 | |
| 82 | $pattern = sprintf( |
| 83 | '/^%s:%s$/i', |
| 84 | preg_quote( $option_name, '/' ), |
| 85 | $pattern |
| 86 | ); |
| 87 | |
| 88 | if ( $single ) { |
| 89 | $matches = $this->get_first_match_option( $pattern ); |
| 90 | |
| 91 | if ( ! $matches ) { |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | return substr( $matches[0], strlen( $option_name ) + 1 ); |
| 96 | } else { |
| 97 | $matches_a = $this->get_all_match_options( $pattern ); |
| 98 | |
| 99 | if ( ! $matches_a ) { |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | $results = array(); |
| 104 | |
| 105 | foreach ( $matches_a as $matches ) { |
| 106 | $results[] = substr( $matches[0], strlen( $option_name ) + 1 ); |
| 107 | } |
| 108 | |
| 109 | return $results; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | |
| 114 | /** |
| 115 | * Retrieves the id option value from the form-tag. |
| 116 | */ |
| 117 | public function get_id_option() { |
| 118 | return $this->get_option( 'id', 'id', true ); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | /** |
| 123 | * Retrieves the class option value from the form-tag. |
| 124 | * |
| 125 | * @param string|array $default_classes Optional. Preset classes as an array |
| 126 | * or a whitespace-separated list. Default empty string. |
| 127 | * @return string|bool A whitespace-separated list of classes. |
| 128 | * False if there is no class to return. |
| 129 | */ |
| 130 | public function get_class_option( $default_classes = '' ) { |
| 131 | if ( is_string( $default_classes ) ) { |
| 132 | $default_classes = explode( ' ', $default_classes ); |
| 133 | } |
| 134 | |
| 135 | $options = array_merge( |
| 136 | (array) $default_classes, |
| 137 | (array) $this->get_option( 'class', 'class' ) |
| 138 | ); |
| 139 | |
| 140 | $options = array_filter( array_unique( $options ) ); |
| 141 | |
| 142 | if ( empty( $options ) ) { |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | return implode( ' ', $options ); |
| 147 | } |
| 148 | |
| 149 | |
| 150 | /** |
| 151 | * Retrieves the size option value from the form-tag. |
| 152 | * |
| 153 | * @param string $default_value Optional default value. |
| 154 | * @return string The option value. |
| 155 | */ |
| 156 | public function get_size_option( $default_value = false ) { |
| 157 | $option = $this->get_option( 'size', 'int', true ); |
| 158 | |
| 159 | if ( $option ) { |
| 160 | return $option; |
| 161 | } |
| 162 | |
| 163 | $matches_a = $this->get_all_match_options( '%^([0-9]*)/[0-9]*$%' ); |
| 164 | |
| 165 | foreach ( (array) $matches_a as $matches ) { |
| 166 | if ( isset( $matches[1] ) and '' !== $matches[1] ) { |
| 167 | return $matches[1]; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | return $default_value; |
| 172 | } |
| 173 | |
| 174 | |
| 175 | /** |
| 176 | * Retrieves the maxlength option value from the form-tag. |
| 177 | * |
| 178 | * @param string $default_value Optional default value. |
| 179 | * @return string The option value. |
| 180 | */ |
| 181 | public function get_maxlength_option( $default_value = false ) { |
| 182 | $option = $this->get_option( 'maxlength', 'int', true ); |
| 183 | |
| 184 | if ( $option ) { |
| 185 | return $option; |
| 186 | } |
| 187 | |
| 188 | $matches_a = $this->get_all_match_options( |
| 189 | '%^(?:[0-9]*x?[0-9]*)?/([0-9]+)$%' |
| 190 | ); |
| 191 | |
| 192 | foreach ( (array) $matches_a as $matches ) { |
| 193 | if ( isset( $matches[1] ) and '' !== $matches[1] ) { |
| 194 | return $matches[1]; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | return $default_value; |
| 199 | } |
| 200 | |
| 201 | |
| 202 | /** |
| 203 | * Retrieves the minlength option value from the form-tag. |
| 204 | * |
| 205 | * @param string $default_value Optional default value. |
| 206 | * @return string The option value. |
| 207 | */ |
| 208 | public function get_minlength_option( $default_value = false ) { |
| 209 | $option = $this->get_option( 'minlength', 'int', true ); |
| 210 | |
| 211 | if ( $option ) { |
| 212 | return $option; |
| 213 | } else { |
| 214 | return $default_value; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | |
| 219 | /** |
| 220 | * Retrieves the cols option value from the form-tag. |
| 221 | * |
| 222 | * @param string $default_value Optional default value. |
| 223 | * @return string The option value. |
| 224 | */ |
| 225 | public function get_cols_option( $default_value = false ) { |
| 226 | $option = $this->get_option( 'cols', 'int', true ); |
| 227 | |
| 228 | if ( $option ) { |
| 229 | return $option; |
| 230 | } |
| 231 | |
| 232 | $matches_a = $this->get_all_match_options( |
| 233 | '%^([0-9]*)x([0-9]*)(?:/[0-9]+)?$%' |
| 234 | ); |
| 235 | |
| 236 | foreach ( (array) $matches_a as $matches ) { |
| 237 | if ( isset( $matches[1] ) and '' !== $matches[1] ) { |
| 238 | return $matches[1]; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | return $default_value; |
| 243 | } |
| 244 | |
| 245 | |
| 246 | /** |
| 247 | * Retrieves the rows option value from the form-tag. |
| 248 | * |
| 249 | * @param string $default_value Optional default value. |
| 250 | * @return string The option value. |
| 251 | */ |
| 252 | public function get_rows_option( $default_value = false ) { |
| 253 | $option = $this->get_option( 'rows', 'int', true ); |
| 254 | |
| 255 | if ( $option ) { |
| 256 | return $option; |
| 257 | } |
| 258 | |
| 259 | $matches_a = $this->get_all_match_options( |
| 260 | '%^([0-9]*)x([0-9]*)(?:/[0-9]+)?$%' |
| 261 | ); |
| 262 | |
| 263 | foreach ( (array) $matches_a as $matches ) { |
| 264 | if ( isset( $matches[2] ) and '' !== $matches[2] ) { |
| 265 | return $matches[2]; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | return $default_value; |
| 270 | } |
| 271 | |
| 272 | |
| 273 | /** |
| 274 | * Retrieves a date-type option value from the form-tag. |
| 275 | * |
| 276 | * @param string $option_name A date-type option name, such as 'min' or 'max'. |
| 277 | * @return string|bool The option value in YYYY-MM-DD format. False if the |
| 278 | * option does not exist or the date value is invalid. |
| 279 | */ |
| 280 | public function get_date_option( $option_name ) { |
| 281 | $option_value = $this->get_option( $option_name, '', true ); |
| 282 | |
| 283 | if ( empty( $option_value ) ) { |
| 284 | return false; |
| 285 | } |
| 286 | |
| 287 | $date = apply_filters( 'wpcf7_form_tag_date_option', |
| 288 | null, |
| 289 | array( $option_name => $option_value ) |
| 290 | ); |
| 291 | |
| 292 | if ( $date ) { |
| 293 | $date_pattern = '/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/'; |
| 294 | |
| 295 | if ( preg_match( $date_pattern, $date, $matches ) |
| 296 | and checkdate( $matches[2], $matches[3], $matches[1] ) ) { |
| 297 | return $date; |
| 298 | } |
| 299 | } else { |
| 300 | $datetime_obj = date_create_immutable( |
| 301 | preg_replace( '/[_]+/', ' ', $option_value ), |
| 302 | wp_timezone() |
| 303 | ); |
| 304 | |
| 305 | if ( $datetime_obj ) { |
| 306 | return $datetime_obj->format( 'Y-m-d' ); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | return false; |
| 311 | } |
| 312 | |
| 313 | |
| 314 | /** |
| 315 | * Retrieves the default option value from the form-tag. |
| 316 | * |
| 317 | * @param string|array $default_value Optional default value. |
| 318 | * @param string|array $args Optional options for the option value retrieval. |
| 319 | * @return string|array The option value. If the multiple option is enabled, |
| 320 | * an array of option values. |
| 321 | */ |
| 322 | public function get_default_option( $default_value = '', $args = '' ) { |
| 323 | $args = wp_parse_args( $args, array( |
| 324 | 'multiple' => false, |
| 325 | 'shifted' => false, |
| 326 | ) ); |
| 327 | |
| 328 | $options = (array) $this->get_option( 'default' ); |
| 329 | $values = array(); |
| 330 | |
| 331 | if ( empty( $options ) ) { |
| 332 | return $args['multiple'] ? $values : $default_value; |
| 333 | } |
| 334 | |
| 335 | foreach ( $options as $opt ) { |
| 336 | $opt = sanitize_key( $opt ); |
| 337 | |
| 338 | if ( 'user_' == substr( $opt, 0, 5 ) and is_user_logged_in() ) { |
| 339 | $primary_props = array( 'user_login', 'user_email', 'user_url' ); |
| 340 | $opt = in_array( $opt, $primary_props ) ? $opt : substr( $opt, 5 ); |
| 341 | |
| 342 | $user = wp_get_current_user(); |
| 343 | $user_prop = $user->get( $opt ); |
| 344 | |
| 345 | if ( ! empty( $user_prop ) ) { |
| 346 | if ( $args['multiple'] ) { |
| 347 | $values[] = $user_prop; |
| 348 | } else { |
| 349 | return $user_prop; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | } elseif ( 'post_meta' === $opt and in_the_loop() ) { |
| 354 | if ( $args['multiple'] ) { |
| 355 | $values = array_merge( $values, |
| 356 | get_post_meta( get_the_ID(), $this->name ) |
| 357 | ); |
| 358 | } else { |
| 359 | $val = (string) get_post_meta( get_the_ID(), $this->name, true ); |
| 360 | |
| 361 | if ( strlen( $val ) ) { |
| 362 | return $val; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | } elseif ( 'get' === $opt and isset( $_GET[$this->name] ) ) { |
| 367 | $vals = (array) $_GET[$this->name]; |
| 368 | $vals = array_map( 'wpcf7_sanitize_query_var', $vals ); |
| 369 | |
| 370 | if ( $args['multiple'] ) { |
| 371 | $values = array_merge( $values, $vals ); |
| 372 | } else { |
| 373 | $val = isset( $vals[0] ) ? (string) $vals[0] : ''; |
| 374 | |
| 375 | if ( strlen( $val ) ) { |
| 376 | return $val; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | } elseif ( 'post' === $opt and isset( $_POST[$this->name] ) ) { |
| 381 | $vals = (array) $_POST[$this->name]; |
| 382 | $vals = array_map( 'wpcf7_sanitize_query_var', $vals ); |
| 383 | |
| 384 | if ( $args['multiple'] ) { |
| 385 | $values = array_merge( $values, $vals ); |
| 386 | } else { |
| 387 | $val = isset( $vals[0] ) ? (string) $vals[0] : ''; |
| 388 | |
| 389 | if ( strlen( $val ) ) { |
| 390 | return $val; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | } elseif ( 'shortcode_attr' === $opt ) { |
| 395 | if ( $contact_form = WPCF7_ContactForm::get_current() ) { |
| 396 | $val = $contact_form->shortcode_attr( $this->name ); |
| 397 | |
| 398 | if ( strlen( $val ) ) { |
| 399 | if ( $args['multiple'] ) { |
| 400 | $values[] = $val; |
| 401 | } else { |
| 402 | return $val; |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | } elseif ( preg_match( '/^[0-9_]+$/', $opt ) ) { |
| 408 | $nums = explode( '_', $opt ); |
| 409 | |
| 410 | foreach ( $nums as $num ) { |
| 411 | $num = absint( $num ); |
| 412 | $num = $args['shifted'] ? $num : $num - 1; |
| 413 | |
| 414 | if ( isset( $this->values[$num] ) ) { |
| 415 | if ( $args['multiple'] ) { |
| 416 | $values[] = $this->values[$num]; |
| 417 | } else { |
| 418 | return $this->values[$num]; |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | if ( $args['multiple'] ) { |
| 426 | $values = array_unique( $values ); |
| 427 | return $values; |
| 428 | } else { |
| 429 | return $default_value; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | |
| 434 | /** |
| 435 | * Retrieves the data option value from the form-tag. |
| 436 | * |
| 437 | * @param string|array $args Optional options for the option value retrieval. |
| 438 | * @return mixed The option value. |
| 439 | */ |
| 440 | public function get_data_option( $args = '' ) { |
| 441 | $options = (array) $this->get_option( 'data' ); |
| 442 | |
| 443 | return apply_filters( 'wpcf7_form_tag_data_option', null, $options, $args ); |
| 444 | } |
| 445 | |
| 446 | |
| 447 | /** |
| 448 | * Retrieves the limit option value from the form-tag. |
| 449 | * |
| 450 | * @param int $default_value Optional default value. Default 1048576. |
| 451 | * @return int The option value. |
| 452 | */ |
| 453 | public function get_limit_option( $default_value = MB_IN_BYTES ) { |
| 454 | $pattern = '/^limit:([1-9][0-9]*)([kKmM]?[bB])?$/'; |
| 455 | |
| 456 | $matches = $this->get_first_match_option( $pattern ); |
| 457 | |
| 458 | if ( $matches ) { |
| 459 | $size = (int) $matches[1]; |
| 460 | |
| 461 | if ( ! empty( $matches[2] ) ) { |
| 462 | $kbmb = strtolower( $matches[2] ); |
| 463 | |
| 464 | if ( 'kb' === $kbmb ) { |
| 465 | $size *= KB_IN_BYTES; |
| 466 | } elseif ( 'mb' === $kbmb ) { |
| 467 | $size *= MB_IN_BYTES; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | return $size; |
| 472 | } |
| 473 | |
| 474 | return (int) $default_value; |
| 475 | } |
| 476 | |
| 477 | |
| 478 | /** |
| 479 | * Retrieves the value of the first option matches the given |
| 480 | * regular expression pattern. |
| 481 | * |
| 482 | * @param string $pattern Regular expression pattern. |
| 483 | * @return array|bool Option value as an array of matched strings. |
| 484 | * False if there is no option matches the pattern. |
| 485 | */ |
| 486 | public function get_first_match_option( $pattern ) { |
| 487 | foreach( (array) $this->options as $option ) { |
| 488 | if ( preg_match( $pattern, $option, $matches ) ) { |
| 489 | return $matches; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | return false; |
| 494 | } |
| 495 | |
| 496 | |
| 497 | /** |
| 498 | * Retrieves values of options that match the given |
| 499 | * regular expression pattern. |
| 500 | * |
| 501 | * @param string $pattern Regular expression pattern. |
| 502 | * @return array Array of arrays of strings that match the pattern. |
| 503 | */ |
| 504 | public function get_all_match_options( $pattern ) { |
| 505 | $result = array(); |
| 506 | |
| 507 | foreach( (array) $this->options as $option ) { |
| 508 | if ( preg_match( $pattern, $option, $matches ) ) { |
| 509 | $result[] = $matches; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | return $result; |
| 514 | } |
| 515 | |
| 516 | |
| 517 | /** |
| 518 | * Assigns a value to the specified offset. |
| 519 | * |
| 520 | * @link https://www.php.net/manual/en/arrayaccess.offsetset.php |
| 521 | */ |
| 522 | #[ReturnTypeWillChange] |
| 523 | public function offsetSet( $offset, $value ) { |
| 524 | if ( property_exists( __CLASS__, $offset ) ) { |
| 525 | $this->{$offset} = $value; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | |
| 530 | /** |
| 531 | * Returns the value at specified offset. |
| 532 | * |
| 533 | * @link https://www.php.net/manual/en/arrayaccess.offsetget.php |
| 534 | */ |
| 535 | #[ReturnTypeWillChange] |
| 536 | public function offsetGet( $offset ) { |
| 537 | if ( property_exists( __CLASS__, $offset ) ) { |
| 538 | return $this->{$offset}; |
| 539 | } |
| 540 | |
| 541 | return null; |
| 542 | } |
| 543 | |
| 544 | |
| 545 | /** |
| 546 | * Returns true if the specified offset exists. |
| 547 | * |
| 548 | * @link https://www.php.net/manual/en/arrayaccess.offsetexists.php |
| 549 | */ |
| 550 | #[ReturnTypeWillChange] |
| 551 | public function offsetExists( $offset ) { |
| 552 | return property_exists( __CLASS__, $offset ); |
| 553 | } |
| 554 | |
| 555 | |
| 556 | /** |
| 557 | * Unsets an offset. |
| 558 | * |
| 559 | * @link https://www.php.net/manual/en/arrayaccess.offsetunset.php |
| 560 | */ |
| 561 | #[ReturnTypeWillChange] |
| 562 | public function offsetUnset( $offset ) { |
| 563 | } |
| 564 | |
| 565 | } |
| 566 |