[wp-trac] [WordPress Trac] #30188: Introduce utility functions to check constants

WordPress Trac noreply at wordpress.org
Wed Oct 29 23:31:15 UTC 2014


#30188: Introduce utility functions to check constants
-------------------------------+-----------------------------
 Reporter:  rzen               |      Owner:
     Type:  enhancement        |     Status:  new
 Priority:  normal             |  Milestone:  Awaiting Review
Component:  Posts, Post Types  |    Version:
 Severity:  normal             |   Keywords:
  Focuses:                     |
-------------------------------+-----------------------------
 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:

 {{{#!php
 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

--
Ticket URL: <https://core.trac.wordpress.org/ticket/30188>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list