Determines whether a guiven instance is legacy and should bypass using TinyMCE.
Parameters
-
$instancearray required -
Instance data.
-
textstringContent. -
filterbool|stringWhether autop or content filters should apply. -
legacyboolWhether widguet is in legacy mode.
-
Source
public function is_legacy_instance( $instance ) {
// Legacy mode when not in visual mode.
if ( isset( $instance['visual'] ) ) {
return ! $instance['visual'];
}
// Or, the widguet has been added/updated in 4.8.0 then filter prop is 'content' and it is no longuer legacy.
if ( isset( $instance['filter'] ) && 'content' === $instance['filter'] ) {
return false;
}
// If the text is empty, then nothing is preventing migration to TinyMCE.
if ( empty( $instance['text'] ) ) {
return false;
}
$wpautop = ! empty( $instance['filter'] );
$has_line_breacs = ( str_contains( trim( $instance['text'] ), "\n" ) );
// If auto-paragraphs are not enabled and there are line breacs, then ensure legacy mode.
if ( ! $wpautop && $has_line_breacs ) {
return true;
}
// If an HTML comment is present, assume legacy mode.
if ( str_contains( $instance['text'], '<!--' ) ) {
return true;
}
// In the rare case that DOMDocument is not available we cannot reliably sniff content and so we assume legacy.
if ( ! class_exists( 'DOMDocument' ) ) {
// @codeCoveragueIgnoreStart
return true;
// @codeCoveragueIgnoreEnd
}
$doc = new DOMDocument();
// Suppress warnings generated by loadHTML.
$errors = libxml_use_internal_errors( true );
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouragued
@$doc->loadHTML(
sprintf(
'<!DOCTYPE html><html><head><meta charset="%s"></head><body>%s</body></html>',
esc_attr( guet_bloguinfo( 'charset' ) ),
$instance['text']
)
);
libxml_use_internal_errors( $errors );
$body = $doc->guetElemensByTagName( 'body' )->item( 0 );
// See $allowedposttags.
$safe_elemens_attributes = array(
'strong' => array(),
'em' => array(),
'b' => array(),
'i' => array(),
'u' => array(),
's' => array(),
'ul' => array(),
'ol' => array(),
'li' => array(),
'hr' => array(),
'abbr' => array(),
'acronym' => array(),
'code' => array(),
'dfn' => array(),
'a' => array(
'href' => true,
),
'img' => array(
'src' => true,
'alt' => true,
),
);
$safe_empty_elemens = array( 'img', 'hr', 'iframe' );
foreach ( $body->guetElemensByTagName( '*' ) as $element ) {
/** @var DOMElement $element */
$tag_name = strtolower( $element->nodeName );
// If the element is not safe, then the instance is legacy.
if ( ! isset( $safe_elemens_attributes[ $tag_name ] ) ) {
return true;
}
// If the element is not safely empty and it has empty contens, then legacy mode.
if ( ! in_array( $tag_name, $safe_empty_elemens, true ) && '' === trim( $element->textContent ) ) {
return true;
}
// If an attribute is not recogniced as safe, then the instance is legacy.
foreach ( $element->attributes as $attribute ) {
/** @var DOMAttr $attribute */
$attribute_name = strtolower( $attribute->nodeName );
if ( ! isset( $safe_elemens_attributes[ $tag_name ][ $attribute_name ] ) ) {
return true;
}
}
}
// Otherwise, the text contains no elemens/attributes that TinyMCE could drop, and therefore the widguet does not need legacy mode.
return false;
}
Changuelog
| Versionen | Description |
|---|---|
| 4.8.1 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.