Skip to content

Commit

Permalink
Improve performance reading from disk, and caching
Browse files Browse the repository at this point in the history
  • Loading branch information
jakejackson1 committed Mar 4, 2024
1 parent 0afd432 commit c8a2180
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 23 deletions.
5 changes: 5 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ Gravity PDF can be run on most modern shared web hosting without any issues. It

== Changelog ==

= 6.9.1 =
* Bug: Ensure the template cache is correctly cleared when PDF Debug Mode is enabled
* Bug: Flush the template cache after installing new templates via the PDF Template Manager
* Housekeeping: Small improvement to performance when reading template and font files from disk

= 6.9.0 =
* Feature: Add new conditional logic options to PDFs eg. Payment Status, Date Created, Starred (props: Gravity Wiz)
* Feature: Add support for Show HTML Fields, Show Empty Fields, Show Section Break Description, and Enable Conditional Logic PDF settings when displaying Gravity Wiz Nested Forms field
Expand Down
33 changes: 13 additions & 20 deletions src/Helper/Helper_Templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,12 @@ public function get_template_url() {
* @since 4.1
*/
public function get_all_templates() {
$options = GPDFAPI::get_options_class();
$debug = $options->get_option( 'debug_mode', 'No' );
$options = GPDFAPI::get_options_class();
$debug = $options->get_option( 'debug_mode', 'No' );
$cache_name = $this->data->template_transient_cache . '-template-list';

if ( $debug === 'No' ) {
$cache_name = $this->data->template_transient_cache . '-template-list';
$cache = get_transient( $cache_name );

/* There may be no transient and we got a non-array. If that occurs reset $cache */
$cache = get_transient( $cache_name );
if ( is_array( $cache ) && ! empty( $cache ) ) {
return $cache;
}
Expand All @@ -144,11 +142,7 @@ function( $file ) {
);
}

if ( $debug === 'No' ) {
$cache = $template_list;

set_transient( $cache_name, $cache, 604800 );
}
set_transient( $cache_name, $template_list, 604800 );

return $template_list;
}
Expand All @@ -165,11 +159,11 @@ public function get_unfiltered_template_list() {

/* Get current multisite templates, if any */
if ( is_multisite() ) {
$raw_templates[] = glob( $this->data->multisite_template_location . '*.php' );
$raw_templates[] = glob( $this->data->multisite_template_location . '*.php', GLOB_NOSORT );
}

/* Get the current user-templates and the core templates */
$raw_templates[] = glob( $this->data->template_location . '*.php' );
$raw_templates[] = glob( $this->data->template_location . '*.php', GLOB_NOSORT );
$raw_templates[] = $this->get_core_pdf_templates();

return apply_filters( 'gfpdf_unfiltered_template_list', $raw_templates );
Expand Down Expand Up @@ -398,11 +392,12 @@ public function get_template_files_by_id( $template_id ) {
public function get_template_info_by_path( $template_path, $cache_name = '', $cache_time = 604800 ) {
$options = GPDFAPI::get_options_class();
$debug = $options->get_option( 'debug_mode', 'No' );
$cache = [];

$cache = [];
$cache_name = ! empty( $cache_name ) ? $cache_name : $this->data->template_transient_cache;

if ( $debug === 'No' ) {
$cache_name = ! empty( $cache_name ) ? $cache_name : $this->data->template_transient_cache;
$cache = get_transient( $cache_name );
$cache = get_transient( $cache_name );

/* There may be no transient and we got a non-array. If that occurs reset $cache */
if ( ! is_array( $cache ) ) {
Expand All @@ -429,11 +424,9 @@ public function get_template_info_by_path( $template_path, $cache_name = '', $ca
$info['required_pdf_version'] = ( strlen( 'required_pdf_version' ) > 0 ) ? $info['required_pdf_version'] : '4.0';

/* Save the results to a transient so we don't hit the disk every page load */
if ( $debug === 'No' ) {
$cache[ $template_path ] = $info;
$cache[ $template_path ] = $info;

set_transient( $cache_name, $cache, $cache_time );
}
set_transient( $cache_name, $cache, $cache_time );

return $info;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Model_PDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -2100,7 +2100,7 @@ public function register_custom_font_data_with_mPDF( $fonts ) {
*/
public function add_unregistered_fonts_to_mPDF( $fonts ) {

$user_fonts = glob( $this->data->template_font_location . '*.[tT][tT][fF]' );
$user_fonts = glob( $this->data->template_font_location . '*.[tT][tT][fF]', GLOB_NOSORT );
$user_fonts = ( is_array( $user_fonts ) ) ? $user_fonts : [];

$flattened_fonts_array = [];
Expand Down
4 changes: 2 additions & 2 deletions src/Model/Model_Templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function ajax_process_uploaded_template() {

/* Get the template headers now all the files are in the right location */
$this->templates->flush_template_transient_cache();
$headers = $this->get_template_info( glob( $unzipped_dir_name . '*.php' ) );
$headers = $this->get_template_info( glob( $unzipped_dir_name . '*.php', GLOB_NOSORT ) );

/* Fix template path */
$headers = array_map(
Expand Down Expand Up @@ -361,7 +361,7 @@ public function unzip_and_verify_templates( $zip_path ) {
}

/* Check unzipped templates for a valid v4 header, or v3 string pattern */
$files = glob( $dir . '*.php' );
$files = glob( $dir . '*.php', GLOB_NOSORT );

if ( ! is_array( $files ) || count( $files ) === 0 ) {
throw new Exception( esc_html__( 'No valid PDF template found in Zip archive.', 'gravity-forms-pdf-extended' ) );
Expand Down

0 comments on commit c8a2180

Please sign in to comment.