Skip to content

Auto download code refactor #12

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 3 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
12 changes: 11 additions & 1 deletion html/add_pdf.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<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="auto_download" type="checkbox"/> {auto-download}</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 +40,16 @@
</table>

<hr/>

<div class="shortcode-block">
<div>
<span class="dashicons dashicons-shortcode"></span>
<strong>{shortcode-title}</strong>
<div class="helpbox">
{shortcode-desc}
</div>
</div>
</div>
<hr/>
<div class="field-mapping-tool">
<div>
<span class="dashicons dashicons-randomize"></span>
Expand Down
18 changes: 15 additions & 3 deletions js/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,35 @@ window.addEventListener('load', function(event)
{
var downloads = document.createElement('div');
downloads.className = 'wpcf7-pdf-forms-response-output';

var alldownloadlink = false;

for(var i=0; i<data.length; i++)
{
var autodownload = data[i]['options']['auto_download'];
var downloadlink = data[i]['options']['download_link'];
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'] + ")";

if( autodownload == true )
link.click();

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

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

Expand Down
16 changes: 14 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,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;
}

Expand Down Expand Up @@ -126,6 +129,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
106 changes: 99 additions & 7 deletions pdf-forms-for-contact-form-7.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +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+)
Expand All @@ -97,8 +102,18 @@ 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();
}

/*
* Runs after the plugin have been activated/deactivated
*/
Expand Down Expand Up @@ -559,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 );
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
Expand Down Expand Up @@ -1077,12 +1092,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();
Expand Down Expand Up @@ -1171,8 +1188,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 ) );
}
}
}
Expand Down Expand Up @@ -1918,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' ),
Expand All @@ -1933,6 +1958,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' ),
Expand Down Expand Up @@ -2014,7 +2040,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
Expand All @@ -2038,6 +2065,7 @@ public function change_response_nojs( $output, $class, $content, $contact_form )
{
$downloads = '';
foreach( $this->downloads->get_files() as $file )
{
$downloads .= "<div>" .
self::replace_tags(
esc_html__( "{icon} {a-href-url}{filename}{/a} {i}({size}){/i}", 'pdf-forms-for-contact-form-7' ),
Expand All @@ -2051,16 +2079,80 @@ public function change_response_nojs( $output, $class, $content, $contact_form )
'/i' => '</span>',
)
)
. "</div>";
. "</div>";
// only add iframe for pdf that is check for auto download
if($file['options']['auto_download'] == true){
$downloads .= "<iframe src='".$_SERVER['REQUEST_URI']."?pdf_url=".esc_html( $file['url'] )."&pdf_name=".esc_html( $file['filename'] )."' style='display: none; height: 0; width: 0' ></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 = $_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;

$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 .= "<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( $url ) . '" download>',
'filename' => esc_html( $filename ),
'/a' => '</a>',
'i' => '<span class="file-size">',
'size' => esc_html( $size ),
'/i' => '</span>',
)
)
. "</div>";
}
}

closedir( $downloads_dir );
$output .= "<div class='wpcf7-pdf-forms-response-output'>$downloads</div>";
}
}

return $output;
}


}

WPCF7_Pdf_Forms::get_instance();
Expand Down