[wp-trac] [WordPress Trac] #33161: Create a standard for defining and identifying site environment

WordPress Trac noreply at wordpress.org
Fri May 15 13:18:32 UTC 2020


#33161: Create a standard for defining and identifying site environment
--------------------------------+-----------------------------
 Reporter:  krogsgard           |       Owner:  SergeyBiryukov
     Type:  enhancement         |      Status:  accepted
 Priority:  normal              |   Milestone:  5.5
Component:  Options, Meta APIs  |     Version:  4.2
 Severity:  normal              |  Resolution:
 Keywords:  needs-patch         |     Focuses:
--------------------------------+-----------------------------

Comment (by Clorith):

 I've had this come up a couple times as well of late, and made some
 thoughts on the approach, heck, I've made a quick code-draft with my
 thoughts on how to approach it, I don't want it to be a dashboard setting,
 but I see the value in flexibility.

 System environment variables are worht their weight in gold for those who
 have dedicated stage servers, while constants are fixed values that can be
 used to override this in one-off situations, or where system environment
 variables are not available for whatever reason.

 And of course, filters, you may have a plugin to handle this in various
 cases.

 It's also important to limit the outputs, or at least to an extent, to
 ensure the response is an expected one.

 I'm not 100% sold on filtering the available approved environments or not,
 but I put it in as a thought to not disregard it straight away though.

 {{{#!php
 <?php
 function get_environment() {
         $approved_environments = array(
                 'development',
                 'stage',
                 'production',
         );

         /*
          * Filter specs for allowing other environment types  for very
 unique setups.
          */
         $approved_environments = apply_filters(
 'wp_approved_environments', $approved_environments );

         $current_env = '';

         // Check if a environment variable has been set for max
 flexibility, if `getenv` is available on the system.
         if ( function_exists( 'getenv' ) ) {
                 $has_env = getenv( 'WP_ENVIRONMENT' );
                 if ( false !== $has_env ) {
                         $current_env = $has_env;
                 }
         }

         // Fetch the environment from a constant, this overrides global
 system variables for consistency.
         if ( defined( 'WP_ENVIRONMENT' ) ) {
                 $current_env = WP_ENVIRONMENT;
         }

         /*
          * Filter specs for setting the environment programatically.
          */
         $current_env = apply_filters( 'wp_current_environment',
 $current_env );

         // Make sure the environment is an allowed one, and not
 accidentalyl set to an invalid value to try and avoid breaking stuff
 relying on this.
         if ( ! in_array( $current_env, $approved_environments ) ) {
                 $current_env = 'production';
         }

         return $current_env;
 }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/33161#comment:13>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list