Make WordPress Core

Opened 11 months ago

Last modified 11 months ago

#58357 new defect (bug)

Optimize function wp_replace_in_html_tags() - Blank foreach loop, count in loop

Reported by: mayur8991's profile mayur8991 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 6.2.1
Component: Formatting Keywords: has-patch
Focuses: Cc:

Attachments (1)

58357.diff (1.1 KB) - added by Presskopp 11 months ago.

Download all attachments as: .zip

Change History (7)

#1 @mayur8991
11 months ago

  • Summary changed from Blank foreach loop detected to Blank foreach loop detected - Needs reformatting

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.

#2 @nadimcse
11 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 @Presskopp
11 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.

@Presskopp
11 months ago

#4 @Presskopp
11 months ago

  • Summary changed from Blank foreach loop detected - Needs reformatting to Optimize function wp_replace_in_html_tags() - Blank foreach loop, count in loop

#5 @Presskopp
11 months ago

  • Keywords has-patch added
Note: See TracTickets for help on using tickets.