Commit Graph

32 Commits

Author SHA1 Message Date
spacedmonkey 323d8cb19d Plugins: Store result of call to array_keys, to save repeated calls in WP_Hook class.
In the `WP_Hook` class the function `array_keys` was called every time an array of hook priorities was needed. For sites with lots of filters or actions, this would result in thousands of calls to the `array_keys` function, which uses server resources. Instead of recomputing this array every time it is needed, only compute it when filters are added and removed, then store the result as a class property. Improve unit tests to ensure this behaviour is tested. 

Props spacedmonkey, bor0, flixos90, hellofromTonya, mukesh27.
Fixes #58458.
Built from https://develop.svn.wordpress.org/trunk@56609


git-svn-id: http://core.svn.wordpress.org/trunk@56121 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-18 12:41:18 +00:00
Sergey Biryukov b80ce60f70 Coding Standards: Use pre-increment/decrement for stand-alone statements.
Note: This is enforced by WPCS 3.0.0:

1. There should be no space between an increment/decrement operator and the variable it applies to.
2. Pre-increment/decrement should be favoured over post-increment/decrement for stand-alone statements. “Pre” will in/decrement and then return, “post” will return and then in/decrement. Using the “pre” version is slightly more performant and can prevent future bugs when code gets moved around.

References:
* [https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php/#increment-decrement-operators WordPress PHP Coding Standards: Increment/decrement operators]
* [https://github.com/WordPress/WordPress-Coding-Standards/pull/2130 WPCS: PR #2130 Core: add sniffs to check formatting of increment/decrement operators]

Props jrf.
See #59161, #58831.
Built from https://develop.svn.wordpress.org/trunk@56549


git-svn-id: http://core.svn.wordpress.org/trunk@56061 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-09 09:28:26 +00:00
Sergey Biryukov 9a49b70239 Coding Standards: Remove superfluous blank lines at the end of various classes.
Note: This is enforced by WPCS 3.0.0.

Follow-up to [56536].

Props jrf.
See #59161, #58831.
Built from https://develop.svn.wordpress.org/trunk@56547


git-svn-id: http://core.svn.wordpress.org/trunk@56059 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-08 09:32:23 +00:00
Sergey Biryukov d2a504e6cb Coding Standards: Use strict comparison in `wp-includes/class-wp-hook.php`.
Follow-up to [4955], [38571].

Props aristath, poena, afercia, SergeyBiryukov.
See #58831.
Built from https://develop.svn.wordpress.org/trunk@56511


git-svn-id: http://core.svn.wordpress.org/trunk@56023 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-04 11:40:22 +00:00
Sergey Biryukov d35d467c9c Plugins: Remove `is_object()` check in `WP_Hook:build_preinitialized_hooks()`.
This is a minor performance enhancement:

* If an object is passed, the call to `is_object()` will be redundant.
* If a non-object is passed, the `instanceof` operator (a variant of `is_a()`) will first [https://github.com/php/php-src/blob/f42992f/Zend/zend_builtin_functions.c#L630-L631 check if it is an object] before doing any further processing.

Therefore, no additional processing cycles should be wasted in both cases.

Follow-up to [38571].

Props bor0, johnbillion, davidbaumwald.
Fixes #58290.
Built from https://develop.svn.wordpress.org/trunk@55748


git-svn-id: http://core.svn.wordpress.org/trunk@55260 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-05-11 11:43:21 +00:00
Sergey Biryukov c03305852e Code Modernization: Add `AllowDynamicProperties` attribute to all (parent) classes.
Dynamic (non-explicitly declared) properties are deprecated as of PHP 8.2 and are expected to become a fatal error in PHP 9.0.

There are a number of ways to mitigate this:
* If it is an accidental typo for a declared property: fix the typo.
* For known properties: declare them on the class.
* For unknown properties: add the magic `__get()`, `__set()`, et al. methods to the class or let the class extend `stdClass` which has highly optimized versions of these magic methods built in.
* For unknown ''use'' of dynamic properties, the `#[AllowDynamicProperties]` attribute can be added to the class. The attribute will automatically be inherited by child classes.

Trac ticket #56034 is open to investigate and handle the third and fourth type of situations, however it has become clear this will need more time and will not be ready in time for WP 6.1.

To reduce “noise” in the meantime, both in the error logs of WP users moving onto PHP 8.2, in the test run logs of WP itself, in test runs of plugins and themes, as well as to prevent duplicate tickets from being opened for the same issue, this commit adds the `#[AllowDynamicProperties]` attribute to all “parent” classes in WP.

The logic used for this commit is as follows:
* If a class already has the attribute: no action needed.
* If a class does not `extend`: add the attribute.
* If a class does `extend`:
 - If it extends `stdClass`: no action needed (as `stdClass` supports dynamic properties).
 - If it extends a PHP native class: add the attribute.
 - If it extends a class from one of WP's external dependencies: add the attribute.
* In all other cases: no action — the attribute should not be needed as child classes inherit from the parent.

Whether or not a class contains magic methods has not been taken into account, as a review of the currently existing magic methods has shown that those are generally not sturdy enough and often even set dynamic properties (which they should not). See the [https://www.youtube.com/watch?v=vDZWepDQQVE live stream from August 16, 2022] for more details.

This commit only affects classes in the `src` directory of WordPress core.
* Tests should not get this attribute, but should be fixed to not use dynamic properties instead. Patches for this are already being committed under ticket #56033.
* While a number bundled themes (2014, 2019, 2020, 2021) contain classes, they are not a part of this commit and may be updated separately.

Reference: [https://wiki.php.net/rfc/deprecate_dynamic_properties PHP RFC: Deprecate dynamic properties].

Follow-up to [53922].

Props jrf, hellofromTonya, markjaquith, peterwilsoncc, costdev, knutsp, aristath.
See #56513, #56034.
Built from https://develop.svn.wordpress.org/trunk@54133


git-svn-id: http://core.svn.wordpress.org/trunk@53692 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-12 15:47:14 +00:00
John Blackbourn 614b866cb0 Plugins: Correct the documented allowable types for to the `$callback` parameter of various hook related functions.
These functions don't require the callback to be a valid callable, therefore `array` and `string` are also valid types for this parameter.

Props malthert

Fixes #54440

Built from https://develop.svn.wordpress.org/trunk@52300


git-svn-id: http://core.svn.wordpress.org/trunk@51892 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-12-01 13:22:01 +00:00
Sergey Biryukov 85c1413c2b Code Modernization: Silence the deprecation warnings for missing return type in `WP_Hook`.
This fixes the "Deprecated: Return type of `WP_Hook::[METHODNAME]()` should be compatible with `ArrayAccess::[METHODNAME](): type`" warnings on PHP 8.1.

PHP native interfaces now have declared return types and methods in classes implementing these interfaces need to either have the return type declared (in a covariant compatible manner with the PHP native interface method declaration), or need to silence the deprecation warning using the `#[ReturnTypeWillChange]` attribute.

Follow-up to [51517], [51529].

Props jrf.
See #53635.
Built from https://develop.svn.wordpress.org/trunk@51530


git-svn-id: http://core.svn.wordpress.org/trunk@51141 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-08-03 11:02:00 +00:00
Sergey Biryukov adae6bc86d Docs: Further synchronize documentation for some Plugin API functions.
Follow-up to [50807].

See #50531.
Built from https://develop.svn.wordpress.org/trunk@50811


git-svn-id: http://core.svn.wordpress.org/trunk@50420 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-05-04 15:01:58 +00:00
Sergey Biryukov ef28363c7b Plugins: Standardize the terminology used for actions, filters, and callback functions.
Use `$hook_name` when referring to a filter or action hook name, and `$callback` when referring to a callback function.

This brings more consistency to parameter names in Plugin API functions.

Includes minor code layout fixes for better readability and reordering some functions in a more logical order.

Props johnbillion, jrf, SergeyBiryukov.
Fixes #50531.
Built from https://develop.svn.wordpress.org/trunk@50807


git-svn-id: http://core.svn.wordpress.org/trunk@50416 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-05-04 10:48:03 +00:00
Sergey Biryukov 37662df05e Docs: In various `@return` tags, list the expected type first, instead of `false` or `WP_Error`.
Follow-up to [46696], [47060], [49926], [49927].

See #51800.
Built from https://develop.svn.wordpress.org/trunk@49929


git-svn-id: http://core.svn.wordpress.org/trunk@49628 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-01-04 17:18:04 +00:00
Sergey Biryukov f4d610310c Docs: Synchronize and correct documentation for `has_filter()` and `WP_Hook::has_filter()`.
`WP_Hook::has_filter()` returns true if `$function_to_check` is omitted and the hook being checked has one or more registered callbacks.

Follow-up to [49927].

See #51800.
Built from https://develop.svn.wordpress.org/trunk@49928


git-svn-id: http://core.svn.wordpress.org/trunk@49627 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-01-04 16:58:08 +00:00
John Blackbourn dfe1f9b322 Docs: Promote many `bool` types to `true` or `false` where only that value is used.
See #51800

Built from https://develop.svn.wordpress.org/trunk@49927


git-svn-id: http://core.svn.wordpress.org/trunk@49626 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-01-03 22:04:04 +00:00
John Blackbourn 53da9208dd Docs: Various docblock corrections particularly relating to boolean types.
See #51800

Built from https://develop.svn.wordpress.org/trunk@49926


git-svn-id: http://core.svn.wordpress.org/trunk@49625 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-01-03 21:57:09 +00:00
John Blackbourn 3cee52b362 Docs: Add more information about how to use filters that run before WordPress initialises.
Fixes #50134

Built from https://develop.svn.wordpress.org/trunk@49489


git-svn-id: http://core.svn.wordpress.org/trunk@49248 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2020-11-03 17:36:09 +00:00
John Blackbourn 905460bd5e Docs: Standardise the type name for booleans and integers.
This brings these docs inline with the documentation standards.

Props ravipatel, justinahinon

Fixes #51426

Built from https://develop.svn.wordpress.org/trunk@49120


git-svn-id: http://core.svn.wordpress.org/trunk@48882 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2020-10-10 20:02:05 +00:00
Sergey Biryukov 641c632b0c Coding Standards: Use Yoda conditions where appropriate.
See #49222.
Built from https://develop.svn.wordpress.org/trunk@47219


git-svn-id: http://core.svn.wordpress.org/trunk@47019 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2020-02-09 16:55:09 +00:00
Sergey Biryukov 001ffe81fb Docs: Improve inline comments per the documentation standards.
Includes minor code layout fixes for better readability.

See #48303.
Built from https://develop.svn.wordpress.org/trunk@47122


git-svn-id: http://core.svn.wordpress.org/trunk@46922 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2020-01-29 00:45:18 +00:00
Sergey Biryukov 2900bb8ea7 Docs: Update links to https://secure.php.net/, they now redirect to https://www.php.net/.
See #48303.
Built from https://develop.svn.wordpress.org/trunk@47088


git-svn-id: http://core.svn.wordpress.org/trunk@46888 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2020-01-20 03:14:06 +00:00
Sergey Biryukov 49401759f3 Docs: Correct `@param` type for the function parameter in `tests_add_filter()` and `_test_filter_build_unique_id()`.
Synchronize documentation for `add_filter()`, `tests_add_filter()`, `_wp_filter_build_unique_id()`, `_test_filter_build_unique_id()`.

Add a note that `$tag` and `$priority` are no longer used in `_wp_filter_build_unique_id()` since [46220], and the function always returns a string now.

Props donmhico, remcotolsma, SergeyBiryukov.
Fixes #47407. See #48303.
Built from https://develop.svn.wordpress.org/trunk@46801


git-svn-id: http://core.svn.wordpress.org/trunk@46601 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2019-11-29 21:42:01 +00:00
Sergey Biryukov 60f027a378 Code Modernisation: Replace `call_user_func_array()` in combination with an empty array in `wp-includes/class-wp-hook.php` with `call_user_func()`.
Props jrf.
See #47678.
Built from https://develop.svn.wordpress.org/trunk@46139


git-svn-id: http://core.svn.wordpress.org/trunk@45951 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2019-09-15 11:45:56 +00:00
Sergey Biryukov 0393a78206 Docs: Fix typo in `WP_Hook::resort_active_iterations()` description.
Props itowhid06.
Fixes #47999.
Built from https://develop.svn.wordpress.org/trunk@46082


git-svn-id: http://core.svn.wordpress.org/trunk@45894 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2019-09-08 13:16:55 +00:00
Gary Pendergast 4803fc405e Coding Standards: Fix the `Squiz.PHP.DisallowMultipleAssignments` violations in `wp-includes`.
See #47632.


Built from https://develop.svn.wordpress.org/trunk@45590


git-svn-id: http://core.svn.wordpress.org/trunk@45401 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2019-07-02 23:42:58 +00:00
John Blackbourn 9abcf7881f Docs: Improve and correct documentation for hook-related variadic functions.
See #37402

Built from https://develop.svn.wordpress.org/trunk@45420


git-svn-id: http://core.svn.wordpress.org/trunk@45231 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2019-05-25 22:52:54 +00:00
Sergey Biryukov 08227812a0 Docs: Remove `@static` notations from method DocBlocks in `wp-includes/*` classes.
This tag has been used in the past, but should no longer be used. Just using the `static` keyword in code is enough for PhpDocumentor on PHP5+ to recognize static variables and methods, and PhpDocumentor will mark them as static.

Props birgire.
See #42803.
Built from https://develop.svn.wordpress.org/trunk@42746


git-svn-id: http://core.svn.wordpress.org/trunk@42576 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-02-25 20:22:30 +00:00
Gary Pendergast aaf99e6913 Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.


Built from https://develop.svn.wordpress.org/trunk@42343


git-svn-id: http://core.svn.wordpress.org/trunk@42172 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-11-30 23:11:00 +00:00
Sergey Biryukov da3e29eeba Docs: Use correct order of arguments in the DocBlock for `WP_Hook::has_filter()`.
Props munyagu.
Fixes #41941.
Built from https://develop.svn.wordpress.org/trunk@41551


git-svn-id: http://core.svn.wordpress.org/trunk@41384 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-09-21 10:00:48 +00:00
Drew Jaynes 0860bb2771 Docs: Remove `@access` notations from method DocBlocks in wp-includes/* classes.
Prior to about 2013, many class methods lacked even access modifiers which made the `@access` notations that much more useful. Now that we've gotten to a point where the codebase is more mature from a maintenance perspective and we can finally remove these notations. Notable exceptions to this change include standalone functions notated as private as well as some classes still considered to represent "private" APIs.

See #41452.

Built from https://develop.svn.wordpress.org/trunk@41162


git-svn-id: http://core.svn.wordpress.org/trunk@41002 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-07-27 00:41:44 +00:00
Drew Jaynes 07f01a2e10 Docs: Replace a variety of http links referenced in inline docs with their https counterparts (where possible).
Props johnpgreen.
Fixes #40732.

Built from https://develop.svn.wordpress.org/trunk@40948


git-svn-id: http://core.svn.wordpress.org/trunk@40798 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-06-25 22:06:41 +00:00
Gary Pendergast d1b2a55b44 Plugins: Add a `current_priority()` method to `WP_Hook`.
This allows plugins to determine the currently running priority of a filter.

Fixes #39007.


Built from https://develop.svn.wordpress.org/trunk@39430


git-svn-id: http://core.svn.wordpress.org/trunk@39370 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-12-02 07:10:43 +00:00
Drew Jaynes f5db425059 Docs: Fix minor formatting for inline docs in `WP_Hook` following its introduction in [38571].
See #17817.

Built from https://develop.svn.wordpress.org/trunk@38573


git-svn-id: http://core.svn.wordpress.org/trunk@38516 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 04:17:30 +00:00
Gary Pendergast 5a632be944 Hooks: Add the new class `WP_Hook`, and modify hook handling to make use of it.
Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.

Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.

And so, we introduce `WP_Hook`. By changing `$wp_filter` from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see `::apply_filters()`) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.

Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.


Built from https://develop.svn.wordpress.org/trunk@38571


git-svn-id: http://core.svn.wordpress.org/trunk@38514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-09-08 03:55:31 +00:00