@@ -463,32 +463,51 @@ public function get_merge_value( $tag ) {
463463 }
464464
465465 /**
466- * Parse out the custom fields with entry meta values
466+ * Parse out the custom fields with options or values. Since the options are what is stored, options may need to be
467+ * used to find the value from the field settings.
467468 *
468- * @param string $text
469+ * For example, let's say we have the following options:
470+ * DEPARTMENT / EMAIL
471+ 472+ 469473 *
470- * @return string
474+ * Users need the ability to pass {field:department} and get "Support",
475+ * and {value:department} to get [email protected] 476+ *
477+ * @param string $text The text to parse.
478+ * @param int $entry_id The entry ID.
479+ *
480+ * @return string $text The parsed text.
471481 */
472482 public static function replace_field_tags ( $ text , $ entry_id ) {
473- $ pattern = '/{field:(\w*)}/ ' ;
474-
475- preg_match_all ( $ pattern , $ text , $ matches );
476-
477- // bail out if nothing found to be replaced
478- if ( !$ matches ) {
479- return $ text ;
483+ // Validate data.
484+ if ( empty ( $ text ) || empty ( $ entry_id ) ) {
485+ return ;
480486 }
481487
482- foreach ( $ matches [1 ] as $ index => $ meta_key ) {
483- $ meta_value = weforms_get_entry_meta ( $ entry_id , $ meta_key , true );
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 );
484491
492+ if ( $ is_field ) {
493+ $ meta_key = $ matches_field [1 ];
494+ $ meta_value = weforms_get_entry_meta ( $ entry_id , $ meta_key , true );
485495 if ( is_array ( $ meta_value ) ) {
486496 $ meta_value = implode ( WeForms::$ field_separator , $ meta_value );
487497 }
488-
489- $ text = str_replace ( $ matches [0 ][$ index ], $ meta_value , $ text );
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 );
507+ }
508+ // $text may include HTML tags, only replace tag that was matched.
509+ $ text = str_replace ( $ matches_value [0 ], $ modified_value , $ text );
490510 }
491-
492511 return $ text ;
493512 }
494513
@@ -662,7 +681,7 @@ public function replace_all_fields( $text = '' ) {
662681 $ table .= '— ' ;
663682 }
664683 } elseif ( in_array ( $ value ['type ' ], array ( 'google_map ' ) ) ) {
665- $ table .= $ field_value ['address ' ];
684+ $ table .= $ field_value ['address ' ];
666685 } else {
667686 $ table .= $ field_value ;
668687 }
0 commit comments