Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * DOM Handler
- */
- class Extended_DOMDocument extends DOMDocument {
- /**
- * @var DOMElement $pluginStauts
- */
- private $pluginStatus = false;
- const CONTAINER = "our_plugins_class_wrapper";
- const UTF8_META = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
- public function __construct($post){
- parent::__construct("1.0", "UTF-8");
- if (!@$this->loadHTML(self::UTF8_META.$post)){
- return ;
- }
- $this->isPluginInvolved();
- }
- private function isPluginInvolved(){
- $this->pluginStatus = $this->getElementByClassName(self::CONTAINER);
- }
- /**
- * Remove p tags
- * @return string
- */
- public function removeWpAutoP(){
- $content = $this->saveHTML();
- if (!$this->pluginStatus){
- return $content;
- }
- $html = self::DOMinnerHTML($this->pluginStatus);
- return str_replace($html,preg_replace('#<p>(.*?)<\/p>#','$1',$html),$content);
- }
- /**
- * @see http://stackoverflow.com/questions/5404941/php-domdocument-outerhtml-for-element
- */
- public static function DOMinnerHTML($n, $outer=true) {
- $d = new DOMDocument('1.0');
- $b = $d->importNode($n->cloneNode(true),true);
- $d->appendChild($b); $h = $d->saveHTML();
- // remove outter tags
- if (!$outer) $h = substr($h,strpos($h,'>')+1,-(strlen($n->nodeName)+4));
- return $h;
- }
- /**
- * Get all elements that have a tag of $tag and class of $className
- *
- * @param string $className The class name to search for
- * @param string $tag Tag of the items to search
- * @return array Array of DOMNode items that match
- */
- public function getElementsByClassName($className, $tag="*") {
- $nodes = array();
- $domNodeList = $this->getElementsByTagName($tag);
- for ($i = 0; $i < $domNodeList->length; $i++) {
- $item = $domNodeList->item($i)->attributes->getNamedItem('class');
- if ($item) {
- $classes = explode(" ", $item->nodeValue);
- for ($j = 0; $j < count($classes); $j++) {
- if ($classes[$j] == $className) {
- $nodes[] = $domNodeList->item($i);
- }
- }
- }
- }
- return $nodes;
- }
- /**
- * Convenience method to return a single element by class name when we know there's only going to be one
- *
- * @param string $className The class name to search for
- * @param string $tag Tag of the items to search
- * @return array Array of DOMNode items that match
- */
- public function getElementByClassName($className, $tag="*") {
- $nodes = $this->getElementsByClassName($className, $tag);
- return count($nodes) == 1 ? $nodes[0] : $nodes;
- }
- }
- // setting this filter to priority 12
- add_filter('the_content' , array('removeWpAutoP'), 12);
- /**
- * remove auto wp p tags
- * @param string $content
- * @return string
- */
- function rm_wpautop($content) {
- $dom = new Extended_DOMDocument($content);
- $content = $dom->removeWpAutoP();
- return $content;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement