Opened 10 years ago
Last modified 7 years ago
#30188 assigned enhancement
Introduce utility functions to check constants
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | |
Component: | Posts, Post Types | Keywords: | has-patch dev-feedback needs-testing |
Focuses: | administration | 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)
Change History (10)
#1
@
10 years ago
- Component changed from Posts, Post Types to General
- Keywords has-patch dev-feedback added
#3
@
10 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.
#6
follow-up:
↓ 7
@
9 years 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:
↓ 8
@
9 years 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
@
9 years 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.
I like the idea and the implementation a lot, have thought about that before and a list (array) with constants to check is great.