Completed
Push — add/improve-histogram ( bde1c8...c8533f )
by
unknown
43:24 queued 36:14
created

after-the-deadline.php ➔ AtD_is_allowed()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 4
nop 0
dl 0
loc 13
rs 8.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * Module Name: Spelling and Grammar
4
 * Module Description: Check your spelling, style, and grammar
5
 * Sort Order: 6
6
 * First Introduced: 1.1
7
 * Requires Connection: Yes
8
 * Auto Activate: Yes
9
 * Module Tags: Writing
10
 * Feature: Writing
11
 * Additional Search Queries: after the deadline, afterthedeadline, spell, spellchecker, spelling, grammar, proofreading, style, language, cliche
12
 */
13
14
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
15
	// This wpcom-specific code should eventually be moved elsewhere.
16
17
	function AtD_http_post_timeout_action() {
18
		return 5;
19
	}
20
	add_filter( 'atd_http_post_timeout', 'AtD_http_post_timeout_action' );
21
	function AtD_http_post_error_action( $code ) {
22
		/** This action is documented in modules/widgets/social-media-icons.php */
23
		do_action( 'jetpack_bump_stats_extras', 'atd-remote-error', $code );
24
	}
25
	add_action( 'atd_http_post_error', 'AtD_http_post_error_action' );
26
	function AtD_service_domain_action() {
27
		return 'en.service.afterthedeadline.com';
28
	}
29
	add_filter( 'atd_service_domain', 'AtD_service_domain_action' );
30
	function AtD_update_setting( $user_id, $name, $value ) {
31
		update_user_attribute( $user_id, $name, $value );
32
	}
33
	function AtD_get_setting( $user_id, $name, $single = true ) {
34
		return get_user_attribute( $user_id, $name );
35
	}
36
	function AtD_get_rpc_id() {
37
		return get_bloginfo( 'wpurl' );
38
	}
39
} else {
40
	// This code is used only in Jetpack.
41
42
	add_action( 'jetpack_modules_loaded', 'AtD_load' );
43
	function AtD_load() {
44
		Jetpack::enable_module_configurable( __FILE__ );
45
	}
46
47
	function AtD_update_setting( $user_id, $name, $value ) {
0 ignored issues
show
Best Practice introduced by
The function AtD_update_setting() has been defined more than once; this definition is ignored, only the first definition in this file (L30-32) 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...
48
		return update_user_meta( $user_id, $name, $value );
49
	}
50
	function AtD_get_setting( $user_id, $name, $single = true ) {
0 ignored issues
show
Best Practice introduced by
The function AtD_get_setting() has been defined more than once; this definition is ignored, only the first definition in this file (L33-35) 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
		return get_user_meta( $user_id, $name, $single );
52
	}
53
	function AtD_get_rpc_id() {
0 ignored issues
show
Best Practice introduced by
The function AtD_get_rpc_id() has been defined more than once; this definition is ignored, only the first definition in this file (L36-38) 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 'WPORG-' . md5( get_bloginfo( 'wpurl ') );
55
	}
56
}
57
58
/*
59
 *  Load necessary include files
60
 */
61
include( dirname( __FILE__ ) . '/after-the-deadline/config-options.php' );
62
include( dirname( __FILE__ ) . '/after-the-deadline/config-unignore.php' );
63
include( dirname( __FILE__ ) . '/after-the-deadline/proxy.php' );
64
65
define( 'ATD_VERSION', '20150715' );
66
67
/*
68
 * Display the AtD configuration options
69
 */
70
function AtD_config() {
71
	AtD_display_options_form();
72
	AtD_display_unignore_form();
73
}
74
75
/*
76
 *  Code to update the toolbar with the AtD Button and Install the AtD TinyMCE Plugin
77
 */
78
function AtD_addbuttons() {
79
	/* Don't bother doing this stuff if the current user lacks permissions */
80
	if ( ! AtD_is_allowed() )
81
		return;
82
83
	if ( ! defined( 'ATD_TINYMCE_4' ) ) {
84
		define( 'ATD_TINYMCE_4', ( ! empty( $GLOBALS['tinymce_version'] ) && substr( $GLOBALS['tinymce_version'], 0, 1 ) >= 4 ) );
85
	}
86
87
	/* Add only in Rich Editor mode */
88
	if ( get_user_option( 'rich_editing' ) == 'true' ) {
89
		add_filter( 'mce_external_plugins', 'add_AtD_tinymce_plugin' );
90
		add_filter( 'mce_buttons', 'register_AtD_button' );
91
	}
92
93
	add_action( 'personal_options_update', 'AtD_process_options_update' );
94
	add_action( 'personal_options_update', 'AtD_process_unignore_update' );
95
	add_action( 'profile_personal_options', 'AtD_config' );
96
}
97
98
/*
99
 * Hook into the TinyMCE buttons and replace the current spellchecker
100
 */
101
function register_AtD_button( $buttons ) {
102
	if ( ATD_TINYMCE_4 ) {
103
		// Use the default icon in TinyMCE 4.0 (replaced by dashicons in editor.css)
104
		if ( ! in_array( 'spellchecker', $buttons, true ) ) {
105
			$buttons[] = 'spellchecker';
106
		}
107
108
		return $buttons;
109
	}
110
111
	/* kill the spellchecker.. don't need no steenkin PHP spell checker */
112
	foreach ( $buttons as $key => $button ) {
113
		if ( $button == 'spellchecker' ) {
114
			$buttons[$key] = 'AtD';
115
			return $buttons;
116
		}
117
	}
118
119
	/* hrm... ok add us last plz */
120
	array_push( $buttons, '|', 'AtD' );
121
	return $buttons;
122
}
123
124
/*
125
 * Load the TinyMCE plugin : editor_plugin.js (TinyMCE 3.x) | plugin.js (TinyMCE 4.0)
126
 */
127
function add_AtD_tinymce_plugin( $plugin_array ) {
128
	$plugin = ATD_TINYMCE_4 ? 'plugin' : 'editor_plugin';
129
130
	$plugin_array['AtD'] = add_query_arg( 'v', ATD_VERSION, plugins_url( 'after-the-deadline/tinymce/' . $plugin . '.js', __FILE__ ) );
131
	return $plugin_array;
132
}
133
134
/*
135
 * Update the TinyMCE init block with AtD specific settings
136
 */
137
function AtD_change_mce_settings( $init_array ) {
138
	if ( ! AtD_is_allowed() )
139
		return $init_array;
140
141
	if ( ! is_array( $init_array ) )
142
		$init_array = array();
143
144
	$user = wp_get_current_user();
145
146
	$init_array['atd_rpc_url']        = admin_url( 'admin-ajax.php?action=proxy_atd&_wpnonce=' . wp_create_nonce( 'proxy_atd' ) . '&url=' );
147
	$init_array['atd_ignore_rpc_url'] = admin_url( 'admin-ajax.php?action=atd_ignore&_wpnonce=' . wp_create_nonce( 'atd_ignore' ) . '&phrase=' );
148
	$init_array['atd_rpc_id']         = AtD_get_rpc_id();
149
	$init_array['atd_theme']          = 'wordpress';
150
	$init_array['atd_ignore_enable']  = 'true';
151
	$init_array['atd_strip_on_get']   = 'true';
152
	$init_array['atd_ignore_strings'] = json_encode( explode( ',',  AtD_get_setting( $user->ID, 'AtD_ignored_phrases' ) ) );
153
	$init_array['atd_show_types']     = AtD_get_setting( $user->ID, 'AtD_options' );
154
	$init_array['gecko_spellcheck']   = 'false';
155
156
	return $init_array;
157
}
158
159
/*
160
 * Sanitizes AtD AJAX data to acceptable chars, caller needs to make sure ' is escaped
161
 */
162
function AtD_sanitize( $untrusted ) {
163
	return preg_replace( '/[^a-zA-Z0-9\-\',_ ]/i', "", $untrusted );
164
}
165
166
/*
167
 * AtD HTML Editor Stuff
168
 */
169
function AtD_settings() {
170
	$user = wp_get_current_user();
171
172
	header( 'Content-Type: text/javascript' );
173
174
	/* set the RPC URL for AtD */
175
	echo "AtD.rpc = " . json_encode( esc_url_raw( admin_url( 'admin-ajax.php?action=proxy_atd&_wpnonce=' . wp_create_nonce( 'proxy_atd' ) . '&url=' ) ) ) . ";\n";
176
177
	/* set the API key for AtD */
178
	echo "AtD.api_key = " . json_encode( AtD_get_rpc_id() ) . ";\n";
179
180
	/* set the ignored phrases for AtD */
181
	echo "AtD.setIgnoreStrings(" . json_encode( AtD_get_setting( $user->ID, 'AtD_ignored_phrases' ) ) . ");\n";
182
183
	/* honor the types we want to show */
184
	echo "AtD.showTypes(" . json_encode( AtD_get_setting( $user->ID, 'AtD_options' ) ) .");\n";
185
186
	/* 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 */
187
	$admin_ajax_url = admin_url( 'admin-ajax.php?action=atd_ignore&_wpnonce=' . wp_create_nonce( 'atd_ignore' ) . '&phrase=' );
188
	echo "AtD.rpc_ignore = " . json_encode( esc_url_raw( $admin_ajax_url ) ) . ";\n";
189
190
	die;
191
}
192
193
function AtD_load_javascripts() {
194
	if ( AtD_should_load_on_page() ) {
195
		wp_enqueue_script(
196
			'AtD_core',
197
			Jetpack::get_file_url_for_environment(
198
				'_inc/build/after-the-deadline/atd.core.min.js',
199
				'modules/after-the-deadline/atd.core.js'
200
			),
201
			array(),
202
			ATD_VERSION
203
		);
204
		wp_enqueue_script(
205
			'AtD_quicktags',
206
			Jetpack::get_file_url_for_environment(
207
				'_inc/build/after-the-deadline/atd-nonvis-editor-plugin.min.js',
208
				'modules/after-the-deadline/atd-nonvis-editor-plugin.js'
209
			),
210
			array('quicktags'),
211
			ATD_VERSION
212
		);
213
		wp_enqueue_script(
214
			'AtD_jquery',
215
			Jetpack::get_file_url_for_environment(
216
				'_inc/build/after-the-deadline/jquery.atd.min.js',
217
				'modules/after-the-deadline/jquery.atd.js'
218
			),
219
			array('jquery'),
220
			ATD_VERSION
221
		);
222
		wp_enqueue_script( 'AtD_settings', admin_url( 'admin-ajax.php?action=atd_settings' ), array('AtD_jquery'), ATD_VERSION );
223
		wp_enqueue_script(
224
			'AtD_autoproofread',
225
			Jetpack::get_file_url_for_environment(
226
				'_inc/build/after-the-deadline/atd-autoproofread.min.js',
227
				'modules/after-the-deadline/atd-autoproofread.js'
228
			),
229
			array('AtD_jquery'),
230
			ATD_VERSION
231
		);
232
233
		/* load localized strings for AtD */
234
		wp_localize_script( 'AtD_core', 'AtD_l10n_r0ar', array (
235
			'menu_title_spelling'         => __( 'Spelling', 'jetpack' ),
236
			'menu_title_repeated_word'    => __( 'Repeated Word', 'jetpack' ),
237
238
			'menu_title_no_suggestions'   => __( 'No suggestions', 'jetpack' ),
239
240
			'menu_option_explain'         => __( 'Explain...', 'jetpack' ),
241
			'menu_option_ignore_once'     => __( 'Ignore suggestion', 'jetpack' ),
242
			'menu_option_ignore_always'   => __( 'Ignore always', 'jetpack' ),
243
			'menu_option_ignore_all'      => __( 'Ignore all', 'jetpack' ),
244
245
			'menu_option_edit_selection'  => __( 'Edit Selection...', 'jetpack' ),
246
247
			'button_proofread'            => __( 'proofread', 'jetpack' ),
248
			'button_edit_text'            => __( 'edit text', 'jetpack' ),
249
			'button_proofread_tooltip'    => __( 'Proofread Writing', 'jetpack' ),
250
251
			'message_no_errors_found'     => __( 'No writing errors were found.', 'jetpack' ),
252
			'message_server_error'        => __( 'There was a problem communicating with the Proofreading service. Try again in one minute.', 'jetpack' ),
253
			'message_server_error_short'  => __( 'There was an error communicating with the proofreading service.', 'jetpack' ),
254
255
			'dialog_replace_selection'    => __( 'Replace selection with:', 'jetpack' ),
256
			'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' ),
257
			'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' ),
258
		) );
259
	}
260
}
261
262
/* Spits out user options for auto-proofreading on publish/update */
263
function AtD_load_submit_check_javascripts() {
264
	global $pagenow;
265
266
	$user = wp_get_current_user();
267
	if ( ! $user || $user->ID == 0 )
268
		return;
269
270
	if ( AtD_should_load_on_page() ) {
271
		$atd_check_when = AtD_get_setting( $user->ID, 'AtD_check_when' );
272
273
		if ( !empty( $atd_check_when ) ) {
274
			$check_when = array();
275
			/* Set up the options in json */
276
			foreach( explode( ',', $atd_check_when ) as $option ) {
277
				$check_when[$option] = true;
278
			}
279
			echo '<script type="text/javascript">' . "\n";
280
			echo 'AtD_check_when = ' . json_encode( (object) $check_when ) . ";\n";
281
			echo '</script>' . "\n";
282
		}
283
	}
284
}
285
286
/*
287
 * Check if a user is allowed to use AtD
288
 */
289
function AtD_is_allowed() {
290
	if ( ( defined( 'AtD_FORCED_ON' ) && AtD_FORCED_ON ) ) {
291
		return true;
292
	}
293
	$user = wp_get_current_user();
294
	if ( ! $user || $user->ID == 0 )
295
		return;
296
297
	if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) )
298
		return;
299
300
	return 1;
301
}
302
303
function AtD_load_css() {
304
	if ( AtD_should_load_on_page() ) {
305
		wp_enqueue_style( 'AtD_style', plugins_url( '/after-the-deadline/atd.css', __FILE__ ), null, ATD_VERSION, 'screen' );
306
		wp_style_add_data( 'AtD_style', 'rtl', 'replace' );
307
	}
308
}
309
310
/* Helper used to check if javascript should be added to page. Helps avoid bloat in admin */
311
function AtD_should_load_on_page() {
312
	global $pagenow, $current_screen;
313
314
	$pages = array( 'post.php', 'post-new.php', 'page.php', 'page-new.php', 'admin.php', 'profile.php' );
315
316
	if ( in_array( $pagenow, $pages ) ) {
317
		if ( isset( $current_screen->post_type ) && $current_screen->post_type ) {
318
			return post_type_supports( $current_screen->post_type, 'editor' );
319
		}
320
		return true;
321
	}
322
323
	/**
324
	 * Allows scripts to be loaded via AtD in admin.
325
	 *
326
	 * By default, AtD only enqueues JS on certain admin pages to reduce bloat. The filter allows additional pages to have AtD JS.
327
	 *
328
	 * @module after-the-deadline
329
	 *
330
	 * @since 1.2.3
331
	 *
332
	 * @param bool false Boolean to load or not load AtD scripts in admin.
333
	 */
334
	return apply_filters( 'atd_load_scripts', false );
335
}
336
337
// add button to DFW
338
if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) {
339
	add_filter( 'wp_fullscreen_buttons', 'AtD_fullscreen' );
340
}
341
function AtD_fullscreen($buttons) {
342
	$buttons['spellchecker'] = array( 'title' => __( 'Proofread Writing', 'jetpack' ), 'onclick' => "tinyMCE.execCommand('mceWritingImprovementTool');", 'both' => false );
343
	return $buttons;
344
}
345
346
/* add some vars into the AtD plugin */
347
add_filter( 'tiny_mce_before_init', 'AtD_change_mce_settings' );
348
349
/* load some stuff for non-visual editor */
350
add_action( 'admin_enqueue_scripts', 'AtD_load_javascripts' );
351
add_action( 'admin_enqueue_scripts', 'AtD_load_submit_check_javascripts' );
352
add_action( 'admin_enqueue_scripts', 'AtD_load_css' );
353
354
/* init process for button control */
355
add_action( 'init', 'AtD_addbuttons' );
356
357
/* setup hooks for our PHP functions we want to make available via an AJAX call */
358
add_action( 'wp_ajax_proxy_atd', 'AtD_redirect_call' );
359
add_action( 'wp_ajax_atd_ignore', 'AtD_ignore_call' );
360
add_action( 'wp_ajax_atd_settings', 'AtD_settings' );
361