Make WordPress Core

Changeset 31089


Ignore:
Timestamp:
01/08/2015 06:46:55 AM (10 years ago)
Author:
dd32
Message:

Enable Pretty Permalinks for new sites during install if the server supports it.
See #6481, Props valendesigns, DrewAPicture & ericlewis

File:
1 edited

Legend:

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

    r30982 r31089  
    8787
    8888    wp_install_defaults($user_id);
     89
     90    wp_install_maybe_enable_pretty_permalinks();
    8991
    9092    flush_rewrite_rules();
     
    258260            $wpdb->delete( $wpdb->usermeta, array( 'user_id' => $user_id , 'meta_key' => $wpdb->base_prefix.'1_capabilities' ) );
    259261    }
     262}
     263endif;
     264
     265if ( ! function_exists( 'wp_install_maybe_enable_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_enable_pretty_permalinks() {
     278    global $wp_rewrite;
     279
     280    // Bail if we alredy have permalinks enabled (Multisite)
     281    if ( get_option( 'permalink_structure' ) ) {
     282        return;
     283    }
     284
     285    /*
     286     * The Permalink structures which WordPress should attempt to use.
     287     * The first is designed for mod_rewrite or nginx rewriting.
     288     * The second is PATHINFO based permalinks offered under configurations
     289     * without rewrites enabled.
     290     */
     291    $permalink_structures = array(
     292        '/%year%/%monthnum%/%day%/%postname%/',
     293        '/index.php/%year%/%monthnum%/%day%/%postname%/'
     294    );
     295
     296    foreach ( (array) $permalink_structures as $permalink_structure ) {
     297        // Set the desired Permalink structure to try
     298        $wp_rewrite->set_permalink_structure( $permalink_structure );
     299
     300        /*
     301         * Flush rules with the hard option to force refresh of the web-server's
     302         * rewrite config file (e.g. .htaccess or web.config).
     303         */
     304        $wp_rewrite->flush_rules( true );
     305
     306        // Test against a real WordPress Post, or if none were created, a Page URI
     307        $test_url = get_permalink( 1 );
     308        if ( ! $test_url ) {
     309            $test_url = home_url( '/wordpress-check-for-rewrites/' );
     310        }
     311
     312        /*
     313         * Send a HEAD request to a random page on the site, and check whether
     314         * the 'x-pingback' header is returned as expected.
     315         */
     316        $response          = wp_remote_get( $test_url, array( 'timeout' => 5 ) );
     317        $x_pingback_header = wp_remote_retrieve_header( $response, 'x-pingback' );
     318        $pretty_permalinks = $x_pingback_header && $x_pingback_header === get_bloginfo( 'pingback_url' );
     319
     320        if ( $pretty_permalinks ) {
     321            return true;
     322        }
     323    }
     324
     325    /*
     326     * If it makes it this far, Pretty Permalinks failed to activate.
     327     * Reset and allow the user to select it themselves.
     328     */
     329    $wp_rewrite->set_permalink_structure( '' );
     330    $wp_rewrite->flush_rules( true );
    260331}
    261332endif;
Note: See TracChangeset for help on using the changeset viewer.