| | 265 | if ( ! function_exists( 'wp_install_verify_pretty_permalinks' ) ) : |
| | 266 | /** |
| | 267 | * Verify pretty permalinks. |
| | 268 | * |
| | 269 | * This function will verify that pretty permalinks work. If pretty permalinks |
| | 270 | * fail to work, the web-server will fall back to ugly permalinks by setting |
| | 271 | * 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_verify_pretty_permalinks() { |
| | 278 | global $wp_rewrite; |
| | 279 | |
| | 280 | /* |
| | 281 | * Flush rules with the hard option to force refresh of the web-server's |
| | 282 | * rewrite config file (e.g. .htaccess or web.config). |
| | 283 | */ |
| | 284 | $wp_rewrite->flush_rules( true ); |
| | 285 | |
| | 286 | /* |
| | 287 | * Send a HEAD request to a random page on the site, and check whether |
| | 288 | * the 'x-pingback' header is returned as expected. |
| | 289 | */ |
| | 290 | $response = wp_remote_get( site_url( '/wordpress-check-for-rewrites/' ) ); |
| | 291 | $x_pingback_header = wp_remote_retrieve_header( $response, 'x-pingback' ); |
| | 292 | $pretty_permalinks = $x_pingback_header && $x_pingback_header === get_bloginfo( 'pingback_url' ); |
| | 293 | |
| | 294 | /* |
| | 295 | * Fallback test that will send a HEAD request to the dummy post, |
| | 296 | * and check whether the response code returns 200 as expected. |
| | 297 | */ |
| | 298 | if ( ! $pretty_permalinks ) { |
| | 299 | $response = wp_remote_head( get_permalink( 1 ) ); |
| | 300 | $pretty_permalinks = isset( $dummy_response['response']['code'] ) && 200 == $dummy_response['response']['code']; |
| | 301 | } |
| | 302 | |
| | 303 | /** |
| | 304 | * Filter the pretty permalinks verification. |
| | 305 | * |
| | 306 | * @since 4.2.0 |
| | 307 | * |
| | 308 | * @param bool $pretty_permalinks Pretty permalinks verified. |
| | 309 | */ |
| | 310 | $pretty_permalinks = (bool) apply_filters( 'wp_install_verify_pretty_permalinks', $pretty_permalinks ); |
| | 311 | |
| | 312 | // Set an empty permalink structure to make it ugly again. |
| | 313 | if ( ! $pretty_permalinks ) { |
| | 314 | $wp_rewrite->set_permalink_structure(''); |
| | 315 | } |
| | 316 | } |
| | 317 | endif; |
| | 318 | |