| | 1 | <?php |
| | 2 | /** |
| | 3 | * Extension of TinyMCE GoogleSpell class for Wordpress |
| | 4 | * |
| | 5 | * WP_GoogleSpell extends TinyMCE's GoogleSpell class. It overrides the way in which the |
| | 6 | * http request to google is executed and uses WP_Http instead of curl/fsockopen. |
| | 7 | * |
| | 8 | * In order to use this class the "general.engine" value in config.php |
| | 9 | * (wp-includes/js/tinymce/plugins/spellchecker) needs to be set to "WP_GoogleSpell" |
| | 10 | * instead of "GoogleSpell". |
| | 11 | * |
| | 12 | * @link http://trac.wordpress.org/ticket/9798 Request for proxy support for spellchecking |
| | 13 | * |
| | 14 | * @package WordPress |
| | 15 | * @subpackage TinyMCE |
| | 16 | * @since 2.8.0 |
| | 17 | * @author Benedikt Forchhammer <b.forchhammer@mind2.de> |
| | 18 | */ |
| | 19 | |
| | 20 | // Wordpress functions and classes |
| | 21 | require_once(dirname(dirname(dirname(dirname(dirname(dirname(dirname(__FILE__))))))) . '/wp-load.php'); |
| | 22 | |
| | 23 | // The SpellChecking Engine which we want to extend |
| | 24 | require_once(dirname(__FILE__) . '/GoogleSpell.php'); |
| | 25 | |
| | 26 | class WP_GoogleSpell extends GoogleSpell { |
| | 27 | |
| | 28 | function &_getMatches($lang, $str) { |
| | 29 | if (class_exists('WP_Http')) { |
| | 30 | $http = new WP_Http(); |
| | 31 | |
| | 32 | // request url |
| | 33 | $host = "www.google.com:443"; |
| | 34 | $path = "/tbproxy/spell?lang=" . $lang . "&hl=en"; |
| | 35 | $url = "https://" . $host . $path; |
| | 36 | |
| | 37 | // Setup XML request |
| | 38 | $xml = '<?xml version="1.0" encoding="utf-8" ?><spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="1" ignoreallcaps="1"><text>' . $str . '</text></spellrequest>'; |
| | 39 | |
| | 40 | // set up request arguments. |
| | 41 | // [bforchhammer] sslverify disabled because it did not work on our server / should possibly be enabled by default? |
| | 42 | $args = array( 'body' => $xml, 'sslverify' => false ); |
| | 43 | |
| | 44 | $response = $http->post($url, $args); |
| | 45 | |
| | 46 | if (is_wp_error($response)) { |
| | 47 | echo implode("\n", $response->get_error_messages()); |
| | 48 | return array(); |
| | 49 | } |
| | 50 | else { |
| | 51 | // Grab and parse content |
| | 52 | $matches = array(); |
| | 53 | preg_match_all('/<c o="([^"]*)" l="([^"]*)" s="([^"]*)">([^<]*)<\/c>/', $response['body'], $matches, PREG_SET_ORDER); |
| | 54 | |
| | 55 | return $matches; |
| | 56 | } |
| | 57 | } |
| | 58 | else { |
| | 59 | $this->throwError('WP_Http class has not been loaded'); |
| | 60 | } |
| | 61 | } |
| | 62 | |
| | 63 | /** |
| | 64 | * Checks if a word is on the exception list of "correctly spelled words". |
| | 65 | * |
| | 66 | * @param {String} $word The word to check |
| | 67 | * @param {Bool} false if the word is on the exception list. |
| | 68 | */ |
| | 69 | function isNoWPException($word) { |
| | 70 | return !in_array($word, $this->getWPExceptionList()); |
| | 71 | } |
| | 72 | |
| | 73 | /** |
| | 74 | * Returns suggestions of for a specific word using the list of |
| | 75 | * Wordpress Spelling Exceptions |
| | 76 | * |
| | 77 | * @param {String} $lang Language code like sv or en. |
| | 78 | * @param {String} $word Specific word to get suggestions for. |
| | 79 | * @return {Array} Array of suggestions for the specified word. |
| | 80 | */ |
| | 81 | function getWPSuggestions($lang, $word) { |
| | 82 | $exceptions = $this->getWPExceptionList(); |
| | 83 | |
| | 84 | $suggestions = array(); |
| | 85 | foreach ($exceptions as $suggestion) { |
| | 86 | $p = 0; |
| | 87 | similar_text($word, good, $p); |
| | 88 | if ($p > .8) $suggestions[] = $suggestion; |
| | 89 | } |
| | 90 | return $suggestions; |
| | 91 | } |
| | 92 | |
| | 93 | /** |
| | 94 | * Get List of Wordpress Spelling Exceptions |
| | 95 | * |
| | 96 | * Use filter 'mce_spellcheck_exceptions' to add custom exceptions to this list. |
| | 97 | * |
| | 98 | * @return {Array} List of Spelling Exceptions |
| | 99 | */ |
| | 100 | function getWPExceptionList() { |
| | 101 | $defaults = array('WordPress'); |
| | 102 | return apply_filters('mce_spellcheck_exceptions', $defaults); |
| | 103 | } |
| | 104 | |
| | 105 | /** |
| | 106 | * Spellchecks an array of words. |
| | 107 | * |
| | 108 | * Ignores words that are on the list of Wordpress Spelling Exceptions. |
| | 109 | * |
| | 110 | * @param {String} $lang Language code like sv or en. |
| | 111 | * @param {Array} $words Array of words to spellcheck. |
| | 112 | * @return {Array} Array of misspelled words. |
| | 113 | */ |
| | 114 | function &checkWords($lang, $words) { |
| | 115 | $words = array_filter($words, array($this, 'isNoWPException')); |
| | 116 | return parent::checkWords($lang, $words); |
| | 117 | } |
| | 118 | |
| | 119 | /** |
| | 120 | * Returns suggestions of for a specific word. |
| | 121 | * |
| | 122 | * Merges Wordpress Spelling Exceptions (filter 'mce_spellcheck_exceptions') |
| | 123 | * with google's list of suggestions. |
| | 124 | * |
| | 125 | * @param {String} $lang Language code like sv or en. |
| | 126 | * @param {String} $word Specific word to get suggestions for. |
| | 127 | * @return {Array} Array of suggestions for the specified word. |
| | 128 | */ |
| | 129 | function &getSuggestions($lang, $word) { |
| | 130 | $wp_suggestions = $this->getWPSuggestions($lang, $word); |
| | 131 | $google_suggestions = parent::getSuggestions($lang, $word); |
| | 132 | return array_merge($wp_suggestions, $google_suggestions); |
| | 133 | } |
| | 134 | |
| | 135 | /** |
| | 136 | * Use wordpress function unhtmlentities() |
| | 137 | * |
| | 138 | * @see http://core.trac.wordpress.org/ticket/8689 |
| | 139 | * @see http://core.trac.wordpress.org/ticket/9805 |
| | 140 | */ |
| | 141 | function _unhtmlentities($string) { |
| | 142 | if (function_exists('unhtmlentities')) return unhtmlentities($string); |
| | 143 | else return parent::_unhtmlentities($string); |
| | 144 | } |
| | 145 | } |
| | 146 | ?> |
| | 147 | No newline at end of file |