| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | * drop this in mu-plugins. by dd32 |
|---|
| 4 | */ |
|---|
| 5 | |
|---|
| 6 | add_filter('pre_update_option_siteurl', '_siteurl_pre_change', 5, 2); |
|---|
| 7 | function _siteurl_pre_change( $new, $old ) { |
|---|
| 8 | $valid = _siteurl_is_valid( $new, $old ); |
|---|
| 9 | if ( ! is_wp_error( $valid ) ) |
|---|
| 10 | return $new; |
|---|
| 11 | |
|---|
| 12 | $message = sprintf(__( 'WordPress has detected that changing the WordPress Address of your Site to <code>%s</code> will result in WordPress not being accessable for the following reason(s)') , esc_html( $new ) ); |
|---|
| 13 | $message .= '<ul><li>' . $valid->get_error_message(); |
|---|
| 14 | $data = $valid->get_error_data(); |
|---|
| 15 | if ( is_wp_error( $data ) ) |
|---|
| 16 | $message .= ' <ul><li>' . $data->get_error_message() . '</li></ul>'; |
|---|
| 17 | $message .= '</li></ul>'; |
|---|
| 18 | $message .= '</p><p>' . '<input type="submit" value="' . esc_attr(__('Continue?')) . '" />'; |
|---|
| 19 | wp_die( $message ); |
|---|
| 20 | exit; |
|---|
| 21 | } |
|---|
| 22 | |
|---|
| 23 | function _siteurl_is_valid($new, $old) { |
|---|
| 24 | $_url = add_query_arg( 'testing-redirects', '1', $new ); |
|---|
| 25 | $res = wp_remote_head($_url); |
|---|
| 26 | if ( is_wp_error($res) ) |
|---|
| 27 | return new WP_Error('unknown', __('WordPress cannot verify the suggested site URL'), $res ); |
|---|
| 28 | |
|---|
| 29 | if ( isset($res['headers']['wordpress-redirection']) && 'ok' == $res['headers']['wordpress-redirection'] ) |
|---|
| 30 | wp_die( 'True: <pre>' . print_r(array($_url,$res), true) ); |
|---|
| 31 | |
|---|
| 32 | if ( isset($res['headers']['wordpress-redirection']) && 'not-front-page' == $res['headers']['wordpress-redirection'] ) |
|---|
| 33 | return new WP_Error('not_wordpress_front', __('The Provided URL does not resolve to the front page of WordPress.') ); |
|---|
| 34 | |
|---|
| 35 | if ( '200' == $res['response']['code'] ) // Something exists, but is not the WordPress we expected. |
|---|
| 36 | return new WP_Error('not_wordpress', __('This WordPress Installation was not accessable at that URL.') ); |
|---|
| 37 | |
|---|
| 38 | if ( in_array( $res['response']['code'], array(300, 301, 302) ) ) // Redirection elsewhere. |
|---|
| 39 | return new WP_Error('unknown', sprintf(__('Redirection to %s'), $res['headers']['location']) ); |
|---|
| 40 | |
|---|
| 41 | wp_die( 'Fallthrough:<pre>' . print_r(array($_url,$res), true) ); |
|---|
| 42 | // return true; |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | add_action('template_redirect', '_tr', 100); |
|---|
| 46 | function _tr() { |
|---|
| 47 | if ( isset($_GET['testing-redirects']) ) { |
|---|
| 48 | if ( is_front_page() ) |
|---|
| 49 | header('WordPress-Redirection: ok'); |
|---|
| 50 | else |
|---|
| 51 | header('WordPress-Redirection: not-front-page'); |
|---|
| 52 | die(); |
|---|
| 53 | } |
|---|
| 54 | } |
|---|