Completed
Push — update/sync-users-enqueue-orde... ( d17611...0c48e3 )
by
unknown
29:40
created

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

Complexity

Conditions 2
Paths 2

Size

Total Lines 36
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 25
nc 2
nop 0
dl 0
loc 36
rs 8.8571
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
		Jetpack::module_configuration_load( __FILE__, 'AtD_configuration_load' );
46
	}
47
	function AtD_configuration_load() {
48
		wp_safe_redirect( get_edit_profile_url( get_current_user_id() ) . '#atd' );
49
		exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The function AtD_configuration_load() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
50
	}
51
	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...
52
		return update_user_meta( $user_id, $name, $value );
53
	}
54
	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...
55
		return get_user_meta( $user_id, $name, $single );
56
	}
57
	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...
58
		return 'WPORG-' . md5( get_bloginfo( 'wpurl ') );
59
	}
60
}
61
62
/*
63
 *  Load necessary include files
64
 */
65
include( dirname( __FILE__ ) . '/after-the-deadline/config-options.php' );
66
include( dirname( __FILE__ ) . '/after-the-deadline/config-unignore.php' );
67
include( dirname( __FILE__ ) . '/after-the-deadline/proxy.php' );
68
69
define( 'ATD_VERSION', '20150715' );
70
71
/*
72
 * Display the AtD configuration options
73
 */
74
function AtD_config() {
75
	AtD_display_options_form();
76
	AtD_display_unignore_form();
77
}
78
79
/*
80
 *  Code to update the toolbar with the AtD Button and Install the AtD TinyMCE Plugin
81
 */
82
function AtD_addbuttons() {
83
	/* Don't bother doing this stuff if the current user lacks permissions */
84
	if ( ! AtD_is_allowed() )
85
		return;
86
87
	if ( ! defined( 'ATD_TINYMCE_4' ) ) {
88
		define( 'ATD_TINYMCE_4', ( ! empty( $GLOBALS['tinymce_version'] ) && substr( $GLOBALS['tinymce_version'], 0, 1 ) >= 4 ) );
89
	}
90
91
	/* Add only in Rich Editor mode */
92
	if ( get_user_option( 'rich_editing' ) == 'true' ) {
93
		add_filter( 'mce_external_plugins', 'add_AtD_tinymce_plugin' );
94
		add_filter( 'mce_buttons', 'register_AtD_button' );
95
	}
96
97
	add_action( 'personal_options_update', 'AtD_process_options_update' );
98
	add_action( 'personal_options_update', 'AtD_process_unignore_update' );
99
	add_action( 'profile_personal_options', 'AtD_config' );
100
}
101
102
/*
103
 * Hook into the TinyMCE buttons and replace the current spellchecker
104
 */
105
function register_AtD_button( $buttons ) {
106
	if ( ATD_TINYMCE_4 ) {
107
		// Use the default icon in TinyMCE 4.0 (replaced by dashicons in editor.css)
108
		if ( ! in_array( 'spellchecker', $buttons, true ) ) {
109
			$buttons[] = 'spellchecker';
110
		}
111
112
		return $buttons;
113
	}
114
115
	/* kill the spellchecker.. don't need no steenkin PHP spell checker */
116
	foreach ( $buttons as $key => $button ) {
117
		if ( $button == 'spellchecker' ) {
118
			$buttons[$key] = 'AtD';
119
			return $buttons;
120
		}
121
	}
122
123
	/* hrm... ok add us last plz */
124
	array_push( $buttons, '|', 'AtD' );
125
	return $buttons;
126
}
127
128
/*
129
 * Load the TinyMCE plugin : editor_plugin.js (TinyMCE 3.x) | plugin.js (TinyMCE 4.0)
130
 */
131
function add_AtD_tinymce_plugin( $plugin_array ) {
132
	$plugin = ATD_TINYMCE_4 ? 'plugin' : 'editor_plugin';
133
134
	$plugin_array['AtD'] = add_query_arg( 'v', ATD_VERSION, plugins_url( 'after-the-deadline/tinymce/' . $plugin . '.js', __FILE__ ) );
135
	return $plugin_array;
136
}
137
138
/*
139
 * Update the TinyMCE init block with AtD specific settings
140
 */
141
function AtD_change_mce_settings( $init_array ) {
142
	if ( ! AtD_is_allowed() )
143
		return $init_array;
144
145
	if ( ! is_array( $init_array ) )
146
		$init_array = array();
147
148
	$user = wp_get_current_user();
149
150
	$init_array['atd_rpc_url']        = admin_url( 'admin-ajax.php?action=proxy_atd&_wpnonce=' . wp_create_nonce( 'proxy_atd' ) . '&url=' );
151
	$init_array['atd_ignore_rpc_url'] = admin_url( 'admin-ajax.php?action=atd_ignore&_wpnonce=' . wp_create_nonce( 'atd_ignore' ) . '&phrase=' );
152
	$init_array['atd_rpc_id']         = AtD_get_rpc_id();
153
	$init_array['atd_theme']          = 'wordpress';
154
	$init_array['atd_ignore_enable']  = 'true';
155
	$init_array['atd_strip_on_get']   = 'true';
156
	$init_array['atd_ignore_strings'] = json_encode( explode( ',',  AtD_get_setting( $user->ID, 'AtD_ignored_phrases' ) ) );
157
	$init_array['atd_show_types']     = AtD_get_setting( $user->ID, 'AtD_options' );
158
	$init_array['gecko_spellcheck']   = 'false';
159
160
	return $init_array;
161
}
162
163
/*
164
 * Sanitizes AtD AJAX data to acceptable chars, caller needs to make sure ' is escaped
165
 */
166
function AtD_sanitize( $untrusted ) {
167
	return preg_replace( '/[^a-zA-Z0-9\-\',_ ]/i', "", $untrusted );
168
}
169
170
/*
171
 * AtD HTML Editor Stuff
172
 */
173
function AtD_settings() {
174
	$user = wp_get_current_user();
175
176
	header( 'Content-Type: text/javascript' );
177
178
	/* set the RPC URL for AtD */
179
	echo "AtD.rpc = " . json_encode( esc_url_raw( admin_url( 'admin-ajax.php?action=proxy_atd&_wpnonce=' . wp_create_nonce( 'proxy_atd' ) . '&url=' ) ) ) . ";\n";
180
181
	/* set the API key for AtD */
182
	echo "AtD.api_key = " . json_encode( AtD_get_rpc_id() ) . ";\n";
183
184
	/* set the ignored phrases for AtD */
185
	echo "AtD.setIgnoreStrings(" . json_encode( AtD_get_setting( $user->ID, 'AtD_ignored_phrases' ) ) . ");\n";
186
187
	/* honor the types we want to show */
188
	echo "AtD.showTypes(" . json_encode( AtD_get_setting( $user->ID, 'AtD_options' ) ) .");\n";
189
190
	/* 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 */
191
	$admin_ajax_url = admin_url( 'admin-ajax.php?action=atd_ignore&_wpnonce=' . wp_create_nonce( 'atd_ignore' ) . '&phrase=' );
192
	echo "AtD.rpc_ignore = " . json_encode( esc_url_raw( $admin_ajax_url ) ) . ";\n";
193
194
	die;
0 ignored issues
show
Coding Style Compatibility introduced by
The function AtD_settings() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
195
}
196
197
function AtD_load_javascripts() {
198
	if ( AtD_should_load_on_page() ) {
199
		wp_enqueue_script( 'AtD_core', plugins_url( '/after-the-deadline/atd.core.js', __FILE__ ), array(), ATD_VERSION );
200
		wp_enqueue_script( 'AtD_quicktags', plugins_url( '/after-the-deadline/atd-nonvis-editor-plugin.js', __FILE__ ), array('quicktags'), ATD_VERSION );
201
		wp_enqueue_script( 'AtD_jquery', plugins_url( '/after-the-deadline/jquery.atd.js', __FILE__ ), array('jquery'), ATD_VERSION );
202
		wp_enqueue_script( 'AtD_settings', admin_url() . 'admin-ajax.php?action=atd_settings', array('AtD_jquery'), ATD_VERSION );
203
		wp_enqueue_script( 'AtD_autoproofread', plugins_url( '/after-the-deadline/atd-autoproofread.js', __FILE__ ), array('AtD_jquery'), ATD_VERSION );
204
205
		/* load localized strings for AtD */
206
		wp_localize_script( 'AtD_core', 'AtD_l10n_r0ar', array (
207
			'menu_title_spelling'         => __( 'Spelling', 'jetpack' ),
208
			'menu_title_repeated_word'    => __( 'Repeated Word', 'jetpack' ),
209
210
			'menu_title_no_suggestions'   => __( 'No suggestions', 'jetpack' ),
211
212
			'menu_option_explain'         => __( 'Explain...', 'jetpack' ),
213
			'menu_option_ignore_once'     => __( 'Ignore suggestion', 'jetpack' ),
214
			'menu_option_ignore_always'   => __( 'Ignore always', 'jetpack' ),
215
			'menu_option_ignore_all'      => __( 'Ignore all', 'jetpack' ),
216
217
			'menu_option_edit_selection'  => __( 'Edit Selection...', 'jetpack' ),
218
219
			'button_proofread'            => __( 'proofread', 'jetpack' ),
220
			'button_edit_text'            => __( 'edit text', 'jetpack' ),
221
			'button_proofread_tooltip'    => __( 'Proofread Writing', 'jetpack' ),
222
223
			'message_no_errors_found'     => __( 'No writing errors were found.', 'jetpack' ),
224
			'message_server_error'        => __( 'There was a problem communicating with the Proofreading service. Try again in one minute.', 'jetpack' ),
225
			'message_server_error_short'  => __( 'There was an error communicating with the proofreading service.', 'jetpack' ),
226
227
			'dialog_replace_selection'    => __( 'Replace selection with:', 'jetpack' ),
228
			'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' ),
229
			'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' ),
230
		) );
231
	}
232
}
233
234
/* Spits out user options for auto-proofreading on publish/update */
235
function AtD_load_submit_check_javascripts() {
236
	global $pagenow;
237
238
	$user = wp_get_current_user();
239
	if ( ! $user || $user->ID == 0 )
240
		return;
241
242
	if ( AtD_should_load_on_page() ) {
243
		$atd_check_when = AtD_get_setting( $user->ID, 'AtD_check_when' );
244
245
		if ( !empty( $atd_check_when ) ) {
246
			$check_when = array();
247
			/* Set up the options in json */
248
			foreach( explode( ',', $atd_check_when ) as $option ) {
249
				$check_when[$option] = true;
250
			}
251
			echo '<script type="text/javascript">' . "\n";
252
			echo 'AtD_check_when = ' . json_encode( (object) $check_when ) . ";\n";
253
			echo '</script>' . "\n";
254
		}
255
	}
256
}
257
258
/*
259
 * Check if a user is allowed to use AtD
260
 */
261
function AtD_is_allowed() {
262
	if ( ( defined( 'AtD_FORCED_ON' ) && AtD_FORCED_ON ) ) {
263
		return true;
264
	}
265
	$user = wp_get_current_user();
266
	if ( ! $user || $user->ID == 0 )
267
		return;
268
269
	if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) )
270
		return;
271
272
	return 1;
273
}
274
275
function AtD_load_css() {
276
	if ( AtD_should_load_on_page() ) {
277
		wp_enqueue_style( 'AtD_style', plugins_url( '/after-the-deadline/atd.css', __FILE__ ), null, ATD_VERSION, 'screen' );
278
		wp_style_add_data( 'AtD_style', 'rtl', 'replace' );
279
	}
280
}
281
282
/* Helper used to check if javascript should be added to page. Helps avoid bloat in admin */
283
function AtD_should_load_on_page() {
284
	global $pagenow, $current_screen;
285
286
	$pages = array( 'post.php', 'post-new.php', 'page.php', 'page-new.php', 'admin.php', 'profile.php' );
287
288
	if ( in_array( $pagenow, $pages ) ) {
289
		if ( isset( $current_screen->post_type ) && $current_screen->post_type ) {
290
			return post_type_supports( $current_screen->post_type, 'editor' );
291
		}
292
		return true;
293
	}
294
295
	/**
296
	 * Allows scripts to be loaded via AtD in admin.
297
	 *
298
	 * By default, AtD only enqueues JS on certain admin pages to reduce bloat. The filter allows additional pages to have AtD JS.
299
	 *
300
	 * @module after-the-deadline
301
	 *
302
	 * @since 1.2.3
303
	 *
304
	 * @param bool false Boolean to load or not load AtD scripts in admin.
305
	 */
306
	return apply_filters( 'atd_load_scripts', false );
307
}
308
309
// add button to DFW
310
if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) {
311
	add_filter( 'wp_fullscreen_buttons', 'AtD_fullscreen' );
312
}
313
function AtD_fullscreen($buttons) {
314
	$buttons['spellchecker'] = array( 'title' => __( 'Proofread Writing', 'jetpack' ), 'onclick' => "tinyMCE.execCommand('mceWritingImprovementTool');", 'both' => false );
315
	return $buttons;
316
}
317
318
/* add some vars into the AtD plugin */
319
add_filter( 'tiny_mce_before_init', 'AtD_change_mce_settings' );
320
321
/* load some stuff for non-visual editor */
322
add_action( 'admin_enqueue_scripts', 'AtD_load_javascripts' );
323
add_action( 'admin_enqueue_scripts', 'AtD_load_submit_check_javascripts' );
324
add_action( 'admin_enqueue_scripts', 'AtD_load_css' );
325
326
/* init process for button control */
327
add_action( 'init', 'AtD_addbuttons' );
328
329
/* setup hooks for our PHP functions we want to make available via an AJAX call */
330
add_action( 'wp_ajax_proxy_atd', 'AtD_redirect_call' );
331
add_action( 'wp_ajax_atd_ignore', 'AtD_ignore_call' );
332
add_action( 'wp_ajax_atd_settings', 'AtD_settings' );
333