| 265 | if ( ! 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 | */ |
| 277 | function 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 | * @since 4.2.0 |
| 293 | * |
| 294 | * @param array $permalink_structures An array of Permalink structures. |
| 295 | */ |
| 296 | $permalink_structures = apply_filters( 'wp_install_permalink_structures', array( |
| 297 | '/%year%/%monthnum%/%day%/%postname%/', |
| 298 | '/index.php/%year%/%monthnum%/%day%/%postname%/' |
| 299 | ) ); |
| 300 | |
| 301 | foreach ( (array) $permalink_structures as $permalink_structure ) { |
| 302 | /* |
| 303 | * Set the desired Permalink structure to try |
| 304 | */ |
| 305 | $wp_rewrite->set_permalink_structure( $permalink_structure ); |
| 306 | |
| 307 | /* |
| 308 | * Flush rules with the hard option to force refresh of the web-server's |
| 309 | * rewrite config file (e.g. .htaccess or web.config). |
| 310 | */ |
| 311 | $wp_rewrite->flush_rules( true ); |
| 312 | |
| 313 | /* |
| 314 | * Test against a real WordPress Post, or if none were created, a Page URI |
| 315 | */ |
| 316 | $test_url = get_permalink( 1 ); |
| 317 | if ( ! $test_url ) { |
| 318 | $test_url = home_url( '/wordpress-check-for-rewrites/' ); |
| 319 | } |
| 320 | |
| 321 | /* |
| 322 | * Send a HEAD request to a random page on the site, and check whether |
| 323 | * the 'x-pingback' header is returned as expected. |
| 324 | */ |
| 325 | $response = wp_remote_get( $test_url, array( 'timeout' => 5 ) ); |
| 326 | $x_pingback_header = wp_remote_retrieve_header( $response, 'x-pingback' ); |
| 327 | $pretty_permalinks = $x_pingback_header && $x_pingback_header === get_bloginfo( 'pingback_url' ); |
| 328 | |
| 329 | if ( $pretty_permalinks ) { |
| 330 | return true; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | * If it makes it this far, Pretty Permalinks failed to activate. |
| 336 | * Reset and allow the user to select it themselves. |
| 337 | */ |
| 338 | $wp_rewrite->set_permalink_structure( '' ); |
| 339 | $wp_rewrite->flush_rules( true ); |
| 340 | } |
| 341 | endif; |
| 342 | |