| | 263 | if ( !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 | */ |
| | 273 | function 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 | } |
| | 315 | endif; |
| | 316 | |