#62809 closed enhancement (fixed)
Optimize conditional checks and require_once logic for wp-load.php inclusion
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | 6.8 | Priority: | normal |
Severity: | normal | Version: | 6.7.1 |
Component: | Upload | Keywords: | has-patch |
Focuses: | administration | Cc: |
Description
Are you using either the latest version of WordPress, or the latest development version?
Yes, tested on the latest version of WordPress.
What steps should be taken to consistently reproduce the problem?
This is not a bug but an enhancement. The current code of async-upload.php can be optimized for better readability and efficiency without changing its behavior.
Does the problem occur even when you deactivate all plugins and use the default theme?
N/A (this is an enhancement and not a functional issue).
In case it's relevant to the ticket, what is the expected output or result? What did you see instead?
Code is more concise, maintains readability, and eliminates redundant logic.
No functional changes to the output or behavior.
Current Code:
async-upload.php
if ( defined( 'ABSPATH' ) ) { require_once ABSPATH . 'wp-load.php'; } else { require_once dirname( __DIR__ ) . '/wp-load.php'; }
Proposed Code:
require_once defined('ABSPATH') ? ABSPATH . 'wp-load.php' : dirname(__DIR__) . '/wp-load.php';
Key Improvements:
Combined require_once logic using a ternary operator to reduce redundancy.
Enhanced readability and made the logic more concise.
Additional Information:
Tested on a standard server environment running PHP 8.1 and the latest WordPress version.
No plugins or themes impact this change since it is core logic.
This enhancement aligns with WordPress coding standards and best practices for improving code maintainability.
Gutenberg Issues:
N/A (this enhancement is not specific to Gutenberg).
Change History (7)
This ticket was mentioned in PR #8132 on WordPress/wordpress-develop by hussain-xpeedstudio.
6 months ago
#3
- Keywords has-patch added
#4
@
6 months ago
- Keywords 2nd-opinion close added
Hi there and welcome to Trac!
I don‘t know about others, but the current code is perfectly readable to me. I see zero beed to change this.
#5
@
6 months ago
I can see the small enhanced consistency, as current code repeats the statement require_once
the and filename string wp-load.php
in both conditional (one line) code blocks, by making it a bit more DRY.
But the proposed code doesn't follow the DRY principle consistently when not also avoiding the common wp-load.php
from being repeated. Being really consistent and DRY, the code should become:
require_once ( defined( 'ABSPATH' ) ? ABSPATH : dirname( __DIR__ ) . '/' ) . 'wp-load.php';
#6
@
6 months ago
- Keywords 2nd-opinion close removed
- Milestone changed from Awaiting Review to 6.8
- Owner set to SergeyBiryukov
- Status changed from assigned to reviewing
Hi there, thanks for the ticket! Introduced in [6659] and [7971].
In a variety of other files, including:
wp-admin/admin.php wp-admin/admin-ajax.php wp-admin/install.php wp-admin/install-helper.php wp-admin/maint/repair.php wp-admin/moderation.php wp-admin/tools.php wp-admin/upgrade.php wp-includes/ms-files.php
we just do this:
/** Load WordPress Bootstrap */ require_once dirname( __DIR__ ) . '/wp-load.php';
Since we don't really support relocating wp-admin
or wp-load.php
, I think we can do the same here, checking for ABSPATH
appears to be redundant.
Note that the next line loads wp-admin/admin.php
, which also loads wp-load.php
in line 34 without checking for ABSPATH
.
A similar fragment exists in wp-admin/admin-post.php
, introduced in [8315].
@SergeyBiryukov commented on PR #8132:
6 months ago
#8
Thanks for the PR! Merged in r59634.
Optimize conditional checks and require_once logic for async-upload.php inclusion