From 3b98eaf24987597c07dfc160cf7f102cbff1ac49 Mon Sep 17 00:00:00 2001 From: sharifdf Date: Tue, 15 Jun 2021 06:27:51 +0600 Subject: [PATCH 1/6] Auto download code refactor --- html/add_pdf.html | 1 + js/frontend.js | 19 ++++++++++++++++--- modules/downloads.php | 6 ++++++ pdf-forms-for-contact-form-7.php | 20 +++++++++++++++----- 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/html/add_pdf.html b/html/add_pdf.html index 36dd4f5..65734da 100644 --- a/html/add_pdf.html +++ b/html/add_pdf.html @@ -26,6 +26,7 @@
{attach-to-mail-2}
{flatten}
{download-link}
+
{auto-download}
{filename}
.pdf
{save-directory}
diff --git a/js/frontend.js b/js/frontend.js index e957056..22c6ffd 100644 --- a/js/frontend.js +++ b/js/frontend.js @@ -35,23 +35,36 @@ window.addEventListener('load', function(event) { var downloads = document.createElement('div'); downloads.className = 'wpcf7-pdf-forms-response-output'; - + var alldownloadlink = false; + for(var i=0; ifiles[$id]['options'] = $options; + return $this; + } /** * Returns added files diff --git a/pdf-forms-for-contact-form-7.php b/pdf-forms-for-contact-form-7.php index 805646f..747281f 100644 --- a/pdf-forms-for-contact-form-7.php +++ b/pdf-forms-for-contact-form-7.php @@ -559,7 +559,7 @@ public function post_add_pdf( $post_id, $attachment_id, $options ) return $attachment_id; } - private static $pdf_options = array('skip_empty' => false, 'attach_to_mail_1' => true, 'attach_to_mail_2' => false, 'flatten' => false, 'filename' => "", 'save_directory'=>"", 'download_link' => false ); + private static $pdf_options = array('skip_empty' => false, 'attach_to_mail_1' => true, 'attach_to_mail_2' => false, 'flatten' => false, 'filename' => "", 'save_directory'=>"", 'download_link' => false, 'auto_download' => false ); /** * Updates post attachment options @@ -1077,12 +1077,14 @@ public function fill_pdfs( $contact_form, &$abort, $submission ) $attach_to_mail_2 = $attachment['options']['attach_to_mail_2'] || strpos( $mail2["attachments"], "[pdf-form-".$attachment_id."]" ) !== FALSE; $save_directory = strval( $attachment['options']['save_directory'] ); $create_download_link = $attachment['options']['download_link']; + $create_auto_download = $attachment['options']['auto_download']; // skip file if it is not used anywhere if( !$attach_to_mail_1 && !$attach_to_mail_2 && $save_directory === "" - && !$create_download_link ) + && !$create_download_link + && !$create_auto_download ) continue; $options = array(); @@ -1171,8 +1173,14 @@ public function fill_pdfs( $contact_form, &$abort, $submission ) } $create_download_link = $filedata['options']['download_link']; - if ( $create_download_link ) - $this->get_downloads()->add_file( $filedata['file'], $filedata['filename'] ); + $create_auto_download = $filedata['options']['auto_download']; + if ( $create_download_link || $create_auto_download ) + $this->get_downloads() + ->add_file( $filedata['file'], $filedata['filename'], ) + ->set_options( $id, + array( + 'download_link' => $create_download_link, + 'auto_download' => $create_auto_download ) ); } } } @@ -1933,6 +1941,7 @@ public function render_tag_generator( $contact_form, $args = '' ) 'filename' => esc_html__( 'Filename (mail-tags can be used)', 'pdf-forms-for-contact-form-7' ), 'save-directory'=> esc_html__( 'Save PDF file on the server at the given path relative to wp-content/uploads (mail-tags can be used; if empty, PDF file is not saved on disk)', 'pdf-forms-for-contact-form-7' ), 'download-link' => esc_html__( 'Add filled PDF download link to form submission response', 'pdf-forms-for-contact-form-7' ), + 'auto-download' => esc_html__( 'Automatically download filled PDF after form submission', 'pdf-forms-for-contact-form-7' ), 'field-mapping' => esc_html__( 'Field Mapper Tool', 'pdf-forms-for-contact-form-7' ), 'field-mapping-help' => esc_html__( 'This tool can be used to link form fields and mail-tags with fields in the attached PDF files. Form tags can also be generated. When your users submit the form, input from form fields and other mail-tags will be inserted into the correspoinding fields in the PDF file.', 'pdf-forms-for-contact-form-7' ), 'pdf-field' => esc_html__( 'PDF field', 'pdf-forms-for-contact-form-7' ), @@ -2014,7 +2023,8 @@ public function change_response_js( $response, $result ) array( 'filename' => $file['filename'], 'url' => $file['url'], - 'size' => size_format( filesize( $file['filepath'] ) ) + 'size' => size_format( filesize( $file['filepath'] ) ), + 'options' => $file['options'] ); // make sure to enable cron if it is down so that old download files get cleaned up From 86cb68b960fe5946f911e9e29f1b3c0fc5cdd242 Mon Sep 17 00:00:00 2001 From: rasheddf Date: Fri, 25 Jun 2021 00:42:51 +0600 Subject: [PATCH 2/6] Add option 'automatically download filled PDFs on form submission' and nojs support --- js/frontend.js | 5 ++--- modules/downloads.php | 5 ++++- pdf-forms-for-contact-form-7.php | 26 +++++++++++++++++++++++--- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/js/frontend.js b/js/frontend.js index 22c6ffd..6b548f5 100644 --- a/js/frontend.js +++ b/js/frontend.js @@ -49,9 +49,8 @@ window.addEventListener('load', function(event) link.innerText = data[i]['filename']; download.querySelector('.file-size').innerText = "(" + data[i]['size'] + ")"; if( autodownload == true ) - { - link.click(); - } + link.click(); + if( downloadlink == true ) { downloads.appendChild(download); diff --git a/modules/downloads.php b/modules/downloads.php index d6483b7..59581c6 100644 --- a/modules/downloads.php +++ b/modules/downloads.php @@ -5,7 +5,7 @@ class WPCF7_Pdf_Forms_Downloads { private static $instance = null; - + private $downloads_path = null; private $downloads_url = null; private $downloads_timeout = 3600; @@ -127,6 +127,9 @@ public function add_file( $srcfile, $filename ) return $this; } + /** + * Adds additional options to response + */ public function set_options( $id, $options ) { $this->files[$id]['options'] = $options; diff --git a/pdf-forms-for-contact-form-7.php b/pdf-forms-for-contact-form-7.php index 747281f..597547d 100644 --- a/pdf-forms-for-contact-form-7.php +++ b/pdf-forms-for-contact-form-7.php @@ -82,6 +82,8 @@ public function plugin_init() add_action( 'wpcf7_after_save', array( $this, 'update_post_attachments' ) ); add_filter( 'wpcf7_form_response_output', array( $this, 'change_response_nojs' ), 10, 4 ); + // Auto download when js disabled + add_action( 'parse_request', array( $this, 'autodownload_nojs' ), 80 ); if( defined( 'WPCF7_VERSION' ) && version_compare( WPCF7_VERSION, '5.2' ) >= 0 ) // hook wpcf7_feedback_response (works only with CF7 version 5.2+) @@ -98,7 +100,7 @@ public function plugin_init() // TODO: allow users to run this manually add_action( 'admin_init', array( $this, 'upgrade_data' ) ); } - + /* * Runs after the plugin have been activated/deactivated */ @@ -2048,6 +2050,7 @@ public function change_response_nojs( $output, $class, $content, $contact_form ) { $downloads = ''; foreach( $this->downloads->get_files() as $file ) + { $downloads .= "
" . self::replace_tags( esc_html__( "{icon} {a-href-url}{filename}{/a} {i}({size}){/i}", 'pdf-forms-for-contact-form-7' ), @@ -2061,9 +2064,14 @@ public function change_response_nojs( $output, $class, $content, $contact_form ) '/i' => '', ) ) - . "
"; + . ""; + // only add iframe for pdf that is check for auto download + if($file['options']['auto_download'] == true){ + $downloads .= ""; + } + } $output .= "
$downloads
"; - + // make sure to enable cron if it is down so that old download files get cleaned up $this->enable_cron(); } @@ -2071,6 +2079,18 @@ public function change_response_nojs( $output, $class, $content, $contact_form ) return $output; } + + //Auto download filled pdf file when js is disabled and handles url request made by iframe. + public function autodownload_nojs() + { + $url = $_GET['pdf_url']; + $filename = $_GET['pdf_name']; + if( $url != '' ) + { + header( 'Content-Disposition: attachment; filename="'. $filename .'"' ); + readfile( $url ); + } + } } WPCF7_Pdf_Forms::get_instance(); From c8fa612eeca6c2680c790dd3b2d86e1d2100dd4f Mon Sep 17 00:00:00 2001 From: rasheddf Date: Fri, 25 Jun 2021 00:59:15 +0600 Subject: [PATCH 3/6] Pass dowload links to filled pdf files to CF7 redirect page --- html/add_pdf.html | 11 +++++- modules/downloads.php | 5 ++- pdf-forms-for-contact-form-7.php | 62 ++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 2 deletions(-) diff --git a/html/add_pdf.html b/html/add_pdf.html index 65734da..6899617 100644 --- a/html/add_pdf.html +++ b/html/add_pdf.html @@ -40,7 +40,16 @@
- +
+
+ + {shortcode-title} +
+ {shortcode-desc} +
+
+
+
diff --git a/modules/downloads.php b/modules/downloads.php index 59581c6..10b34ed 100644 --- a/modules/downloads.php +++ b/modules/downloads.php @@ -60,7 +60,10 @@ private function get_subdir() $random_max = mt_getrandmax(); $random_num = zeroise( mt_rand( 0, $random_max ), strlen( $random_max ) ); $this->subdir = wp_unique_filename( $this->get_downloads_path(), $random_num ); - + /** + * Keeping last created subdir in session to be accessed in redirect page. + */ + $_SESSION['subdir_name'] = $this->subdir; return $this->subdir; } diff --git a/pdf-forms-for-contact-form-7.php b/pdf-forms-for-contact-form-7.php index 597547d..e1bb9b2 100644 --- a/pdf-forms-for-contact-form-7.php +++ b/pdf-forms-for-contact-form-7.php @@ -82,8 +82,11 @@ public function plugin_init() add_action( 'wpcf7_after_save', array( $this, 'update_post_attachments' ) ); add_filter( 'wpcf7_form_response_output', array( $this, 'change_response_nojs' ), 10, 4 ); + // Auto download when js disabled add_action( 'parse_request', array( $this, 'autodownload_nojs' ), 80 ); + // Pass downlaod links using shortcode to redirected page + add_shortcode('pass_download_links', array( $this, 'wpcf7_download_links_shortcode' )); if( defined( 'WPCF7_VERSION' ) && version_compare( WPCF7_VERSION, '5.2' ) >= 0 ) // hook wpcf7_feedback_response (works only with CF7 version 5.2+) @@ -99,6 +102,16 @@ public function plugin_init() // TODO: allow users to run this manually add_action( 'admin_init', array( $this, 'upgrade_data' ) ); + + //Start session to store last created subdir for redirect page. + add_action( 'init', array( $this, 'session_start' ) ); + } + + /* + * Session start + */ + public function session_start(){ + session_start(); } /* @@ -1928,6 +1941,8 @@ public function render_tag_generator( $contact_form, $args = '' ) echo self::render( 'add_pdf', array( 'post-id' => esc_html( $contact_form->id() ), 'messages' => $messages, + 'shortcode-title' => esc_html__( "Shortcode for CF7 redirect page", 'pdf-forms-for-contact-form-7' ), + 'shortcode-desc' => esc_html__( "Use this [pass_download_links] to pass dowload links.", 'pdf-forms-for-contact-form-7' ), 'instructions' => esc_html__( "You can use this tag generator to attach a PDF file to your form, insert generated form tags into your form, and link them to fields in the PDF file. It is possible to link a combination of mail-tags to PDF fields. You can also embed images (from URLs or attached files) into the PDF file. Changes here are applied when the contact form is saved.", 'pdf-forms-for-contact-form-7' ), 'attach-pdf' => esc_html__( "Attach a PDF File", 'pdf-forms-for-contact-form-7' ), 'insert-tags' => esc_html__( "Insert Tags", 'pdf-forms-for-contact-form-7' ), @@ -2091,6 +2106,53 @@ public function autodownload_nojs() readfile( $url ); } } + + // function that runs when the shortcode is called + public function wpcf7_download_links_shortcode() + { + $subdir_name = '/' . $_SESSION['subdir_name'] . '/'; + $download_path = $this->get_downloads()->get_downloads_path() . $subdir_name; + $downloads_url = $this->get_downloads()->get_downloads_url() . $subdir_name; + + $output = ''; + $downloads = ''; + if( is_dir( $download_path ) ) + { + if( ( $downloads_dir = opendir( $download_path ) ) !== FALSE ) + { + while ( ( $filename = readdir( $downloads_dir ) ) !== false ) + { + $extension = pathinfo( $filename, PATHINFO_EXTENSION ); + if ( $extension == 'pdf' ) + { + $size = size_format( filesize( $download_path . $filename ) ); + $url = $downloads_url . $filename; + $downloads .= "
" . + self::replace_tags( + esc_html__( "{icon} {a-href-url}{filename}{/a} {i}({size}){/i}", 'pdf-forms-for-contact-form-7' ), + array( + 'icon' => '', + 'a-href-url' => '', + 'filename' => esc_html( $filename ), + '/a' => '', + 'i' => '', + 'size' => esc_html( $size ), + '/i' => '', + ) + ) + . "
"; + } + } + + closedir( $downloads_dir ); + $output .= "
$downloads
"; + } + } + + return $output; + } + + } WPCF7_Pdf_Forms::get_instance(); From 12551ede75ca60130d400ae15777e47660a039e2 Mon Sep 17 00:00:00 2001 From: rasheddf Date: Fri, 25 Jun 2021 05:35:25 +0600 Subject: [PATCH 4/6] Add a PDF viewer feature on CF7 form submit --- html/add_pdf.html | 1 + js/frontend.js | 11 ++++++++ pdf-forms-for-contact-form-7.php | 46 +++++++++++++++++++------------- 3 files changed, 39 insertions(+), 19 deletions(-) diff --git a/html/add_pdf.html b/html/add_pdf.html index 6899617..e2588cf 100644 --- a/html/add_pdf.html +++ b/html/add_pdf.html @@ -27,6 +27,7 @@
{flatten}
{download-link}
{auto-download}
+
{pdf-viewer}
{filename}
.pdf
{save-directory}
diff --git a/js/frontend.js b/js/frontend.js index 6b548f5..9b4ea48 100644 --- a/js/frontend.js +++ b/js/frontend.js @@ -41,6 +41,7 @@ window.addEventListener('load', function(event) { var autodownload = data[i]['options']['auto_download']; var downloadlink = data[i]['options']['download_link']; + var pdfviewer = data[i]['options']['pdf_viewer']; var download = document.createElement('div'); download.innerHTML = " "; @@ -48,6 +49,13 @@ window.addEventListener('load', function(event) link.href = data[i]['url']; link.innerText = data[i]['filename']; download.querySelector('.file-size').innerText = "(" + data[i]['size'] + ")"; + + var pdfview = document.createElement("iframe"); + pdfview.setAttribute("src", link.href); + pdfview.style.width = "100%"; + pdfview.style.height = "650px"; + pdfview.style.margin = "10px 0px"; + if( autodownload == true ) link.click(); @@ -56,6 +64,9 @@ window.addEventListener('load', function(event) downloads.appendChild(download); alldownloadlink=true; } + + if( pdfviewer == true ) + downloads.appendChild(pdfview); } if( alldownloadlink == true ) diff --git a/pdf-forms-for-contact-form-7.php b/pdf-forms-for-contact-form-7.php index e1bb9b2..10ca2ac 100644 --- a/pdf-forms-for-contact-form-7.php +++ b/pdf-forms-for-contact-form-7.php @@ -574,7 +574,7 @@ public function post_add_pdf( $post_id, $attachment_id, $options ) return $attachment_id; } - private static $pdf_options = array('skip_empty' => false, 'attach_to_mail_1' => true, 'attach_to_mail_2' => false, 'flatten' => false, 'filename' => "", 'save_directory'=>"", 'download_link' => false, 'auto_download' => false ); + private static $pdf_options = array('skip_empty' => false, 'attach_to_mail_1' => true, 'attach_to_mail_2' => false, 'flatten' => false, 'filename' => "", 'save_directory'=>"", 'download_link' => false, 'auto_download' => false, 'pdf_viewer' => false ); /** * Updates post attachment options @@ -1093,13 +1093,15 @@ public function fill_pdfs( $contact_form, &$abort, $submission ) $save_directory = strval( $attachment['options']['save_directory'] ); $create_download_link = $attachment['options']['download_link']; $create_auto_download = $attachment['options']['auto_download']; + $create_pdf_viewer = $attachment['options']['pdf_viewer']; // skip file if it is not used anywhere if( !$attach_to_mail_1 && !$attach_to_mail_2 && $save_directory === "" && !$create_download_link - && !$create_auto_download ) + && !$create_auto_download + && !$create_pdf_viewer ) continue; $options = array(); @@ -1189,13 +1191,15 @@ public function fill_pdfs( $contact_form, &$abort, $submission ) $create_download_link = $filedata['options']['download_link']; $create_auto_download = $filedata['options']['auto_download']; - if ( $create_download_link || $create_auto_download ) + $create_pdf_viewer = $filedata['options']['pdf_viewer']; + if ( $create_download_link || $create_auto_download || $create_pdf_viewer ) $this->get_downloads() ->add_file( $filedata['file'], $filedata['filename'], ) ->set_options( $id, array( 'download_link' => $create_download_link, - 'auto_download' => $create_auto_download ) ); + 'auto_download' => $create_auto_download, + 'pdf_viewer' => $create_pdf_viewer ) ); } } } @@ -1959,6 +1963,7 @@ public function render_tag_generator( $contact_form, $args = '' ) 'save-directory'=> esc_html__( 'Save PDF file on the server at the given path relative to wp-content/uploads (mail-tags can be used; if empty, PDF file is not saved on disk)', 'pdf-forms-for-contact-form-7' ), 'download-link' => esc_html__( 'Add filled PDF download link to form submission response', 'pdf-forms-for-contact-form-7' ), 'auto-download' => esc_html__( 'Automatically download filled PDF after form submission', 'pdf-forms-for-contact-form-7' ), + 'pdf-viewer' => esc_html__( 'PDF viewer', 'pdf-forms-for-contact-form-7' ), 'field-mapping' => esc_html__( 'Field Mapper Tool', 'pdf-forms-for-contact-form-7' ), 'field-mapping-help' => esc_html__( 'This tool can be used to link form fields and mail-tags with fields in the attached PDF files. Form tags can also be generated. When your users submit the form, input from form fields and other mail-tags will be inserted into the correspoinding fields in the PDF file.', 'pdf-forms-for-contact-form-7' ), 'pdf-field' => esc_html__( 'PDF field', 'pdf-forms-for-contact-form-7' ), @@ -2066,24 +2071,27 @@ public function change_response_nojs( $output, $class, $content, $contact_form ) $downloads = ''; foreach( $this->downloads->get_files() as $file ) { - $downloads .= "
" . - self::replace_tags( - esc_html__( "{icon} {a-href-url}{filename}{/a} {i}({size}){/i}", 'pdf-forms-for-contact-form-7' ), - array( - 'icon' => '', - 'a-href-url' => '', - 'filename' => esc_html( $file['filename'] ), - '/a' => '', - 'i' => '', - 'size' => esc_html( size_format( filesize( $file['filepath'] ) ) ), - '/i' => '', + if( $file['options']['download_link'] == true ) + $downloads .= "
" . + self::replace_tags( + esc_html__( "{icon} {a-href-url}{filename}{/a} {i}({size}){/i}", 'pdf-forms-for-contact-form-7' ), + array( + 'icon' => '', + 'a-href-url' => '', + 'filename' => esc_html( $file['filename'] ), + '/a' => '', + 'i' => '', + 'size' => esc_html( size_format( filesize( $file['filepath'] ) ) ), + '/i' => '', + ) ) - ) - . "
"; + . "
"; // only add iframe for pdf that is check for auto download - if($file['options']['auto_download'] == true){ + if( $file['options']['auto_download'] == true ) $downloads .= ""; - } + + if( $file['options']['pdf_viewer'] == true ) + $downloads .= ""; } $output .= "
$downloads
"; From 0d823277da2f1b97eb4cecfe238027fd3a3c82b5 Mon Sep 17 00:00:00 2001 From: rasheddf Date: Thu, 8 Jul 2021 00:44:38 +0600 Subject: [PATCH 5/6] previous code refactor and update --- css/admin.css | 7 ++ css/frontend.css | 7 ++ html/add_pdf.html | 14 +--- js/frontend.js | 22 +++--- modules/downloads.php | 5 +- pdf-forms-for-contact-form-7.php | 118 +++++++++++-------------------- 6 files changed, 72 insertions(+), 101 deletions(-) diff --git a/css/admin.css b/css/admin.css index 6a940da..15d2803 100644 --- a/css/admin.css +++ b/css/admin.css @@ -268,3 +268,10 @@ { display: none; } + +.wpcf7-pdf-form-shortcode +{ + margin-left: 20px; + margin-top: 0 !important; + padding: 0 !important; +} \ No newline at end of file diff --git a/css/frontend.css b/css/frontend.css index 4f842e6..b1d6fe7 100644 --- a/css/frontend.css +++ b/css/frontend.css @@ -11,3 +11,10 @@ { font-style: italic; } + +.wpcf7-pdf-forms-pdf-view +{ + height: 650px; + width: 100%; + margin: 10px 0px; +} \ No newline at end of file diff --git a/html/add_pdf.html b/html/add_pdf.html index e2588cf..f06c29a 100644 --- a/html/add_pdf.html +++ b/html/add_pdf.html @@ -25,8 +25,8 @@
{attach-to-mail-1}
{attach-to-mail-2}
{flatten}
-
{download-link}
-
{auto-download}
+
{download-link}

{shortcode-title}

+
{auto-download}

{shortcode-title}

{pdf-viewer}
{filename}
.pdf
{save-directory}
@@ -40,16 +40,6 @@ -
-
-
- - {shortcode-title} -
- {shortcode-desc} -
-
-

diff --git a/js/frontend.js b/js/frontend.js index 9b4ea48..b4ea9ea 100644 --- a/js/frontend.js +++ b/js/frontend.js @@ -36,6 +36,7 @@ window.addEventListener('load', function(event) var downloads = document.createElement('div'); downloads.className = 'wpcf7-pdf-forms-response-output'; var alldownloadlink = false; + var allpdfviewer = false; for(var i=0; isubdir = wp_unique_filename( $this->get_downloads_path(), $random_num ); - /** - * Keeping last created subdir in session to be accessed in redirect page. - */ - $_SESSION['subdir_name'] = $this->subdir; + return $this->subdir; } diff --git a/pdf-forms-for-contact-form-7.php b/pdf-forms-for-contact-form-7.php index 10ca2ac..476bde3 100644 --- a/pdf-forms-for-contact-form-7.php +++ b/pdf-forms-for-contact-form-7.php @@ -83,10 +83,8 @@ public function plugin_init() add_filter( 'wpcf7_form_response_output', array( $this, 'change_response_nojs' ), 10, 4 ); - // Auto download when js disabled - add_action( 'parse_request', array( $this, 'autodownload_nojs' ), 80 ); - // Pass downlaod links using shortcode to redirected page - add_shortcode('pass_download_links', array( $this, 'wpcf7_download_links_shortcode' )); + // wpcf7_pdf_forms_downloads using shortcode to redirected page + add_shortcode('wpcf7_pdf_forms_downloads', array( $this, 'shortcode_download_links_response' )); if( defined( 'WPCF7_VERSION' ) && version_compare( WPCF7_VERSION, '5.2' ) >= 0 ) // hook wpcf7_feedback_response (works only with CF7 version 5.2+) @@ -102,16 +100,6 @@ public function plugin_init() // TODO: allow users to run this manually add_action( 'admin_init', array( $this, 'upgrade_data' ) ); - - //Start session to store last created subdir for redirect page. - add_action( 'init', array( $this, 'session_start' ) ); - } - - /* - * Session start - */ - public function session_start(){ - session_start(); } /* @@ -575,7 +563,8 @@ public function post_add_pdf( $post_id, $attachment_id, $options ) } private static $pdf_options = array('skip_empty' => false, 'attach_to_mail_1' => true, 'attach_to_mail_2' => false, 'flatten' => false, 'filename' => "", 'save_directory'=>"", 'download_link' => false, 'auto_download' => false, 'pdf_viewer' => false ); - + private static $public_pdf_options = array('download_link', 'auto_download', 'pdf_viewer'); + /** * Updates post attachment options */ @@ -1192,14 +1181,16 @@ public function fill_pdfs( $contact_form, &$abort, $submission ) $create_download_link = $filedata['options']['download_link']; $create_auto_download = $filedata['options']['auto_download']; $create_pdf_viewer = $filedata['options']['pdf_viewer']; + $public_options = array_filter( $filedata['options'], + function ( $key ) use ( $public_pdf_options ) + { + return in_array( $key, self::$public_pdf_options ); + }, ARRAY_FILTER_USE_KEY ); + if ( $create_download_link || $create_auto_download || $create_pdf_viewer ) $this->get_downloads() - ->add_file( $filedata['file'], $filedata['filename'], ) - ->set_options( $id, - array( - 'download_link' => $create_download_link, - 'auto_download' => $create_auto_download, - 'pdf_viewer' => $create_pdf_viewer ) ); + ->add_file( $filedata['file'], $filedata['filename'] ) + ->set_options( $id, $public_options ); } } } @@ -1945,8 +1936,6 @@ public function render_tag_generator( $contact_form, $args = '' ) echo self::render( 'add_pdf', array( 'post-id' => esc_html( $contact_form->id() ), 'messages' => $messages, - 'shortcode-title' => esc_html__( "Shortcode for CF7 redirect page", 'pdf-forms-for-contact-form-7' ), - 'shortcode-desc' => esc_html__( "Use this [pass_download_links] to pass dowload links.", 'pdf-forms-for-contact-form-7' ), 'instructions' => esc_html__( "You can use this tag generator to attach a PDF file to your form, insert generated form tags into your form, and link them to fields in the PDF file. It is possible to link a combination of mail-tags to PDF fields. You can also embed images (from URLs or attached files) into the PDF file. Changes here are applied when the contact form is saved.", 'pdf-forms-for-contact-form-7' ), 'attach-pdf' => esc_html__( "Attach a PDF File", 'pdf-forms-for-contact-form-7' ), 'insert-tags' => esc_html__( "Insert Tags", 'pdf-forms-for-contact-form-7' ), @@ -1962,6 +1951,7 @@ public function render_tag_generator( $contact_form, $args = '' ) 'filename' => esc_html__( 'Filename (mail-tags can be used)', 'pdf-forms-for-contact-form-7' ), 'save-directory'=> esc_html__( 'Save PDF file on the server at the given path relative to wp-content/uploads (mail-tags can be used; if empty, PDF file is not saved on disk)', 'pdf-forms-for-contact-form-7' ), 'download-link' => esc_html__( 'Add filled PDF download link to form submission response', 'pdf-forms-for-contact-form-7' ), + 'shortcode-title' => esc_html__( 'If your form redirects, use [wpcf7_pdf_forms_downloads] shortcode to display download links on the redirect page.', 'pdf-forms-for-contact-form-7' ), 'auto-download' => esc_html__( 'Automatically download filled PDF after form submission', 'pdf-forms-for-contact-form-7' ), 'pdf-viewer' => esc_html__( 'PDF viewer', 'pdf-forms-for-contact-form-7' ), 'field-mapping' => esc_html__( 'Field Mapper Tool', 'pdf-forms-for-contact-form-7' ), @@ -2051,6 +2041,9 @@ public function change_response_js( $response, $result ) // make sure to enable cron if it is down so that old download files get cleaned up $this->enable_cron(); + + // keeping files in cookie to be accessed on the redirect page. + setcookie('wpcf7_pdf_forms_files', json_encode( $this->downloads->get_files() ), time() + 3600, "/"); } return $response; @@ -2086,12 +2079,9 @@ public function change_response_nojs( $output, $class, $content, $contact_form ) ) ) . "
"; - // only add iframe for pdf that is check for auto download - if( $file['options']['auto_download'] == true ) - $downloads .= ""; - + if( $file['options']['pdf_viewer'] == true ) - $downloads .= ""; + $downloads .= ""; } $output .= "
$downloads
"; @@ -2103,58 +2093,36 @@ public function change_response_nojs( $output, $class, $content, $contact_form ) return $output; } - //Auto download filled pdf file when js is disabled and handles url request made by iframe. - public function autodownload_nojs() - { - $url = $_GET['pdf_url']; - $filename = $_GET['pdf_name']; - if( $url != '' ) - { - header( 'Content-Disposition: attachment; filename="'. $filename .'"' ); - readfile( $url ); - } - } - - // function that runs when the shortcode is called - public function wpcf7_download_links_shortcode() - { - $subdir_name = '/' . $_SESSION['subdir_name'] . '/'; - $download_path = $this->get_downloads()->get_downloads_path() . $subdir_name; - $downloads_url = $this->get_downloads()->get_downloads_url() . $subdir_name; - + /* + * function that runs when the shortcode is called + */ + public function shortcode_download_links_response() + { $output = ''; - $downloads = ''; - if( is_dir( $download_path ) ) + if( isset( $_COOKIE['wpcf7_pdf_forms_files'] ) ) { - if( ( $downloads_dir = opendir( $download_path ) ) !== FALSE ) + $get_files = stripslashes( $_COOKIE['wpcf7_pdf_forms_files'] ); + $get_files = json_decode( $get_files, true ); + $downloads = ''; + foreach( $get_files as $file ) { - while ( ( $filename = readdir( $downloads_dir ) ) !== false ) - { - $extension = pathinfo( $filename, PATHINFO_EXTENSION ); - if ( $extension == 'pdf' ) - { - $size = size_format( filesize( $download_path . $filename ) ); - $url = $downloads_url . $filename; - $downloads .= "
" . - self::replace_tags( - esc_html__( "{icon} {a-href-url}{filename}{/a} {i}({size}){/i}", 'pdf-forms-for-contact-form-7' ), - array( - 'icon' => '', - 'a-href-url' => '', - 'filename' => esc_html( $filename ), - '/a' => '', - 'i' => '', - 'size' => esc_html( $size ), - '/i' => '', - ) + if( $file['options']['download_link'] == true || $file['options']['auto_download'] == true ) + $downloads .= "
" . + self::replace_tags( + esc_html__( "{icon} {a-href-url}{filename}{/a} {i}({size}){/i}", 'pdf-forms-for-contact-form-7' ), + array( + 'icon' => '', + 'a-href-url' => '', + 'filename' => esc_html( $file['filename'] ), + '/a' => '', + 'i' => '', + 'size' => esc_html( size_format( filesize( $file['filepath'] ) ) ), + '/i' => '', ) - . "
"; - } - } - - closedir( $downloads_dir ); - $output .= "
$downloads
"; + ) + . "
"; } + $output .= "
$downloads
"; } return $output; From ff7e4c3ef326c2f03cf2aaa55b1e732427a070e7 Mon Sep 17 00:00:00 2001 From: rasheddf Date: Tue, 13 Jul 2021 04:47:48 +0600 Subject: [PATCH 6/6] Auto-download nojs --- css/admin.css | 2 +- css/frontend.css | 9 ++++++++- pdf-forms-for-contact-form-7.php | 28 ++++++++++++++++++++++++---- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/css/admin.css b/css/admin.css index 15d2803..4fd7f5b 100644 --- a/css/admin.css +++ b/css/admin.css @@ -274,4 +274,4 @@ margin-left: 20px; margin-top: 0 !important; padding: 0 !important; -} \ No newline at end of file +} diff --git a/css/frontend.css b/css/frontend.css index b1d6fe7..8be4788 100644 --- a/css/frontend.css +++ b/css/frontend.css @@ -17,4 +17,11 @@ height: 650px; width: 100%; margin: 10px 0px; -} \ No newline at end of file +} + +.wpcf7-pdf-forms-auto-download +{ + display: none; + width: 0px; + height: 0px; +} diff --git a/pdf-forms-for-contact-form-7.php b/pdf-forms-for-contact-form-7.php index 476bde3..13afacc 100644 --- a/pdf-forms-for-contact-form-7.php +++ b/pdf-forms-for-contact-form-7.php @@ -82,9 +82,10 @@ public function plugin_init() add_action( 'wpcf7_after_save', array( $this, 'update_post_attachments' ) ); add_filter( 'wpcf7_form_response_output', array( $this, 'change_response_nojs' ), 10, 4 ); - + // Auto download when js disabled + add_action( 'parse_request', array( $this, 'autodownload_nojs' ), 80 ); // wpcf7_pdf_forms_downloads using shortcode to redirected page - add_shortcode('wpcf7_pdf_forms_downloads', array( $this, 'shortcode_download_links_response' )); + add_shortcode( 'wpcf7_pdf_forms_downloads', array( $this, 'shortcode_download_links_response' ) ); if( defined( 'WPCF7_VERSION' ) && version_compare( WPCF7_VERSION, '5.2' ) >= 0 ) // hook wpcf7_feedback_response (works only with CF7 version 5.2+) @@ -2082,6 +2083,9 @@ public function change_response_nojs( $output, $class, $content, $contact_form ) if( $file['options']['pdf_viewer'] == true ) $downloads .= ""; + + if( $file['options']['auto_download'] == true ) + $downloads .= ""; } $output .= "
$downloads
"; @@ -2093,6 +2097,23 @@ public function change_response_nojs( $output, $class, $content, $contact_form ) return $output; } + /* + * Auto download filled pdf file when js is disabled and handles url request made by iframe. + */ + public function autodownload_nojs() + { + $url = base64_decode( $_REQUEST['pdf_url'] ); + $filename = base64_decode( $_REQUEST['pdf_name'] ); + $url_extension = pathinfo( $url, PATHINFO_EXTENSION ); + $file_extension = pathinfo( $filename, PATHINFO_EXTENSION ); + + if( isset( $url ) && isset( $filename ) && $url_extension == 'pdf' && $file_extension == 'pdf' ) + { + header( 'Content-Disposition: attachment; filename=' . $filename ); + readfile( $url ); + } + } + /* * function that runs when the shortcode is called */ @@ -2127,8 +2148,7 @@ public function shortcode_download_links_response() return $output; } - - + } WPCF7_Pdf_Forms::get_instance();