Make WordPress Core

Changeset 34828


Ignore:
Timestamp:
10/05/2015 03:05:26 PM (11 years ago)
Author:
boonebgorges
Message:

Use wp_installing() instead of WP_INSTALLING constant.

The WP_INSTALLING constant is a flag that WordPress sets in a number of
places, telling the system that options should be fetched directly from the
database instead of from the cache, that WP should not ping wordpress.org for
updates, that the normal "not installed" checks should be bypassed, and so on.

A constant is generally necessary for this purpose, because the flag is
typically set before the WP bootstrap, meaning that WP functions are not yet
available. However, it is possible - notably, during wpmu_create_blog() -
for the "installing" flag to be set after WP has already loaded. In these
cases, WP_INSTALLING would be set for the remainder of the process, since
there's no way to change a constant once it's defined. This, in turn, polluted
later function calls that ought to have been outside the scope of site
creation, particularly the non-caching of option data. The problem was
particularly evident in the case of the automated tests, where WP_INSTALLING
was set the first time a site was created, and remained set for the rest of the
suite.

The new wp_installing() function allows developers to fetch the current
installation status (when called without any arguments) or to set the
installation status (when called with a boolean true or false). Use of
the WP_INSTALLING constant is still supported; wp_installing() will default
to true if the constant is defined during the bootstrap.

Props boonebgorges, jeremyfelt.
See #31130.

Location:
trunk
Files:
25 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/admin.php

    r34265 r34828  
    8585
    8686// Schedule trash collection
    87 if ( !wp_next_scheduled('wp_scheduled_delete') && !defined('WP_INSTALLING') )
     87if ( ! wp_next_scheduled( 'wp_scheduled_delete' ) && ! wp_installing() )
    8888        wp_schedule_event(time(), 'daily', 'wp_scheduled_delete');
    8989
  • trunk/src/wp-admin/includes/class-wp-upgrader.php

    r34778 r34828  
    26052605                        return true;
    26062606
    2607                 if ( defined( 'WP_INSTALLING' ) )
     2607                if ( wp_installing() )
    26082608                        return true;
    26092609
  • trunk/src/wp-admin/includes/file.php

    r34658 r34828  
    10821082
    10831083                unset($stored_credentials['password'], $stored_credentials['port'], $stored_credentials['private_key'], $stored_credentials['public_key']);
    1084                 if ( ! defined( 'WP_INSTALLING' ) ) {
     1084                if ( ! wp_installing() ) {
    10851085                        update_option( 'ftp_credentials', $stored_credentials );
    10861086                }
  • trunk/src/wp-admin/includes/misc.php

    r34824 r34828  
    262262 */
    263263function update_home_siteurl( $old_value, $value ) {
    264         if ( defined( "WP_INSTALLING" ) )
     264        if ( wp_installing() )
    265265                return;
    266266
  • trunk/src/wp-admin/includes/translation-install.php

    r32643 r34828  
    9595 */
    9696function wp_get_available_translations() {
    97         if ( ! defined( 'WP_INSTALLING' ) && false !== ( $translations = get_site_transient( 'available_translations' ) ) ) {
     97        if ( ! wp_installing() && false !== ( $translations = get_site_transient( 'available_translations' ) ) ) {
    9898                return $translations;
    9999        }
  • trunk/src/wp-includes/functions.php

    r34793 r34828  
    12611261
    12621262/**
     1263 * Check or set whether WordPress is in "installation" mode.
     1264 *
     1265 * If the `WP_INSTALLING` constant is defined during the bootstrap, `wp_installing()` will default to `true`.
     1266 *
     1267 * @since 4.4.0
     1268 *
     1269 * @staticvar bool $installing
     1270 *
     1271 * @param bool $is_installing Optional. True to set WP into Installing mode, false to turn Installing mode off.
     1272 *                            Omit this parameter if you only want to fetch the current status.
     1273 * @return bool True if WP is installing, otherwise false. When a `$is_installing` is passed, the function will
     1274 *              report whether WP was in installing mode prior to the change to `$is_installing`.
     1275 */
     1276function wp_installing( $is_installing = null ) {
     1277        static $installing = null;
     1278
     1279        // Support for the `WP_INSTALLING` constant, defined before WP is loaded.
     1280        if ( is_null( $installing ) ) {
     1281                $installing = defined( 'WP_INSTALLING' ) && WP_INSTALLING;
     1282        }
     1283
     1284        if ( ! is_null( $is_installing ) ) {
     1285                $old_installing = $installing;
     1286                $installing = $is_installing;
     1287                return (bool) $old_installing;
     1288        }
     1289
     1290        return (bool) $installing;
     1291}
     1292
     1293/**
    12631294 * Test whether blog is already installed.
    12641295 *
    … …  
    12861317
    12871318        $suppress = $wpdb->suppress_errors();
    1288         if ( ! defined( 'WP_INSTALLING' ) ) {
     1319        if ( ! wp_installing() ) {
    12891320                $alloptions = wp_load_alloptions();
    12901321        }
    … …  
    33383369
    33393370        // If installing or in the admin, provide the verbose message.
    3340         if ( defined('WP_INSTALLING') || defined('WP_ADMIN') )
     3371        if ( wp_installing() || defined( 'WP_ADMIN' ) )
    33413372                wp_die($wpdb->error);
    33423373
  • trunk/src/wp-includes/l10n.php

    r34781 r34828  
    5454        if ( is_multisite() ) {
    5555                // Don't check blog option when installing.
    56                 if ( defined( 'WP_INSTALLING' ) || ( false === $ms_locale = get_option( 'WPLANG' ) ) ) {
     56                if ( wp_installing() || ( false === $ms_locale = get_option( 'WPLANG' ) ) ) {
    5757                        $ms_locale = get_network_option( 'WPLANG' );
    5858                }
    … …  
    636636        }
    637637
    638         if ( is_admin() || defined( 'WP_INSTALLING' ) || ( defined( 'WP_REPAIRING' ) && WP_REPAIRING ) ) {
     638        if ( is_admin() || wp_installing() || ( defined( 'WP_REPAIRING' ) && WP_REPAIRING ) ) {
    639639                load_textdomain( 'default', WP_LANG_DIR . "/admin-$locale.mo" );
    640640        }
  • trunk/src/wp-includes/load.php

    r34462 r34828  
    166166 */
    167167function wp_maintenance() {
    168         if ( !file_exists( ABSPATH . '.maintenance' ) || defined( 'WP_INSTALLING' ) )
     168        if ( ! file_exists( ABSPATH . '.maintenance' ) || wp_installing() )
    169169                return;
    170170
    … …  
    476476function wp_not_installed() {
    477477        if ( is_multisite() ) {
    478                 if ( ! is_blog_installed() && ! defined( 'WP_INSTALLING' ) ) {
     478                if ( ! is_blog_installed() && ! wp_installing() ) {
    479479                        nocache_headers();
    480480
    481481                        wp_die( __( 'The site you have requested is not installed properly. Please contact the system administrator.' ) );
    482482                }
    483         } elseif ( ! is_blog_installed() && ! defined( 'WP_INSTALLING' ) ) {
     483        } elseif ( ! is_blog_installed() && ! wp_installing() ) {
    484484                nocache_headers();
    485485
    … …  
    540540        $plugins = array();
    541541        $active_plugins = (array) get_option( 'active_plugins', array() );
    542         if ( empty( $active_plugins ) || defined( 'WP_INSTALLING' ) )
     542        if ( empty( $active_plugins ) || wp_installing() )
    543543                return $plugins;
    544544
  • trunk/src/wp-includes/ms-functions.php

    r34778 r34828  
    11171117                return new WP_Error( 'blog_taken', __( 'Sorry, that site already exists!' ) );
    11181118
    1119         if ( !defined('WP_INSTALLING') )
    1120                 define( 'WP_INSTALLING', true );
     1119        if ( ! wp_installing() ) {
     1120                wp_installing( true );
     1121        }
    11211122
    11221123        if ( ! $blog_id = insert_blog($domain, $path, $site_id) )
    … …  
    21732174                return;
    21742175
    2175         if ( !wp_next_scheduled('update_network_counts') && !defined('WP_INSTALLING') )
     2176        if ( ! wp_next_scheduled('update_network_counts') && ! wp_installing() )
    21762177                wp_schedule_event(time(), 'twicedaily', 'update_network_counts');
    21772178}
  • trunk/src/wp-includes/ms-settings.php

    r34819 r34828  
    137137
    138138        // @todo Investigate when exactly this can occur.
    139         if ( empty( $current_blog ) && defined( 'WP_INSTALLING' ) ) {
     139        if ( empty( $current_blog ) && wp_installing() ) {
    140140                $current_blog = new stdClass;
    141141                $current_blog->blog_id = $blog_id = 1;
  • trunk/src/wp-includes/option.php

    r34779 r34828  
    5454                return false;
    5555
    56         if ( ! defined( 'WP_INSTALLING' ) ) {
     56        if ( ! wp_installing() ) {
    5757                // prevent non-existent options from triggering multiple queries
    5858                $notoptions = wp_cache_get( 'notoptions', 'options' );
    … …  
    172172        global $wpdb;
    173173
    174         if ( !defined( 'WP_INSTALLING' ) || !is_multisite() )
     174        if ( ! wp_installing() || ! is_multisite() )
    175175                $alloptions = wp_cache_get( 'alloptions', 'options' );
    176176        else
    … …  
    186186                        $alloptions[$o->option_name] = $o->option_value;
    187187                }
    188                 if ( !defined( 'WP_INSTALLING' ) || !is_multisite() )
     188                if ( ! wp_installing() || ! is_multisite() )
    189189                        wp_cache_add( 'alloptions', $alloptions, 'options' );
    190190        }
    … …  
    205205        global $wpdb;
    206206
    207         if ( !is_multisite() || wp_using_ext_object_cache() || defined( 'WP_INSTALLING' ) )
     207        if ( ! is_multisite() || wp_using_ext_object_cache() || wp_installing() )
    208208                return;
    209209
    … …  
    333333        }
    334334
    335         if ( ! defined( 'WP_INSTALLING' ) ) {
     335        if ( ! wp_installing() ) {
    336336                $alloptions = wp_load_alloptions();
    337337                if ( isset( $alloptions[$option] ) ) {
    … …  
    434434                return false;
    435435
    436         if ( ! defined( 'WP_INSTALLING' ) ) {
     436        if ( ! wp_installing() ) {
    437437                if ( 'yes' == $autoload ) {
    438438                        $alloptions = wp_load_alloptions();
    … …  
    510510
    511511        $result = $wpdb->delete( $wpdb->options, array( 'option_name' => $option ) );
    512         if ( ! defined( 'WP_INSTALLING' ) ) {
     512        if ( ! wp_installing() ) {
    513513                if ( 'yes' == $row->autoload ) {
    514514                        $alloptions = wp_load_alloptions();
    … …  
    630630        } else {
    631631                $transient_option = '_transient_' . $transient;
    632                 if ( ! defined( 'WP_INSTALLING' ) ) {
     632                if ( ! wp_installing() ) {
    633633                        // If option is not in alloptions, it is not autoloaded and thus has a timeout
    634634                        $alloptions = wp_load_alloptions();
  • trunk/src/wp-includes/script-loader.php

    r34778 r34828  
    830830        global $_wp_admin_css_colors;
    831831
    832         if ( defined('WP_INSTALLING') )
     832        if ( wp_installing() )
    833833                return preg_replace( '#^wp-admin/#', './', $src );
    834834
  • trunk/src/wp-includes/theme.php

    r34554 r34828  
    772772         * @param bool true Validation flag to check the current theme.
    773773         */
    774         if ( defined('WP_INSTALLING') || ! apply_filters( 'validate_current_theme', true ) )
     774        if ( wp_installing() || ! apply_filters( 'validate_current_theme', true ) )
    775775                return true;
    776776
  • trunk/src/wp-includes/update.php

    r32635 r34828  
    2323 */
    2424function wp_version_check( $extra_stats = array(), $force_check = false ) {
    25         if ( defined( 'WP_INSTALLING' ) ) {
     25        if ( wp_installing() ) {
    2626                return;
    2727        }
    … …  
    188188 */
    189189function wp_update_plugins( $extra_stats = array() ) {
    190         if ( defined( 'WP_INSTALLING' ) ) {
     190        if ( wp_installing() ) {
    191191                return;
    192192        }
    … …  
    345345 */
    346346function wp_update_themes( $extra_stats = array() ) {
    347         if ( defined( 'WP_INSTALLING' ) ) {
     347        if ( wp_installing() ) {
    348348                return;
    349349        }
    … …  
    637637 */
    638638function wp_schedule_update_checks() {
    639         if ( !wp_next_scheduled('wp_version_check') && !defined('WP_INSTALLING') )
     639        if ( ! wp_next_scheduled( 'wp_version_check' ) && ! wp_installing() )
    640640                wp_schedule_event(time(), 'twicedaily', 'wp_version_check');
    641641
    642         if ( !wp_next_scheduled('wp_update_plugins') && !defined('WP_INSTALLING') )
     642        if ( ! wp_next_scheduled( 'wp_update_plugins' ) && ! wp_installing() )
    643643                wp_schedule_event(time(), 'twicedaily', 'wp_update_plugins');
    644644
    645         if ( !wp_next_scheduled('wp_update_themes') && !defined('WP_INSTALLING') )
     645        if ( ! wp_next_scheduled( 'wp_update_themes' ) && ! wp_installing() )
    646646                wp_schedule_event(time(), 'twicedaily', 'wp_update_themes');
    647647
    648         if ( ! wp_next_scheduled( 'wp_maybe_auto_update' ) && ! defined( 'WP_INSTALLING' ) ) {
     648        if ( ! wp_next_scheduled( 'wp_maybe_auto_update' ) && ! wp_installing() ) {
    649649                // Schedule auto updates for 7 a.m. and 7 p.m. in the timezone of the site.
    650650                $next = strtotime( 'today 7am' );
  • trunk/src/wp-settings.php

    r33748 r34828  
    322322
    323323// Load the functions for the active theme, for both parent and child theme if applicable.
    324 if ( ! defined( 'WP_INSTALLING' ) || 'wp-activate.php' === $pagenow ) {
     324if ( ! wp_installing() || 'wp-activate.php' === $pagenow ) {
    325325        if ( TEMPLATEPATH !== STYLESHEETPATH && file_exists( STYLESHEETPATH . '/functions.php' ) )
    326326                include( STYLESHEETPATH . '/functions.php' );
  • trunk/tests/phpunit/includes/factory.php

    r32659 r34828  
    174174                $blog = wpmu_create_blog( $args['domain'], $args['path'], $args['title'], $user_id, $meta, $args['site_id'] );
    175175                $wpdb->suppress_errors( $suppress );
     176
     177                // Tell WP we're done installing.
     178                wp_installing( false );
     179
    176180                return $blog;
    177181        }
  • trunk/tests/phpunit/includes/testcase.php

    r34810 r34828  
    1414        protected static $hooks_saved = array();
    1515        protected static $ignore_files;
    16 
    17         protected $db_version;
    1816
    1917        /**
    … …  
    5957
    6058                add_filter( 'wp_mail', array( $this, 'set_wp_mail_globals' ) );
    61 
    62                 /*
    63                  * During multisite tests, WP_INSTALLING forces `get_option()` to miss the cache, which causes problems
    64                  * with our query-counting cache tests. As a workaround in the case of tests that require checking
    65                  * 'db_version' (such as any test that uses the Term Meta API), we filter 'pre_option_db_version' and
    66                  * avoid hitting the database.
    67                  *
    68                  * See #31130.
    69                  */
    70                 $this->db_version = get_option( 'db_version' );
    71                 if ( is_multisite() ) {
    72                         add_filter( 'pre_option_db_version', array( $this, 'db_version' ) );
    73                 }
    7459        }
    7560
    … …  
    636621
    637622        /**
    638          * Return the current database version without hitting the database.
    639          *
    640          * This is used to bypass cache problems with some multisite tests. See #31130.
    641          *
    642          * @todo Don't do this anymore once #31130 is fixed.
    643          *
    644          * @since 4.4.0
    645          */
    646         public function db_version() {
    647                 return $this->db_version;
    648         }
    649 
    650         /**
    651623         * Utility method that resets permalinks and flushes rewrites.
    652624         *
  • trunk/tests/phpunit/tests/comment/getPageOfComment.php

    r34806 r34828  
    6161         */
    6262        public function test_subsequent_calls_should_hit_cache() {
    63                 // `get_page_of_comment()` calls `get_option()`, which is not properly cached when WP_INSTALLING.
    64                 if ( is_multisite() ) {
    65                         $this->markTestSkipped( 'Not testable in MS: wpmu_create_blog() defines WP_INSTALLING, which causes cache misses.' );
    66                 }
    67 
    6863                global $wpdb;
    6964
  • trunk/tests/phpunit/tests/general/archives.php

    r31764 r34828  
    1616        function test_get_archives_cache() {
    1717                global $wpdb;
    18 
    19                 if ( is_multisite() ) {
    20                         $this->markTestSkipped( 'Not testable in MS: wpmu_create_blog() defines WP_INSTALLING, which causes cache misses.' );
    21                 }
    2218
    2319                $this->factory->post->create_many( 15, array( 'post_type' => 'post' ) );
  • trunk/tests/phpunit/tests/option/transient.php

    r34767 r34828  
    4747         */
    4848        function test_transient_data_with_timeout() {
    49                 if ( is_multisite() ) {
    50                         $this->markTestSkipped( 'Not testable in MS: wpmu_create_blog() defines WP_INSTALLING.' );
    51                 }
    52 
    5349                $key = rand_str();
    5450                $value = rand_str();
    … …  
    7268         */
    7369        function test_transient_add_timeout() {
    74                 if ( is_multisite() ) {
    75                         $this->markTestSkipped( 'Not testable in MS: wpmu_create_blog() defines WP_INSTALLING.' );
    76                 }
    77 
    7870                $key = rand_str();
    7971                $value = rand_str();
    … …  
    127119         */
    128120        function test_nonexistent_key_old_timeout() {
    129                 if ( is_multisite() ) {
    130                         $this->markTestSkipped( 'Not testable in MS: wpmu_create_blog() defines WP_INSTALLING.' );
    131                 }
    132 
    133121                // Create a transient
    134122                $key = 'test_transient';
  • trunk/tests/phpunit/tests/option/updateOption.php

    r34766 r34828  
    2121         */
    2222        public function test_should_set_autoload_yes_for_nonexistent_option_when_autoload_param_is_missing() {
    23                 if ( is_multisite() ) {
    24                         $this->markTestSkipped( 'Not testable in MS: wpmu_create_blog() defines WP_INSTALLING, which causes cache misses.' );
    25                 }
    26 
    2723                global $wpdb;
    2824                $this->flush_cache();
    … …  
    4541         */
    4642        public function test_should_set_autoload_yes_for_nonexistent_option_when_autoload_param_is_yes() {
    47                 if ( is_multisite() ) {
    48                         $this->markTestSkipped( 'Not testable in MS: wpmu_create_blog() defines WP_INSTALLING, which causes cache misses.' );
    49                 }
    50 
    5143                global $wpdb;
    5244                $this->flush_cache();
    … …  
    6961         */
    7062        public function test_should_set_autoload_no_for_nonexistent_option_when_autoload_param_is_no() {
    71                 if ( is_multisite() ) {
    72                         $this->markTestSkipped( 'Not testable in MS: wpmu_create_blog() defines WP_INSTALLING, which causes cache misses.' );
    73                 }
    74 
    7563                global $wpdb;
    7664                $this->flush_cache();
    … …  
    9482         */
    9583        public function test_should_set_autoload_no_for_nonexistent_option_when_autoload_param_is_false() {
    96                 if ( is_multisite() ) {
    97                         $this->markTestSkipped( 'Not testable in MS: wpmu_create_blog() defines WP_INSTALLING, which causes cache misses.' );
    98                 }
    99 
    10084                global $wpdb;
    10185                $this->flush_cache();
    … …  
    119103         */
    120104        public function test_autoload_should_be_updated_for_existing_option_when_value_is_changed() {
    121                 if ( is_multisite() ) {
    122                         $this->markTestSkipped( 'Not testable in MS: wpmu_create_blog() defines WP_INSTALLING, which causes cache misses.' );
    123                 }
    124 
    125105                global $wpdb;
    126106                add_option( 'foo', 'bar', '', 'no' );
    … …  
    144124         */
    145125        public function test_autoload_should_not_be_updated_for_existing_option_when_value_is_unchanged() {
    146                 if ( is_multisite() ) {
    147                         $this->markTestSkipped( 'Not testable in MS: wpmu_create_blog() defines WP_INSTALLING, which causes cache misses.' );
    148                 }
    149 
    150126                global $wpdb;
    151127                add_option( 'foo', 'bar', '', 'yes' );
    … …  
    170146         */
    171147        public function test_autoload_should_not_be_updated_for_existing_option_when_value_is_changed_but_no_value_of_autoload_is_provided() {
    172                 if ( is_multisite() ) {
    173                         $this->markTestSkipped( 'Not testable in MS: wpmu_create_blog() defines WP_INSTALLING, which causes cache misses.' );
    174                 }
    175 
    176148                global $wpdb;
    177149                add_option( 'foo', 'bar', '', 'yes' );
  • trunk/tests/phpunit/tests/post/getPostClass.php

    r32997 r34828  
    109109                global $wpdb;
    110110
    111                 if ( is_multisite() ) {
    112                         $this->markTestSkipped( 'Not testable in MS: wpmu_create_blog() defines WP_INSTALLING, which causes cache misses.' );
    113                 }
    114 
    115111                register_taxonomy( 'wptests_tax', 'post' );
    116112                wp_set_post_terms( $this->post_id, array( 'foo', 'bar' ), 'wptests_tax' );
  • trunk/tests/phpunit/tests/term/getTerms.php

    r34529 r34828  
    382382        public function test_child_of_should_skip_query_when_specified_parent_is_not_found_in_hierarchy_cache() {
    383383                global $wpdb;
    384 
    385                 if ( is_multisite() ) {
    386                         $this->markTestSkipped( 'Not testable in MS: wpmu_create_blog() defines WP_INSTALLING, which causes cache misses.' );
    387                 }
    388384
    389385                register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true, ) );
    … …  
    13281324                global $wpdb;
    13291325
    1330                 if ( is_multisite() ) {
    1331                         $this->markTestSkipped( 'Not testable in MS: wpmu_create_blog() defines WP_INSTALLING, which causes cache misses.' );
    1332                 }
    1333 
    13341326                register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true, ) );
    13351327
  • trunk/tests/phpunit/tests/term/wpInsertTerm.php

    r34809 r34828  
    469469         */
    470470        public function test_wp_insert_term_duplicate_slug_different_taxonomy_before_410_schema_change() {
    471 
    472                 // See #31130.
    473471                $old_db_version = 30055;
    474                 if ( is_multisite() ) {
    475                         $_db_version = $this->db_version;
    476                         $this->db_version = $old_db_version;
    477                 } else {
    478                         update_option( 'db_version', $old_db_version );
    479                 }
     472                update_option( 'db_version', $old_db_version );
    480473
    481474                register_taxonomy( 'wptests_tax', 'post' );
    … …  
    503496                $this->assertSame( 'foo-2', $new_term->slug );
    504497                $this->assertNotEquals( $new_term->term_id, $term->term_id );
    505 
    506                 if ( is_multisite() ) {
    507                         $this->db_version = $_db_version;
    508                 }
    509498
    510499                _unregister_taxonomy( 'wptests_tax', 'post' );
  • trunk/tests/phpunit/tests/user/capabilities.php

    r34450 r34828  
    874874
    875875        function test_current_user_can_for_blog() {
     876                global $wpdb;
     877
    876878                $user = new WP_User( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
    877879                $old_uid = get_current_user_id();
    … …  
    885887                }
    886888
     889                $suppress = $wpdb->suppress_errors();
    887890                $this->assertFalse( current_user_can_for_blog( 12345, 'edit_posts' ) );
     891                $wpdb->suppress_errors( $suppress );
    888892
    889893                $blog_id = $this->factory->blog->create( array( 'user_id' => $user->ID ) );
Note: See TracChangeset for help on using the changeset viewer.