GravityPDF / gravity-pdf

Gravity PDF is the ultimate third-party PDF automation tool for generating digital PDFs using the popular form-builder plugin Gravity Forms.
https://gravitypdf.com
89 stars 20 forks source link

Memory issue with lots of classes on Rich Text Field #1536

Open jakejackson1 opened 5 months ago

jakejackson1 commented 5 months ago

Description This problem can occur if a user copy/paste code from another source that includes a lot of class names.

Consider stripping all the classes from the Rich Text field when output in a Core / Universal PDF.

add_filter( 'gfpdf_pdf_field_content_textarea', function( $value, $field, $entry, $form ) {
    $allowed_tags = array_map( function( $item ) {
        unset( $item['class'] );

        return $item;
    }, wp_kses_allowed_html( 'post' ) );

    return wp_kses( $value, $allowed_tags );
}, 10, 4 );
JurriaanK commented 5 months ago

@jakejackson1 I believe my colleague @sandervdwnl reported this issue. For your consideration: In our custom template, where it is possible to use the textarea field value as a merge tag, we solved it using the new WP_HTML_Tag_Processor (WP 6.2+)

/**
 * Reduce classes of HTML that is being used to generate a PDF up to a maximum of 5.
 *
 * @param string $html PDF template HTML in string format
 * @param array  $form The current Gravity Forms array
 * @param array  $entry The raw Gravity Forms Entry array.
 * @param array  $settings The current PDF configuration array.
 * @param object $Helper_PDF The initialised GFPDFHelperHelper_PDF class
 * @return string $html The updated html.
 */
function reduce_classes_in_html_of_pdf( $html, $form, $entry, $settings, $Helper_PDF ) {

    $processor = new WP_HTML_Tag_Processor( $html );

    // Iterate through all tags.
    while ( $processor->next_tag() ) {
        $classes = $processor->get_attribute( 'class' );
        if ( $classes !== null ) {
            $class_array = explode( ' ', $classes );
            // Limit the number of classes to 5.
            if ( count( $class_array ) > 5 ) {
                $class_array = array_slice( $class_array, 0, 5 );
                // Set the new class attribute value.
                $processor->set_attribute( 'class', implode( ' ', $class_array ) );
            }
        }
    }
    // Get the modified HTML.
    return $processor->get_updated_html();
}
add_filter( 'gfpdf_pdf_html_output', 'reduce_classes_in_html_of_pdf', 20, 5 );