~canonical-sysadmins/wordpress/4.8.1

« back to all changes in this revision

Viewing changes to wp-admin/includes/upgrade.php

  • Committer: Barry Price
  • Date: 2016-08-17 04:49:28 UTC
  • mto: This revision was merged to the branch mainline in revision 22.
  • Revision ID: barry.price@canonical.com-20160817044928-viijiwb4tl8jwzmp
new upstream release 4.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
 */
39
39
function wp_install( $blog_title, $user_name, $user_email, $public, $deprecated = '', $user_password = '', $language = '' ) {
40
40
        if ( !empty( $deprecated ) )
41
 
                _deprecated_argument( __FUNCTION__, '2.6' );
 
41
                _deprecated_argument( __FUNCTION__, '2.6.0' );
42
42
 
43
43
        wp_check_mysql_version();
44
44
        wp_cache_flush();
191
191
        $wpdb->insert( $wpdb->term_relationships, array('term_taxonomy_id' => $cat_tt_id, 'object_id' => 1) );
192
192
 
193
193
        // Default comment
194
 
        $first_comment_author = __('Mr WordPress');
 
194
        $first_comment_author = __( 'A WordPress Commenter' );
 
195
        $first_comment_email = 'wapuu@wordpress.example';
195
196
        $first_comment_url = 'https://wordpress.org/';
196
 
        $first_comment = __('Hi, this is a comment.
197
 
To delete a comment, just log in and view the post's comments. There you will have the option to edit or delete them.');
 
197
        $first_comment = __( 'Hi, this is a comment.
 
198
To get started with moderating, editing, and deleting comments, please visit the Comments screen in the dashboard.
 
199
Commenter avatars come from <a href="https://gravatar.com">Gravatar</a>.' );
198
200
        if ( is_multisite() ) {
199
201
                $first_comment_author = get_site_option( 'first_comment_author', $first_comment_author );
 
202
                $first_comment_email = get_site_option( 'first_comment_email', $first_comment_email );
200
203
                $first_comment_url = get_site_option( 'first_comment_url', network_home_url() );
201
204
                $first_comment = get_site_option( 'first_comment', $first_comment );
202
205
        }
203
206
        $wpdb->insert( $wpdb->comments, array(
204
207
                'comment_post_ID' => 1,
205
208
                'comment_author' => $first_comment_author,
206
 
                'comment_author_email' => '',
 
209
                'comment_author_email' => $first_comment_email,
207
210
                'comment_author_url' => $first_comment_url,
208
211
                'comment_date' => $now,
209
212
                'comment_date_gmt' => $now_gmt,
317
320
                 */
318
321
                $wp_rewrite->flush_rules( true );
319
322
 
320
 
                // Test against a real WordPress Post, or if none were created, a random 404 page.
321
 
                $test_url = get_permalink( 1 );
 
323
                $test_url = '';
322
324
 
323
 
                if ( ! $test_url ) {
324
 
                        $test_url = home_url( '/wordpress-check-for-rewrites/' );
 
325
                // Test against a real WordPress Post
 
326
                $first_post = get_page_by_path( sanitize_title( _x( 'hello-world', 'Default post slug' ) ), OBJECT, 'post' );
 
327
                if ( $first_post ) {
 
328
                        $test_url = get_permalink( $first_post->ID );
325
329
                }
326
330
 
327
331
                /*
552
556
        if ( $wp_current_db_version < 36686 )
553
557
                upgrade_450();
554
558
 
 
559
        if ( $wp_current_db_version < 37965 )
 
560
                upgrade_460();
 
561
 
555
562
        maybe_disable_link_manager();
556
563
 
557
564
        maybe_disable_automattic_widgets();
1688
1695
}
1689
1696
 
1690
1697
/**
 
1698
 * Executes changes made in WordPress 4.6.0.
 
1699
 *
 
1700
 * @ignore
 
1701
 * @since 4.6.0
 
1702
 *
 
1703
 * @global int $wp_current_db_version Current database version.
 
1704
 */
 
1705
function upgrade_460() {
 
1706
        global $wp_current_db_version;
 
1707
 
 
1708
        // Remove unused post meta.
 
1709
        if ( $wp_current_db_version < 37854 ) {
 
1710
                delete_post_meta_by_key( '_post_restored_from' );
 
1711
        }
 
1712
 
 
1713
        // Remove plugins with callback as an array object/method as the uninstall hook, see #13786.
 
1714
        if ( $wp_current_db_version < 37965 ) {
 
1715
                $uninstall_plugins = get_option( 'uninstall_plugins', array() );
 
1716
 
 
1717
                if ( ! empty( $uninstall_plugins ) ) {
 
1718
                        foreach ( $uninstall_plugins as $basename => $callback ) {
 
1719
                                if ( is_array( $callback ) && is_object( $callback[0] ) ) {
 
1720
                                        unset( $uninstall_plugins[ $basename ] );
 
1721
                                }
 
1722
                        }
 
1723
 
 
1724
                        update_option( 'uninstall_plugins', $uninstall_plugins );
 
1725
                }
 
1726
        }
 
1727
}
 
1728
 
 
1729
/**
1691
1730
 * Executes network-level upgrade routines.
1692
1731
 *
1693
1732
 * @since 3.0.0
2104
2143
        }
2105
2144
 
2106
2145
        /**
2107
 
         * Filter the dbDelta SQL queries.
 
2146
         * Filters the dbDelta SQL queries.
2108
2147
         *
2109
2148
         * @since 3.3.0
2110
2149
         *
2133
2172
        }
2134
2173
 
2135
2174
        /**
2136
 
         * Filter the dbDelta SQL queries for creating tables and/or databases.
 
2175
         * Filters the dbDelta SQL queries for creating tables and/or databases.
2137
2176
         *
2138
2177
         * Queries filterable via this hook contain "CREATE TABLE" or "CREATE DATABASE".
2139
2178
         *
2144
2183
        $cqueries = apply_filters( 'dbdelta_create_queries', $cqueries );
2145
2184
 
2146
2185
        /**
2147
 
         * Filter the dbDelta SQL queries for inserting or updating.
 
2186
         * Filters the dbDelta SQL queries for inserting or updating.
2148
2187
         *
2149
2188
         * Queries filterable via this hook contain "INSERT INTO" or "UPDATE".
2150
2189
         *
2183
2222
                // Separate field lines into an array.
2184
2223
                $flds = explode("\n", $qryline);
2185
2224
 
2186
 
                // todo: Remove this?
2187
 
                //echo "<hr/><pre>\n".print_r(strtolower($table), true).":\n".print_r($cqueries, true)."</pre><hr/>";
2188
 
 
2189
2225
                // For every field line specified in the query.
2190
 
                foreach ($flds as $fld) {
 
2226
                foreach ( $flds as $fld ) {
 
2227
                        $fld = trim( $fld, " \t\n\r\0\x0B," ); // Default trim characters, plus ','.
2191
2228
 
2192
2229
                        // Extract the field name.
2193
 
                        preg_match("|^([^ ]*)|", trim($fld), $fvals);
 
2230
                        preg_match( '|^([^ ]*)|', $fld, $fvals );
2194
2231
                        $fieldname = trim( $fvals[1], '`' );
 
2232
                        $fieldname_lowercased = strtolower( $fieldname );
2195
2233
 
2196
2234
                        // Verify the found field name.
2197
2235
                        $validfield = true;
2198
 
                        switch (strtolower($fieldname)) {
2199
 
                        case '':
2200
 
                        case 'primary':
2201
 
                        case 'index':
2202
 
                        case 'fulltext':
2203
 
                        case 'unique':
2204
 
                        case 'key':
2205
 
                                $validfield = false;
2206
 
                                $indices[] = trim(trim($fld), ", \n");
2207
 
                                break;
 
2236
                        switch ( $fieldname_lowercased ) {
 
2237
                                case '':
 
2238
                                case 'primary':
 
2239
                                case 'index':
 
2240
                                case 'fulltext':
 
2241
                                case 'unique':
 
2242
                                case 'key':
 
2243
                                case 'spatial':
 
2244
                                        $validfield = false;
 
2245
 
 
2246
                                        /*
 
2247
                                         * Normalize the index definition.
 
2248
                                         *
 
2249
                                         * This is done so the definition can be compared against the result of a
 
2250
                                         * `SHOW INDEX FROM $table_name` query which returns the current table
 
2251
                                         * index information.
 
2252
                                         */
 
2253
 
 
2254
                                        // Extract type, name and columns from the definition.
 
2255
                                        preg_match(
 
2256
                                                  '/^'
 
2257
                                                .   '(?P<index_type>'             // 1) Type of the index.
 
2258
                                                .       'PRIMARY\s+KEY|(?:UNIQUE|FULLTEXT|SPATIAL)\s+(?:KEY|INDEX)|KEY|INDEX'
 
2259
                                                .   ')'
 
2260
                                                .   '\s+'                         // Followed by at least one white space character.
 
2261
                                                .   '(?:'                         // Name of the index. Optional if type is PRIMARY KEY.
 
2262
                                                .       '`?'                      // Name can be escaped with a backtick.
 
2263
                                                .           '(?P<index_name>'     // 2) Name of the index.
 
2264
                                                .               '(?:[0-9a-zA-Z$_-]|[\xC2-\xDF][\x80-\xBF])+'
 
2265
                                                .           ')'
 
2266
                                                .       '`?'                      // Name can be escaped with a backtick.
 
2267
                                                .       '\s+'                     // Followed by at least one white space character.
 
2268
                                                .   ')*'
 
2269
                                                .   '\('                          // Opening bracket for the columns.
 
2270
                                                .       '(?P<index_columns>'
 
2271
                                                .           '.+?'                 // 3) Column names, index prefixes, and orders.
 
2272
                                                .       ')'
 
2273
                                                .   '\)'                          // Closing bracket for the columns.
 
2274
                                                . '$/im',
 
2275
                                                $fld,
 
2276
                                                $index_matches
 
2277
                                        );
 
2278
 
 
2279
                                        // Uppercase the index type and normalize space characters.
 
2280
                                        $index_type = strtoupper( preg_replace( '/\s+/', ' ', trim( $index_matches['index_type'] ) ) );
 
2281
 
 
2282
                                        // 'INDEX' is a synonym for 'KEY', standardize on 'KEY'.
 
2283
                                        $index_type = str_replace( 'INDEX', 'KEY', $index_type );
 
2284
 
 
2285
                                        // Escape the index name with backticks. An index for a primary key has no name.
 
2286
                                        $index_name = ( 'PRIMARY KEY' === $index_type ) ? '' : '`' . $index_matches['index_name'] . '`';
 
2287
 
 
2288
                                        // Parse the columns. Multiple columns are separated by a comma.
 
2289
                                        $index_columns = array_map( 'trim', explode( ',', $index_matches['index_columns'] ) );
 
2290
 
 
2291
                                        // Normalize columns.
 
2292
                                        foreach ( $index_columns as &$index_column ) {
 
2293
                                                // Extract column name and number of indexed characters (sub_part).
 
2294
                                                preg_match(
 
2295
                                                          '/'
 
2296
                                                        .   '`?'                      // Name can be escaped with a backtick.
 
2297
                                                        .       '(?P<column_name>'    // 1) Name of the column.
 
2298
                                                        .           '(?:[0-9a-zA-Z$_-]|[\xC2-\xDF][\x80-\xBF])+'
 
2299
                                                        .       ')'
 
2300
                                                        .   '`?'                      // Name can be escaped with a backtick.
 
2301
                                                        .   '(?:'                     // Optional sub part.
 
2302
                                                        .       '\s*'                 // Optional white space character between name and opening bracket.
 
2303
                                                        .       '\('                  // Opening bracket for the sub part.
 
2304
                                                        .           '\s*'             // Optional white space character after opening bracket.
 
2305
                                                        .           '(?P<sub_part>'
 
2306
                                                        .               '\d+'         // 2) Number of indexed characters.
 
2307
                                                        .           ')'
 
2308
                                                        .           '\s*'             // Optional white space character before closing bracket.
 
2309
                                                        .        '\)'                 // Closing bracket for the sub part.
 
2310
                                                        .   ')?'
 
2311
                                                        . '/',
 
2312
                                                        $index_column,
 
2313
                                                        $index_column_matches
 
2314
                                                );
 
2315
 
 
2316
                                                // Escape the column name with backticks.
 
2317
                                                $index_column = '`' . $index_column_matches['column_name'] . '`';
 
2318
 
 
2319
                                                // Append the optional sup part with the number of indexed characters.
 
2320
                                                if ( isset( $index_column_matches['sub_part'] ) ) {
 
2321
                                                        $index_column .= '(' . $index_column_matches['sub_part'] . ')';
 
2322
                                                }
 
2323
                                        }
 
2324
 
 
2325
                                        // Build the normalized index definition and add it to the list of indices.
 
2326
                                        $indices[] = "{$index_type} {$index_name} (" . implode( ',', $index_columns ) . ")";
 
2327
 
 
2328
                                        // Destroy no longer needed variables.
 
2329
                                        unset( $index_column, $index_column_matches, $index_matches, $index_type, $index_name, $index_columns );
 
2330
 
 
2331
                                        break;
2208
2332
                        }
2209
 
                        $fld = trim($fld);
2210
2333
 
2211
2334
                        // If it's a valid field, add it to the field array.
2212
 
                        if ($validfield) {
2213
 
                                $cfields[strtolower($fieldname)] = trim($fld, ", \n");
 
2335
                        if ( $validfield ) {
 
2336
                                $cfields[ $fieldname_lowercased ] = $fld;
2214
2337
                        }
2215
2338
                }
2216
2339
 
2217
2340
                // For every field in the table.
2218
 
                foreach ($tablefields as $tablefield) {
 
2341
                foreach ( $tablefields as $tablefield ) {
 
2342
                        $tablefield_field_lowercased = strtolower( $tablefield->Field );
 
2343
                        $tablefield_type_lowercased = strtolower( $tablefield->Type );
2219
2344
 
2220
2345
                        // If the table field exists in the field array ...
2221
 
                        if (array_key_exists(strtolower($tablefield->Field), $cfields)) {
 
2346
                        if ( array_key_exists( $tablefield_field_lowercased, $cfields ) ) {
2222
2347
 
2223
2348
                                // Get the field type from the query.
2224
 
                                preg_match("|".$tablefield->Field." ([^ ]*( unsigned)?)|i", $cfields[strtolower($tablefield->Field)], $matches);
 
2349
                                preg_match( '|`?' . $tablefield->Field . '`? ([^ ]*( unsigned)?)|i', $cfields[ $tablefield_field_lowercased ], $matches );
2225
2350
                                $fieldtype = $matches[1];
 
2351
                                $fieldtype_lowercased = strtolower( $fieldtype );
2226
2352
 
2227
2353
                                // Is actual field type different from the field type in query?
2228
2354
                                if ($tablefield->Type != $fieldtype) {
2229
2355
                                        $do_change = true;
2230
 
                                        if ( in_array( strtolower( $fieldtype ), $text_fields ) && in_array( strtolower( $tablefield->Type ), $text_fields ) ) {
2231
 
                                                if ( array_search( strtolower( $fieldtype ), $text_fields ) < array_search( strtolower( $tablefield->Type ), $text_fields ) ) {
 
2356
                                        if ( in_array( $fieldtype_lowercased, $text_fields ) && in_array( $tablefield_type_lowercased, $text_fields ) ) {
 
2357
                                                if ( array_search( $fieldtype_lowercased, $text_fields ) < array_search( $tablefield_type_lowercased, $text_fields ) ) {
2232
2358
                                                        $do_change = false;
2233
2359
                                                }
2234
2360
                                        }
2235
2361
 
2236
 
                                        if ( in_array( strtolower( $fieldtype ), $blob_fields ) && in_array( strtolower( $tablefield->Type ), $blob_fields ) ) {
2237
 
                                                if ( array_search( strtolower( $fieldtype ), $blob_fields ) < array_search( strtolower( $tablefield->Type ), $blob_fields ) ) {
 
2362
                                        if ( in_array( $fieldtype_lowercased, $blob_fields ) && in_array( $tablefield_type_lowercased, $blob_fields ) ) {
 
2363
                                                if ( array_search( $fieldtype_lowercased, $blob_fields ) < array_search( $tablefield_type_lowercased, $blob_fields ) ) {
2238
2364
                                                        $do_change = false;
2239
2365
                                                }
2240
2366
                                        }
2241
2367
 
2242
2368
                                        if ( $do_change ) {
2243
 
                                        // Add a query to change the column type
2244
 
                                                $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[strtolower($tablefield->Field)];
 
2369
                                                // Add a query to change the column type.
 
2370
                                                $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN `{$tablefield->Field}` " . $cfields[ $tablefield_field_lowercased ];
2245
2371
                                                $for_update[$table.'.'.$tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}";
2246
2372
                                        }
2247
2373
                                }
2248
2374
 
2249
 
                                // Get the default value from the array
2250
 
                                        // todo: Remove this?
2251
 
                                        //echo "{$cfields[strtolower($tablefield->Field)]}<br>";
2252
 
                                if (preg_match("| DEFAULT '(.*?)'|i", $cfields[strtolower($tablefield->Field)], $matches)) {
 
2375
                                // Get the default value from the array.
 
2376
                                if ( preg_match( "| DEFAULT '(.*?)'|i", $cfields[ $tablefield_field_lowercased ], $matches ) ) {
2253
2377
                                        $default_value = $matches[1];
2254
2378
                                        if ($tablefield->Default != $default_value) {
2255
2379
                                                // Add a query to change the column's default value
2256
 
                                                $cqueries[] = "ALTER TABLE {$table} ALTER COLUMN {$tablefield->Field} SET DEFAULT '{$default_value}'";
 
2380
                                                $cqueries[] = "ALTER TABLE {$table} ALTER COLUMN `{$tablefield->Field}` SET DEFAULT '{$default_value}'";
2257
2381
                                                $for_update[$table.'.'.$tablefield->Field] = "Changed default value of {$table}.{$tablefield->Field} from {$tablefield->Default} to {$default_value}";
2258
2382
                                        }
2259
2383
                                }
2260
2384
 
2261
2385
                                // Remove the field from the array (so it's not added).
2262
 
                                unset($cfields[strtolower($tablefield->Field)]);
 
2386
                                unset( $cfields[ $tablefield_field_lowercased ] );
2263
2387
                        } else {
2264
2388
                                // This field exists in the table, but not in the creation queries?
2265
2389
                        }
2302
2426
                                if ( 'FULLTEXT' === strtoupper( $index_data['index_type'] ) ) {
2303
2427
                                        $index_string .= 'FULLTEXT ';
2304
2428
                                }
 
2429
                                if ( 'SPATIAL' === strtoupper( $index_data['index_type'] ) ) {
 
2430
                                        $index_string .= 'SPATIAL ';
 
2431
                                }
2305
2432
                                $index_string .= 'KEY ';
2306
 
                                if ($index_name != 'PRIMARY') {
2307
 
                                        $index_string .= $index_name;
 
2433
                                if ( 'PRIMARY' !== $index_name  ) {
 
2434
                                        $index_string .= '`' . $index_name . '`';
2308
2435
                                }
2309
2436
                                $index_columns = '';
2310
2437
 
2311
2438
                                // For each column in the index.
2312
2439
                                foreach ($index_data['columns'] as $column_data) {
2313
 
                                        if ($index_columns != '') $index_columns .= ',';
 
2440
                                        if ( $index_columns != '' ) {
 
2441
                                                $index_columns .= ',';
 
2442
                                        }
2314
2443
 
2315
2444
                                        // Add the field to the column list string.
2316
 
                                        $index_columns .= $column_data['fieldname'];
 
2445
                                        $index_columns .= '`' . $column_data['fieldname'] . '`';
2317
2446
                                        if ($column_data['subpart'] != '') {
2318
2447
                                                $index_columns .= '('.$column_data['subpart'].')';
2319
2448
                                        }
2332
2461
                                        if ( ! ( ( $aindex = array_search( $index_string, $indices ) ) === false ) ) {
2333
2462
                                                unset( $indices[ $aindex ] );
2334
2463
                                                break;
2335
 
                                                // todo: Remove this?
2336
 
                                                //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br />Found index:".$index_string."</pre>\n";
2337
2464
                                        }
2338
2465
                                }
2339
 
                                // todo: Remove this?
2340
 
                                //else echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br /><b>Did not find index:</b>".$index_string."<br />".print_r($indices, true)."</pre>\n";
2341
2466
                        }
2342
2467
                }
2343
2468
 
2355
2480
        $allqueries = array_merge($cqueries, $iqueries);
2356
2481
        if ($execute) {
2357
2482
                foreach ($allqueries as $query) {
2358
 
                        // todo: Remove this?
2359
 
                        //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">".print_r($query, true)."</pre>\n";
2360
2483
                        $wpdb->query($query);
2361
2484
                }
2362
2485
        }
2800
2923
        }
2801
2924
 
2802
2925
        /**
2803
 
         * Filter if upgrade routines should be run on global tables.
 
2926
         * Filters if upgrade routines should be run on global tables.
2804
2927
         *
2805
2928
         * @param bool $should_upgrade Whether to run the upgrade routines on global tables.
2806
2929
         */