Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor magic methods in WP_List_Table, WP_User_Query, WP_Text_Diff_Renderer_Table and WP_Admin_Bar classes #3535

Conversation

anton-vlasenko
Copy link

@anton-vlasenko anton-vlasenko commented Oct 27, 2022

This PR aims to refactor magic methods in WP_List_Table, WP_User_Query, WP_Text_Diff_Renderer_Table, and WP_Admin_Bar classes to make them compatible with PHP 8.2. Also, it ensures that developers will get a warning when using dynamic properties on these classes.

Trac ticket: https://core.trac.wordpress.org/ticket/56876
Trac ticket: https://core.trac.wordpress.org/ticket/58896


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.

@anton-vlasenko anton-vlasenko force-pushed the fix/disallow-dynamic-properties-in-wp-list-table branch from a164aed to f90602a Compare October 27, 2022 20:34
@anton-vlasenko anton-vlasenko marked this pull request as ready for review October 27, 2022 21:26
@jeffpaul
Copy link
Member

jeffpaul commented Nov 4, 2022

@SergeyBiryukov @jrfnl pinging you here in hopes one of you can help give this a review and help it land in 6.1.1... thank you!

@jeffpaul
Copy link
Member

jeffpaul commented Nov 4, 2022

@hellofromtonya also pinging you as well, any help with review/commit on this for 6.1.1 would be appreciated... thank you!

@anton-vlasenko anton-vlasenko force-pushed the fix/disallow-dynamic-properties-in-wp-list-table branch from 7493f22 to a1dc1d8 Compare January 23, 2023 22:15
@anton-vlasenko anton-vlasenko changed the title Disallow dynamic properties in WP_List_Table Disallow dynamic properties in WP_List_Table and WP_User_Query Jan 25, 2023
@anton-vlasenko
Copy link
Author

anton-vlasenko commented Jan 25, 2023

I want to add a fix for another class to this PR because the fixes are similar.
So please don't review for now.

The PR is ready for review.

@anton-vlasenko anton-vlasenko changed the title Disallow dynamic properties in WP_List_Table and WP_User_Query Disallow dynamic properties in WP_List_Table and WP_User_Query classes Jan 26, 2023
…ons.

The firing order of dataProviders vs. set_up_before_class() seems to depend on the PHPUnit version.
@anton-vlasenko anton-vlasenko changed the title Disallow dynamic properties in WP_List_Table and WP_User_Query classes Disallow dynamic properties in WP_List_Table, WP_User_Query and WP_Text_Diff_Renderer_Table classes Feb 9, 2023
It creates issues.
WP_Admin_Bar::$menu property should be declared instead to avoid any class behavior inconsistency.
For example, isset($wp_admin_bar->menu) will return false, but $wp_admin_bar::$menu returns a value.
It's safe to remove it as it doesn't break BC.
See https://wpdirectory.net/search/01GC9XDX4EBTTDF7G8AJNYRCJW
All the plugins check if it exists before using it.
And it doesn't exist because this property was never defined in the __isset magic method.
…ies method.

2. Add dataprovider.
3. Add comments.
2. Rename parameters in the named dataset.
3. Update comments.
Copy link
Contributor

@audrasjb audrasjb left a comment

Choose a reason for hiding this comment

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

I added a few very small change requests.

src/wp-admin/includes/class-wp-list-table.php Outdated Show resolved Hide resolved
src/wp-admin/includes/class-wp-list-table.php Outdated Show resolved Hide resolved
src/wp-includes/class-wp-text-diff-renderer-table.php Outdated Show resolved Hide resolved
__( 'The "%1$s" property isn\'t defined. Dynamic properties are deprecated in PHP 8.2 and above.' ),
$name
),
'6.3.0'
Copy link
Contributor

Choose a reason for hiding this comment

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

Use 6.2.0

Copy link
Author

Choose a reason for hiding this comment

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

Good catch.
Fixed in a9e364e

src/wp-includes/class-wp-user-query.php Outdated Show resolved Hide resolved
src/wp-includes/class-wp-user-query.php Outdated Show resolved Hide resolved
@audrasjb
Copy link
Contributor

Hi there, thanks for the PR!
I added a few very small change requests.

* @return string|array|void
*/
public function __get( $name ) {
switch ( $name ) {
Copy link
Author

Choose a reason for hiding this comment

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

I've researched if these properties are being used (including WP plugins), and it looks like they can be safely removed without breaking BC.

Copy link
Contributor

Choose a reason for hiding this comment

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

@anton-vlasenko is there a search query you used to investigate plugins and themes? If no, how did you investigate usage?

Copy link
Author

Choose a reason for hiding this comment

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

That's a good question, @hellofromtonya.
Unfortunately, I'm not writing down every search query that I use (if we are talking about wpdirectory).
I committed these changes sometime last year, so I can't recall.
The best I can do is to search again and post the queries here.

Copy link
Author

Choose a reason for hiding this comment

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

I'm not writing down every search query that I use

I was wrong, @hellofromtonya . I added the search query to a commit message.

So, I've used these search queries to find usages of $proto and $menu properties:
https://wpdirectory.net/search/01GC9XDX4EBTTDF7G8AJNYRCJW
https://wpdirectory.net/search/01GSTP3YTZP7R6PSHAZ51YPQBD

I was not able to find any occurrences of WP_Admin_Bar::$proto.
As for the WP_Admin_Bar::$menu, there are some occurrences, but isset() is always used to check if the property exists. So it should be safe to remove the $menu property.

Copy link
Author

@anton-vlasenko anton-vlasenko Feb 21, 2023

Choose a reason for hiding this comment

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

@hellofromtonya
I've changed my mind and brought WP_Admin_Bar::$menu back.
There is no reliable way to check all the plugins and themes because there are too many search results.
https://wpdirectory.net/search/01GSTW1X69TBN8FH3SY7V8KPY5 returns only a few results, all of which seem good (meaning that WP_Admin_Bar::$menu can be removed).
But if I use slightly broader search criteria (https://wpdirectory.net/search/01GSTW68SM7MNRZD0Z9AVBH6R0) I get about 4800 results. It's unrealistic to check all or at least the most popular plugins/themes.
So, to stay safe, I'd prefer to keep the WP_Admin_Bar::$menu property for now.
At the same time, WP_Admin_Bar::$proto can be removed. It doesn't seem to be used anywhere.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you for the search queries @anton-vlasenko.

Reviewing the first query for menu shows that at least 1 plugin (s2 Member Framework) is using this property.

As discussed in the livestream, the plan is:

  • Remove the __get().
  • Since menu is being used, add it as a public property on the class with a default value of array().
	/**
	 * Deprecated menu property.
	 *
	 * @since 3.1.0
	 * @deprecated 3.3.0 Modify admin bar nodes with WP_Admin_Bar::get_node(), WP_Admin_Bar::add_node(), and WP_Admin_Bar::remove_node().
	 * @var array
	 */
	public $menu = array();
  • What about proto (for protocol) property?

From the livestream, there was consensus to remove it if unused, but with an action to research what the property is and why it was added.

History on the proto property:

  • r16038 added this property.
  • r19501 moved it from the initialize() method to __get().
  • When 3.1 released, the protocol handling switched to use get_admin_url(), admin_url(), etc functions instead of the proto property. But the property remained in the code, unused.

Would removing it be a BC break? If not used, then no. But if some plugin or theme in the wild uses it, then yes it is a BC break.

Let's remove it with this reasoning:

  • It's unused in Core and none found in wporg search
  • It was introduced in the 3.1 cycle but then replaced with URL get functions during that same cycle.
  • It's reasonable then to say that it should have been removed in 3.1, but was missed. It appears to be dead code.

Copy link
Author

Choose a reason for hiding this comment

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

I've just submitted a PR that implements the changes proposed in the @hellofromtonya 's comment above. Thanks!
#4133

@anton-vlasenko anton-vlasenko changed the title Disallow dynamic properties in WP_List_Table, WP_User_Query and WP_Text_Diff_Renderer_Table classes Refactor magic methods in WP_List_Table, WP_User_Query, WP_Text_Diff_Renderer_Table and WP_Admin_Bar classes Feb 17, 2023
It's not possible (or very problematic) to find out whether other plugins use this property in a safe manner (i.e., checking if it exists with (isset) before using it).
@hellofromtonya
Copy link
Contributor

Closing this PR. Instead each of these classes will get a separate PR and commit to encapsulate their contextual history and individual considerations.

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.

4 participants