| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | Plugin Name: Site/Homeurl inline validation |
|---|
| 4 | Plugin URI: http://dd32.id.au/?test-plugin |
|---|
| 5 | Description: Site/Homeurl inline validation |
|---|
| 6 | Version: 1.0 |
|---|
| 7 | Author: DD32 |
|---|
| 8 | Author URI: http://dd32.id.au/ |
|---|
| 9 | */ |
|---|
| 10 | |
|---|
| 11 | add_action('admin_head-options-general.php', '_siteurl_js'); |
|---|
| 12 | function _siteurl_js() { |
|---|
| 13 | ?> |
|---|
| 14 | <script type="text/javascript"> |
|---|
| 15 | //<![CDATA[ |
|---|
| 16 | jQuery(document).ready(function($){ |
|---|
| 17 | $('#siteurl, #home').blur( function() { |
|---|
| 18 | var oldurl = new Array(); |
|---|
| 19 | oldurl['siteurl'] = '<?php echo js_escape(get_option('siteurl')); ?>'; |
|---|
| 20 | oldurl['home'] = '<?php echo js_escape(get_option('home')); ?>'; |
|---|
| 21 | var spinnerurl = '<?php echo js_escape(admin_url('images/wpspin_light.gif')); ?>'; |
|---|
| 22 | var okurl = '<?php echo js_escape(admin_url('images/yes.png')); ?>'; |
|---|
| 23 | var errorurl = '<?php echo js_escape(admin_url('images/no.png')); ?>'; |
|---|
| 24 | |
|---|
| 25 | var url = $(this).val(); |
|---|
| 26 | var id = $(this).attr('id'); |
|---|
| 27 | |
|---|
| 28 | //Remove the old items if they still exist. |
|---|
| 29 | $('#'+id+'-error, #'+id+'-result, #'+id+'-spinner').remove(); |
|---|
| 30 | |
|---|
| 31 | if ( url == oldurl[id] ) |
|---|
| 32 | return; //URL unchanged. |
|---|
| 33 | |
|---|
| 34 | //Add a spinner. |
|---|
| 35 | var field = $('#' + id); |
|---|
| 36 | field.after('<img src="'+spinnerurl+'" id="'+id+'-spinner" />'); |
|---|
| 37 | |
|---|
| 38 | $.post(ajaxurl, {'action': 'check_siteurl', 'url': url}, function(data) { |
|---|
| 39 | $('#'+id+'-spinner').remove(); |
|---|
| 40 | var result = data.split(':', 1)[0]; |
|---|
| 41 | var resultdata = data.substring(result.length + 1); |
|---|
| 42 | |
|---|
| 43 | switch( result ) { |
|---|
| 44 | |
|---|
| 45 | case 'ok': |
|---|
| 46 | field.after('<img src="'+okurl+'" id="'+id+'-result" />'); |
|---|
| 47 | break; |
|---|
| 48 | case 'redirect': |
|---|
| 49 | var text = '<?php echo __('Error: The entered URL redirects to another page, are you certain you did not mean <code>%s</code>?') ?>'; |
|---|
| 50 | text = text.replace('%s', resultdata); |
|---|
| 51 | field.after('<span id="'+id+'-result"><img src="'+errorurl+'" /> <strong>'+text+'</strong></span>'); |
|---|
| 52 | break; |
|---|
| 53 | case 'error': |
|---|
| 54 | var text = '<?php echo __("Warning: An unknown error(%s) occured during validation, please make sure you\'ve entered the url correctly.") ?>'; |
|---|
| 55 | text = text.replace('%s', resultdata); |
|---|
| 56 | field.after('<span id="'+id+'-result"><img src="'+errorurl+'" /> <strong>'+text+'</strong></span>'); |
|---|
| 57 | break; |
|---|
| 58 | case 'notwordpressroot': |
|---|
| 59 | var text = '<?php echo __("Warning: WordPress was detected at the given URL, however it is not the WordPress front page, lease make sure you\'ve entered it correctly.") ?>'; |
|---|
| 60 | field.after('<span id="'+id+'-result"><img src="'+errorurl+'" /> <strong>'+text+'</strong></span>'); |
|---|
| 61 | break; |
|---|
| 62 | case 'notwordpress': |
|---|
| 63 | var text = '<?php echo __("Warning: WordPress could not be detected at the given URL, Please make sure you\'ve entered it correctly.") ?>'; |
|---|
| 64 | field.after('<span id="'+id+'-result"><img src="'+errorurl+'" /> <strong>'+text+'</strong></span>'); |
|---|
| 65 | break; |
|---|
| 66 | default: |
|---|
| 67 | case 'unsupported': |
|---|
| 68 | //Nothing. |
|---|
| 69 | break; |
|---|
| 70 | } |
|---|
| 71 | }); |
|---|
| 72 | }); |
|---|
| 73 | //ajaxurl |
|---|
| 74 | }); |
|---|
| 75 | //]]> |
|---|
| 76 | </script> |
|---|
| 77 | <?php |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | add_action('template_redirect', '_siteurl_check_header'); |
|---|
| 81 | function _siteurl_check_header() { |
|---|
| 82 | if ( isset($_GET['siteurl_check']) ) { |
|---|
| 83 | if ( ! is_front_page() ) |
|---|
| 84 | header('wordpress-siteurl-check: false'); |
|---|
| 85 | else |
|---|
| 86 | header('wordpress-siteurl-check: true'); |
|---|
| 87 | die(); |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | add_action('wp_ajax_check_siteurl', '_siteurl_ajax_handler'); |
|---|
| 92 | function _siteurl_ajax_handler() { |
|---|
| 93 | $url = $_POST['url']; |
|---|
| 94 | $url = add_query_arg('siteurl_check', 'true', $url); |
|---|
| 95 | $transports = array( new WP_Http_Curl(), new WP_Http_Fsockopen() ); //Only these 2 can handle Redirects well. |
|---|
| 96 | $result = false; |
|---|
| 97 | foreach ( $transports as $trans ) { |
|---|
| 98 | if ( $trans->test() ) { |
|---|
| 99 | $result = $trans->request($url, array( |
|---|
| 100 | 'method' => 'HEAD', |
|---|
| 101 | 'timeout' => apply_filters( 'http_request_timeout', 5), |
|---|
| 102 | 'redirection' => 0, |
|---|
| 103 | 'httpversion' => apply_filters( 'http_request_version', '1.0'), |
|---|
| 104 | 'user-agent' => apply_filters( 'http_headers_useragent', 'WordPress/' . $GLOBALS['wp_version'] . '; ' . get_bloginfo( 'url' ) ), |
|---|
| 105 | 'blocking' => true, |
|---|
| 106 | 'headers' => array(), |
|---|
| 107 | 'cookies' => array(), |
|---|
| 108 | 'body' => null, |
|---|
| 109 | 'compress' => false, |
|---|
| 110 | 'decompress' => true, |
|---|
| 111 | 'sslverify' => true |
|---|
| 112 | )); |
|---|
| 113 | break; |
|---|
| 114 | } |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | if ( false === $result ) { |
|---|
| 118 | die('unsupported'); |
|---|
| 119 | |
|---|
| 120 | } elseif ( is_wp_error($result) ) { |
|---|
| 121 | die('error:' . $result->get_error_message() ); |
|---|
| 122 | |
|---|
| 123 | } elseif ( '200' == $result['response']['code'] && !isset($result['headers']['wordpress-siteurl-check']) ) { |
|---|
| 124 | die('notwordpress'); |
|---|
| 125 | |
|---|
| 126 | } elseif ( isset($result['headers']['wordpress-siteurl-check']) && 'true' != $result['headers']['wordpress-siteurl-check'] ) { |
|---|
| 127 | die('notwordpressroot'); |
|---|
| 128 | |
|---|
| 129 | } elseif ( '301' == $result['response']['code'] || '302' == $result['response']['code'] ) { |
|---|
| 130 | die('redirect:' . remove_query_arg('siteurl_check', $result['headers']['location']) ); |
|---|
| 131 | |
|---|
| 132 | } elseif ( '200' != $result['response']['code'] ) { |
|---|
| 133 | die('error:' . $result['response']['message'] ); |
|---|
| 134 | |
|---|
| 135 | } else { |
|---|
| 136 | die('ok'); |
|---|
| 137 | |
|---|
| 138 | } |
|---|
| 139 | } |
|---|