Skip to content

Commit 4e6e289

Browse files
committed
Fixed an issue with smart tags rendering: smart tag values were HTML-escaped when we need plain text
1 parent 1628091 commit 4e6e289

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

pdf-forms-for-wpforms.php

+38-4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class Pdf_Forms_For_WPForms
3737
private $storage = null;
3838
private $tmp_dir = null;
3939
private $wpforms_mail_attachments = array();
40+
private $need_plaintext_smart_tag_value_flag = false;
4041

4142
private function __construct()
4243
{
@@ -78,6 +79,8 @@ public function plugin_init()
7879
add_action( 'admin_menu', array( $this, 'register_services' ) );
7980

8081
add_filter( 'wpforms_save_form_args', array( $this, 'wpforms_save_form_args' ), 10, 3 );
82+
83+
add_filter( 'wpforms_smarttags_process_value', array( $this, 'plaintext_smart_tag_value_workaround_filter' ), PHP_INT_MAX, 6 );
8184
// fill_pdfs: we can't use wpforms_process_complete (because notifications have already been sent) and wpforms_process because uploaded files haven't been processed yet
8285
add_filter( 'wpforms_process_after_filter', array( $this, 'fill_pdfs' ), 999999, 3 );
8386
add_action( 'wpforms_process_complete', array( $this, 'remove_tmp_dir' ), 99, 0 );
@@ -839,6 +842,37 @@ public static function decode_form_settings( $data )
839842
return $form_settings;
840843
}
841844

845+
/**
846+
* Wrapper for WPForms' wpforms_process_smart_tags()
847+
*
848+
* The function wpforms_process_smart_tags() formats smart tag values for HTML output (via `wp_kses_post()`), but we need plain text.
849+
* Additional issue is that the content we are passing into wpforms_process_smart_tags() is plain text mixed with smart tags, but the smart tags will be formatted in HTML.
850+
* So, we need to use a workaround to make sure we get plain text smart tag values.
851+
*/
852+
public function wpforms_process_smart_tags( $content, $form_data, $fields = [], $entry_id = '', $context = '' )
853+
{
854+
// enable flag to convert smart tag values from HTML to plain text
855+
$this->need_plaintext_smart_tag_value_flag = true;
856+
857+
$value = wpforms_process_smart_tags( $content, $form_data, $fields, $entry_id, $context );
858+
859+
// disable conversion
860+
$this->need_plaintext_smart_tag_value_flag = false;
861+
862+
return $value;
863+
}
864+
865+
/**
866+
* Filter the smart tag values late to convert HTML to plain text when we are inside our own wpforms_process_smart_tags call
867+
*/
868+
public function plaintext_smart_tag_value_workaround_filter( $value, $tag_name, $form_data, $fields, $entry_id, $smart_tag_object )
869+
{
870+
if( $this->need_plaintext_smart_tag_value_flag )
871+
return html_entity_decode( strip_tags( $value ), ENT_QUOTES, 'UTF-8' );
872+
else
873+
return $value;
874+
}
875+
842876
/**
843877
* We need to fill the pdf's document fields and then create attachment file and attach them
844878
*/
@@ -881,7 +915,7 @@ public function fill_pdfs( $wpforms_fields, $entry, $form_data )
881915
if( isset( $embed["wpf_field"] ) )
882916
$url = $wpforms_fields[$embed["wpf_field"]]['value'];
883917
if( isset( $embed['smart_tags'] ) )
884-
$url = wpforms_process_smart_tags( $embed["smart_tags"], $form_data, $wpforms_fields, $entry_id );
918+
$url = $this->wpforms_process_smart_tags( $embed["smart_tags"], $form_data, $wpforms_fields, $entry_id );
885919

886920
if( $url != null )
887921
{
@@ -1034,7 +1068,7 @@ public function fill_pdfs( $wpforms_fields, $entry, $form_data )
10341068
$data[$field] = $wpforms_fields[$mapping["wpf_field"]]['value'];
10351069

10361070
if( isset( $mapping["smart_tags"] ) )
1037-
$data[$field] = wpforms_process_smart_tags( $mapping["smart_tags"], $form_data, $wpforms_fields, $entry_id );
1071+
$data[$field] = $this->wpforms_process_smart_tags( $mapping["smart_tags"], $form_data, $wpforms_fields, $entry_id );
10381072

10391073
if( $multiple )
10401074
{
@@ -1281,7 +1315,7 @@ public function fill_pdfs( $wpforms_fields, $entry, $form_data )
12811315

12821316
$destfilename = strval( $attachment['options']['filename'] );
12831317
if( $destfilename != "" )
1284-
$destfilename = strval( wpforms_process_smart_tags( $destfilename, $form_data, $wpforms_fields, $entry_id ) );
1318+
$destfilename = strval( $this->wpforms_process_smart_tags( $destfilename, $form_data, $wpforms_fields, $entry_id ) );
12851319
if( $destfilename == "" )
12861320
$destfilename = sanitize_file_name( get_the_title( $attachment_id ) );
12871321

@@ -1356,7 +1390,7 @@ public function fill_pdfs( $wpforms_fields, $entry, $form_data )
13561390
$path_elements = explode( "/", $save_directory );
13571391
$tag_replaced_path_elements = array();
13581392
foreach ( $path_elements as $key => $value )
1359-
$tag_replaced_path_elements[$key] = wpforms_process_smart_tags( $value, $form_data, $wpforms_fields, $entry_id );
1393+
$tag_replaced_path_elements[$key] = $this->wpforms_process_smart_tags( $value, $form_data, $wpforms_fields, $entry_id );
13601394

13611395
foreach( $tag_replaced_path_elements as $elmid => &$new_element )
13621396
{

0 commit comments

Comments
 (0)