Skip to content

Autodownload nojs code refactor #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions css/admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,10 @@
{
display: none;
}

.wpcf7-pdf-form-shortcode
{
margin-left: 20px;
margin-top: 0 !important;
padding: 0 !important;
}
14 changes: 14 additions & 0 deletions css/frontend.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,17 @@
{
font-style: italic;
}

.wpcf7-pdf-forms-pdf-view
{
height: 650px;
width: 100%;
margin: 10px 0px;
}

.wpcf7-pdf-forms-auto-download
{
display: none;
width: 0px;
height: 0px;
}
5 changes: 3 additions & 2 deletions html/add_pdf.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
<div><input data-option="attach_to_mail_1" type="checkbox"/> {attach-to-mail-1}</div>
<div><input data-option="attach_to_mail_2" type="checkbox"/> {attach-to-mail-2}</div>
<div><input data-option="flatten" type="checkbox"/> {flatten}</div>
<div><input data-option="download_link" type="checkbox"/> {download-link}</div>
<div><input data-option="download_link" type="checkbox"/> {download-link} <p class="wpcf7-pdf-form-shortcode" > {shortcode-title} </p></div>
<div><input data-option="auto_download" type="checkbox"/> {auto-download} <p class="wpcf7-pdf-form-shortcode" > {shortcode-title} </p></div>
<div><input data-option="pdf_viewer" type="checkbox"/> {pdf-viewer}</div>
<div>{filename}<br/><input data-option="filename" type="text"/>.pdf</div>
<div>{save-directory}<br/><input data-option="save_directory" type="text"/></div>
</div>
Expand All @@ -39,7 +41,6 @@
</table>

<hr/>

<div class="field-mapping-tool">
<div>
<span class="dashicons dashicons-randomize"></span>
Expand Down
35 changes: 30 additions & 5 deletions js/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,48 @@ 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; i<data.length; i++)
{
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 = "<span class='dashicons dashicons-download'></span><a href='' download></a> <span class='file-size'></span>";

var link = download.querySelector('a');
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", data[i]['url']);
pdfview.className = 'wpcf7-pdf-forms-pdf-view';

downloads.appendChild(download);
if( autodownload == true )
link.click();

if( downloadlink == true )
{
downloads.appendChild(download);
alldownloadlink = true;
}

if( pdfviewer == true )
{
downloads.appendChild(pdfview);
allpdfviewer = true;
}
}

var form = formDiv.querySelector('.wpcf7-form');
if(form)
if( alldownloadlink == true || allpdfviewer == true )
{
var form = formDiv.querySelector('.wpcf7-form');
if(form)
form.appendChild(downloads);
}
}
}

Expand Down
13 changes: 11 additions & 2 deletions modules/downloads.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -60,7 +60,7 @@ 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 );

return $this->subdir;
}

Expand Down Expand Up @@ -126,6 +126,15 @@ public function add_file( $srcfile, $filename )

return $this;
}

/**
* Adds additional options to response
*/
public function set_options( $id, $options )
{
$this->files[$id]['options'] = $options;
return $this;
}

/**
* Returns added files
Expand Down
112 changes: 100 additions & 12 deletions pdf-forms-for-contact-form-7.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +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' ) );

if( defined( 'WPCF7_VERSION' ) && version_compare( WPCF7_VERSION, '5.2' ) >= 0 )
// hook wpcf7_feedback_response (works only with CF7 version 5.2+)
Expand All @@ -98,7 +102,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
*/
Expand Down Expand Up @@ -559,8 +563,9 @@ 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, 'pdf_viewer' => false );
private static $public_pdf_options = array('download_link', 'auto_download', 'pdf_viewer');

/**
* Updates post attachment options
*/
Expand Down Expand Up @@ -1077,12 +1082,16 @@ 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'];
$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_download_link
&& !$create_auto_download
&& !$create_pdf_viewer )
continue;

$options = array();
Expand Down Expand Up @@ -1171,8 +1180,18 @@ 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'];
$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, $public_options );
}
}
}
Expand Down Expand Up @@ -1933,6 +1952,9 @@ 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' ),
'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' ),
Expand Down Expand Up @@ -2014,11 +2036,15 @@ 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
$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;
Expand All @@ -2038,6 +2064,70 @@ public function change_response_nojs( $output, $class, $content, $contact_form )
{
$downloads = '';
foreach( $this->downloads->get_files() as $file )
{
if( $file['options']['download_link'] == true )
$downloads .= "<div>" .
self::replace_tags(
esc_html__( "{icon} {a-href-url}{filename}{/a} {i}({size}){/i}", 'pdf-forms-for-contact-form-7' ),
array(
'icon' => '<span class="dashicons dashicons-download"></span>',
'a-href-url' => '<a href="' . esc_html( $file['url'] ) . '" download>',
'filename' => esc_html( $file['filename'] ),
'/a' => '</a>',
'i' => '<span class="file-size">',
'size' => esc_html( size_format( filesize( $file['filepath'] ) ) ),
'/i' => '</span>',
)
)
. "</div>";

if( $file['options']['pdf_viewer'] == true )
$downloads .= "<iframe src='".esc_html( $file['url'] )."' class='wpcf7-pdf-forms-pdf-view' ></iframe>";

if( $file['options']['auto_download'] == true )
$downloads .= "<iframe src='".esc_html( $_SERVER['REQUEST_URI'] )."?pdf_url=".esc_html( base64_encode( $file['url'] ) )."&pdf_name=".esc_html( base64_encode( $file['filename'] ) )."' class='wpcf7-pdf-forms-auto-download' ></iframe>";
}
$output .= "<div class='wpcf7-pdf-forms-response-output'>$downloads</div>";

// make sure to enable cron if it is down so that old download files get cleaned up
$this->enable_cron();
}
}

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
*/
public function shortcode_download_links_response()
{
$output = '';
if( isset( $_COOKIE['wpcf7_pdf_forms_files'] ) )
{
$get_files = stripslashes( $_COOKIE['wpcf7_pdf_forms_files'] );
$get_files = json_decode( $get_files, true );
$downloads = '';
foreach( $get_files as $file )
{
if( $file['options']['download_link'] == true || $file['options']['auto_download'] == true )
$downloads .= "<div>" .
self::replace_tags(
esc_html__( "{icon} {a-href-url}{filename}{/a} {i}({size}){/i}", 'pdf-forms-for-contact-form-7' ),
Expand All @@ -2052,15 +2142,13 @@ public function change_response_nojs( $output, $class, $content, $contact_form )
)
)
. "</div>";
$output .= "<div class='wpcf7-pdf-forms-response-output'>$downloads</div>";

// make sure to enable cron if it is down so that old download files get cleaned up
$this->enable_cron();
}
$output .= "<div class='wpcf7-pdf-forms-response-output'>$downloads</div>";
}

return $output;
}

}

WPCF7_Pdf_Forms::get_instance();
Expand Down