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

353 lines 10.9 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.6
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.5";
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(), '081120181109', false );
194 wp_register_script( 'ProofreaderConfig', plugin_dir_url( __FILE__ ) . '/assets/proofreaderConfig.js', array( 'wscbundle' ), '081120181110', true );
195 wp_register_script( 'ProofreaderInstance', plugin_dir_url( __FILE__ ) . '/assets/instance.js', array( 'wscbundle' ), '171220181251', true );
196 wp_register_script( 'GutenbergEnvironment', plugin_dir_url( __FILE__ ) . '/assets/gutenberg-environment.js', array( 'ProofreaderConfig','wp-blocks' ), '171220181251', true );
197 }
198
199 function init_proofreader_js() {
200 $key_for_proofreader = $this->get_customer_id();
201 $slang = $this->get_slang();
202 $settingsSections = ( $this->get_customer_id() === self::TRIAL_CUSTOMER_ID ) ?
203 [ 'options', 'languages', 'about' ]
204 : [ 'options', 'languages', 'dictionaries', 'about' ];
205 $enableGrammar = ( $this->get_customer_id() === self::TRIAL_CUSTOMER_ID ) ? 'false' : 'true';
206 $badge_button_optinon = ( $this->get_badge_button_optinon() === self::BADGE_BUTTON ) ? 'false' : 'true';
207 $wsc_proofreader_config = array(
208 'key_for_proofreader' => $key_for_proofreader,
209 'slang' => $slang,
210 'settingsSections' => $settingsSections,
211 'enableGrammar' => $enableGrammar,
212 'disableBadgeButton' => $badge_button_optinon,
213 );
214 wp_enqueue_script( 'wscbundle' );
215 wp_enqueue_script( 'ProofreaderConfig' );
216 wp_enqueue_script( 'GutenbergEnvironment' );
217 //todo additional module with activation to gutenberg
218 //gutenberg environment
219 wp_localize_script( 'ProofreaderConfig', 'WSCProofreaderConfig', $wsc_proofreader_config );
220 }
221
222 /**
223 * Get customer ID or trial ID if not set
224 *
225 * @return string customer ID
226 */
227 public function get_customer_id() {
228 $customer_id = $this->get_option( 'customer_id', self::TRIAL_CUSTOMER_ID );
229
230 if ( empty( $customer_id ) ) {
231 return self::TRIAL_CUSTOMER_ID;
232 }
233
234 return $customer_id;
235 }
236
237 public function get_slang() {
238 $slang = $this->get_option( 'slang', self::SLANG );
239
240 if ( empty( $slang ) ) {
241 return self::SLANG;
242 }
243
244 return $slang;
245 }
246
247 public function get_badge_button_optinon() {
248 $badge_button_optinon = $this->get_option( 'disable_badge_button', self::BADGE_BUTTON );
249
250 return $badge_button_optinon;
251 }
252
253 public function get_option( $name, $default = '' ) {
254 return ( isset( $this->options[ $name ] ) ) ? $this->options[ $name ] : $default;
255 }
256
257 function api_proofreader_info() {
258 $ajax_nonce = wp_create_nonce( "webspellchecker-proofreader" );
259 $key_for_proofreader = $this->get_customer_id();
260 $slang = $this->get_slang();
261 $enableGrammar = ( $this->get_customer_id() === self::TRIAL_CUSTOMER_ID ) ? 'false' : 'true';
262 $wsc_proofreader_config = array(
263 'key_for_proofreader' => $key_for_proofreader,
264 'slang' => $slang,
265 'ajax_nonce' => $ajax_nonce,
266 'enableGrammar' => $enableGrammar
267 );
268 wp_enqueue_script( 'wscbundle' );
269 wp_enqueue_script( 'ProofreaderInstance' );
270 wp_localize_script( 'ProofreaderInstance', 'ProofreaderInstance', $wsc_proofreader_config );
271 }
272
273
274 function get_proofreader_info_callback() {
275 $current_lang = $this->get_slang();
276 check_ajax_referer( 'webspellchecker-proofreader', 'security' );
277 $proofreader_info = $_POST['getInfoResult'];
278 update_option( 'wsc_proofreader_info', $proofreader_info );
279 ob_start();
280 ?>
281 <select class="regular" name="wsc_proofreader[slang]" id="wsc_proofreader[slang]">
282 <?php foreach ( $proofreader_info['langList']['ltr'] as $key => $value ): ?>
283 <option <?php if ( $current_lang === $key ) {
284 echo 'selected';
285 } ?> value="<?php echo $key; ?>"><?php echo $value; ?></option>
286 <?php endforeach; ?>
287 </select>
288 <?php
289 wp_send_json( ob_get_clean() );
290 wp_die();
291 }
292
293 public static function fix_for_gutenberg() {
294 add_action( 'wp_insert_post_data', function ( $data, $postarr ) {
295 if ( 'publish' == $data['post_status'] ) {
296 $string = $data['post_content'];
297 $new_string = preg_replace( '#(<span class=."wsc-spelling-problem." .*?>)(.*?)(</span>)#', '$2', $string );
298 $new_string = preg_replace( '#(<span class=."wsc-grammar-problem." .*?>)(.*?)(</span>)#', '$2', $new_string );
299 $new_string = preg_replace( '#(<span class=."rangySelectionBoundary." .*?>)(.*?)(</span>)#', '$2', $new_string );
300 $new_string = preg_replace( '#(<span class="wsc-spelling-problem".*?>)(.*?)(</span>)#', '$2', $new_string );
301 $new_string = preg_replace( '#(<span class="wsc-grammar-problem" .*?>)(.*?)(</span>)#', '$2', $new_string );
302 $new_string = preg_replace( '#(<span class="rangySelectionBoundary" .*?>)(.*?)(</span>)#', '$2', $new_string );
303 $new_string = preg_replace( '#(<span class=..rangySelectionBoundary.. .*?>)(.*?)(</span>)#', '$2', $new_string );
304 $data['post_content'] = $new_string;
305 }
306
307 return $data;
308 }, 100, 2 );
309 }
310 }
311
312 function WSC() {
313 return WProofreader::instance();
314 }
315
316 if ( is_admin() ) {
317 WSC();
318 }
319 /**
320 * fix for Gutenberg
321 * todo write more cleaner
322 */
323 if ( true === is_gutenberg_active() ) {
324 WProofreader::fix_for_gutenberg();
325 };
326
327 function is_gutenberg_active() {
328 $gutenberg = false;
329 $block_editor = false;
330
331 if ( has_filter( 'replace_editor', 'gutenberg_init' ) ) {
332 $gutenberg = true;
333 }
334
335 if ( version_compare( $GLOBALS['wp_version'], '5.0', '>' ) ) {
336 $block_editor = true;
337 }
338
339 if ( ! $gutenberg && ! $block_editor ) {
340 return false;
341 }
342
343 include_once ABSPATH . 'wp-admin/includes/plugin.php';
344
345 if ( ! is_plugin_active( 'classic-editor/classic-editor.php' ) ) {
346 return true;
347 }
348
349 $use_block_editor = ( get_option( 'classic-editor-replace' ) === 'no-replace' );
350
351 return $use_block_editor;
352 }
353