Make WordPress Core

Opened 21 months ago

Closed 21 months ago

Last modified 21 months ago

#62809 closed enhancement (fixed)

Optimize conditional checks and require_once logic for wp-load.php inclusion

Reported by: hussain896 Owned by: SergeyBiryukov
Priority: normal Milestone: 6.8
Component: Upload Version: 6.7.1
Severity: normal Keywords: has-patch
Cc: Focuses: administration

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)

#2 @hussain896
21 months ago

Optimize conditional checks and require_once logic for async-upload.php inclusion

This ticket was mentioned in ​PR #8132 on ​WordPress/wordpress-develop by ​hussain-xpeedstudio.


21 months ago
#3

  • Keywords has-patch added

#4 @swissspidy
21 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 @knutsp
21 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 @SergeyBiryukov
21 months ago

  • Keywords 2nd-opinion close removed
  • Milestone Awaiting Review → 6.8
  • Owner set to SergeyBiryukov
  • Status assigned → 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].

Last edited 21 months ago by SergeyBiryukov (previous) (diff)

#7 @SergeyBiryukov
21 months ago

  • Resolution → fixed
  • Status reviewing → closed

In 59634:

Upload: Remove redundant check for ABSPATH in wp-admin/async-upload.php.

This aims to bring consistency with a similar fragment in other files, since relocating wp-admin or wp-load.php is not supported at this time.

Follow-up to [6659], [7971], [8315].

Props hussain896, swissspidy, knutsp, SergeyBiryukov.
Fixes #62809.

​@SergeyBiryukov commented on ​PR #8132:


21 months ago
#8

Thanks for the PR! Merged in r59634.

Note: See TracTickets for help on using tickets.