Skip to content

Commit 85e9110

Browse files
committed
Added unavailable tag name tracking
1 parent de99239 commit 85e9110

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

js/admin.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ jQuery(document).ready(function($) {
9898
jQuery.ajax({
9999
url: wpcf7_pdf_forms.ajax_url,
100100
type: 'POST',
101-
data: { 'action': 'wpcf7_pdf_forms_query_tags', 'attachments': attachments, 'all': all, 'nonce': wpcf7_pdf_forms.ajax_nonce },
101+
data: { 'action': 'wpcf7_pdf_forms_query_tags', 'attachments': attachments, 'all': all, 'wpcf7-form': wpcf7_form.val(), 'nonce': wpcf7_pdf_forms.ajax_nonce },
102102
cache: false,
103103
dataType: 'json',
104104

@@ -1260,9 +1260,10 @@ jQuery(document).ready(function($) {
12601260
clearMessages();
12611261

12621262
var data = new FormData();
1263+
data.append("action", 'wpcf7_pdf_forms_get_attachment_info');
12631264
data.append("post_id", post_id);
12641265
data.append("file_id", file_id);
1265-
data.append("action", 'wpcf7_pdf_forms_get_attachment_info');
1266+
data.append("wpcf7-form", wpcf7_form.val());
12661267
data.append("nonce", wpcf7_pdf_forms.ajax_nonce);
12671268

12681269
// submit request

pdf-forms-for-contact-form-7.php

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,6 +1221,7 @@ public function wp_ajax_get_attachment_info()
12211221
throw new Exception( __( "Nonce mismatch", 'pdf-forms-for-contact-form-7' ) );
12221222

12231223
$attachment_id = $_POST[ 'file_id' ];
1224+
$form = isset( $_POST['wpcf7-form'] ) ? wp_unslash( $_POST['wpcf7-form'] ) : null;
12241225

12251226
$filepath = get_attached_file( $attachment_id );
12261227
if( !$filepath )
@@ -1236,13 +1237,19 @@ public function wp_ajax_get_attachment_info()
12361237
)
12371238
);
12381239

1240+
$cf7_fields = $this->query_cf7_fields( $form );
1241+
1242+
$unavailableNames = array();
1243+
foreach( $cf7_fields as $cf7_field )
1244+
$unavailableNames[] = $cf7_field['name'];
1245+
1246+
$info = $this->get_info( $attachment_id );
1247+
$info['fields'] = $this->query_pdf_fields( $attachment_id, $unavailableNames );
1248+
12391249
$options = array( );
12401250
foreach( self::$pdf_options as $option => $default )
12411251
$options[$option] = $default;
12421252

1243-
$info = $this->get_info( $attachment_id );
1244-
$info['fields'] = $this->query_pdf_fields( $attachment_id );
1245-
12461253
return wp_send_json( array(
12471254
'success' => true,
12481255
'attachment_id' => $attachment_id,
@@ -1386,7 +1393,7 @@ private static function escape_tag_value($value)
13861393
* Generates CF7 field tag based on field data
13871394
* $tagName must already be sanitized
13881395
*/
1389-
private static function generate_tag( $field, &$tagName )
1396+
private static function generate_tag( $field, &$tagName, $unavailableNames = array() )
13901397
{
13911398
$type = strval($field['type']);
13921399

@@ -1494,7 +1501,7 @@ private static function generate_tag( $field, &$tagName )
14941501
$tagOptions .= 'readonly ';
14951502
}
14961503

1497-
$unavailableNames = array(
1504+
$reservedNames = array(
14981505
'm','p','posts','w','cat','withcomments','withoutcomments'
14991506
,'s','search','exact','sentence','calendar','page','paged'
15001507
,'more','tb','pb','author','order','orderby','year','monthnum'
@@ -1503,6 +1510,7 @@ private static function generate_tag( $field, &$tagName )
15031510
,'attachment_id','subpost','subpost_id','preview','robots','taxonomy'
15041511
,'term','cpage','post_type','embed'
15051512
);
1513+
$unavailableNames = array_merge( $unavailableNames, $reservedNames );
15061514
if( array_search( $tagName, $unavailableNames ) !== FALSE )
15071515
{
15081516
$tagName .= '-0000';
@@ -1525,6 +1533,7 @@ public function wp_ajax_query_tags()
15251533

15261534
$attachments = isset( $_POST['attachments'] ) ? $_POST['attachments'] : null;
15271535
$all = isset( $_POST['all'] ) ? $_POST['all'] : null;
1536+
$form = isset( $_POST['wpcf7-form'] ) ? wp_unslash( $_POST['wpcf7-form'] ) : null;
15281537

15291538
if( !isset($attachments) || !is_array($attachments) )
15301539
$attachments = array();
@@ -1542,8 +1551,13 @@ public function wp_ajax_query_tags()
15421551
$tags = __( "This PDF file does not appear to contain a PDF form. See https://acrobat.adobe.com/us/en/acrobat/how-to/create-fillable-pdf-forms-creator.html for more information.", 'pdf-forms-for-contact-form-7' );
15431552
else
15441553
{
1545-
$tags = "";
1554+
$cf7_fields = $this->query_cf7_fields( $form );
1555+
1556+
$unavailableNames = array();
1557+
foreach( $cf7_fields as $cf7_field )
1558+
$unavailableNames[] = $cf7_field['name'];
15461559

1560+
$tags = "";
15471561
foreach ( $fields as $attachment_id => $fs )
15481562
foreach ( $fs as $field )
15491563
{
@@ -1558,13 +1572,15 @@ public function wp_ajax_query_tags()
15581572
$tag_flag = "all";
15591573

15601574
$tagName = self::wpcf7_field_name_encode( $tag_flag, $name );
1561-
$generated_tag = self::generate_tag( $field, $tagName );
1575+
$generated_tag = self::generate_tag( $field, $tagName, $unavailableNames );
15621576

15631577
if( $generated_tag === null)
15641578
continue;
15651579

15661580
$tag .= ' ' . $generated_tag;
15671581
$tags .= $tag . "\n\n";
1582+
1583+
$unavailableNames[] = $tagName;
15681584
}
15691585
}
15701586
}
@@ -1587,7 +1603,7 @@ public function wp_ajax_query_tags()
15871603
/**
15881604
* Helper function used in wp-admin interface
15891605
*/
1590-
private function query_pdf_fields( $attachment_id )
1606+
private function query_pdf_fields( $attachment_id, &$unavailableNames = array() )
15911607
{
15921608
$fields = $this->get_fields( $attachment_id );
15931609
foreach( $fields as $id => &$field )
@@ -1613,10 +1629,11 @@ private function query_pdf_fields( $attachment_id )
16131629
$slug = sanitize_title( $name );
16141630
if( preg_match( '/^[a-zA-Z]$/', $slug[0] ) === 0 )
16151631
$slug = 'f-'.$slug;
1616-
$tag_hint = self::generate_tag( $field, $slug );
1632+
$tag_hint = self::generate_tag( $field, $slug, $unavailableNames );
16171633
$field['id'] = $encoded_name;
16181634
$field['tag_hint'] = $tag_hint;
16191635
$field['tag_name'] = $slug;
1636+
$unavailableNames[] = $slug;
16201637
}
16211638

16221639
return $fields;
@@ -1641,6 +1658,12 @@ public function wp_ajax_preload_data()
16411658
if ( ! current_user_can( 'wpcf7_edit_contact_form', $post_id ) )
16421659
throw new Exception( __( "Permission denied", 'pdf-forms-for-contact-form-7' ) );
16431660

1661+
$cf7_fields = $this->query_cf7_fields( $form );
1662+
1663+
$unavailableNames = array();
1664+
foreach( $cf7_fields as $cf7_field )
1665+
$unavailableNames[] = $cf7_field['name'];
1666+
16441667
$attachments = array();
16451668
$attachment_ids = array();
16461669
foreach( $this->post_get_all_pdfs( $post_id ) as $attachment_id => $attachment )
@@ -1650,7 +1673,7 @@ public function wp_ajax_preload_data()
16501673
$options = $attachment['options'];
16511674

16521675
$info = $this->get_info( $attachment_id );
1653-
$info['fields'] = $this->query_pdf_fields( $attachment_id );
1676+
$info['fields'] = $this->query_pdf_fields( $attachment_id, $unavailableNames );
16541677

16551678
$attachments[] = array(
16561679
'attachment_id' => $attachment_id,
@@ -1661,8 +1684,6 @@ public function wp_ajax_preload_data()
16611684
$attachment_ids[] = $attachment_id;
16621685
}
16631686

1664-
$cf7_fields = $this->query_cf7_fields( $form );
1665-
16661687
$mappings = self::get_meta( $post_id, 'mappings' );
16671688
if( $mappings )
16681689
$mappings = json_decode( $mappings, true );

0 commit comments

Comments
 (0)