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