WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 18 months ago

#30188 assigned enhancement

Introduce utility functions to check constants

Reported by: rzen Owned by: chriscct7
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: General Keywords: has-patch dev-feedback needs-testing
Focuses: Cc:

Description

At the moment it's pretty obnoxious to check the various DOING_* constants throughout core and within plugins and elsewhere. The annoyance is compounded whenever we need to verify multiple constants, for example on the save_post hook:

function do_some_post_stuff_the_current_way() {

        // Bail if doing autosave
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
                return;
        }

        // Bail if doing AJAX
        if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
                return;
        }

        // Bail if running cron
        if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
                return;
        }

        // Maybe some other checks...

        // Do my stuff..

}
add_action( 'save_post', 'do_some_post_stuff_the_current_way' );

I initially set out to solve this problem exclusively for saving posts, but became waylaid just in naming such a function (What are we checking exactly? The environment state/context? The mechanism that triggered save_post? etc). I spent the whole day thinking about it and realized the solution reaches beyond just saving post.

Enter wp_check_constants() and is_constant_true().

The former accepts a single or array of constants, the latter only validates one. In these we confirm first that the constant is defined and then that it is explicitly set to true. Full stop.

I've written a few different tests to support that the function works as advertised. If the general consensus here is that these functions are useful I'd also be happy to submit patches that introduce them throughout core in place of the current defined( 'FOO' ) && FOO conditions.

Related: #25669

Attachments (1)

30188.diff (2.5 KB) - added by rzen 3 years ago.

Download all attachments as: .zip

Change History (9)

@rzen
3 years ago

#1 @rzen
3 years ago

  • Component changed from Posts, Post Types to General
  • Keywords has-patch dev-feedback added

#2 @nofearinc
3 years ago

I like the idea and the implementation a lot, have thought about that before and a list (array) with constants to check is great.

#3 @TimothyBlynJacobs
3 years ago

The one ( maybe only ) awesome thing about constants is that they autocomplete. I never, for the life of me can remember what the exact name of one of them is. Is there anyway to keep that auto completeness? Not sure.

#4 @chriscct7
18 months ago

  • Keywords needs-testing added

#5 @chriscct7
18 months ago

  • Owner set to chriscct7
  • Status changed from new to assigned

#6 follow-up: @nacin
18 months ago

This feels like an unnecessary over-abstraction. I'd rather improve the ability to save meta boxes, not suppress basic defined logic.

#7 in reply to: ↑ 6 ; follow-up: @chriscct7
18 months ago

Replying to nacin:

This feels like an unnecessary over-abstraction. I'd rather improve the ability to save meta boxes, not suppress basic defined logic.

Metaboxes?

#8 in reply to: ↑ 7 @nacin
18 months ago

Replying to chriscct7:

Replying to nacin:

This feels like an unnecessary over-abstraction. I'd rather improve the ability to save meta boxes, not suppress basic defined logic.

Metaboxes?

This structure — checking a bunch of (DOING_) constants in a row — is commonly seen in a save_post hook to handle the saving of a meta box.

Note: See TracTickets for help on using tickets.