Skip to content

Commit

Permalink
Add Preview PDF feature to Form PDF Settings
Browse files Browse the repository at this point in the history
  • Loading branch information
jakejackson1 committed Aug 29, 2024
1 parent 27e452d commit 7793e23
Show file tree
Hide file tree
Showing 66 changed files with 1,216 additions and 575 deletions.
7 changes: 6 additions & 1 deletion src/Helper/Helper_Abstract_Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -2272,7 +2272,12 @@ public function toggle_callback( $args ) {
<div class="gform-settings-description gform-kitchen-sink"><?php echo wp_kses_post( $args['desc'] ); ?></div>

<span class="gform-settings-input__container">
<input type="checkbox" id="gfpdf_settings[<?php echo esc_attr( $args['id'] ); ?>]" name="gfpdf_settings[<?php echo esc_attr( $args['id'] ); ?>]" value="Yes" <?php echo checked( $value, 'Yes', false ); ?> />
<input type="checkbox"
id="gfpdf_settings[<?php echo esc_attr( $args['id'] ); ?>]"
name="gfpdf_settings[<?php echo esc_attr( $args['id'] ); ?>]"
class="gfpdf-input-toggle"
value="Yes"
<?php checked( $value, 'Yes' ); ?> />
<label class="gform-field__toggle-container" for="gfpdf_settings[<?php echo esc_attr( $args['id'] ); ?>]">
<?php if ( ! empty( $name ) ): ?>
<span class="screen-reader-text"><?php echo esc_html( $name ); ?></span>
Expand Down
3 changes: 2 additions & 1 deletion src/Helper/Helper_Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,9 @@ public function get_localised_script_data( Helper_Abstract_Options $options, Hel

'searchPlaceholder' => esc_html__( 'Search the Gravity PDF Knowledgebase...', 'gravity-forms-pdf-extended' ),
'searchResultHeadingText' => esc_html__( 'Gravity PDF Documentation', 'gravity-forms-pdf-extended' ),
'noResultText' => esc_html__( 'It doesn\'t look like there are any topics related to your issue.', 'gravity-forms-pdf-extended' ),
'noResultText' => esc_html__( "It doesn't look like there are any topics related to your issue.", 'gravity-forms-pdf-extended' ),
'getSearchResultError' => esc_html__( 'An error occurred. Please try again', 'gravity-forms-pdf-extended' ),
'getPreviewResultError' => esc_html__( 'An error occurred. Please try again', 'gravity-forms-pdf-extended' ),

'requiresGravityPdfVersion' => esc_html__( 'Requires Gravity PDF v%s', 'gravity-forms-pdf-extended' ),
'templateNotCompatibleWithGravityPdfVersion' => esc_html__( 'This PDF template is not compatible with your version of Gravity PDF. This template required Gravity PDF v%s.', 'gravity-forms-pdf-extended' ),
Expand Down
14 changes: 1 addition & 13 deletions src/Helper/Helper_Options_Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,19 +251,7 @@ public function get_registered_fields() {
'required' => true,
'desc' => __( 'Add a descriptive label to help you differentiate between multiple PDF settings.', 'gravity-forms-pdf-extended' ),
'schema' => [
'arg_options' => [
'validate_callback' => function( $param, $request, $key ) {
if ( ! empty( $param ) ) {
return true;
}

return new WP_Error(
'rest_invalid_param',
__( 'PDF name must be a non-empty string.', 'gravity-forms-pdf-extended' ),
[ 'status' => 400 ]
);
},
],
'default' => __( 'PDF', 'gravity-forms-pdf-extended' ),
],
],

Expand Down
4 changes: 1 addition & 3 deletions src/Model/Model_Form_Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,6 @@ public function register_custom_appearance_settings( $settings ) {
* @since 4.0
*/
public function get_template_name_from_current_page() {

/* phpcs:disable WordPress.Security.NonceVerification.Recommended */
$pid = ! empty( $_GET['pid'] ) ? sanitize_html_class( rgget( 'pid' ) ) : sanitize_html_class( rgpost( 'gform_pdf_id' ) );
$form_id = isset( $_GET['id'] ) ? (int) $_GET['id'] : 0;
/* phpcs:enable */
Expand All @@ -640,7 +638,7 @@ public function get_template_name_from_current_page() {
}
}

return $template;
return apply_filters( 'gfpdf_template_for_current_page', $template );
}

/**
Expand Down
138 changes: 75 additions & 63 deletions src/Model/Model_PDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,71 +270,9 @@ public function process_pdf( $pid, $lid, $action = 'view' ) {
return $path_to_pdf;
}

/* Verify the PDF can be sent to the client */
if ( headers_sent( $filename, $linenumber ) ) {
$this->log->error(
'Server headers already sent',
[
'filename' => $filename,
'linenumber' => $linenumber,
]
);

return new WP_Error( 'headers_sent', __( 'The PDF cannot be displayed because the server headers have already been sent.', 'gravity-forms-pdf-extended' ) );
}

/* Force any active buffers to close and delete its content */
while ( ob_get_level() > 0 ) {
ob_end_clean();
}

do_action( 'gfpdf_post_view_or_download_pdf', $path_to_pdf, $form, $entry, $settings, $action );

/* Send the PDF to the client */
header( 'Content-Type: application/pdf' );

/*
* Set the filename, supporting the new utf-8 syntax + backwards compatibility
* Refer to RFC 8187 https://www.rfc-editor.org/rfc/rfc8187.html
*/
header(
sprintf(
'Content-Disposition: %1$s; filename="%2$s"; filename*=utf-8\'\'%2$s',
$action === 'view' ? 'inline' : 'attachment',
rawurlencode( basename( $path_to_pdf ) ),
)
);

/* only add the length if the server is not using compression */
if ( empty( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) {
header( sprintf( 'Content-Length: %d', filesize( $path_to_pdf ) ) );
}

/* Tell client to download the file */
if ( $action === 'download' ) {
header( 'Content-Description: File Transfer' );
header( 'Content-Transfer-Encoding: binary' );
}

/* Set appropriate headers for local browser caching */
$last_modified_time = filemtime( $path_to_pdf );
$etag = md5( $path_to_pdf ); /* the file path includes a unique hash that automatically changes when a PDF does */

header( sprintf( 'Last-Modified: %s GMT', gmdate( 'D, d M Y H:i:s', $last_modified_time ) ) );
header( sprintf( 'Etag: %s', $etag ) );
header( 'Cache-Control: no-cache, private' );
header( 'Pragma: no-cache' );
header( 'Expires: 0' );

/* Tell client they can display the PDF from the local cache if it is still current */
if ( ! empty( $_SERVER['HTTP_IF_NONE_MATCH'] ) && $_SERVER['HTTP_IF_NONE_MATCH'] === $etag ) {
header( 'HTTP/1.1 304 Not Modified' );
exit;
}

readfile( $path_to_pdf ); /* phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_readfile */

exit;
$this->send_pdf_to_browser( $path_to_pdf, $action );
}

/**
Expand Down Expand Up @@ -2658,4 +2596,78 @@ public function gp_populate_anything_hydrate_form( $form, $entry ) {

return $hydrated_form;
}

/**
* Send a PDF file to the browser
*
* @param string $path_to_pdf Absolute path to PDF on disk
* @param string $action Either "view" or "download"
*
* @since 6.12
*/
public function send_pdf_to_browser( $path_to_pdf, $action = 'view' ) {
/* Verify the PDF can be sent to the client */
if ( headers_sent( $filename, $linenumber ) ) {
$this->log->error(
'Server headers already sent',
[
'filename' => $filename,
'linenumber' => $linenumber,
]
);

return new WP_Error( 'headers_sent', __( 'The PDF cannot be displayed because the server headers have already been sent.', 'gravity-forms-pdf-extended' ) );
}

/* Force any active buffers to close and delete its content */
while ( ob_get_level() > 0 ) {
ob_end_clean();
}

/* Send the PDF to the client */
header( 'Content-Type: application/pdf' );

/*
* Set the filename, supporting the new utf-8 syntax + backwards compatibility
* Refer to RFC 8187 https://www.rfc-editor.org/rfc/rfc8187.html
*/
header(
sprintf(
'Content-Disposition: %1$s; filename="%2$s"; filename*=utf-8\'\'%2$s',
$action === 'view' ? 'inline' : 'attachment',
rawurlencode( basename( $path_to_pdf ) ),
)
);

/* only add the length if the server is not using compression */
if ( empty( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) {
header( sprintf( 'Content-Length: %d', filesize( $path_to_pdf ) ) );
}

/* Tell client to download the file */
if ( $action !== 'view' ) {
header( 'Content-Description: File Transfer' );
header( 'Content-Transfer-Encoding: binary' );
}

/* Set appropriate headers for local browser caching */
$last_modified_time = filemtime( $path_to_pdf );
$etag = md5( $path_to_pdf ); /* the file path includes a unique hash that automatically changes when a PDF does */

header( sprintf( 'Last-Modified: %s GMT', gmdate( 'D, d M Y H:i:s', $last_modified_time ) ) );
header( sprintf( 'Etag: %s', $etag ) );
header( 'Cache-Control: no-cache, private' );
header( 'Pragma: no-cache' );
header( 'Expires: 0' );

/* Tell client they can display the PDF from the local cache if it is still current */
if ( ! empty( $_SERVER['HTTP_IF_NONE_MATCH'] ) && $_SERVER['HTTP_IF_NONE_MATCH'] === $etag ) {
header( 'HTTP/1.1 304 Not Modified' );
exit;
}

readfile( $path_to_pdf ); /* phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_readfile */

exit;
}
}
Loading

0 comments on commit 7793e23

Please sign in to comment.