Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jakejackson1 opened this issue Jun 5, 2024 · 1 comment
Open

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

jakejackson1 opened this issue Jun 5, 2024 · 1 comment
Labels

Comments

@jakejackson1
Copy link
Member

jakejackson1 commented Jun 5, 2024

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
Copy link

JurriaanK commented Jun 10, 2024

@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 );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants