| 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.2 |
| 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 PLUGIN_VERSION = "2.2"; |
| 20 |
private static $instance = null; |
| 21 |
private $js_added = false; |
| 22 |
private $settings; |
| 23 |
protected $options; |
| 24 |
|
| 25 |
public static function instance() { |
| 26 |
if ( null == self::$instance ) { |
| 27 |
self::$instance = new self; |
| 28 |
} |
| 29 |
|
| 30 |
return self::$instance; |
| 31 |
} |
| 32 |
|
| 33 |
public function __construct() { |
| 34 |
$this->includes(); |
| 35 |
$this->settings = new WSC_Settings( |
| 36 |
__( 'WProofreader', 'webspellchecker' ), |
| 37 |
__( 'WProofreader', 'webspellchecker' ), |
| 38 |
'spell-checker-settings' |
| 39 |
); |
| 40 |
|
| 41 |
$this->options = ! empty( get_option( WSC_Settings::OPTION_NAME ) ) ? get_option( WSC_Settings::OPTION_NAME ) : array(); |
| 42 |
|
| 43 |
if ( empty( $this->options ) ) { |
| 44 |
//set default setting |
| 45 |
$this->options['enable_on_posts'] = 'on'; |
| 46 |
$this->options['enable_on_pages'] = 'on'; |
| 47 |
$this->options['enable_on_products'] = 'off'; |
| 48 |
$this->options['enable_on_categories'] = 'on'; |
| 49 |
$this->options['enable_on_tags'] = 'on'; |
| 50 |
update_option( 'wsc_proofreader_version', self::PLUGIN_VERSION ); |
| 51 |
} |
| 52 |
|
| 53 |
$this->options['customer_id'] = $this->get_customer_id(); |
| 54 |
|
| 55 |
add_action( 'admin_enqueue_scripts', array( $this, 'register_proofreader_scripts' ) ); |
| 56 |
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'add_action_links' ) ); |
| 57 |
add_action( 'admin_head', array( $this, 'init_proofreader' ) ); |
| 58 |
$this->check_version(); |
| 59 |
|
| 60 |
add_action( 'wp_ajax_get_proofreader_info_callback', array( $this, 'get_proofreader_info_callback' ) ); |
| 61 |
|
| 62 |
do_action( 'wsc_loaded' ); |
| 63 |
} |
| 64 |
|
| 65 |
public function check_version() { |
| 66 |
//todo add Class for upgrade plugin |
| 67 |
if ( get_option( 'wsc_proofreader_version' ) !== self::PLUGIN_VERSION ) { |
| 68 |
//Clear old options in versions below 2 |
| 69 |
delete_option( 'wsc' ); |
| 70 |
update_option( 'wsc_proofreader_version', self::PLUGIN_VERSION ); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
public function init_proofreader() { |
| 75 |
$editable_post_type = $this->get_editable_post_type(); |
| 76 |
$screen = get_current_screen(); |
| 77 |
if ( $screen->base === 'settings_page_spell-checker-settings' ) { |
| 78 |
$this->api_proofreader_info(); |
| 79 |
} |
| 80 |
|
| 81 |
foreach ( $this->options as $option => $on ) { |
| 82 |
if ( $option === 'enable_on_categories' && $on === 'on' ) { |
| 83 |
if ( $screen->id === 'edit-category' |
| 84 |
|| $screen->id === 'edit-product_cat' |
| 85 |
|| $screen->id === 'edit-wpsc_product_category' ) { |
| 86 |
$this->init_proofreader_js(); |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
foreach ( $this->options as $option => $on ) { |
| 92 |
if ( $option === 'enable_on_tags' && $on === 'on' ) { |
| 93 |
if ( $screen->id === 'edit-product_tag' |
| 94 |
|| $screen->id === 'edit-post_tag' ) { |
| 95 |
$this->init_proofreader_js(); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
if ( false !== $editable_post_type ) { |
| 101 |
|
| 102 |
foreach ( $this->options as $option => $on ) { |
| 103 |
|
| 104 |
if ( $option === 'enable_on_posts' && $on === 'on' ) { |
| 105 |
|
| 106 |
if ( 0 === strcasecmp( 'post', $editable_post_type ) ) { |
| 107 |
$this->init_proofreader_js(); |
| 108 |
} |
| 109 |
|
| 110 |
break; |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
foreach ( $this->options as $option => $on ) { |
| 117 |
|
| 118 |
if ( $option === 'enable_on_pages' && $on === 'on' ) { |
| 119 |
|
| 120 |
if ( 0 === strcasecmp( 'page', $editable_post_type ) ) { |
| 121 |
$this->init_proofreader_js(); |
| 122 |
} |
| 123 |
|
| 124 |
break; |
| 125 |
|
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
foreach ( $this->options as $option => $on ) { |
| 130 |
|
| 131 |
if ( $option === 'enable_on_products' && $on === 'on' ) { |
| 132 |
if ( $screen->id === 'wpsc-product' ) { |
| 133 |
$this->init_proofreader_js(); |
| 134 |
} |
| 135 |
|
| 136 |
if ( $screen->id === 'edit-product_cat' ) { |
| 137 |
$this->init_proofreader_js(); |
| 138 |
} |
| 139 |
if ( $screen->id === 'edit-product_tag' ) { |
| 140 |
|
| 141 |
$this->init_proofreader_js(); |
| 142 |
} |
| 143 |
if ( 0 === strcasecmp( 'product', $editable_post_type ) ) { |
| 144 |
$this->init_proofreader_js(); |
| 145 |
} |
| 146 |
|
| 147 |
break; |
| 148 |
|
| 149 |
} |
| 150 |
|
| 151 |
} |
| 152 |
|
| 153 |
return $this->js_added = true; |
| 154 |
} |
| 155 |
|
| 156 |
return $this->js_added = false; |
| 157 |
} |
| 158 |
|
| 159 |
public function includes() { |
| 160 |
require_once dirname( __FILE__ ) . '/vendor/class.settings-api.php'; |
| 161 |
require_once dirname( __FILE__ ) . '/includes/class-wsc-settings.php'; |
| 162 |
} |
| 163 |
|
| 164 |
public function get_editable_post_type() { |
| 165 |
$screen = get_current_screen(); |
| 166 |
|
| 167 |
if ( $screen->post_type === $screen->id ) { |
| 168 |
$post_type = $screen->post_type; |
| 169 |
|
| 170 |
return $post_type; |
| 171 |
} |
| 172 |
|
| 173 |
return false; |
| 174 |
} |
| 175 |
|
| 176 |
function add_action_links( $links ) { |
| 177 |
$mylinks = array( |
| 178 |
'<a href="' . admin_url( 'options-general.php?page=spell-checker-settings' ) . '">' . __( 'Settings', 'webspellchecker' ) . '</a>', |
| 179 |
); |
| 180 |
|
| 181 |
return array_merge( $links, $mylinks ); |
| 182 |
} |
| 183 |
|
| 184 |
function register_proofreader_scripts() { |
| 185 |
wp_register_script( 'wscbundle', 'https://svc.webspellchecker.net/spellcheck31/wscbundle/wscbundle.js', array(), '081120181109', false ); |
| 186 |
wp_register_script( 'ProofreaderConfig', plugin_dir_url( __FILE__ ) . '/assets/proofreaderConfig.js', array( 'wscbundle' ), '081120181110', true ); |
| 187 |
wp_register_script( 'ProofreaderInstance', plugin_dir_url( __FILE__ ) . '/assets/instance.js', array( 'wscbundle' ), '171220181251', true ); |
| 188 |
} |
| 189 |
|
| 190 |
function init_proofreader_js() { |
| 191 |
$key_for_proofreader = $this->get_customer_id(); |
| 192 |
$slang = $this->get_slang(); |
| 193 |
$settingsSections = ( $this->get_customer_id() === self::TRIAL_CUSTOMER_ID ) ? |
| 194 |
[ 'options', 'languages', 'about' ] |
| 195 |
: [ 'options', 'languages', 'dictionaries', 'about' ]; |
| 196 |
$enableGrammar = ( $this->get_customer_id() === self::TRIAL_CUSTOMER_ID ) ? 'false' : 'true'; |
| 197 |
$wsc_proofreader_config = array( |
| 198 |
'key_for_proofreader' => $key_for_proofreader, |
| 199 |
'slang' => $slang, |
| 200 |
'settingsSections' => $settingsSections, |
| 201 |
'enableGrammar' => $enableGrammar |
| 202 |
); |
| 203 |
wp_enqueue_script( 'wscbundle' ); |
| 204 |
wp_enqueue_script( 'ProofreaderConfig' ); |
| 205 |
wp_localize_script( 'ProofreaderConfig', 'WSCProofreaderConfig', $wsc_proofreader_config ); |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Get customer ID or trial ID if not set |
| 210 |
* |
| 211 |
* @return string customer ID |
| 212 |
*/ |
| 213 |
public function get_customer_id() { |
| 214 |
$customer_id = $this->get_option( 'customer_id', self::TRIAL_CUSTOMER_ID ); |
| 215 |
|
| 216 |
if ( empty( $customer_id ) ) { |
| 217 |
return self::TRIAL_CUSTOMER_ID; |
| 218 |
} |
| 219 |
|
| 220 |
return $customer_id; |
| 221 |
} |
| 222 |
|
| 223 |
public function get_slang() { |
| 224 |
$slang = $this->get_option( 'slang', self::SLANG ); |
| 225 |
|
| 226 |
if ( empty( $slang ) ) { |
| 227 |
return self::SLANG; |
| 228 |
} |
| 229 |
|
| 230 |
return $slang; |
| 231 |
} |
| 232 |
|
| 233 |
public function get_option( $name, $default = '' ) { |
| 234 |
return ( isset( $this->options[ $name ] ) ) ? $this->options[ $name ] : $default; |
| 235 |
} |
| 236 |
|
| 237 |
function api_proofreader_info() { |
| 238 |
$ajax_nonce = wp_create_nonce( "webspellchecker-proofreader" ); |
| 239 |
$key_for_proofreader = $this->get_customer_id(); |
| 240 |
$slang = $this->get_slang(); |
| 241 |
$enableGrammar = ( $this->get_customer_id() === self::TRIAL_CUSTOMER_ID ) ? 'false' : 'true'; |
| 242 |
$wsc_proofreader_config = array( |
| 243 |
'key_for_proofreader' => $key_for_proofreader, |
| 244 |
'slang' => $slang, |
| 245 |
'ajax_nonce' => $ajax_nonce, |
| 246 |
'enableGrammar' => $enableGrammar |
| 247 |
); |
| 248 |
wp_enqueue_script( 'wscbundle' ); |
| 249 |
wp_enqueue_script( 'ProofreaderInstance' ); |
| 250 |
wp_localize_script( 'ProofreaderInstance', 'ProofreaderInstance', $wsc_proofreader_config ); |
| 251 |
} |
| 252 |
|
| 253 |
function get_proofreader_info_callback() { |
| 254 |
$current_lang = $this->get_slang(); |
| 255 |
check_ajax_referer( 'webspellchecker-proofreader', 'security' ); |
| 256 |
$proofreader_info = $_POST['getInfoResult']; |
| 257 |
update_option( 'wsc_proofreader_info', $proofreader_info ); |
| 258 |
ob_start(); |
| 259 |
?> |
| 260 |
<select class="regular" name="wsc_proofreader[slang]" id="wsc_proofreader[slang]"> |
| 261 |
<?php foreach ( $proofreader_info['langList']['ltr'] as $key => $value ): ?> |
| 262 |
<option <?php if ( $current_lang === $key ) { |
| 263 |
echo 'selected'; |
| 264 |
} ?> value="<?php echo $key; ?>"><?php echo $value; ?></option> |
| 265 |
<?php endforeach; ?> |
| 266 |
</select> |
| 267 |
<?php |
| 268 |
wp_send_json( ob_get_clean() ); |
| 269 |
wp_die(); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
function WSC() { |
| 274 |
return WProofreader::instance(); |
| 275 |
} |
| 276 |
|
| 277 |
if ( is_admin() ) { |
| 278 |
WSC(); |
| 279 |
} |
| 280 |
|