WordPress.org

Make WordPress Core

Ticket #6481: wp-admin_includes_upgrade.php.diff

File wp-admin_includes_upgrade.php.diff, 3.2 KB (added by loushou, 5 years ago)

wp-admin/includes/upgrade.php 4.0 file patch for pretty permalink test

  • wp-admin/includes/upgrade.php

     
    117117 * @param int $user_id User ID.
    118118 */
    119119function wp_install_defaults( $user_id ) {
    120         global $wpdb, $wp_rewrite, $table_prefix;
     120        global $wpdb, $table_prefix;
    121121
    122122        // Default category
    123123        $cat_name = __('Uncategorized');
     
    240240                update_user_meta( $user_id, 'show_welcome_panel', 1 );
    241241        elseif ( ! is_super_admin( $user_id ) && ! metadata_exists( 'user', $user_id, 'show_welcome_panel' ) )
    242242                update_user_meta( $user_id, 'show_welcome_panel', 2 );
     243       
     244        // perform a proper test on installation of whether the pretty permalinks can be used on this server
     245        // https://core.trac.wordpress.org/ticket/6481
     246        wp_install_default_permalinks();
    243247
    244248        if ( is_multisite() ) {
    245                 // Flush rules to pick up the new page.
    246                 $wp_rewrite->init();
    247                 $wp_rewrite->flush_rules();
    248 
    249249                $user = new WP_User($user_id);
    250250                $wpdb->update( $wpdb->options, array('option_value' => $user->user_email), array('option_name' => 'admin_email') );
    251251
     
    260260}
    261261endif;
    262262
     263if ( !function_exists('wp_install_default_permalinks') ) :
     264/**
     265 * Tests whether pretty permalinks can be used on this installation.
     266 *
     267 * Performs a test to see if pretty permalinks can be used on this server by default. If the server
     268 * is configurable to allow this, the use the basic /%postname%/ permalink style as the default
     269 * instead of the ugly ?p=123 permalink style.
     270 *
     271 * @since 4.1.0
     272 */
     273function wp_install_default_permalinks() {
     274        global $wp_rewrite;
     275
     276        if ( ! $wp_rewrite->using_permalinks() && ! $wp_rewrite->using_index_permalinks() ) {
     277                // if we are not already using pretty permalinks (probably from a repair of an old install)
     278
     279                $wp_rewrite->init();
     280
     281                // attempt to use pretty permalinks
     282                $wp_rewrite->set_permalink_structure( '/%postname%/' );
     283
     284                // force writing of the relevant config file
     285                $wp_rewrite->flush_rules( true );
     286
     287                // test to make sure that the pretty permalink strucutre is working. test the permalink of the post
     288                // we just created in the wp_install_defaults() function, with post_id 1
     289                $response = wp_remote_request( get_permalink( 1 ), array(
     290                        'method'    => 'HEAD',
     291                        'blocking'  => true,
     292                        'timeout'   => 0.1,
     293                        'sslverify' => apply_filters( 'https_local_ssl_verify', false )
     294                ) );
     295
     296                $worked = false;
     297
     298                // parse the response
     299                if ( is_array( $response ) && isset( $response['response'], $response['response']['code'] ) && $response['response']['code'] == 200 )
     300                        $worked = true;
     301
     302                // if this test failed, then fallback to no permalink_structure, AKA ugly permalinks
     303                if ( ! $worked ) {
     304                        $wp_rewrite->init();
     305                        $wp_rewrite->set_permalink_structure( '' );
     306                        $wp_rewrite->flush_rules();
     307                }
     308        } else if ( is_multisite() ) {
     309                // oterhwise still recalculate the rewrite rules as before when multisite, so that the sites
     310                // get the new page that was created by the wp_install_defaults() function
     311                $wp_rewrite->init();
     312                $wp_rewrite->flush_rules();
     313        }
     314}
     315endif;
     316
    263317if ( !function_exists('wp_new_blog_notification') ) :
    264318/**
    265319 * {@internal Missing Short Description}}