Improve DOMDocument-using filters
Remove HTML document stuff that's not supposed to be there.
This commit is contained in:
parent
32e118f312
commit
9e012a5a5b
@ -429,15 +429,20 @@ if ( ! function_exists( 'fictioneer_add_epub_chapters' ) ) {
|
||||
}
|
||||
|
||||
// Start cleaning up content...
|
||||
$content = str_replace( '<body>', '<div class="content__inner">', $content );
|
||||
$content = str_replace( '</body>', '</div>', $content );
|
||||
if ( strpos( $content, '<body>' ) !== false && strpos( $content, '</body>' ) !== false ) {
|
||||
$content = str_replace( '<body>', '<div class="content__inner">', $content );
|
||||
$content = str_replace( '</body>', '</div>', $content );
|
||||
} else {
|
||||
$content = '<div class="content__inner">' . $content . '</div>';
|
||||
}
|
||||
|
||||
$content = str_replace( 'data-type="URL"', '', $content );
|
||||
$content = preg_replace( ['(\s+)u', '(^\s|\s$)u'], [' ', ''], $content );
|
||||
$content = preg_replace( '/data-align="([^"]*)"/', '', $content );
|
||||
|
||||
// Create temporary file to continue cleaning up content...
|
||||
$inner = new DOMDocument();
|
||||
libxml_use_internal_errors( true );
|
||||
$inner = new DOMDocument();
|
||||
$inner->loadHTML( '<?xml encoding="UTF-8">' . $content );
|
||||
libxml_clear_errors();
|
||||
$inner->preserveWhiteSpace = false;
|
||||
@ -449,6 +454,9 @@ if ( ! function_exists( 'fictioneer_add_epub_chapters' ) ) {
|
||||
$frame->appendChild( $doc->importNode( $node, true ) );
|
||||
}
|
||||
|
||||
unset( $inner ); // No longer required
|
||||
unset( $inner_finder ); // No longer required
|
||||
|
||||
// Remove sensitive-alternatives (only full chapters) from chapter file
|
||||
foreach ( $finder->query( "//*[contains(@class, 'sensitive-alternative')]" ) as $node ) {
|
||||
$node->parentNode->removeChild( $node );
|
||||
@ -513,7 +521,7 @@ if ( ! function_exists( 'fictioneer_add_epub_chapters' ) ) {
|
||||
$new_img->setAttribute( 'alt', $alt );
|
||||
|
||||
// Add image to list
|
||||
$image_list[] = [$path_parts['filename'] . $extension, substr( $extension, 1 )];
|
||||
$image_list[] = [ $path_parts['filename'] . $extension, substr( $extension, 1 ) ];
|
||||
|
||||
// Replace wrapped image node with cleaned image node
|
||||
$node->parentNode->insertBefore( $new_img, $node->nextSibling );
|
||||
|
@ -582,9 +582,15 @@ function fictioneer_add_lightbox_to_post_images( $content ) {
|
||||
$img->setAttribute( 'tabindex', '0' );
|
||||
};
|
||||
|
||||
$content = $doc->saveHTML();
|
||||
$content = str_replace( '<?xml encoding="UTF-8">', '', $content );
|
||||
// Extract and save body content
|
||||
$body = $doc->getElementsByTagName( 'body' )->item( 0 );
|
||||
$content = $body ? $doc->saveHTML( $body ) : '';
|
||||
$content = preg_replace( '/<\/?body>/', '', $content );
|
||||
|
||||
// Release memory
|
||||
unset( $doc );
|
||||
|
||||
// Continue filter
|
||||
return $content;
|
||||
}
|
||||
|
||||
@ -751,9 +757,15 @@ function fictioneer_embed_consent_wrappers( $content ) {
|
||||
$twitter->parentNode->insertBefore( $consent_element );
|
||||
}
|
||||
|
||||
$content = $dom->saveHTML();
|
||||
$content = str_replace( '<?xml encoding="UTF-8">', '', $content );
|
||||
// Extract and save body content
|
||||
$body = $dom->getElementsByTagName( 'body' )->item( 0 );
|
||||
$content = $body ? $dom->saveHTML( $body ) : '';
|
||||
$content = preg_replace( '/<\/?body>/', '', $content );
|
||||
|
||||
// Release memory
|
||||
unset( $dom );
|
||||
|
||||
// Continue filter
|
||||
return $content;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user