| Server IP : 47.82.179.145 / Your IP : 216.73.217.129 Web Server : nginx/1.26.3 System : Linux iZt4n9czhka2zvqfajdlr2Z 6.8.0-63-generic #66-Ubuntu SMP PREEMPT_DYNAMIC Fri Jun 13 20:25:30 UTC 2025 x86_64 User : www ( 1001) PHP Version : 8.3.30 Disable Function : passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /www/wwwroot/scuipturepactory.com/wp-content/themes/plover/core/src/Toolkits/Html/ |
Upload File : |
<?php
namespace Plover\Core\Toolkits\Html;
use DOMDocument;
use DOMElement;
/**
* Wrapper for DOMDocument
*
* @since 1.0.0
*/
class Document {
/**
* HTML DOM
*
* @var DOMDocument
*/
private $dom;
/**
* Create Html document instance.
*
* @param string $raw
*/
public function __construct( $raw ) {
$this->dom = new DOMDocument();
if ( ! $raw ) {
return;
}
$libxml_previous_state = libxml_use_internal_errors( true );
$this->dom->preserveWhiteSpace = false;
if ( defined( 'LIBXML_HTML_NOIMPLIED' ) && defined( 'LIBXML_HTML_NODEFDTD' ) ) {
$options = LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD;
} else {
if ( defined( 'LIBXML_HTML_NOIMPLIED' ) ) {
$options = LIBXML_HTML_NOIMPLIED;
} else {
if ( defined( 'LIBXML_HTML_NODEFDTD' ) ) {
$options = LIBXML_HTML_NODEFDTD;
} else {
$options = 0;
}
}
}
// @see https://stackoverflow.com/questions/13280200/convert-unicode-to-html-entities-hex.
$html = preg_replace_callback(
'/[\x{80}-\x{10FFFF}]/u',
static function ( array $matches ) {
return sprintf(
'&#x%s;',
ltrim(
strtoupper(
bin2hex(
iconv(
'UTF-8',
'UCS-4',
current( $matches )
)
)
),
'0'
)
);
},
$raw
);
$this->dom->loadHTML( $html, $options );
$this->dom->formatOutput = true;
libxml_clear_errors();
libxml_use_internal_errors( $libxml_previous_state );
}
/**
* Return DOM document instance.
*
* @return DOMDocument
*/
public function get_dom() {
return $this->dom;
}
/**
* Save as raw HTML string
*
* @return string
*/
public function save_html() {
return $this->dom->saveHTML();
}
/**
* Get root/first element
*
* @return Element|null
*/
public function get_root_element() {
return $this->get_element_by_tag_name( '*' );
}
/**
* @param string $tag
* @param int $index
*
* @return Element|null
*/
public function get_element_by_tag_name( string $tag, int $index = 0 ) {
$element = $this->dom->getElementsByTagName( $tag )->item( $index );
if ( ! $element ) {
return null;
}
return $this->sanitize_element( $element );
}
/**
* @param $node
*
* @return Element|null
*/
private function sanitize_element( $node ) {
if ( $node && $node->nodeType === XML_ELEMENT_NODE ) {
return new Element( $node );
}
return null;
}
/**
* @param array $tags
*
* @return Element|null
*/
public function get_element_by_tags_priority( $tags = array( '*' ) ) {
foreach ( $tags as $tag ) {
$el = $this->get_element_by_tag_name( $tag );
if ( $el ) {
return $el;
}
}
return null;
}
/**
* @param Element $element
*
* @return void
*/
public function append_element( $element ) {
$this->dom->appendChild( $element->get_dom_element() );
}
/**
* @param string $tag
*
* @return Element|false|null
*/
public function create_element( $tag ) {
$element = null;
try {
$element = $this->dom->createElement( $tag );
} catch ( \Exception $e ) {
new \WP_Error( 'invalid_dom_tag', $e->getMessage() );
}
if ( is_null( $element ) ) {
return null;
}
return $this->sanitize_element( $element );
}
}