| 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_remote_post() instead of curl/fsockopen. |
| 7 | * |
| 8 | * The class also adds a way of adding custom words to the dictionary, like for example |
| 9 | * "WordPress". This list of exceptions can be modified by implementing the |
| 10 | * "mce_spellcheck_exceptions" filter. |
| 11 | * |
| 12 | * In order to use this class the "general.engine" value in config.php |
| 13 | * (wp-includes/js/tinymce/plugins/spellchecker) needs to be set to "WP_GoogleSpell" |
| 14 | * instead of "GoogleSpell". |
| 15 | * |
| 16 | * @link http://trac.wordpress.org/ticket/9798 Request for proxy support for spellchecking |
| 17 | * |
| 18 | * @package WordPress |
| 19 | * @subpackage TinyMCE |
| 20 | * @since 2.8.0 |
| 21 | * @author Benedikt Forchhammer <b.forchhammer@mind2.de> |
| 22 | */ |
| 23 | |
| 24 | // Wordpress functions and classes |
| 25 | require_once(dirname(dirname(dirname(dirname(dirname(dirname(dirname(__FILE__))))))) . '/wp-load.php'); |
| 26 | |
| 27 | // The SpellChecking Engine that we want to extend |
| 28 | require_once(dirname(__FILE__) . '/GoogleSpell.php'); |
| 29 | |
| 30 | class WP_GoogleSpell extends GoogleSpell { |
| 31 | |
| 32 | function &_getMatches($lang, $str) { |
| 33 | if (function_exists('wp_remote_post')) { |
| 34 | // request url |
| 35 | $url = "https://www.google.com/tbproxy/spell?lang=" . $lang . "&hl=en"; |
| 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 | $args = array( 'body' => $xml, 'sslverify' => false ); |
| 42 | |
| 43 | // execute request and get response |
| 44 | $response = wp_remote_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>/', wp_remote_retrieve_body($response), $matches, PREG_SET_ORDER); |
| 54 | |
| 55 | return $matches; |
| 56 | } |
| 57 | } |
| 58 | else { |
| 59 | $this->throwError('wp_remote_post() function has not been loaded.'); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Checks if a word is not 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, true otherwise. |
| 68 | */ |
| 69 | function isNoException($word) { |
| 70 | return !in_array($word, $this->getExceptionList()); |
| 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 getExceptionSuggestions($lang, $word) { |
| 82 | $exceptions = $this->getExceptionList(); |
| 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 getExceptionList() { |
| 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, 'isNoException')); |
| 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->getExceptionSuggestions($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 |