Completed
Push — add/staging-display ( f17340...7d47df )
by
unknown
11:26
created

modules/after-the-deadline.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Module Name: Spelling and Grammar
4
 * Module Description: Check your spelling, style, and grammar with the After the Deadline proofreading service.
5
 * Sort Order: 6
6
 * First Introduced: 1.1
7
 * Requires Connection: Yes
8
 * Auto Activate: Yes
9
 * Module Tags: Writing
10
 * Additional Search Queries: after the deadline, afterthedeadline, spell, spellchecker, spelling, grammar, proofreading, style, language, cliche
11
 */
12
13
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
14
	// This wpcom-specific code should eventually be moved elsewhere.
15
16
	function AtD_http_post_timeout_action() {
17
		return 5;
18
	}
19
	add_filter( 'atd_http_post_timeout', 'AtD_http_post_timeout_action' );
20
	function AtD_http_post_error_action( $code ) {
21
		/** This action is documented in modules/widgets/social-media-icons.php */
22
		do_action( 'jetpack_bump_stats_extras', 'atd-remote-error', $code );
23
	}
24
	add_action( 'atd_http_post_error', 'AtD_http_post_error_action' );
25
	function AtD_service_domain_action() {
26
		return 'en.service.afterthedeadline.com';
27
	}
28
	add_filter( 'atd_service_domain', 'AtD_service_domain_action' );
29
	function AtD_update_setting( $user_id, $name, $value ) {
30
		update_user_attribute( $user_id, $name, $value );
31
	}
32
	function AtD_get_setting( $user_id, $name, $single = true ) {
33
		return get_user_attribute( $user_id, $name );
34
	}
35
	function AtD_get_rpc_id() {
36
		return get_bloginfo( 'wpurl' );
37
	}
38
} else {
39
	// This code is used only in Jetpack.
40
41
	add_action( 'jetpack_modules_loaded', 'AtD_load' );
42
	function AtD_load() {
43
		Jetpack::enable_module_configurable( __FILE__ );
44
		Jetpack::module_configuration_load( __FILE__, 'AtD_configuration_load' );
45
	}
46
	function AtD_configuration_load() {
47
		wp_safe_redirect( get_edit_profile_url( get_current_user_id() ) . '#atd' );
48
		exit;
49
	}
50
	function AtD_update_setting( $user_id, $name, $value ) {
0 ignored issues
show
The function AtD_update_setting() has been defined more than once; this definition is ignored, only the first definition in this file (L29-31) is considered.

This check looks for functions that have already been defined in the same file.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
51
		update_user_meta( $user_id, $name, $value );
52
	}
53
	function AtD_get_setting( $user_id, $name, $single = true ) {
0 ignored issues
show
The function AtD_get_setting() has been defined more than once; this definition is ignored, only the first definition in this file (L32-34) is considered.

This check looks for functions that have already been defined in the same file.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
54
		return get_user_meta( $user_id, $name, $single );
55
	}
56
	function AtD_get_rpc_id() {
0 ignored issues
show
The function AtD_get_rpc_id() has been defined more than once; this definition is ignored, only the first definition in this file (L35-37) is considered.

This check looks for functions that have already been defined in the same file.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
57
		return 'WPORG-' . md5( get_bloginfo( 'wpurl ') );
58
	}
59
}
60
61
/*
62
 *  Load necessary include files
63
 */
64
include( dirname( __FILE__ ) . '/after-the-deadline/config-options.php' );
65
include( dirname( __FILE__ ) . '/after-the-deadline/config-unignore.php' );
66
include( dirname( __FILE__ ) . '/after-the-deadline/proxy.php' );
67
68
define( 'ATD_VERSION', '20150715' );
69
70
/*
71
 * Display the AtD configuration options
72
 */
73
function AtD_config() {
74
	AtD_display_options_form();
75
	AtD_display_unignore_form();
76
}
77
78
/*
79
 *  Code to update the toolbar with the AtD Button and Install the AtD TinyMCE Plugin
80
 */
81
function AtD_addbuttons() {
82
	/* Don't bother doing this stuff if the current user lacks permissions */
83
	if ( ! AtD_is_allowed() )
84
		return;
85
86
	if ( ! defined( 'ATD_TINYMCE_4' ) ) {
87
		define( 'ATD_TINYMCE_4', ( ! empty( $GLOBALS['tinymce_version'] ) && substr( $GLOBALS['tinymce_version'], 0, 1 ) >= 4 ) );
88
	}
89
90
	/* Add only in Rich Editor mode */
91
	if ( get_user_option( 'rich_editing' ) == 'true' ) {
92
		add_filter( 'mce_external_plugins', 'add_AtD_tinymce_plugin' );
93
		add_filter( 'mce_buttons', 'register_AtD_button' );
94
	}
95
96
	add_action( 'personal_options_update', 'AtD_process_options_update' );
97
	add_action( 'personal_options_update', 'AtD_process_unignore_update' );
98
	add_action( 'profile_personal_options', 'AtD_config' );
99
}
100
101
/*
102
 * Hook into the TinyMCE buttons and replace the current spellchecker
103
 */
104
function register_AtD_button( $buttons ) {
105
	if ( ATD_TINYMCE_4 ) {
106
		// Use the default icon in TinyMCE 4.0 (replaced by dashicons in editor.css)
107
		if ( ! in_array( 'spellchecker', $buttons, true ) ) {
108
			$buttons[] = 'spellchecker';
109
		}
110
111
		return $buttons;
112
	}
113
114
	/* kill the spellchecker.. don't need no steenkin PHP spell checker */
115
	foreach ( $buttons as $key => $button ) {
116
		if ( $button == 'spellchecker' ) {
117
			$buttons[$key] = 'AtD';
118
			return $buttons;
119
		}
120
	}
121
122
	/* hrm... ok add us last plz */
123
	array_push( $buttons, '|', 'AtD' );
124
	return $buttons;
125
}
126
127
/*
128
 * Load the TinyMCE plugin : editor_plugin.js (TinyMCE 3.x) | plugin.js (TinyMCE 4.0)
129
 */
130
function add_AtD_tinymce_plugin( $plugin_array ) {
131
	$plugin = ATD_TINYMCE_4 ? 'plugin' : 'editor_plugin';
132
133
	$plugin_array['AtD'] = plugins_url( 'after-the-deadline/tinymce/' . $plugin . '.js?v=' . ATD_VERSION, __FILE__ );
134
	return $plugin_array;
135
}
136
137
/*
138
 * Update the TinyMCE init block with AtD specific settings
139
 */
140
function AtD_change_mce_settings( $init_array ) {
141
	if ( ! AtD_is_allowed() )
142
		return $init_array;
143
144
	if ( ! is_array( $init_array ) )
145
		$init_array = array();
146
147
	$user = wp_get_current_user();
148
149
	$init_array['atd_rpc_url']        = admin_url( 'admin-ajax.php?action=proxy_atd&_wpnonce=' . wp_create_nonce( 'proxy_atd' ) . '&url=' );
150
	$init_array['atd_ignore_rpc_url'] = admin_url( 'admin-ajax.php?action=atd_ignore&_wpnonce=' . wp_create_nonce( 'atd_ignore' ) . '&phrase=' );
151
	$init_array['atd_rpc_id']         = AtD_get_rpc_id();
152
	$init_array['atd_theme']          = 'wordpress';
153
	$init_array['atd_ignore_enable']  = 'true';
154
	$init_array['atd_strip_on_get']   = 'true';
155
	$init_array['atd_ignore_strings'] = json_encode( explode( ',',  AtD_get_setting( $user->ID, 'AtD_ignored_phrases' ) ) );
156
	$init_array['atd_show_types']     = AtD_get_setting( $user->ID, 'AtD_options' );
157
	$init_array['gecko_spellcheck']   = 'false';
158
159
	return $init_array;
160
}
161
162
/*
163
 * Sanitizes AtD AJAX data to acceptable chars, caller needs to make sure ' is escaped
164
 */
165
function AtD_sanitize( $untrusted ) {
166
	return preg_replace( '/[^a-zA-Z0-9\-\',_ ]/i', "", $untrusted );
167
}
168
169
/*
170
 * AtD HTML Editor Stuff
171
 */
172
function AtD_settings() {
173
	$user = wp_get_current_user();
174
175
	header( 'Content-Type: text/javascript' );
176
177
	/* set the RPC URL for AtD */
178
	echo "AtD.rpc = " . json_encode( esc_url_raw( admin_url( 'admin-ajax.php?action=proxy_atd&_wpnonce=' . wp_create_nonce( 'proxy_atd' ) . '&url=' ) ) ) . ";\n";
179
180
	/* set the API key for AtD */
181
	echo "AtD.api_key = " . json_encode( AtD_get_rpc_id() ) . ";\n";
182
183
	/* set the ignored phrases for AtD */
184
	echo "AtD.setIgnoreStrings(" . json_encode( AtD_get_setting( $user->ID, 'AtD_ignored_phrases' ) ) . ");\n";
185
186
	/* honor the types we want to show */
187
	echo "AtD.showTypes(" . json_encode( AtD_get_setting( $user->ID, 'AtD_options' ) ) .");\n";
188
189
	/* this is not an AtD/jQuery setting but I'm putting it in AtD to make it easy for the non-viz plugin to find it */
190
	$admin_ajax_url = admin_url( 'admin-ajax.php?action=atd_ignore&_wpnonce=' . wp_create_nonce( 'atd_ignore' ) . '&phrase=' );
191
	echo "AtD.rpc_ignore = " . json_encode( esc_url_raw( $admin_ajax_url ) ) . ";\n";
192
193
	die;
194
}
195
196
function AtD_load_javascripts() {
197
	if ( AtD_should_load_on_page() ) {
198
		wp_enqueue_script( 'AtD_core', plugins_url( '/after-the-deadline/atd.core.js', __FILE__ ), array(), ATD_VERSION );
199
		wp_enqueue_script( 'AtD_quicktags', plugins_url( '/after-the-deadline/atd-nonvis-editor-plugin.js', __FILE__ ), array('quicktags'), ATD_VERSION );
200
		wp_enqueue_script( 'AtD_jquery', plugins_url( '/after-the-deadline/jquery.atd.js', __FILE__ ), array('jquery'), ATD_VERSION );
201
		wp_enqueue_script( 'AtD_settings', admin_url() . 'admin-ajax.php?action=atd_settings', array('AtD_jquery'), ATD_VERSION );
202
		wp_enqueue_script( 'AtD_autoproofread', plugins_url( '/after-the-deadline/atd-autoproofread.js', __FILE__ ), array('AtD_jquery'), ATD_VERSION );
203
204
		/* load localized strings for AtD */
205
		wp_localize_script( 'AtD_core', 'AtD_l10n_r0ar', array (
206
			'menu_title_spelling'         => __( 'Spelling', 'jetpack' ),
207
			'menu_title_repeated_word'    => __( 'Repeated Word', 'jetpack' ),
208
209
			'menu_title_no_suggestions'   => __( 'No suggestions', 'jetpack' ),
210
211
			'menu_option_explain'         => __( 'Explain...', 'jetpack' ),
212
			'menu_option_ignore_once'     => __( 'Ignore suggestion', 'jetpack' ),
213
			'menu_option_ignore_always'   => __( 'Ignore always', 'jetpack' ),
214
			'menu_option_ignore_all'      => __( 'Ignore all', 'jetpack' ),
215
216
			'menu_option_edit_selection'  => __( 'Edit Selection...', 'jetpack' ),
217
218
			'button_proofread'            => __( 'proofread', 'jetpack' ),
219
			'button_edit_text'            => __( 'edit text', 'jetpack' ),
220
			'button_proofread_tooltip'    => __( 'Proofread Writing', 'jetpack' ),
221
222
			'message_no_errors_found'     => __( 'No writing errors were found.', 'jetpack' ),
223
			'message_server_error'        => __( 'There was a problem communicating with the Proofreading service. Try again in one minute.', 'jetpack' ),
224
			'message_server_error_short'  => __( 'There was an error communicating with the proofreading service.', 'jetpack' ),
225
226
			'dialog_replace_selection'    => __( 'Replace selection with:', 'jetpack' ),
227
			'dialog_confirm_post_publish' => __( "The proofreader has suggestions for this post. Are you sure you want to publish it?\n\nPress OK to publish your post, or Cancel to view the suggestions and edit your post.", 'jetpack' ),
228
			'dialog_confirm_post_update'  => __( "The proofreader has suggestions for this post. Are you sure you want to update it?\n\nPress OK to update your post, or Cancel to view the suggestions and edit your post.", 'jetpack' ),
229
		) );
230
	}
231
}
232
233
/* Spits out user options for auto-proofreading on publish/update */
234
function AtD_load_submit_check_javascripts() {
235
	global $pagenow;
236
237
	$user = wp_get_current_user();
238
	if ( ! $user || $user->ID == 0 )
239
		return;
240
241
	if ( AtD_should_load_on_page() ) {
242
		$atd_check_when = AtD_get_setting( $user->ID, 'AtD_check_when' );
243
244
		if ( !empty( $atd_check_when ) ) {
245
			$check_when = array();
246
			/* Set up the options in json */
247
			foreach( explode( ',', $atd_check_when ) as $option ) {
248
				$check_when[$option] = true;
249
			}
250
			echo '<script type="text/javascript">' . "\n";
251
			echo 'AtD_check_when = ' . json_encode( (object) $check_when ) . ";\n";
252
			echo '</script>' . "\n";
253
		}
254
	}
255
}
256
257
/*
258
 * Check if a user is allowed to use AtD
259
 */
260
function AtD_is_allowed() {
261
	if ( ( defined( 'AtD_FORCED_ON' ) && AtD_FORCED_ON ) ) {
262
		return true;
263
	}
264
	$user = wp_get_current_user();
265
	if ( ! $user || $user->ID == 0 )
266
		return;
267
268
	if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) )
269
		return;
270
271
	return 1;
272
}
273
274
function AtD_load_css() {
275
	if ( AtD_should_load_on_page() ) {
276
		if( is_rtl() ) {
277
			wp_enqueue_style( 'AtD_style', plugins_url( '/after-the-deadline/rtl/atd-rtl.css', __FILE__ ), null, ATD_VERSION, 'screen' );
278
		} else {
279
			wp_enqueue_style( 'AtD_style', plugins_url( '/after-the-deadline/atd.css', __FILE__ ), null, ATD_VERSION, 'screen' );
280
		}
281
	}
282
}
283
284
/* Helper used to check if javascript should be added to page. Helps avoid bloat in admin */
285
function AtD_should_load_on_page() {
286
	global $pagenow, $current_screen;
287
288
	$pages = array( 'post.php', 'post-new.php', 'page.php', 'page-new.php', 'admin.php', 'profile.php' );
289
290
	if ( in_array( $pagenow, $pages ) ) {
291
		if ( isset( $current_screen->post_type ) && $current_screen->post_type ) {
292
			return post_type_supports( $current_screen->post_type, 'editor' );
293
		}
294
		return true;
295
	}
296
297
	/**
298
	 * Allows scripts to be loaded via AtD in admin.
299
	 *
300
	 * By default, AtD only enqueues JS on certain admin pages to reduce bloat. The filter allows additional pages to have AtD JS.
301
	 *
302
	 * @module after-the-deadline
303
	 *
304
	 * @since 1.2.3
305
	 *
306
	 * @param bool false Boolean to load or not load AtD scripts in admin.
307
	 */
308
	return apply_filters( 'atd_load_scripts', false );
309
}
310
311
// add button to DFW
312
if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) {
313
	add_filter( 'wp_fullscreen_buttons', 'AtD_fullscreen' );
314
}
315
function AtD_fullscreen($buttons) {
316
	$buttons['spellchecker'] = array( 'title' => __( 'Proofread Writing', 'jetpack' ), 'onclick' => "tinyMCE.execCommand('mceWritingImprovementTool');", 'both' => false );
317
	return $buttons;
318
}
319
320
/* add some vars into the AtD plugin */
321
add_filter( 'tiny_mce_before_init', 'AtD_change_mce_settings' );
322
323
/* load some stuff for non-visual editor */
324
add_action( 'admin_enqueue_scripts', 'AtD_load_javascripts' );
325
add_action( 'admin_enqueue_scripts', 'AtD_load_submit_check_javascripts' );
326
add_action( 'admin_enqueue_scripts', 'AtD_load_css' );
327
328
/* init process for button control */
329
add_action( 'init', 'AtD_addbuttons' );
330
331
/* setup hooks for our PHP functions we want to make available via an AJAX call */
332
add_action( 'wp_ajax_proxy_atd', 'AtD_redirect_call' );
333
add_action( 'wp_ajax_atd_ignore', 'AtD_ignore_call' );
334
add_action( 'wp_ajax_atd_settings', 'AtD_settings' );
335