Skip to content

Commit

Permalink
Continue cleaning up the code
Browse files Browse the repository at this point in the history
  • Loading branch information
jakejackson1 committed Jul 17, 2024
1 parent 33592e7 commit 0b1033e
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 42 deletions.
42 changes: 42 additions & 0 deletions api.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,48 @@ public static function delete_pdf( $form_id, $pdf_id ) {
return $options->delete_pdf( $form_id, $pdf_id );
}

/**
* Retrieve the filename of the PDF document
* The .pdf extension is not returned by default (pass true to the 3rd param)
*
* See https://docs.gravitypdf.com/v6/developers/api/get_pdf_filename/ for more information about this method
*
* @param int $entry_id The Gravity Form entry ID
* @param string $pdf_id The Gravity PDF ID number (the pid number in the URL when viewing a setting in the admin area)
* @param bool $include_extension Whether to include the .pdf extension in the filename
*
* @return string|WP_Error Return the filename of the PDF, or a WP_Error on failure
*
* @return string|WP_Error
*
* @since 6.12
*/
public static function get_pdf_filename( $entry_id, $pdf_id, bool $include_extension ) {
$form_class = static::get_form_class();

/* Get our entry */
$entry = $form_class->get_entry( $entry_id );
if ( is_wp_error( $entry ) ) {
return new WP_Error( 'invalid_entry', esc_html__( 'Make sure to pass in a valid Gravity Forms Entry ID', 'gravity-forms-pdf-extended' ) );
}

/* Get our settings */
$setting = static::get_pdf( $entry['form_id'], $pdf_id );
if ( is_wp_error( $setting ) ) {
return new WP_Error( 'invalid_pdf_setting', esc_html__( 'Could not located the PDF Settings. Ensure you pass in a valid PDF ID.', 'gravity-forms-pdf-extended' ) );
}

/** @var \GFPDF\Model\Model_PDF $pdf */
$pdf = static::get_mvc_class( 'Model_PDF' );

$filename = $pdf->get_pdf_name( $setting, $entry );
if ( $include_extension ) {
$filename .= '.pdf';
}

return $filename;
}

/**
* Retrieve an array of the global Gravity PDF settings (this doesn't include individual form configuration details - see GPDFAPI::get_form_pdfs)
*
Expand Down
6 changes: 3 additions & 3 deletions src/Controller/Controller_Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ class Controller_Cache {

public function init() {
//gfpdf_pre_pdf_generation -> view/download PDF
// View_PDF::maybe_view_form_data() disables cache
// Handle "print" option ($_GET['print']) or [gravitypdf print="1"] or {Report:pdf:6063bd0362dda:print}
// View_PDF::maybe_view_form_data() disables cache
// Handle "print" option ($_GET['print']) or [gravitypdf print="1"] or {Report:pdf:6063bd0362dda:print}

// $pdf_override = apply_filters( 'gfpdf_override_pdf_bypass', false, $pdf ); <-- bypass cache
// $pdf_override = apply_filters( 'gfpdf_override_pdf_bypass', false, $pdf ); <-- bypass cache
}

}
79 changes: 75 additions & 4 deletions src/Controller/Controller_PDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,10 @@ public function add_filters() {
add_filter( 'gfpdf_pdf_html_output', [ $this->view, 'autoprocess_core_template_options' ], 5, 4 );

/* Cleanup filters */
add_filter( 'gform_before_resend_notifications', [ $this->model, 'resend_notification_pdf_cleanup' ], 10, 2 );
// @TODO
//add_filter( 'gform_before_resend_notifications', [ $this->model, 'resend_notification_pdf_cleanup' ], 10, 2 );

/* Third Party Conflict Fixes */
/* Backwards compatibility and conflict fixes */
add_filter( 'gfpdf_pre_view_or_download_pdf', [ $this, 'sgoptimizer_html_minification_fix' ] );
add_filter( 'gfpdf_legacy_pre_view_or_download_pdf', [ $this, 'sgoptimizer_html_minification_fix' ] );
add_filter(
Expand All @@ -231,6 +232,72 @@ function() {
}
);

add_filter(
'gfpdf_current_pdf_settings_object',
function( $pdf_settings, $form, $entry ) {
return $this->model->apply_backwards_compatibility_filters( $pdf_settings, $entry );
},
10,
3
);

/* Force PDF cache bypass if the user wants to view the mark-up */
add_action(
'gfpdf_view_or_download_pdf',
function() {

/* @TODO - move to shared file */
$options = \GPDFAPI::get_options_class();

/* Disregard if `?html` URL parameter doesn't exist */
if ( ! rgget( 'html' ) ) {
return;
}

/* Disregard if PDF Debug Mode off AND the environment is production */
if ( $options->get_option( 'debug_mode', 'No' ) === 'No' && ( ! function_exists( 'wp_get_environment_type' ) || wp_get_environment_type() === 'production' ) ) {
return;
}

/* Check if user has permission to view info */
if ( ! $this->gform->has_capability( 'gravityforms_edit_forms' ) ) {
return;
}

/* Bypass cache */
add_filter( 'gfpdf_override_pdf_bypass', '__return_true' );
}
);

/* Display form data if the user wants to view it */
add_action(
'gfpdf_view_or_download_pdf',
function( $form, $entry ) {

/* @TODO - move to shared file */
$options = \GPDFAPI::get_options_class();

if ( ! rgget( 'data' ) ) {
return;
}

/* Disregard if PDF Debug Mode off AND the environment is production */
if ( $options->get_option( 'debug_mode', 'No' ) === 'No' && ( ! function_exists( 'wp_get_environment_type' ) || wp_get_environment_type() === 'production' ) ) {
return;
}

/* Check if user has permission to view info */
/* @TODO - correct permission? */
if ( ! $this->gform->has_capability( 'gravityforms_view_settings' ) ) {
return;
}

$this->view->view_form_data( \GPDFAPI::get_form_data( $entry['id'] ) );
},
10,
2
);

/* Meta boxes */
add_filter( 'gform_entry_detail_meta_boxes', [ $this->model, 'register_pdf_meta_box' ], 10, 3 );

Expand Down Expand Up @@ -350,14 +417,18 @@ public function remove_pre_pdf_hooks() {
}

/**
* Prevent the PDF Endpoints being indexed
* Try to prevent the PDF being indexed/cached
*
* @since 5.2
*/
public function prevent_index() {
if ( ! headers_sent() ) {
header( 'X-Robots-Tag: noindex, nofollow', true );
}

if ( ! defined( 'DONOTCACHEPAGE' ) ) {
define( 'DONOTCACHEPAGE', true );
}
}

/**
Expand Down Expand Up @@ -422,7 +493,7 @@ private function pdf_error( $error ) {
if ( $this->gform->has_capability( 'gravityforms_view_settings' ) || in_array( $error->get_error_code(), $whitelist_errors, true ) ) {
wp_die( esc_html( $error->get_error_message() ), $status_code ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
} else {
wp_die( esc_html__( 'There was a problem generating your PDF', 'gravity-forms-pdf-extended' ), $status_code ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
wp_die( esc_html__( 'There was a problem creating the PDF', 'gravity-forms-pdf-extended' ), $status_code ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
}
20 changes: 14 additions & 6 deletions src/Helper/Helper_PDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ public function __construct( $entry, $settings, Helper_Abstract_Form $gform, Hel
$this->form = apply_filters( 'gfpdf_current_form_object', $this->gform->get_form( $entry['form_id'] ), $entry, 'initialize_pdf_class' );

$this->set_path();
$this->set_print_dialog( ! empty( $settings['print'] ) );
}

/**
Expand Down Expand Up @@ -284,6 +285,7 @@ public function render_html( $args = [], $html = '' ) {
*
* @throws MpdfException
* @since 4.0
* @since 6.12 All PDF requests have been standardized to use the functions/methods in \GPDFAPI::create_pdf(), and the DISPLAY/DOWNLOAD options are no longer used by core
*/
public function generate() {

Expand Down Expand Up @@ -519,7 +521,7 @@ public function set_JS( $js ) {

/**
*
* Get the current Gravity Form Entry
* Get the current Gravity Forms Entry
*
* @return array
* @since 4.0
Expand All @@ -539,6 +541,16 @@ public function get_settings() {
return $this->settings;
}

/**
* Get the current Gravity Forms form object
*
* @return array
* @since 6.12
*/
public function get_form() {
return $this->form;
}

/**
* Get the current PDF Name
*
Expand Down Expand Up @@ -868,13 +880,9 @@ protected function load_html( $args = [] ) {
*/
protected function maybe_display_raw_html( $html ) {

/* @TODO - move to shared file */
$options = \GPDFAPI::get_options_class();

/* Disregard if PDF is being saved */
if ( $this->output === 'SAVE' ) {
return;
}

/* Disregard if `?html` URL parameter doesn't exist */
if ( ! rgget( 'html' ) ) {
return;
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Model_Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
*/
class Model_Cache extends Helper_Abstract_Model {

}
}
Loading

0 comments on commit 0b1033e

Please sign in to comment.