From 4b740bae8c2fc9345b2d8e2505ef8b2db1c60450 Mon Sep 17 00:00:00 2001 From: Rens Tillmann Date: Tue, 20 Oct 2026 04:10:07 +0200 Subject: [PATCH] fix(pdf): attach the PDF font script to super-common so optimizers keep it in order (6.4.x) --- src/docs/changelog.md | 4 ++ src/includes/class-shortcodes.php | 26 +++++++-- tests/test-pdf-font-inline-script.php | 78 +++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 tests/test-pdf-font-inline-script.php diff --git a/src/docs/changelog.md b/src/docs/changelog.md index 273ac2b9..14208f6d 100644 --- a/src/docs/changelog.md +++ b/src/docs/changelog.md @@ -9,6 +9,10 @@ - [PDF Generator Add-on](https://renstillmann.github.io/super-forms/#/pdf-generator-add-on) +## Unreleased - Version 6.4.010-beta + +- **Fix:** The PDF Generator font script for non-Latin languages is now attached to the `super-common` script, so it always runs after `super_common_i18n` is defined, even when a JavaScript optimization plugin reorders scripts. If `super-common` was already printed, it is still printed in the footer. + ## 2026-10-09 - Version 6.4.009-beta - **Fix:** Date submissions use the default `dd-mm-yy` pattern when the saved format, or selected custom format, is missing or empty, while preserving explicit formats and strict date validation. diff --git a/src/includes/class-shortcodes.php b/src/includes/class-shortcodes.php index e5677eaf..97eb8718 100644 --- a/src/includes/class-shortcodes.php +++ b/src/includes/class-shortcodes.php @@ -6886,13 +6886,15 @@ public static function super_form_func( $atts, $elements_only=false ) { file_put_contents($font_file, $font_data); } // ponytail: without the fonts the browser falls back to the default PDF font path. - if( $fonts_ready ) add_action('wp_footer', function($arguments) use ($language) { + if( $fonts_ready ) { $link = SUPER_PLUGIN_FILE . 'includes/extensions/pdf-generator/fonts/'.$language; + add_action('wp_footer', function($arguments) use ($link) { ?> -.woff2') format('woff2');}$link)) . ';'); + } } } } @@ -6901,6 +6903,22 @@ public static function super_form_func( $atts, $elements_only=false ) { return do_shortcode( $result ); } + /** + * Attach the PDF font script to `super-common` so it always runs after the localized + * `super_common_i18n`, even when an optimization plugin moves scripts around. + * When `super-common` was already printed (e.g. in the head), WordPress would never output + * the inline data, so fall back to the footer. + */ + private static function add_pdf_font_script( $js ) { + if( !wp_script_is('super-common', 'done') ) { + wp_add_inline_script('super-common', $js, 'after'); + return; + } + add_action('wp_footer', function() use ($js) { + echo ''; + }, 100); + } + } endif; diff --git a/tests/test-pdf-font-inline-script.php b/tests/test-pdf-font-inline-script.php new file mode 100644 index 00000000..1fe9fd20 --- /dev/null +++ b/tests/test-pdf-font-inline-script.php @@ -0,0 +1,78 @@ +original_wp_scripts = isset($GLOBALS['wp_scripts']) ? $GLOBALS['wp_scripts'] : null; + $GLOBALS['wp_scripts'] = new WP_Scripts(); + $font_dir = SUPER_PLUGIN_DIR . '/includes/extensions/pdf-generator/fonts/'; + if( !is_dir($font_dir) ) wp_mkdir_p($font_dir); + // Existing non-empty files are reused so nothing is downloaded; only files this test creates are removed. + foreach( array('json' => '{"regular":"x"}', 'woff' => 'x', 'woff2' => 'x') as $ext => $content ) { + $file = $font_dir . 'cyrillic.' . $ext; + if( file_exists($file) && filesize($file)>0 ) continue; + file_put_contents($file, $content); + $this->created_font_files[] = $file; + } + } + + public function tear_down() { + foreach( $this->created_font_files as $file ) { + if( file_exists($file) ) unlink($file); + } + $this->created_font_files = array(); + $GLOBALS['wp_scripts'] = $this->original_wp_scripts; + parent::tear_down(); + } + + private function render_cyrillic_pdf_form() { + // Build `_pdf` the way the plugin does, so super_form_func finds every key it reads. + $pdf = SUPER_PDF_Generator::get_default_pdf_settings( array( + 'generate' => 'true', + 'textRendering' => 'true', + 'language' => 'cyrillic', + ) ); + $form_id = $this->create_form( 'publish', array(), array( '_pdf' => $pdf ) ); + return SUPER_Shortcodes::super_form_func( array( 'id' => (string) $form_id ) ); + } + + public function test_fonts_attach_to_super_common_after_block() { + $this->render_cyrillic_pdf_form(); + $after = wp_scripts()->get_data( 'super-common', 'after' ); + $this->assertIsArray( $after ); + $this->assertStringContainsString( 'super_common_i18n.fonts', implode( "\n", $after ) ); + $this->assertStringContainsString( 'pdf-generator', implode( "\n", $after ) ); + } + + public function test_fonts_print_after_localized_var() { + $this->render_cyrillic_pdf_form(); + ob_start(); + wp_print_scripts( array( 'super-common' ) ); + $out = ob_get_clean(); + $var_pos = strpos( $out, 'var super_common_i18n' ); + $fonts_pos = strpos( $out, 'super_common_i18n.fonts' ); + $after_pos = strpos( $out, 'id="super-common-js-after"' ); + $this->assertNotFalse( $var_pos, $out ); + $this->assertNotFalse( $fonts_pos, $out ); + $this->assertNotFalse( $after_pos, $out ); + $this->assertLessThan( $fonts_pos, $var_pos ); + $this->assertLessThan( $fonts_pos, $after_pos ); + } + + public function test_fonts_fall_back_to_footer_when_super_common_already_printed() { + wp_scripts()->done[] = 'super-common'; + $this->render_cyrillic_pdf_form(); + // Core's deprecated skip-link footer callback is unrelated; the test hooks are restored after each test. + remove_action( 'wp_footer', 'the_block_template_skip_link' ); + ob_start(); + do_action( 'wp_footer' ); + $out = ob_get_clean(); + $this->assertStringContainsString( 'super_common_i18n.fonts', $out ); + $this->assertStringContainsString( "font-family:'SF-Unicode'", $out ); + } +}