Opened 11 years ago
Closed 2 months ago
#30188 closed enhancement (wontfix)
Introduce utility functions to check constants
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | Priority: | normal | |
| Severity: | normal | Version: | |
| Component: | Editor | Keywords: | has-patch |
| 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)
Change History (11)
#1
@
11 years ago
- Component changed from Posts, Post Types to General
- Keywords has-patch dev-feedback added
#3
@
11 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
@
10 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
@
10 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
@
10 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.
#9
@
8 years ago
- Component changed from General to Posts, Post Types
- Focuses administration added
Related: #41953
#10
@
2 months ago
- Component changed from Posts, Post Types to Editor
- Focuses administration removed
- Keywords dev-feedback needs-testing removed
- Milestone Awaiting Review deleted
- Resolution set to wontfix
- Status changed from assigned to closed
As @nacin suggested 10 years ago, this is unnecessary, and you should not bail all of this contants always, specially nowadays with the Editor being the current standard
For example, don't bail early in save_post because DOING_AJAX is true, the block editor can save via AJAX/REST and this might skip real saves.
I'm closing this because it has not aged very well and using these constants are nowadays are pretty standard. Also the save logic in Editor has changed massively, and this was only relevant back in the days of the old Editor which is not bringing any traction regarding this topic anymore (as we can see, has not brought any additional conversation, for more than 8 years)
I like the idea and the implementation a lot, have thought about that before and a list (array) with constants to check is great.