Make WordPress Core

Opened 7 weeks ago

Last modified 6 weeks ago

#63211 accepted defect (bug)

Optimize wp_slash() and Prevent Unnecessary Code Execution

Reported by: dilipbheda's profile dilipbheda Owned by: audrasjb's profile audrasjb
Milestone: 6.9 Priority: normal
Severity: normal Version:
Component: General Keywords: has-patch
Focuses: Cc:

Description

Add a return statement in wp_slash() to prevent unnecessary execution.

Optimization Changes:

  • Directly return the processed array using array_map() to avoid redundant execution.
  • Ensured early returns to prevent unnecessary checks for string values.

Performance Impact:

  • Eliminates redundant variable assignments.
  • Prevents unnecessary execution of string checks when $value is an array.
  • Improves function efficiency while maintaining expected behavior.

Change History (4)

This ticket was mentioned in PR #8630 on WordPress/wordpress-develop by @dilipbheda.


7 weeks ago
#1

#2 @audrasjb
7 weeks ago

  • Milestone changed from Awaiting Review to 6.9
  • Owner set to audrasjb
  • Status changed from new to accepted

#3 @staurand
6 weeks ago

Maybe we should move the string test condition first.

<?php
if ( is_string( $value ) ) {

There will always be more strings as param than array?

#4 @dilipbheda
6 weeks ago

@staurand From my point of view, we can't always predict what type of value a user might pass into wp_slash(), so it's important to structure the function defensively.

The best approach is to check for arrays first using array_map() to ensure recursive processing happens before applying addslashes() to string values. This way, nested arrays are properly handled without skipping over any values.

That said, the current implementation looks good to me. Let’s wait for feedback from others as well.

Note: See TracTickets for help on using tickets.