Last active
December 29, 2018 20:00
-
-
Save jonathanbardo/b70c139da0b1a4382f8b to your computer and use it in GitHub Desktop.
Opposite of wpautop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Do the opposite of wpautop | |
* | |
* @param string $s | |
* | |
* @return string | |
*/ | |
public static function reverse_wpautop( $s ) { | |
//remove any new lines already in there | |
$s = str_replace( "\n", '', $s ); | |
//replace <br /> with \n | |
$s = str_replace( [ '<br />', '<br>', '<br/>'], "\n", $s ); | |
//replace </p> with \n\n | |
$s = str_replace( '</p>', "\n\n", $s ); | |
// Remove all characters from beginning and end of line | |
return self::filter_html( $s ); | |
} | |
/** | |
* Function to remove all unwanted tags from the html and thus keeping it clean. | |
* | |
* @param $input | |
* | |
* @param string $add_to_allowed_tag | |
* @return array|mixed|string | |
*/ | |
public static function filter_html( $input, $add_to_allowed_tag = '' ) { | |
// God WordPress this is not very kind | |
$value = wp_unslash( $input ); | |
// Remove empty paragraph and create | |
$value = str_replace( [ ' ', '<p> </p>' ], [ '', '' ], $value ); | |
$value = str_replace( [ '<p></p>' ], [ '' ], $value ); | |
$value = str_replace( [ '<b>', '</b>' ], [ '<strong>', '</strong>' ], $value ); | |
$value = str_replace( [ '<i>', '</i>' ], [ '<em>', '</em>' ], $value ); | |
// Remove unallowed tags | |
$allowed_tags = '<a><strong><em><ol><ul><li>' . $add_to_allowed_tag; | |
$value = strip_tags( $value, $allowed_tags ); | |
/** | |
* Little hack to remove all attributes in an html string | |
*/ | |
$dom = new \DOMDocument; | |
libxml_use_internal_errors( true ); | |
/** | |
* This is to have DOMDocument interpret the string as UTF-8 | |
*/ | |
$dom->loadHTML( '<meta http-equiv="content-type" content="text/html; charset=utf-8">' . $value ); | |
$xpath = new \DOMXPath( $dom ); | |
$nodes = $xpath->query( '//@*' ); | |
foreach ( $nodes as $node ) { | |
$parent = $node->parentNode; | |
if ( ! is_object( $parent ) ) { | |
continue; | |
} | |
/** | |
* We want to keep anchor attributes | |
*/ | |
if ( 'a' === $parent->tagName && $parent->hasAttributes() ) { | |
$allowed_attrs = [ | |
'title', | |
'target', | |
'href', | |
]; | |
for ( $i = $parent->attributes->length - 1; $i >= 0; --$i ) { | |
if ( ! in_array( $parent->attributes->item( 0 )->name, $allowed_attrs ) ) { | |
$parent->removeAttributeNode( $parent->attributes->item( $i ) ); | |
} | |
} | |
} else { | |
$node->parentNode->removeAttribute( $node->nodeName ); | |
} | |
} | |
/** | |
* Make sure DOMDocument didn't introduce any more unallowed tags like <body> | |
*/ | |
$value = strip_tags( $dom->saveHTML(), $allowed_tags ); | |
libxml_clear_errors(); | |
return trim( $value ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment