Opened 5 months ago
Last modified 4 months ago
#58357 new defect (bug)
Optimize function wp_replace_in_html_tags() - Blank foreach loop, count in loop
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | 6.2.1 |
Component: | Formatting | Keywords: | has-patch |
Focuses: | Cc: |
Description
Blank foreach loop found in formatting.php file
https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/formatting.php#L761
Attachments (1)
Change History (7)
#1
@
5 months ago
- Summary changed from Blank foreach loop detected to Blank foreach loop detected - Needs reformatting
#2
@
5 months ago
I've checked the purpose of the foreach loop and from my perspective, we can use these two built-in functions
array_key_first()
and reset()
instead of the foreach loop.
Example:
<?php $needle = array_key_first($replace_pairs); $replace = reset($replace_pairs);
#3
@
4 months ago
The provided code by @nadimcse won't work in PHP 5.6.20, which is still marked as compatible with WordPress, see https://wordpress.org/about/requirements/
But another optimization can be made, by putting the count( $textarr )
out of the for-loop. Patch attached.
Note: See
TracTickets for help on using
tickets.
I checked from my side. That
$needle
and$replace
gives the key and value respectively, for the last item of array.We can use array_slice() function instead of running that foreach() loop.