Make WordPress Core

Opened 3 weeks ago

Closed 3 weeks ago

Last modified 3 weeks ago

#61374 closed defect (bug) (fixed)

HTML Processor: ::create_fragment returns self when subclassed

Reported by: jonsurrell's profile jonsurrell Owned by: jonsurrell's profile jonsurrell
Milestone: 6.6 Priority: normal
Severity: normal Version:
Component: HTML API Keywords: has-patch has-unit-tests
Focuses: Cc:

Description

In a subclass of WP_HTML_Processor when an instance is created from the static ::create_fragment method the instance will be an instance of WP_HTML_Processor and not and instance of the subclass:

<?php
class Sub extends WP_HTML_Processor {}
$processor = Sub::create_fragment( '<div>' );
get_class( $processor ); // "WP_HTML_Processor" !!

It's very unlikely that the parent class instance is intended to be created from the subclass.

This can be fixed by creating static from the ::create_fragment method.

Change History (6)

This ticket was mentioned in PR #6729 on WordPress/wordpress-develop by @jonsurrell.


3 weeks ago
#1

  • Keywords has-patch has-unit-tests added

In a subclass of WP_HTML_Processor when an instance is created from the static ::create_fragment method the instance will be an instance of WP_HTML_Processor and _not_ and instance of the subclass:

class Sub extends WP_HTML_Processor {}
$processor = Sub::create_fragment( '<div>' );
get_class( $processor ); // "WP_HTML_Processor" !!

It's very unlikely that the parent class instance is intended to be created from the subclass.

Trac ticket: https://core.trac.wordpress.org/ticket/61374

This ticket was mentioned in PR #6729 on WordPress/wordpress-develop by @jonsurrell.


3 weeks ago
#2

Trac ticket: Core-61374

In a subclass of WP_HTML_Processor when an instance is created from the static ::create_fragment method the instance will be an instance of WP_HTML_Processor and _not_ and instance of the subclass:

class Sub extends WP_HTML_Processor {}
$processor = Sub::create_fragment( '<div>' );
get_class( $processor ); // "WP_HTML_Processor" !!

It's very unlikely that the parent class instance is intended to be created from the subclass.

#3 @dmsnell
3 weeks ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 58363:

HTML API: Return subclass from ::create_fragment

When the WP_HTML_Processor was introduced with its ::create_fragment()
static creator method, that method has been returning a new self(...).
Unfortunately, this means that subclasses cannot use that method since it
will return the WP_HTML_Processor instead of the subclass.

With this patch, the static creator method returns new static(...) to preserve
the intended behavior. A new test asserts this behavior for future changes.

Developed in https://github.com/WordPress/wordpress-develop/pull/6729
Discussed in https://core.trac.wordpress.org/ticket/61374

Props dmsnell, jonsurrell.
Follow-up to [56274].
Fixes #61374.

This ticket was mentioned in PR #6729 on WordPress/wordpress-develop by @jonsurrell.


3 weeks ago
#5

Trac ticket: Core-61374

In a subclass of WP_HTML_Processor when an instance is created from the static ::create_fragment method the instance will be an instance of WP_HTML_Processor and _not_ and instance of the subclass:

class Sub extends WP_HTML_Processor {}
$processor = Sub::create_fragment( '<div>' );
get_class( $processor ); // "WP_HTML_Processor" !!

It's very unlikely that the parent class instance is intended to be created from the subclass.

#6 @dmsnell
3 weeks ago

  • Milestone changed from Awaiting Review to 6.6
Note: See TracTickets for help on using tickets.