Completed
Push — update/import-sync-detection ( 0bf98c...8808a0 )
by
unknown
25:48 queued 17:49
created

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

Complexity

Conditions 2
Paths 2

Size

Total Lines 68

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 68
rs 8.6981
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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