WordPress.org

Make WordPress Core

Ticket #6481: 6481.13.diff

File 6481.13.diff, 3.4 KB (added by valendesigns, 6 years ago)
  • src/wp-admin/includes/upgrade.php

    diff --git src/wp-admin/includes/upgrade.php src/wp-admin/includes/upgrade.php
    index b72303a..3efe4d6 100644
    function wp_install( $blog_title, $user_name, $user_email, $public, $deprecated 
    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.') ) );
    As a new WordPress user, you should go to <a href=\"%s\">your dashboard</a> to d 
    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        // Bail if we alredy have permalinks enabled (Multisite)
     281        if ( get_option( 'permalink_structure' ) ) {
     282                return;
     283        }
     284
     285        /**
     286         * Filter the permalink structures WordPress should attempt to use during instalation.
     287         *
     288         * The first default array item is designed for mod_rewrite or nginx rewriting,
     289         * the second is for PATHINFO based permalinks offered under configurations
     290         * without rewrites enabled.
     291         *
     292         * @since 4.2.0
     293         *
     294         * @param array $permalink_structures An array of potential Permalink structures.
     295         *                                    Default is array containing
     296         *                                    '/%year%/%monthnum%/%day%/%postname%/' and
     297         *                                    '/index.php/%year%/%monthnum%/%day%/%postname%/'.
     298         */
     299        $permalink_structures = apply_filters( 'wp_install_permalink_structures', array(
     300                '/%year%/%monthnum%/%day%/%postname%/',
     301                '/index.php/%year%/%monthnum%/%day%/%postname%/'
     302        ) );
     303
     304        foreach ( (array) $permalink_structures as $permalink_structure ) {
     305                // Set the desired Permalink structure to try
     306                $wp_rewrite->set_permalink_structure( $permalink_structure );
     307
     308                /*
     309                 * Flush rules with the hard option to force refresh of the web-server's
     310                 * rewrite config file (e.g. .htaccess or web.config).
     311                 */
     312                $wp_rewrite->flush_rules( true );
     313
     314                // Test against a real WordPress Post, or if none were created, a Page URI
     315                $test_url = get_permalink( 1 );
     316                if ( ! $test_url ) {
     317                        $test_url = home_url( '/wordpress-check-for-rewrites/' );
     318                }
     319
     320                /*
     321                 * Send a HEAD request to a random page on the site, and check whether
     322                 * the 'x-pingback' header is returned as expected.
     323                 */
     324                $response          = wp_remote_get( $test_url, array( 'timeout' => 5 ) );
     325                $x_pingback_header = wp_remote_retrieve_header( $response, 'x-pingback' );
     326                $pretty_permalinks = $x_pingback_header && $x_pingback_header === get_bloginfo( 'pingback_url' );
     327
     328                if ( $pretty_permalinks ) {
     329                        return true;
     330                }
     331        }
     332
     333        /*
     334         * If it makes it this far, Pretty Permalinks failed to activate.
     335         * Reset and allow the user to select it themselves.
     336         */
     337        $wp_rewrite->set_permalink_structure( '' );
     338        $wp_rewrite->flush_rules( true );
     339}
     340endif;
     341
    263342if ( !function_exists('wp_new_blog_notification') ) :
    264343/**
    265344 * {@internal Missing Short Description}}