| | 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 checkWPWord($word) { |
| | 70 | $exceptions = array_keys($this->getWPExceptionList()); |
| | 71 | return !in_array($word, $exceptions); |
| | 72 | } |
| | 73 | |
| | 74 | /** |
| | 75 | * Returns suggestions of for a specific word using the list of |
| | 76 | * Wordpress Spelling Exceptions |
| | 77 | * |
| | 78 | * @param {String} $lang Language code like sv or en. |
| | 79 | * @param {String} $word Specific word to get suggestions for. |
| | 80 | * @return {Array} Array of suggestions for the specified word. |
| | 81 | */ |
| | 82 | function getWPSuggestions($lang, $word) { |
| | 83 | $exceptions = $this->getWPExceptionList(); |
| | 84 | |
| | 85 | $suggestions = array(); |
| | 86 | foreach ($exceptions as $good => $bad) { |
| | 87 | if (in_array($word, $bad)) $suggestions[] = $good; |
| | 88 | } |
| | 89 | return $suggestions; |
| | 90 | } |
| | 91 | |
| | 92 | /** |
| | 93 | * Get List of Wordpress Spelling Exceptions |
| | 94 | * |
| | 95 | * Use filter 'mce_spellcheck_exceptions' to add custom exceptions to this list. |
| | 96 | * |
| | 97 | * @return {Array} List of Spelling Exceptions in the format array( $correctly-spelled-word -> array( 'misspelled', 'words', 'to be replaced')) |
| | 98 | */ |
| | 99 | function getWPExceptionList() { |
| | 100 | $defaults = array( |
| | 101 | 'WordPress' => array('Word Press', 'wordpress', 'wordPress', 'Wordpress') |
| | 102 | ); |
| | 103 | return apply_filters('mce_spellcheck_exceptions', $defaults); |
| | 104 | } |
| | 105 | |
| | 106 | /** |
| | 107 | * Spellchecks an array of words. |
| | 108 | * |
| | 109 | * Ignores words that are on the list of Wordpress Spelling Exceptions. |
| | 110 | * |
| | 111 | * @param {String} $lang Language code like sv or en. |
| | 112 | * @param {Array} $words Array of words to spellcheck. |
| | 113 | * @return {Array} Array of misspelled words. |
| | 114 | */ |
| | 115 | function &checkWords($lang, $words) { |
| | 116 | $words = array_filter($words, array($this, 'checkWPWord')); |
| | 117 | return parent::checkWords($lang, $words); |
| | 118 | } |
| | 119 | |
| | 120 | /** |
| | 121 | * Returns suggestions of for a specific word. |
| | 122 | * |
| | 123 | * Merges Wordpress Spelling Exceptions (filter 'mce_spellcheck_exceptions') |
| | 124 | * with google's list of suggestions. |
| | 125 | * |
| | 126 | * @param {String} $lang Language code like sv or en. |
| | 127 | * @param {String} $word Specific word to get suggestions for. |
| | 128 | * @return {Array} Array of suggestions for the specified word. |
| | 129 | */ |
| | 130 | function &getSuggestions($lang, $word) { |
| | 131 | $wp_suggestions = $this->getWPSuggestions($lang, $word); |
| | 132 | $google_suggestions = parent::getSuggestions($lang, $word); |
| | 133 | return array_merge($wp_suggestions, $google_suggestions); |
| | 134 | } |
| | 135 | |
| | 136 | /** |
| | 137 | * GoogleSpell::_unhtmlentities remove e option from preg_replace calls |
| | 138 | * |
| | 139 | * @see http://core.trac.wordpress.org/ticket/8689 |
| | 140 | */ |
| | 141 | function _unhtmlentities($string) { |
| | 142 | $string = preg_replace('~&#x([0-9a-f]+);~i', 'chr(hexdec("\\1"))', $string); |
| | 143 | $string = preg_replace('~&#([0-9]+);~', 'chr(\\1)', $string); |
| | 144 | |
| | 145 | $trans_tbl = get_html_translation_table(HTML_ENTITIES); |
| | 146 | $trans_tbl = array_flip($trans_tbl); |
| | 147 | |
| | 148 | return strtr($string, $trans_tbl); |
| | 149 | } |
| | 150 | } |
| | 151 | ?> |
| | 152 | No newline at end of file |