Skip to content
Merged
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
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

## Unreleased

- **Fix:** PDF Generator font data (Cyrillic and Arabic) is now attached to the Super Forms common script, so it still loads when a JavaScript optimization plugin reorders or delays scripts.
- **Fix:** PDF Generator "Enable debug mode" now only downloads the PDF and no longer submits the form, so no entry is saved and no e-mails are sent.
- **Fix:** When the PDF cannot be created, a message is shown and the visitor can close it and try again, instead of the form staying on "Generating PDF file..." forever. Nothing is submitted in that case.

Expand Down
36 changes: 18 additions & 18 deletions includes/class-shortcodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -6524,27 +6524,11 @@ public static function super_form_func( $atts, $elements_only=false ) {
if($settings['_pdf']['textRendering']==='true'){
// Cyrillic compatible font
if($settings['_pdf']['cyrillicText']==='true') {
add_action( 'wp_footer', function(){
?>
<script>
super_common_i18n.fonts = {
NotoSans: JSON.parse('<?php echo file_get_contents(SUPER_PLUGIN_DIR . '/includes/extensions/pdf-generator/fonts.json'); ?>')
};
</script>
<?php
}, 100 );
self::add_pdf_font_script( "super_common_i18n.fonts = {NotoSans: JSON.parse('" . file_get_contents(SUPER_PLUGIN_DIR . '/includes/extensions/pdf-generator/fonts.json') . "')};" );
}
// Arabic compatible font
if($settings['_pdf']['arabicText']==='true') {
add_action( 'wp_footer', function(){
?>
<script>
super_common_i18n.fonts = {
NotoSans: JSON.parse('<?php echo file_get_contents(SUPER_PLUGIN_DIR . '/includes/extensions/pdf-generator/fonts-arabic.json'); ?>')
};
</script>
<?php
}, 100 );
self::add_pdf_font_script( "super_common_i18n.fonts = {NotoSans: JSON.parse('" . file_get_contents(SUPER_PLUGIN_DIR . '/includes/extensions/pdf-generator/fonts-arabic.json') . "')};" );
}
}
}
Expand All @@ -6553,6 +6537,22 @@ public static function super_form_func( $atts, $elements_only=false ) {
return do_shortcode( $result );
}

/**
* Attach PDF font data to the `super-common` script so it always runs after
* the localized `super_common_i18n` object, also when optimization plugins
* reorder scripts. Falls back to a footer script when `super-common` was
* already printed (WordPress never outputs inline data for a printed handle).
*/
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 '<script>' . $js . '</script>';
}, 100 );
}

}

endif;
72 changes: 72 additions & 0 deletions tests/test-security-pdf-font-inline-script.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* PDF Generator font data must be attached to the `super-common` script so it
* runs after the localized `super_common_i18n` object.
*/

if ( ! class_exists( 'Super_Forms_Upload_Security_Test_Case' ) ) {
require_once __DIR__ . '/test-security-upload-00-base.php';
}

class Test_PDF_Font_Inline_Script extends Super_Forms_Upload_Security_Test_Case {

public function set_up() {
parent::set_up();
$GLOBALS['wp_scripts'] = new WP_Scripts();
}

public function tear_down() {
$GLOBALS['wp_scripts'] = new WP_Scripts();
parent::tear_down();
}

private function render_pdf_form() {
if ( ! class_exists( 'SUPER_PDF_Generator' ) ) {
require_once dirname( __DIR__ ) . '/includes/extensions/pdf-generator/pdf-generator.php';
}
$pdf = SUPER_PDF_Generator::get_default_pdf_settings( array() );
$pdf['generate'] = 'true';
$pdf['textRendering'] = 'true';
$pdf['cyrillicText'] = 'true';
$pdf['arabicText'] = 'false';
$form = $this->create_form( 'publish', array(), array( '_pdf' => $pdf ) );
SUPER_Shortcodes::super_form_func( array( 'id' => (string) $form ) );
}

public function test_fonts_attach_to_super_common_after_block() {
$this->render_pdf_form();

$after = wp_scripts()->get_data( 'super-common', 'after' );
$this->assertIsArray( $after );
$this->assertStringContainsString( 'super_common_i18n.fonts', implode( "\n", $after ) );
}

public function test_fonts_print_after_localized_var() {
$this->render_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 );
$this->assertNotFalse( $fonts_pos );
$this->assertNotFalse( $after_pos );
$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_pdf_form();

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 );
}
}
Loading