Make WordPress Core

Opened 20 months ago

Closed 5 months ago

#58357 closed defect (bug) (duplicate)

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

Reported by: mayur8991's profile mayur8991 Owned by:
Milestone: 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 20 months ago.

Download all attachments as: .zip

Change History (8)

#1 @mayur8991
20 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
20 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
20 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
20 months ago

#4 @Presskopp
20 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
20 months ago

  • Keywords has-patch added

#7 @SergeyBiryukov
5 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Hi there, thanks for the ticket!

There was some further discussion about this in #61860, let's continue there.

Note: See TracTickets for help on using tickets.