From 3b98eaf24987597c07dfc160cf7f102cbff1ac49 Mon Sep 17 00:00:00 2001 From: sharifdf Date: Tue, 15 Jun 2021 06:27:51 +0600 Subject: [PATCH 1/3] 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/3] 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/3] 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();