[wp-trac] [WordPress Trac] #58087: Fix the 'data' dynamic property in WP_Term

WordPress Trac noreply at wordpress.org
Wed Apr 5 19:18:20 UTC 2023


#58087: Fix the 'data' dynamic property in WP_Term
--------------------------------------------+------------------------------
 Reporter:  antonvlasenko                   |       Owner:  hellofromTonya
     Type:  enhancement                     |      Status:  assigned
 Priority:  normal                          |   Milestone:  6.3
Component:  Taxonomy                        |     Version:  trunk
 Severity:  minor                           |  Resolution:
 Keywords:  php82 has-patch has-unit-tests  |     Focuses:  coding-
                                            |  standards
--------------------------------------------+------------------------------
Changes (by antonvlasenko):

 * keywords:  dev-feedback php82 has-patch has-unit-tests => php82 has-patch
     has-unit-tests


Comment:

 I have considered several possible solutions to the issue with
 `WP_Term::$data`.

 1. Explicitly declaring the `$data` property and initializing it in the
 class constructor.

 This will cause the `$data` property to be returned when calling  `foreach
 ($wp_term as $property_value)` and `(array) $wp_term` (please see
 [https://3v4l.org/a4NBe this code example]).
 However, this leads to fatal errors when adding new categories and tags on
 the `wp-admin/edit-tags.php` page.

 As @hellofromTonya clearly pointed out in her detailed note above,
 [https://core.trac.wordpress.org/ticket/34348#comment:3 it was decided] to
 abandon the explicit declaration of the `WP_Term::$data` property earlier
 for this very reason.

 One may argue that this problem can be solved by removing the `data` key
 from the array wherever the `WP_Term` object is converted to an array.
 However, this is almost certain to introduce bugs in plugins.

 Moreover, it is [https://wpdirectory.net/search/01GX9A587ZHEDDHB9EMCJH3060
 virtually impossible] to track where this object-to-array conversion is
 used in plugins, let alone fix the associated bugs.

 2. Adding the missing magic methods, such as `__set()`, `__unset()`, and
 `__isset()` to the `WP_Term` class to ensure proper operation of the class
 with this dynamic property.

 This may seem like a quick fix, but it is not the best solution from an
 architectural point of view.

 Dynamic properties should not be relied upon to obtain data about the
 object, and having a class method instead would be a more appropriate
 approach. This would avoid potential issues with dynamic properties in the
 future and would improve the overall design of the `WP_Term` class.

 In light of this, I propose:
 1. Implementing a new `WP_Term::get_raw_data()` method, which would return
 the same information that the `WP_Term::$data` property contained;
 2. Deprecating the `WP_Term::$data` dynamic property in favour of
 `WP_Term::get_raw_data()` and removing it in one of the future WordPress
 releases.

 [https://github.com/WordPress/wordpress-develop/pull/4307 The PR I've just
 submitted] implements the proposed changes.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/58087#comment:6>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list