Skip to content

Conversation

@hellofromtonya
Copy link
Contributor

@hellofromtonya hellofromtonya commented Aug 22, 2024

This PR implements Approach 2 to add full support to WP_Term for dynamic properties.

Adds dynamic property support to WP_Term.

Removes the #[AllowDynamicProperties] attribute from the class.

Adds a full set of magic methods.

Adds a new private property called $dynamic_properties to hold all of the dynamic properties that interact with __get() and/or __set().

The magic methods work with the new WP_Term::$dynamic_property:

  • When getting or setting, the dynamic property is added to WP_Term::$dynamic_property.
  • When unsetting, the dynamic property is removed from WP_Term::$dynamic_property.
  • When checking if isset(), it uses WP_Term::$dynamic_property.

WP_Term::to_array(): Adjusts and uses public and dynamic props.

Restores previous behavior of WP_Term::to_array() to return an associative array of all the public and dynamic properties. It does not include the new private WP_Term::$dynamic_property property in the returned array.

Type casting from object to array, e.g. (array) $term, is different with the full set of magic methods. It creates an associative array with:

  • all of the declared properties (including the new private WP_Term::$dynamic_properties property).
  • does not include the dynamic properties (as they are now stored in the WP_Term::$dynamic_properties property).

Instead of type casting, WP_Term::to_array() should be used. Instances in Core are replaced to use it.

TODO

  • Remove the class annotation.
  • Add full set of magic methods.
  • Populate the array with the dynamic properties in WP_Term::to_array().
  • Add unit tests.
  • Manually test in local site.
  • Manually test with plugins that (a) do array type casting and/or add dynamic properties (such as WooCommerce).

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


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@github-actions
Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Dynamic properties are stored in a private internal array called 'dynamic_properties',
which are used within the new full set of magic methods.
@hellofromtonya hellofromtonya force-pushed the try/wp_term/support-dynamic-props branch from a07dbcf to 9c01706 Compare August 22, 2024 22:29
Restores the behavior of WP_Term::to_array() to return an associative array of all the public and dynamic properties.

Type casting from object to array, e.g. `(array) $term`, is different with the full set of magic methods. It creates an associative array with:
* all of the declared properties (including the new private `WP_Term::$dynamic_properties` property).
* does not include the dynamic properties (as they are now stored in the `WP_Term::$dynamic_properties` property).

Instead of type casting, `WP_Term::to_array()` should be used. Instances in Core are replaced to use it.
@hellofromtonya hellofromtonya marked this pull request as ready for review August 23, 2024 16:20
@github-actions
Copy link

github-actions bot commented Aug 23, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props hellofromtonya, antonvlasenko.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@hellofromtonya hellofromtonya marked this pull request as draft August 23, 2024 16:20
@anton-vlasenko
Copy link

I've tried to test this PR since it's the preferred solution out of the two, but the unit tests are failing. Could you please take a look, @hellofromtonya?

@hellofromtonya hellofromtonya force-pushed the try/wp_term/support-dynamic-props branch from 9ce3437 to 7b2ee37 Compare August 26, 2024 20:36
@hellofromtonya
Copy link
Contributor Author

hellofromtonya commented Aug 26, 2024

I've tried to test this PR since it's the preferred solution out of the two, but the unit tests are failing. Could you please take a look, @hellofromtonya?

@anton-vlasenko The tests that are failing are for the __get() magic method. I'm still working on that dataset and its test setup. For now, I force pushed a change to skip over that test.

@hellofromtonya hellofromtonya force-pushed the try/wp_term/support-dynamic-props branch from 7b2ee37 to c8340ae Compare August 26, 2024 22:31
'what' => 'term',
'position' => $level,
'supplemental' => (array) $tag,
'supplemental' => $tag->to_array(),
Copy link

@anton-vlasenko anton-vlasenko Aug 27, 2024

Choose a reason for hiding this comment

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

This change is fine and is necessary to keep adding tags in wp-admin working (otherwise JavaScript just silently fails as the spinner keeps spinning on the edit-tags.php page despite the tag being added), but it seems that the Customer Reviews for WooCommerce plugin may stop working if it doesn't adopt the same change. Source: https://www.wpdirectory.net/search/01J69GH2CS5JB61HFCBF8FGTQS
As noted in https://core.trac.wordpress.org/ticket/61890#comment:13, a developer note will be necessary to inform developers of this plugin, as well as other plugins that convert WP_Term objects to arrays, about the upcoming changes.

I initially avoided this approach because I was uncertain about how it would affect many of the existing plugins, some of which are very popular.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tested the Customer Reviews for WooCommerce plugin with WooCommerce and this patch applied. It worked with no issues.

@anton-vlasenko did it not work for you in your testing?

I'm now thinking the adoption of $term->to_array() is not mandatory.

@adrianduffell questions made me realize, there are 2 distinct use cases for converting the term object into an array:

  • Use $term->to_array() when you need an array that includes all dynamic properties.
  • Use (array) $term when you need only the declared properties, i.e. no dynamic properties.

And yes, both of these use cases will be in a dev note for this change.

Copy link
Contributor Author

@hellofromtonya hellofromtonya Aug 27, 2024

Choose a reason for hiding this comment

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

Wait. I see the issue.

Testing it in the Reviews > Review Tagging > Create New Tag. When adding a new review tag, the issue exists where it keeps spinning and does not show the new tag. Upon refresh, the tag is there.

custom-reviews-for-wc

Here's the test report.

@hellofromtonya hellofromtonya changed the title WP_Term: Add dynamic property support. Approach 2: WP_Term: Add dynamic property support. Aug 27, 2024
@hellofromtonya hellofromtonya force-pushed the try/wp_term/support-dynamic-props branch 2 times, most recently from b75d44a to 2f1ccae Compare August 27, 2024 19:50
Splits the tests into 1 method under test per test file/class.

Adds tests for __get(), __set(), __isset(), __unset(), and to_array().
@hellofromtonya hellofromtonya marked this pull request as ready for review August 27, 2024 21:23
@hellofromtonya
Copy link
Contributor Author

@adrianduffell raised a valid point:

I would see that all PHP language constructs such as (array) type casting would be supported by WordPress, unless forbidden in the coding standards.

This PR (Approach 2) does not meet that expectation.

In order to continue support (array) $terms type casting while ensuring it matches the same results as the WP_Term::to_array(), this PR is not a viable solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants