PluginProbe
WProofreader spell & grammar check plugin for WordPress / 2.4
WProofreader spell & grammar check plugin for WordPress v2.4
3.2.1 3.2.2 3.2.0 trunk 1.0 1.1 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.6.1 2.6.10 2.6.2 2.6.3 2.6.4 2.6.5 2.6.6 2.6.7 2.6.8 2.6.9 2.7.0 2.7.1 All 28 releases
webspellchecker / webspellchecker.php

webspellchecker.php in WProofreader spell & grammar check plugin for WordPress 2.4, at webspellchecker.php

337 lines 10.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: WProofreader
4 * Plugin URI: https://webspellchecker.com/
5 * Description: Check spelling and grammar on your site automatically with multilingual WProofreader plugin.
6 * Version: 2.4
7 * Author: WebSpellChecker
8 * Author URI: https://webspellchecker.com/
9 * Text Domain: webspellchecker
10 */
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit; // Exit if accessed directly.
14 }
15
16 final class WProofreader {
17 const TRIAL_CUSTOMER_ID = '1:cma3h3-HTiyU3-JL08g4-SRyuS1-a9c0F3-kH6Cu-OlMHS-thcSV2-HlGmv3-YzRCN2-qrKY42-uPc';
18 const SLANG = 'en_US';
19 const BADGE_BUTTON = 'off';
20 const PLUGIN_VERSION = "2.4";
21 private static $instance = null;
22 private $js_added = false;
23 private $settings;
24 protected $options;
25
26 public static function instance() {
27 if ( null == self::$instance ) {
28 self::$instance = new self;
29 }
30
31 return self::$instance;
32 }
33
34 public function __construct() {
35 $this->includes();
36 $this->settings = new WSC_Settings(
37 __( 'WProofreader', 'webspellchecker' ),
38 __( 'WProofreader', 'webspellchecker' ),
39 'spell-checker-settings'
40 );
41
42 $this->options = ! empty( get_option( WSC_Settings::OPTION_NAME ) ) ? get_option( WSC_Settings::OPTION_NAME ) : array();
43
44 if ( empty( $this->options ) ) {
45 //set default setting
46 $this->options['enable_on_posts'] = 'on';
47 $this->options['enable_on_pages'] = 'on';
48 $this->options['enable_on_products'] = 'off';
49 $this->options['enable_on_categories'] = 'on';
50 $this->options['enable_on_tags'] = 'on';
51 update_option( 'wsc_proofreader_version', self::PLUGIN_VERSION );
52 }
53
54 $this->options['customer_id'] = $this->get_customer_id();
55
56 add_action( 'admin_enqueue_scripts', array( $this, 'register_proofreader_scripts' ) );
57 add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'add_action_links' ) );
58 add_action( 'admin_head', array( $this, 'init_proofreader' ) );
59
60 $this->check_version();
61
62 add_action( 'wp_ajax_get_proofreader_info_callback', array( $this, 'get_proofreader_info_callback' ) );
63 do_action( 'wsc_loaded' );
64 }
65
66 public function check_version() {
67 //todo add Class for upgrade plugin
68 if ( get_option( 'wsc_proofreader_version' ) !== self::PLUGIN_VERSION ) {
69 //Clear old options in versions below 2
70 delete_option( 'wsc' );
71 update_option( 'wsc_proofreader_version', self::PLUGIN_VERSION );
72 }
73 }
74
75 public function init_proofreader() {
76 $editable_post_type = $this->get_editable_post_type();
77 $screen = get_current_screen();
78 if ( $screen->base === 'settings_page_spell-checker-settings' ) {
79 $this->api_proofreader_info();
80 }
81
82 foreach ( $this->options as $option => $on ) {
83 if ( $option === 'enable_on_categories' && $on === 'on' ) {
84 if ( $screen->id === 'edit-category'
85 || $screen->id === 'edit-product_cat'
86 || $screen->id === 'edit-wpsc_product_category' ) {
87 $this->init_proofreader_js();
88 }
89 }
90 }
91
92 foreach ( $this->options as $option => $on ) {
93 if ( $option === 'enable_on_tags' && $on === 'on' ) {
94 if ( $screen->id === 'edit-product_tag'
95 || $screen->id === 'edit-post_tag' ) {
96 $this->init_proofreader_js();
97 }
98 }
99 }
100
101 if ( false !== $editable_post_type ) {
102
103 foreach ( $this->options as $option => $on ) {
104
105 if ( $option === 'enable_on_posts' && $on === 'on' ) {
106
107 if ( 0 === strcasecmp( 'post', $editable_post_type ) ) {
108 $this->init_proofreader_js();
109 }
110 break;
111 }
112 }
113
114 foreach ( $this->options as $option => $on ) {
115
116 if ( $option === 'enable_on_pages' && $on === 'on' ) {
117
118 if ( 0 === strcasecmp( 'page', $editable_post_type ) ) {
119 $this->init_proofreader_js();
120 }
121 break;
122 }
123 }
124
125 foreach ( $this->options as $option => $on ) {
126
127 if ( $option === 'enable_on_products' && $on === 'on' ) {
128 if ( $screen->id === 'wpsc-product' ) {
129 $this->init_proofreader_js();
130 }
131
132 if ( $screen->id === 'edit-product_cat' ) {
133 $this->init_proofreader_js();
134 }
135 if ( $screen->id === 'edit-product_tag' ) {
136
137 $this->init_proofreader_js();
138 }
139 if ( 0 === strcasecmp( 'product', $editable_post_type ) ) {
140 $this->init_proofreader_js();
141 }
142
143 break;
144
145 }
146
147 }
148
149 return $this->js_added = true;
150 }
151
152 return $this->js_added = false;
153 }
154
155 public function includes() {
156 require_once dirname( __FILE__ ) . '/vendor/class.settings-api.php';
157 require_once dirname( __FILE__ ) . '/includes/class-wsc-settings.php';
158 }
159
160 public function get_editable_post_type() {
161 $screen = get_current_screen();
162
163 if ( $screen->post_type === $screen->id ) {
164 $post_type = $screen->post_type;
165
166 return $post_type;
167 }
168
169 return false;
170 }
171
172 function add_action_links( $links ) {
173 $mylinks = array(
174 '<a href="' . admin_url( 'options-general.php?page=spell-checker-settings' ) . '">' . __( 'Settings', 'webspellchecker' ) . '</a>',
175 );
176
177 return array_merge( $links, $mylinks );
178 }
179
180 function register_proofreader_scripts() {
181 wp_register_script( 'wscbundle', 'https://svc.webspellchecker.net/spellcheck31/wscbundle/wscbundle.js', array(), '081120181109', false );
182 wp_register_script( 'ProofreaderConfig', plugin_dir_url( __FILE__ ) . '/assets/proofreaderConfig.js', array( 'wscbundle' ), '081120181110', true );
183 wp_register_script( 'ProofreaderInstance', plugin_dir_url( __FILE__ ) . '/assets/instance.js', array( 'wscbundle' ), '171220181251', true );
184 }
185
186 function init_proofreader_js() {
187 $key_for_proofreader = $this->get_customer_id();
188 $slang = $this->get_slang();
189 $settingsSections = ( $this->get_customer_id() === self::TRIAL_CUSTOMER_ID ) ?
190 [ 'options', 'languages', 'about' ]
191 : [ 'options', 'languages', 'dictionaries', 'about' ];
192 $enableGrammar = ( $this->get_customer_id() === self::TRIAL_CUSTOMER_ID ) ? 'false' : 'true';
193 $badge_button_optinon = ( $this->get_badge_button_optinon() === self::BADGE_BUTTON ) ? 'true' : 'false';
194 $wsc_proofreader_config = array(
195 'key_for_proofreader' => $key_for_proofreader,
196 'slang' => $slang,
197 'settingsSections' => $settingsSections,
198 'enableGrammar' => $enableGrammar,
199 'disableBadgeButton' => $badge_button_optinon,
200 );
201 wp_enqueue_script( 'wscbundle' );
202 wp_enqueue_script( 'ProofreaderConfig' );
203 wp_localize_script( 'ProofreaderConfig', 'WSCProofreaderConfig', $wsc_proofreader_config );
204 }
205
206 /**
207 * Get customer ID or trial ID if not set
208 *
209 * @return string customer ID
210 */
211 public function get_customer_id() {
212 $customer_id = $this->get_option( 'customer_id', self::TRIAL_CUSTOMER_ID );
213
214 if ( empty( $customer_id ) ) {
215 return self::TRIAL_CUSTOMER_ID;
216 }
217
218 return $customer_id;
219 }
220
221 public function get_slang() {
222 $slang = $this->get_option( 'slang', self::SLANG );
223
224 if ( empty( $slang ) ) {
225 return self::SLANG;
226 }
227
228 return $slang;
229 }
230
231 public function get_badge_button_optinon() {
232 $badge_button_optinon = $this->get_option( 'disable_badge_button', self::BADGE_BUTTON );
233
234 return $badge_button_optinon;
235 }
236
237 public function get_option( $name, $default = '' ) {
238 return ( isset( $this->options[ $name ] ) ) ? $this->options[ $name ] : $default;
239 }
240
241 function api_proofreader_info() {
242 $ajax_nonce = wp_create_nonce( "webspellchecker-proofreader" );
243 $key_for_proofreader = $this->get_customer_id();
244 $slang = $this->get_slang();
245 $enableGrammar = ( $this->get_customer_id() === self::TRIAL_CUSTOMER_ID ) ? 'false' : 'true';
246 $wsc_proofreader_config = array(
247 'key_for_proofreader' => $key_for_proofreader,
248 'slang' => $slang,
249 'ajax_nonce' => $ajax_nonce,
250 'enableGrammar' => $enableGrammar
251 );
252 wp_enqueue_script( 'wscbundle' );
253 wp_enqueue_script( 'ProofreaderInstance' );
254 wp_localize_script( 'ProofreaderInstance', 'ProofreaderInstance', $wsc_proofreader_config );
255 }
256
257
258 function get_proofreader_info_callback() {
259 $current_lang = $this->get_slang();
260 check_ajax_referer( 'webspellchecker-proofreader', 'security' );
261 $proofreader_info = $_POST['getInfoResult'];
262 update_option( 'wsc_proofreader_info', $proofreader_info );
263 ob_start();
264 ?>
265 <select class="regular" name="wsc_proofreader[slang]" id="wsc_proofreader[slang]">
266 <?php foreach ( $proofreader_info['langList']['ltr'] as $key => $value ): ?>
267 <option <?php if ( $current_lang === $key ) {
268 echo 'selected';
269 } ?> value="<?php echo $key; ?>"><?php echo $value; ?></option>
270 <?php endforeach; ?>
271 </select>
272 <?php
273 wp_send_json( ob_get_clean() );
274 wp_die();
275 }
276
277 public static function fix_for_gutenberg() {
278 add_action( 'wp_insert_post_data', function ( $data, $postarr ) {
279 if ( 'publish' == $data['post_status'] ) {
280 $string = $data['post_content'];
281 $new_string = preg_replace( '#(<span class=."wsc-spelling-problem." .*?>)(.*?)(</span>)#', '$2', $string );
282 $new_string = preg_replace( '#(<span class=."wsc-grammar-problem." .*?>)(.*?)(</span>)#', '$2', $new_string );
283 $new_string = preg_replace( '#(<span class=."rangySelectionBoundary." .*?>)(.*?)(</span>)#', '$2', $new_string );
284 $new_string = preg_replace( '#(<span class="wsc-spelling-problem".*?>)(.*?)(</span>)#', '$2', $new_string );
285 $new_string = preg_replace( '#(<span class="wsc-grammar-problem" .*?>)(.*?)(</span>)#', '$2', $new_string );
286 $new_string = preg_replace( '#(<span class="rangySelectionBoundary" .*?>)(.*?)(</span>)#', '$2', $new_string );
287 $new_string = preg_replace( '#(<span class=..rangySelectionBoundary.. .*?>)(.*?)(</span>)#', '$2', $new_string );
288 $data['post_content'] = $new_string;
289 }
290
291 return $data;
292 }, 100, 2 );
293 }
294 }
295
296 function WSC() {
297 return WProofreader::instance();
298 }
299
300 if ( is_admin() ) {
301 WSC();
302 }
303 /**
304 * fix for Gutenberg
305 * todo write more cleaner
306 */
307 if ( true === is_gutenberg_active() ) {
308 WProofreader::fix_for_gutenberg();
309 };
310
311 function is_gutenberg_active() {
312 $gutenberg = false;
313 $block_editor = false;
314
315 if ( has_filter( 'replace_editor', 'gutenberg_init' ) ) {
316 $gutenberg = true;
317 }
318
319 if ( version_compare( $GLOBALS['wp_version'], '5.0', '>' ) ) {
320 $block_editor = true;
321 }
322
323 if ( ! $gutenberg && ! $block_editor ) {
324 return false;
325 }
326
327 include_once ABSPATH . 'wp-admin/includes/plugin.php';
328
329 if ( ! is_plugin_active( 'classic-editor/classic-editor.php' ) ) {
330 return true;
331 }
332
333 $use_block_editor = ( get_option( 'classic-editor-replace' ) === 'no-replace' );
334
335 return $use_block_editor;
336 }
337