Opened 10 months ago
Closed 3 months ago
#63211 closed defect (bug) (fixed)
Optimize wp_slash() and Prevent Unnecessary Code Execution
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| 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
$valueis an array. - Improves function efficiency while maintaining expected behavior.
Change History (8)
This ticket was mentioned in PR #8630 on WordPress/wordpress-develop by @dilipbheda.
10 months ago
#1
#2
@
10 months ago
- Milestone changed from Awaiting Review to 6.9
- Owner set to audrasjb
- Status changed from new to accepted
#4
@
10 months 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.
#5
@
5 months ago
- Keywords needs-testing added
Honestly, I agree and this looks good. Returning early for arrays makes the function a bit cleaner, and I don’t see any downside with this change, though I'm adding needs-testing just in case.
Maybe we should move the string test condition first.
There will always be more strings as param than array?