| @@ -474,40 +474,53 @@ | ||
| 474 | 474 | * Users need the ability to pass {field:department} and get "Support", |
| 475 | 475 | * and {value:department} to get support@email.com |
| 476 | 476 | * |
| 477 | 477 | * @param string $text The text to parse. |
| 478 | - * @param int $entry_id The entry ID. | |
| 478 | + * @param int $entry_id The entry ID. Optional. Default null if tags to be replaced are on the frontend. | |
| 479 | 479 | * |
| 480 | 480 | * @return string $text The parsed text. |
| 481 | 481 | */ |
| 482 | - public static function replace_field_tags( $text, $entry_id ) { | |
| 482 | + public static function replace_field_tags( $text, $entry_id = null ) { | |
| 483 | 483 | // Validate data. |
| 484 | - if ( empty( $text ) || empty( $entry_id ) ) { | |
| 484 | + if ( empty( $text ) || !isset( $entry_id ) ) { | |
| 485 | 485 | return; |
| 486 | 486 | } |
| 487 | 487 | |
| 488 | 488 | // Users looking for {field:something} or {value:something}, determine which one. |
| 489 | - $is_field = preg_match( '/{field:(\w*)}/', $text, $matches_field ); | |
| 490 | - $is_value = preg_match( '/{value:(\w*)}/', $text, $matches_value ); | |
| 489 | + $is_field = preg_match_all( '/{field:(\w*)}/', $text, $matches_field ); | |
| 490 | + $is_value = preg_match_all( '/{value:(\w*)}/', $text, $matches_value ); | |
| 491 | 491 | |
| 492 | 492 | if ( $is_field ) { |
| 493 | - $meta_key = $matches_field[1]; | |
| 494 | - $meta_value = weforms_get_entry_meta( $entry_id, $meta_key, true ); | |
| 495 | - if ( is_array( $meta_value ) ) { | |
| 496 | - $meta_value = implode( WeForms::$field_separator, $meta_value ); | |
| 493 | + $meta_keys = $matches_field[1]; | |
| 494 | + // Create an array of meta values to replace. | |
| 495 | + $meta_values = array(); | |
| 496 | + foreach ( $meta_keys as $meta_key ) { | |
| 497 | + $meta_value = weforms_get_entry_meta( $entry_id, $meta_key, true ); | |
| 498 | + // Add values to the array. | |
| 499 | + array_push( $meta_values, $meta_value ); | |
| 500 | + if ( is_array( $meta_value ) ) { | |
| 501 | + $meta_value = implode( WeForms::$field_separator, $meta_value ); | |
| 502 | + } | |
| 497 | 503 | } |
| 498 | - // $text may include HTML tags, only replace tag that was matched. | |
| 499 | - $text = str_replace( $matches_field[0], $meta_value, $text ); | |
| 500 | - } elseif ( $is_value ) { | |
| 501 | - $meta_key = $matches_value[1]; | |
| 502 | - $form_field_values = WeForms_Form_Entry::get_form( $entry_id )->get_field_values()[ $meta_key ]['options']; | |
| 503 | - $meta_value = weforms_get_entry_meta( $entry_id, $meta_key, true ); | |
| 504 | - $modified_value = array_search( $meta_value, $form_field_values ); | |
| 505 | - if ( is_array( $modified_value ) ) { | |
| 506 | - $modified_value = implode( WeForms::$field_separator, $modified_value ); | |
| 504 | + // $text may include HTML tags, only replace tag that was matched. Replace all matches. | |
| 505 | + $text = str_replace( $matches_field[0], $meta_values, $text ); | |
| 506 | + } | |
| 507 | + if ( $is_value ) { | |
| 508 | + $meta_keys = $matches_value[1]; | |
| 509 | + // Create an array of modified values to replace. | |
| 510 | + $modified_values = array(); | |
| 511 | + foreach ( $meta_keys as $meta_key ) { | |
| 512 | + $form_field_values = WeForms_Form_Entry::get_form( $entry_id )->get_field_values()[ $meta_key ]['options']; | |
| 513 | + $meta_value = weforms_get_entry_meta( $entry_id, $meta_key, true ); | |
| 514 | + $modified_value = array_search( $meta_value, $form_field_values ); | |
| 515 | + // Add values to the array. | |
| 516 | + array_push( $modified_values, $modified_value ); | |
| 517 | + if ( is_array( $modified_value ) ) { | |
| 518 | + $modified_value = implode( WeForms::$field_separator, $modified_value ); | |
| 519 | + } | |
| 507 | 520 | } |
| 508 | 521 | // $text may include HTML tags, only replace tag that was matched. |
| 509 | - $text = str_replace( $matches_value[0], $modified_value, $text ); | |
| 522 | + $text = str_replace( $matches_value[0], $modified_values, $text ); | |
| 510 | 523 | } |
| 511 | 524 | return $text; |
| 512 | 525 | } |
| 513 | 526 | |