PluginProbe
WProofreader spell & grammar check plugin for WordPress / 2.7.1
WProofreader spell & grammar check plugin for WordPress v2.7.1
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.7.1, at webspellchecker.php

366 lines 11.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: WProofreader checks spelling, grammar, and style in real-time while editing in WordPress.
6 * Version: 2.7.1
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 = 'on';
20 const PLUGIN_VERSION = "2.7.1";
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 $additionalCPT = apply_filters( 'wproofreader_add_cpt', $CPT = array() );
150
151 if ( ! empty( $additionalCPT ) ) {
152
153 foreach ( $additionalCPT as $key => $value ) {
154 if ( 0 === strcasecmp( $value, $editable_post_type ) ) {
155 $this->init_proofreader_js();
156 }
157 }
158
159 }
160
161 return $this->js_added = true;
162 }
163
164 return $this->js_added = false;
165 }
166
167 public function includes() {
168 require_once dirname( __FILE__ ) . '/vendor/class.settings-api.php';
169 require_once dirname( __FILE__ ) . '/includes/class-wsc-settings.php';
170 }
171
172 public function get_editable_post_type() {
173 $screen = get_current_screen();
174
175 if ( $screen->post_type === $screen->id ) {
176 $post_type = $screen->post_type;
177
178 return $post_type;
179 }
180
181 return false;
182 }
183
184 function add_action_links( $links ) {
185 $mylinks = array(
186 '<a href="' . admin_url( 'options-general.php?page=spell-checker-settings' ) . '">' . __( 'Settings', 'webspellchecker' ) . '</a>',
187 );
188
189 return array_merge( $links, $mylinks );
190 }
191
192 function register_proofreader_scripts() {
193 wp_register_script( 'wscbundle', 'https://svc.webspellchecker.net/spellcheck31/wscbundle/wscbundle.js', array(), '23090950', false );
194 wp_register_script( 'ProofreaderConfig', plugin_dir_url( __FILE__ ) . '/assets/proofreaderConfig.js', array( 'wscbundle' ), '23090950', true );
195 wp_register_script( 'ProofreaderInstance', plugin_dir_url( __FILE__ ) . '/assets/instance.js', array( 'wscbundle' ), '23090950', true );
196 wp_register_script( 'GutenbergEnvironment', plugin_dir_url( __FILE__ ) . '/assets/gutenberg-environment.js', array(
197 'ProofreaderConfig',
198 'wp-blocks'
199 ), '171220181251', true );
200 }
201
202 function init_proofreader_js() {
203 global $current_screen;
204
205 $key_for_proofreader = $this->get_customer_id();
206 $slang = $this->get_slang();
207 $settingsSections = ( $this->get_customer_id() === self::TRIAL_CUSTOMER_ID ) ?
208 [ 'options', 'languages', 'about', 'general' ]
209 : [ 'options', 'languages', 'dictionaries', 'about', 'general' ];
210 $enableGrammar = ( $this->get_customer_id() === self::TRIAL_CUSTOMER_ID ) ? 'false' : 'true';
211 $badge_button_optinon = ( $this->get_badge_button_optinon() === self::BADGE_BUTTON ) ? 'true' : 'false';
212 $wsc_proofreader_config = array(
213 'key_for_proofreader' => $key_for_proofreader,
214 'slang' => $slang,
215 'settingsSections' => $settingsSections,
216 'enableGrammar' => $enableGrammar,
217 'disableBadgeButton' => $badge_button_optinon,
218 );
219 wp_enqueue_script( 'wscbundle' );
220 wp_enqueue_script( 'ProofreaderConfig' );
221 $current_screen = get_current_screen();
222 if ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() ) {
223 wp_enqueue_script( 'GutenbergEnvironment' );
224 }
225
226 //todo additional module with activation to gutenberg
227 //gutenberg environment
228 wp_localize_script( 'ProofreaderConfig', 'WSCProofreaderConfig', $wsc_proofreader_config );
229 }
230
231 /**
232 * Get customer ID or trial ID if not set
233 *
234 * @return string customer ID
235 */
236 public function get_customer_id() {
237 $customer_id = $this->get_option( 'customer_id', self::TRIAL_CUSTOMER_ID );
238
239 if ( empty( $customer_id ) ) {
240 return self::TRIAL_CUSTOMER_ID;
241 }
242
243 return $customer_id;
244 }
245
246 public function get_slang() {
247 $slang = $this->get_option( 'slang', self::SLANG );
248
249 if ( empty( $slang ) ) {
250 return self::SLANG;
251 }
252
253 return $slang;
254 }
255
256 public function get_badge_button_optinon() {
257 $badge_button_optinon = $this->get_option( 'disable_badge_button', self::BADGE_BUTTON );
258
259 return $badge_button_optinon;
260 }
261
262 public function get_option( $name, $default = '' ) {
263 return ( isset( $this->options[ $name ] ) ) ? $this->options[ $name ] : $default;
264 }
265
266 function api_proofreader_info() {
267 $ajax_nonce = wp_create_nonce( "webspellchecker-proofreader" );
268 $key_for_proofreader = $this->get_customer_id();
269 $slang = $this->get_slang();
270 $enableGrammar = ( $this->get_customer_id() === self::TRIAL_CUSTOMER_ID ) ? 'false' : 'true';
271 $wsc_proofreader_config = array(
272 'key_for_proofreader' => $key_for_proofreader,
273 'slang' => $slang,
274 'ajax_nonce' => $ajax_nonce,
275 'enableGrammar' => $enableGrammar
276 );
277 wp_enqueue_script( 'wscbundle' );
278 wp_enqueue_script( 'ProofreaderInstance' );
279 wp_localize_script( 'ProofreaderInstance', 'ProofreaderInstance', $wsc_proofreader_config );
280 }
281
282
283 function get_proofreader_info_callback() {
284 $current_lang = $this->get_slang();
285 check_ajax_referer( 'webspellchecker-proofreader', 'security' );
286 $proofreader_info = $_POST['getInfoResult'];
287 update_option( 'wsc_proofreader_info', $proofreader_info );
288 $languages = array_merge(
289 $proofreader_info['langList']['ltr'] ?? [],
290 $proofreader_info['langList']['rtl'] ?? []
291 );
292 ob_start();
293 ?>
294 <select class="regular" name="wsc_proofreader[slang]" id="wsc_proofreader[slang]">
295 <?php foreach ( $languages as $key => $value ): ?>
296 <option <?php if ( $current_lang === $key ) {
297 echo 'selected';
298 } ?> value="<?php echo $key; ?>"><?php echo $value; ?></option>
299 <?php endforeach; ?>
300 </select>
301 <?php
302 wp_send_json( ob_get_clean() );
303 wp_die();
304 }
305
306 public static function fix_for_gutenberg() {
307 add_action( 'wp_insert_post_data', function ( $data, $postarr ) {
308 if ( 'publish' == $data['post_status'] ) {
309 $string = $data['post_content'];
310 $new_string = preg_replace( '#(<span class=."wsc-spelling-problem." .*?>)(.*?)(</span>)#', '$2', $string );
311 $new_string = preg_replace( '#(<span class=."wsc-grammar-problem." .*?>)(.*?)(</span>)#', '$2', $new_string );
312 $new_string = preg_replace( '#(<span class=."rangySelectionBoundary." .*?>)(.*?)(</span>)#', '$2', $new_string );
313 $new_string = preg_replace( '#(<span class="wsc-spelling-problem".*?>)(.*?)(</span>)#', '$2', $new_string );
314 $new_string = preg_replace( '#(<span class="wsc-grammar-problem" .*?>)(.*?)(</span>)#', '$2', $new_string );
315 $new_string = preg_replace( '#(<span class="rangySelectionBoundary" .*?>)(.*?)(</span>)#', '$2', $new_string );
316 $new_string = preg_replace( '#(<span class=..rangySelectionBoundary.. .*?>)(.*?)(</span>)#', '$2', $new_string );
317 $data['post_content'] = $new_string;
318 }
319
320 return $data;
321 }, 100, 2 );
322 }
323 }
324
325 function WSC() {
326 return WProofreader::instance();
327 }
328
329 if ( is_admin() ) {
330 WSC();
331 }
332 /**
333 * fix for Gutenberg
334 * todo write more cleaner
335 */
336 if ( true === is_gutenberg_active() ) {
337 WProofreader::fix_for_gutenberg();
338 };
339
340 function is_gutenberg_active() {
341 $gutenberg = false;
342 $block_editor = false;
343
344 if ( has_filter( 'replace_editor', 'gutenberg_init' ) ) {
345 $gutenberg = true;
346 }
347
348 if ( version_compare( $GLOBALS['wp_version'], '5.0', '>' ) ) {
349 $block_editor = true;
350 }
351
352 if ( ! $gutenberg && ! $block_editor ) {
353 return false;
354 }
355
356 include_once ABSPATH . 'wp-admin/includes/plugin.php';
357
358 if ( ! is_plugin_active( 'classic-editor/classic-editor.php' ) ) {
359 return true;
360 }
361
362 $use_block_editor = ( get_option( 'classic-editor-replace' ) === 'no-replace' );
363
364 return $use_block_editor;
365 }
366