@@ -466,45 +466,53 @@ public function get_merge_value( $tag ) {
466466 * Parse out the custom fields with options or values. Since the options are what is stored, options may need to be
467467 * used to find the value from the field settings.
468468 *
469+ * For example, let's say we have the following options:
470+ * DEPARTMENT / EMAIL
471+ 472+ 473+ *
474+ * Users need the ability to pass {field:department} and get "Support",
475+ * and {value:department} to get [email protected] 476+ *
469477 * @param string $text
470478 * @param int $entry_id
471479 *
472480 * @return string
473481 */
474- public function replace_field_tags ( $ text , $ entry_id ) {
475- $ pattern_field = '/{field:(\w*)}/ ' ;
476- $ pattern_value = '/{value:(\w*)}/ ' ;
477- switch ( $ text ) {
478- case '' :
479- return $ text ;
480- break ;
481- case ( preg_match ( $ pattern_field , $ text , $ matches ) ? true : false ):
482- $ meta_key = $ matches [1 ];
483- $ meta_value = weforms_get_entry_meta ( $ entry_id , $ meta_key , true );
484- if ( is_array ( $ meta_value ) ) {
485- $ meta_value = implode ( WeForms::$ field_separator , $ meta_value );
486- }
487- $ text = str_replace ( $ matches [0 ], $ meta_value , $ text );
488- return $ text ;
489- break ;
490- case ( preg_match ( $ pattern_value , $ text , $ matches ) ? true : false ):
491- $ meta_key = $ matches [1 ];
492- $ form = weforms ()->form ->get ( $ this ->args ['form_id ' ] );
493- $ form_field = $ form ->get_field_values ();
494- $ form_field_values = $ form_field [ $ meta_key ]['options ' ];
495- $ meta_value = weforms_get_entry_meta ( $ entry_id , $ meta_key , true );
496- // The modified value is the value of the field in the form since it is not stored.
497- $ modified_value = array_search ( $ meta_value , $ form_field_values );
498- if ( is_array ( $ modified_value ) ) {
499- $ modified_value = implode ( WeForms::$ field_separator , $ modified_value );
500- }
501- $ text = str_replace ( $ matches [0 ], $ modified_value , $ text );
502- return $ text ;
503- break ;
504- default :
505- return $ text ;
506- break ;
482+ public static function replace_field_tags ( $ text , $ entry_id ) {
483+ // Validate data.
484+ if ( empty ( $ text ) || empty ( $ entry_id ) ) {
485+ return ;
507486 }
487+
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 );
491+
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 );
497+ }
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+ $ form_object = WeForms_Form_Entry::get_form_id ( $ entry_id );
502+ $ meta_key = $ matches_value [1 ];
503+ $ form = weforms ()->form ->get ( $ form_object [0 ]->form_id );
504+ $ form_field = $ form ->get_field_values ();
505+ $ form_field_values = $ form_field [ $ meta_key ]['options ' ];
506+ $ meta_value = weforms_get_entry_meta ( $ entry_id , $ meta_key , true );
507+ $ modified_value = array_search ( $ meta_value , $ form_field_values );
508+ if ( is_array ( $ modified_value ) ) {
509+ $ modified_value = implode ( WeForms::$ field_separator , $ modified_value );
510+ }
511+ // $text may include HTML tags, only replace tag that was matched.
512+ $ text = str_replace ( $ matches_value [0 ], $ modified_value , $ text );
513+ }
514+
515+ return $ text ;
508516 }
509517
510518 /**
@@ -619,8 +627,8 @@ public function replace_tags( $text = '' ) {
619627 $ merge_values = array_values ( $ this ->merge_tags );
620628
621629 $ text = str_replace ( $ merge_keys , $ merge_values , $ text );
622- $ text = $ this -> replace_field_tags ( $ text , $ this ->args ['entry_id ' ] );
623- $ text = $ this -> replace_file_tags ( $ text , $ this ->args ['entry_id ' ] );
630+ $ text = static :: replace_field_tags ( $ text , $ this ->args ['entry_id ' ] );
631+ $ text = static :: replace_file_tags ( $ text , $ this ->args ['entry_id ' ] );
624632
625633 return $ text ;
626634 }
0 commit comments