Skip to content
58 changes: 58 additions & 0 deletions src/wp-includes/html-api/class-wp-html-open-elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,56 @@ class WP_HTML_Open_Elements {
*/
private $has_p_in_button_scope = false;

/**
* A function that will be called when an item is popped off the stack of open elements.
*
* The function will be called with the popped item as its argument.
*
* @since 6.6.0
*
* @var Closure
*/
private $pop_handler = null;

/**
* A function that will be called when an item is pushed onto the stack of open elements.
*
* The function will be called with the pushed item as its argument.
*
* @since 6.6.0
*
* @var Closure
*/
private $push_handler = null;

/**
* Sets a pop handler that will be called when an item is popped off the stack of
* open elements.
*
* The function will be called with the pushed item as its argument.
*
* @since 6.6.0
*
* @param Closure $handler The handler function.
*/
public function set_pop_handler( Closure $handler ) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason this is specifically a Closure and isn't just more generally a callable?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Closures cannot be serialized or deserialized, meaning that there's no possible way to prep a database record with user input that sets something unexpected here.

$this->pop_handler = $handler;
}

/**
* Sets a push handler that will be called when an item is pushed onto the stack of
* open elements.
*
* The function will be called with the pushed item as its argument.
*
* @since 6.6.0
*
* @param Closure $handler The handler function.
*/
public function set_push_handler( Closure $handler ) {
$this->push_handler = $handler;
}

/**
* Reports if a specific node is in the stack of open elements.
*
Expand Down Expand Up @@ -429,6 +479,10 @@ public function after_element_push( $item ) {
$this->has_p_in_button_scope = true;
break;
}

if ( null !== $this->push_handler ) {
( $this->push_handler )( $item );
}
}

/**
Expand Down Expand Up @@ -458,5 +512,9 @@ public function after_element_pop( $item ) {
$this->has_p_in_button_scope = $this->has_element_in_button_scope( 'P' );
break;
}

if ( null !== $this->pop_handler ) {
( $this->pop_handler )( $item );
}
}
}
Loading