PluginProbe
WProofreader spell & grammar check plugin for WordPress / 3.2.0
WProofreader spell & grammar check plugin for WordPress v3.2.0
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 3.2.0, at webspellchecker.php

305 lines 9.0 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: 3.2.0
7 * Author: WebSpellChecker
8 * Author URI: https://webspellchecker.com/
9 * Text Domain: webspellchecker
10 * Domain Path: /languages
11 * Requires PHP: 7.4
12 * Requires at least: 6.3
13 * Tested up to: 6.9
14 */
15
16 if ( ! defined( 'ABSPATH' ) ) {
17 exit;
18 }
19
20 if ( ! class_exists( 'WProofreader', false ) ) {
21 final class WProofreader {
22
23 const TRIAL_CUSTOMER_ID = '1:cma3h3-HTiyU3-JL08g4-SRyuS1-a9c0F3-kH6Cu-OlMHS-thcSV2-HlGmv3-YzRCN2-qrKY42-uPc';
24 const DEFAULT_LANGUAGE = 'en_US';
25 const DEFAULT_BADGE_TOGGLE_OPTION = 'on';
26 const PLUGIN_VERSION = '3.2.0';
27
28 const SCRIPT_HANDLE_BUNDLE = 'wsc_bundle';
29 const SCRIPT_HANDLE_CONFIG = 'wsc_config';
30 const SCRIPT_HANDLE_ENV_CHECKER = 'wsc_environment_checker';
31 const SCRIPT_HANDLE_INSTANCE = 'wsc_instance';
32 const SCRIPT_HANDLE_GUTENBERG_ENV = 'wsc_gutenberg_environment';
33 const SCRIPT_HANDLE_CLASSIC_ENV = 'wsc_classic_environment';
34
35 const L10N_OBJECT_CONFIG = 'WSCProofreaderConfig';
36 const L10N_OBJECT_INSTANCE = 'ProofreaderInstance';
37 const L10N_OBJECT_SERVICE = 'WSCServiceConfig';
38
39 const SETTING_ENABLE_POSTS = 'enable_on_posts';
40 const SETTING_ENABLE_PAGES = 'enable_on_pages';
41 const SETTING_ENABLE_PRODUCTS = 'enable_on_products';
42 const SETTING_ENABLE_CATEGORIES = 'enable_on_categories';
43 const SETTING_ENABLE_TAGS = 'enable_on_tags';
44 const SETTING_ENABLE_ADMIN = 'enable_in_admin';
45 const SETTING_ENABLE_FRONTEND = 'enable_on_frontend';
46 const SETTING_LANGUAGE = 'slang';
47 const SETTING_BADGE_BUTTON = 'disable_badge_button';
48 const SETTING_CUSTOMER_ID = 'customer_id';
49
50 const OPTION_ON = 'on';
51 const OPTION_OFF = 'off';
52
53 const POST_TYPE_POST = 'post';
54 const POST_TYPE_PAGE = 'page';
55 const POST_TYPE_PRODUCT = 'product';
56 const POST_TYPE_WPSC_PRODUCT = 'wpsc-product';
57
58 /** @var self|null */
59 private static $instance = null;
60
61 /** @var WSC_Settings */
62 private $settings;
63
64 /** @var array */
65 private $options = array();
66
67 /** @var string */
68 private $plugin_url;
69
70 /** @var string */
71 private $plugin_dir;
72
73 /** @var WProofreader_Context_Detector */
74 private $context_detector;
75
76 /** @var WProofreader_Context_Evaluator */
77 private $context_evaluator;
78
79 /** @var WProofreader_Config_Builder */
80 private $config_builder;
81
82 /** @var WProofreader_Assets */
83 private $assets;
84
85 /**
86 * Retrieve singleton instance.
87 *
88 * @return self
89 */
90 public static function instance() {
91 if ( null === self::$instance ) {
92 self::$instance = new self();
93 }
94
95 return self::$instance;
96 }
97
98 private function __construct() {
99 $this->plugin_url = plugin_dir_url( __FILE__ );
100 $this->plugin_dir = plugin_dir_path( __FILE__ );
101
102 $this->include_dependencies();
103 $this->bootstrap_settings();
104 $this->maybe_migrate_version();
105 $this->bootstrap_services();
106 $this->register_hooks();
107
108 do_action( 'wsc_loaded' );
109 }
110
111 private function __clone() {}
112
113 public function __wakeup() {
114 throw new \RuntimeException( 'WProofreader cannot be unserialized.' );
115 }
116
117 /**
118 * Include required dependencies.
119 */
120 private function include_dependencies() {
121 require_once __DIR__ . '/vendor/class.settings-api.php';
122 require_once __DIR__ . '/includes/class-wsc-settings.php';
123 require_once __DIR__ . '/includes/class-wproofreader-context-detector.php';
124 require_once __DIR__ . '/includes/class-wproofreader-context-evaluator.php';
125 require_once __DIR__ . '/includes/class-wproofreader-config-builder.php';
126 require_once __DIR__ . '/includes/class-wproofreader-assets.php';
127 require_once __DIR__ . '/includes/class-wproofreader-ajax.php';
128 require_once __DIR__ . '/includes/class-wproofreader-content-cleaner.php';
129 }
130
131 /**
132 * Initialize settings and merge with defaults.
133 */
134 private function bootstrap_settings() {
135 $this->settings = new WSC_Settings(
136 __( 'WProofreader', 'webspellchecker' ),
137 __( 'WProofreader', 'webspellchecker' ),
138 'spell-checker-settings'
139 );
140
141 $default_options = array(
142 self::SETTING_ENABLE_POSTS => self::OPTION_ON,
143 self::SETTING_ENABLE_PAGES => self::OPTION_ON,
144 self::SETTING_ENABLE_PRODUCTS => self::OPTION_ON,
145 self::SETTING_ENABLE_CATEGORIES => self::OPTION_ON,
146 self::SETTING_ENABLE_TAGS => self::OPTION_ON,
147 self::SETTING_ENABLE_ADMIN => self::OPTION_OFF,
148 self::SETTING_ENABLE_FRONTEND => self::OPTION_OFF,
149 self::SETTING_LANGUAGE => self::DEFAULT_LANGUAGE,
150 self::SETTING_BADGE_BUTTON => self::DEFAULT_BADGE_TOGGLE_OPTION,
151 );
152
153 $stored_options = get_option( WSC_Settings::OPTION_NAME, array() );
154 $this->options = wp_parse_args( $stored_options, $default_options );
155 $this->options[ self::SETTING_CUSTOMER_ID ] = $this->get_customer_id();
156 }
157
158 /**
159 * Build collaborator objects.
160 */
161 private function bootstrap_services() {
162 $this->context_detector = new WProofreader_Context_Detector();
163 $this->context_evaluator = new WProofreader_Context_Evaluator( $this );
164 $this->config_builder = new WProofreader_Config_Builder( $this );
165 $this->assets = new WProofreader_Assets(
166 $this,
167 $this->context_detector,
168 $this->context_evaluator,
169 $this->config_builder,
170 $this->plugin_url,
171 $this->plugin_dir
172 );
173 }
174
175 /**
176 * Register WordPress hooks.
177 */
178 private function register_hooks() {
179 add_action( 'init', array( $this, 'load_textdomain' ) );
180 add_action( 'init', array( $this->assets, 'register_scripts' ) );
181 add_action( 'enqueue_block_assets', array( $this->assets, 'enqueue_for_block_editor' ), 110 );
182 add_action( 'admin_enqueue_scripts', array( $this->assets, 'enqueue_for_admin' ), 110 );
183 add_action( 'elementor/editor/after_enqueue_scripts', array( $this->assets, 'enqueue_for_elementor_editor' ), 110 );
184 add_action( 'wp_enqueue_scripts', array( $this->assets, 'enqueue_for_frontend' ), 10 );
185 add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'add_action_links' ) );
186 add_action( 'wp_ajax_get_proofreader_info_callback', array( 'WProofreader_Ajax', 'get_proofreader_info' ) );
187 add_action( 'init', array( 'WProofreader_Content_Cleaner', 'register' ) );
188 }
189
190 /**
191 * Load translations.
192 */
193 public function load_textdomain() {
194 load_plugin_textdomain( 'webspellchecker', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
195 }
196
197 /**
198 * Handle version migration and legacy cleanup.
199 *
200 * Runs in admin requests only so REST/cron/front-end loads never write options.
201 */
202 private function maybe_migrate_version() {
203 if ( ! is_admin() ) {
204 return;
205 }
206
207 if ( get_option( 'wsc_proofreader_version' ) !== self::PLUGIN_VERSION ) {
208 delete_option( 'wsc' );
209 update_option( 'wsc_proofreader_version', self::PLUGIN_VERSION );
210 }
211 }
212
213 /**
214 * Whether the current installation uses the Free trial service id.
215 *
216 * @return bool
217 */
218 public function is_free_edition(): bool {
219 return $this->get_customer_id() === self::TRIAL_CUSTOMER_ID;
220 }
221
222 /**
223 * Get customer ID or fall back to trial ID.
224 *
225 * @return string
226 */
227 public function get_customer_id(): string {
228 $customer_id = (string) $this->get_option( self::SETTING_CUSTOMER_ID, self::TRIAL_CUSTOMER_ID );
229
230 return ( '' === trim( $customer_id ) ) ? self::TRIAL_CUSTOMER_ID : $customer_id;
231 }
232
233 /**
234 * Get selected language with fallback.
235 *
236 * @return string
237 */
238 public function get_language(): string {
239 $language = (string) $this->get_option( self::SETTING_LANGUAGE, self::DEFAULT_LANGUAGE );
240
241 return ( '' === trim( $language ) ) ? self::DEFAULT_LANGUAGE : $language;
242 }
243
244 /**
245 * Get badge button option.
246 *
247 * @return string
248 */
249 public function get_badge_button_option(): string {
250 return (string) $this->get_option( self::SETTING_BADGE_BUTTON, self::DEFAULT_BADGE_TOGGLE_OPTION );
251 }
252
253 /**
254 * Get option value.
255 *
256 * @param string $name Option key.
257 * @param mixed $default Default value.
258 * @return mixed
259 */
260 public function get_option( $name, $default = '' ) {
261 return isset( $this->options[ $name ] ) ? $this->options[ $name ] : $default;
262 }
263
264 /**
265 * Get all normalized options.
266 *
267 * @return array
268 */
269 public function get_options(): array {
270 return $this->options;
271 }
272
273 /**
274 * Add settings link in plugins list.
275 *
276 * @param array $links Existing action links.
277 * @return array
278 */
279 public function add_action_links( $links ) {
280 $url = esc_url( add_query_arg( 'page', 'spell-checker-settings', admin_url( 'options-general.php' ) ) );
281
282 $settings_link = sprintf(
283 '<a href="%s">%s</a>',
284 $url,
285 esc_html__( 'Settings', 'webspellchecker' )
286 );
287
288 return array_merge( $links, array( $settings_link ) );
289 }
290 }
291 }
292
293 if ( ! function_exists( 'wsc_proofreader' ) ) {
294 /**
295 * Get singleton instance.
296 *
297 * @return WProofreader
298 */
299 function wsc_proofreader() {
300 return WProofreader::instance();
301 }
302 }
303
304 wsc_proofreader();
305