Make WordPress Core

Ticket #6481: 6481.11.diff

File 6481.11.diff, 4.6 KB (added by dd32, 10 years ago)
  • src/wp-admin/includes/upgrade.php

     
    7575                update_user_option($user_id, 'default_password_nag', true, true);
    7676                $email_password = true;
    7777        } else if ( !$user_id ) {
    7878                // Password has been provided
    7979                $message = '<em>'.__('Your chosen password.').'</em>';
    8080                $user_id = wp_create_user($user_name, $user_password, $user_email);
    8181        } else {
    8282                $message = __('User already exists. Password inherited.');
    8383        }
    8484
    8585        $user = new WP_User($user_id);
    8686        $user->set_role('administrator');
    8787
    8888        wp_install_defaults($user_id);
    8989
     90        wp_install_maybe_pretty_permalinks();
     91
    9092        flush_rewrite_rules();
    9193
    9294        wp_new_blog_notification($blog_title, $guessurl, $user_id, ($email_password ? $user_password : __('The password you chose during the install.') ) );
    9395
    9496        wp_cache_flush();
    9597
    9698        /**
    9799         * Fires after a site is fully installed.
    98100         *
    99101         * @since 3.9.0
    100102         *
    101103         * @param WP_User $user The site owner.
    102104         */
    103105        do_action( 'wp_install', $user );
    104106
     
    248250
    249251                $user = new WP_User($user_id);
    250252                $wpdb->update( $wpdb->options, array('option_value' => $user->user_email), array('option_name' => 'admin_email') );
    251253
    252254                // Remove all perms except for the login user.
    253255                $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id != %d AND meta_key = %s", $user_id, $table_prefix.'user_level') );
    254256                $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id != %d AND meta_key = %s", $user_id, $table_prefix.'capabilities') );
    255257
    256258                // Delete any caps that snuck into the previously active blog. (Hardcoded to blog 1 for now.) TODO: Get previous_blog_id.
    257259                if ( !is_super_admin( $user_id ) && $user_id != 1 )
    258260                        $wpdb->delete( $wpdb->usermeta, array( 'user_id' => $user_id , 'meta_key' => $wpdb->base_prefix.'1_capabilities' ) );
    259261        }
    260262}
    261263endif;
    262264
     265if ( ! function_exists( 'wp_install_maybe_pretty_permalinks' ) ) :
     266/**
     267 * Enable pretty permalinks if available.
     268 *
     269 * This function will enable pretty permalinks if it can verify they work.
     270 * If all pretty permalinks formats fail to work, WordPress will fall back
     271 * to ugly permalinks by setting an empty permalink structure.
     272 *
     273 * @since 4.2.0
     274 *
     275 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
     276 */
     277function wp_install_maybe_pretty_permalinks() {
     278        global $wp_rewrite;
     279
     280        /*
     281         * Bail if we alredy have permalinks enabled (Multisite)
     282         */
     283        if ( get_option( 'permalink_structure' ) ) {
     284                return;
     285        }
     286
     287        /*
     288         * The Permalink structures which WordPress should attempt to use.
     289         * The first is designed for mod_rewrite or nginx rewriting.
     290         * The second is PATHINFO based permalinks offered under configurations without rewrites enabled.
     291         */
     292        $permalink_structures = array(
     293                '/%year%/%monthnum%/%day%/%postname%/',
     294                '/index.php/%year%/%monthnum%/%day%/%postname%/'
     295        );
     296
     297        foreach ( $permalink_structures as $permalink_structure ) {
     298                /*
     299                 * Set the desired Permalink structure to try
     300                 */
     301                $wp_rewrite->set_permalink_structure( $permalink_structure );
     302
     303                /*
     304                 * Flush rules with the hard option to force refresh of the web-server's
     305                 * rewrite config file (e.g. .htaccess or web.config).
     306                 */
     307                $wp_rewrite->flush_rules( true );
     308
     309                /*
     310                 * Test against a real WordPress Post, or if none were created, a Page URI
     311                 */
     312                $test_url = get_permalink( 1 );
     313                if ( ! $test_url ) {
     314                        $test_url = home_url( '/wordpress-check-for-rewrites/' );
     315                }
     316
     317                /*
     318                 * Send a HEAD request to a random page on the site, and check whether
     319                 * the 'x-pingback' header is returned as expected.
     320                 */
     321                $response          = wp_remote_get( $test_url, array( 'timeout' => 5 ) );
     322                $x_pingback_header = wp_remote_retrieve_header( $response, 'x-pingback' );
     323                $pretty_permalinks = $x_pingback_header && $x_pingback_header === get_bloginfo( 'pingback_url' );
     324
     325                if ( $pretty_permalinks ) {
     326                        return true;
     327                }
     328        }
     329
     330        /*
     331         * If it makes it this far, Pretty Permalinks failed to activate.
     332         * Reset and allow the user to select it themselves.
     333         */
     334        $wp_rewrite->set_permalink_structure( '' );
     335        $wp_rewrite->flush_rules( true );
     336}
     337endif;
     338
    263339if ( !function_exists('wp_new_blog_notification') ) :
    264340/**
    265341 * {@internal Missing Short Description}}
    266342 *
    267343 * {@internal Missing Long Description}}
    268344 *
    269345 * @since 2.1.0
    270346 *
    271347 * @param string $blog_title Blog title.
    272348 * @param string $blog_url Blog url.
    273349 * @param int $user_id User ID.
    274350 * @param string $password User's Password.
    275351 */
    276352function wp_new_blog_notification($blog_title, $blog_url, $user_id, $password) {
    277353        $user = new WP_User( $user_id );