WP_Widget_Text::is_legacy_instance( array $instance ): bool
- Since
- 4.8.1
- Source
wp-includes/widgets/class-wp-widget-text.php:90
Compatibility
- WordPress
- since 4.8.1
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$instancearray- Instance data.
$textstringContent.$filterbool|stringWhether autop or content filters should apply.$legacyboolWhether widget is in legacy mode.
Return value
bool- Whether Text widget instance contains legacy data.
Performance profile
How much work a call to WP_Widget_Text::is_legacy_instance() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Scales with input
- Instructions
- 6–87
- Plugin surface
- None
- Called by
- 1
Reads stored settings via get_option(), cached per request but not free on a cold cache.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 103.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()one call below WP_Widget_Text::is_legacy_instance() - optionoption read or write
get_option()one call below WP_Widget_Text::is_legacy_instance()
Further down the call graph this can also reach cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 4 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Widget_Text::is_legacy_instance() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 6–23 | none |
!isset($instance) && !empty($instance) | 57–62 | libxml_use_internal_errors(), get_bloginfo(), esc_attr(), ->loadHTML(), libxml_use_internal_errors(), ->getElementsByTagName(), ->item(), ->getElementsByTagName() |
!isset($instance) && !empty($instance) && !->getElementsByTagName() | 65–75 | libxml_use_internal_errors(), get_bloginfo(), esc_attr(), ->loadHTML(), libxml_use_internal_errors(), ->getElementsByTagName(), ->item(), ->getElementsByTagName(), strtolower() |
!isset($instance) && !empty($instance) && !->getElementsByTagName() && isset([…][$tag_name]) && !$element && !isset($attribute_name) | 79–87 | libxml_use_internal_errors(), get_bloginfo(), esc_attr(), ->loadHTML(), libxml_use_internal_errors(), ->getElementsByTagName(), ->item(), ->getElementsByTagName(), strtolower(), strtolower() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 103 | 6–87 | 16 | |
| 8.5 | 103 | 6–87 | 16 | |
| 8.4 | 103 | 6–87 | 16 | 12 fewer instructions than PHP 8.3 |
| 8.3 | 115 | 6–99 | 16 | |
| 8.2 | 115 | 6–99 | 16 | |
| 8.1 | 115 | 6–99 | 16 | 3 fewer instructions than PHP 7.4 |
| 7.4 | 118 | 6–102 | 16 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 4
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- esc_attr()Escaping for HTML attributes.
- get_bloginfo()Retrieves information about the current site.
- DOMDocument::__construct()
Used by · 1
- WP_Widget_Text::form()Outputs the Text widget settings form.
Source code
public function is_legacy_instance( $instance ) { // Legacy mode when not in visual mode. if ( isset( $instance['visual'] ) ) { return ! $instance['visual']; } // Or, the widget has been added/updated in 4.8.0 then filter prop is 'content' and it is no longer 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_breaks = ( str_contains( trim( $instance['text'] ), "\n" ) ); // If auto-paragraphs are not enabled and there are line breaks, then ensure legacy mode. if ( ! $wpautop && $has_line_breaks ) { 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' ) ) { // @codeCoverageIgnoreStart return true; // @codeCoverageIgnoreEnd } $doc = new DOMDocument(); // Suppress warnings generated by loadHTML. $errors = libxml_use_internal_errors( true ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged @$doc->loadHTML( sprintf( '<!DOCTYPE html><html><head><meta charset="%s"></head><body>%s</body></html>', esc_attr( get_bloginfo( 'charset' ) ), $instance['text'] ) ); libxml_use_internal_errors( $errors ); $body = $doc->getElementsByTagName( 'body' )->item( 0 ); // See $allowedposttags. $safe_elements_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_elements = array( 'img', 'hr', 'iframe' ); foreach ( $body->getElementsByTagName( '*' ) as $element ) {Changelog
Introduced in 4.8.1. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/widgets/class-wp-widget-text.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.