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

config-unignore.php ➔ AtD_display_unignore_form()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 99

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 0
dl 0
loc 99
rs 8.0218
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
 *  Called by the TinyMCE plugin when Ignore Always is clicked (setup as an action through admin-ajax.php)
4
 */
5
function AtD_ignore_call() {
6
7
	if ( ! AtD_is_allowed() ) {
8
		return;
9
	}
10
11
	$user = wp_get_current_user();
12
13
	if ( ! $user || $user->ID == 0 ) {
14
		return;
15
	}
16
17
	check_admin_referer( 'atd_ignore' );
18
19
	$ignores = explode( ',', AtD_get_setting( $user->ID, 'AtD_ignored_phrases' ) );
20
	array_push( $ignores, $_GET['phrase'] );
21
22
	$ignores = array_filter( array_map( 'strip_tags', $ignores ) );
23
24
	AtD_update_setting( $user->ID, 'AtD_ignored_phrases', implode( ',', $ignores ) );
25
26
	header( 'Content-Type: text/xml' );
27
	echo '<success></success>';
28
	die();
29
}
30
31
/*
32
 *  Called when a POST occurs, used to save AtD ignored phrases
33
 */
34
function AtD_process_unignore_update() {
35
36
	if ( ! AtD_is_allowed() ) {
37
		return;
38
	}
39
40
	if ( ! isset( $_POST['AtD_ignored_phrases'] ) ) {
41
		return;
42
	}
43
44
		$user = wp_get_current_user();
45
46
	if ( ! $user || $user->ID == 0 ) {
47
			return;
48
	}
49
50
	$ignores = array_filter( array_map( 'strip_tags', explode( ',', $_POST['AtD_ignored_phrases'] ) ) );
51
		AtD_update_setting( $user->ID, 'AtD_ignored_phrases', join( ',', $ignores ) );
52
}
53
54
/*
55
 *  Display the AtD unignore form on a page
56
 */
57
function AtD_display_unignore_form() {
58
59
	if ( ! AtD_is_allowed() ) {
60
		return;
61
	}
62
63
	$user = wp_get_current_user();
64
65
	if ( ! $user || $user->ID == 0 ) {
66
		return;
67
	}
68
69
	$ignores = AtD_get_setting( $user->ID, 'AtD_ignored_phrases' );
70
?>
71
<script>
72
function atd_show_phrases( ignored )
73
{
74
	var element = jQuery( '#atd_ignores' ),
75
		items   = [],
76
		delLink;
77
78
	ignored.sort();
79
80
	element.empty();
81
	for ( var i = 0; i < ignored.length; i++ ) {
82
		if ( ignored[i].length > 0 ) {
83
			delLink = jQuery( '<span id="atd_' + i + '">&nbsp;</span>' );
84
			delLink
85
				.text( delLink.text() + ignored[i] )
86
				.prepend( jQuery( '<a class="ntdelbutton">X</a>' ).data( 'ignored', ignored[i] ) );
87
			element.append( delLink ).append( '<br />' );
88
		}
89
	}
90
}
91
92
function atd_unignore( phrase ) {
93
	/* get the ignored values and remove the unwanted phrase */
94
	var ignored = jQuery( '#AtD_ignored_phrases' ).val().split( /,/g );
95
		ignored = jQuery.map(ignored, function(value, index) { return value == phrase ? null : value; });
96
		jQuery( '#AtD_ignored_phrases' ).val( ignored.join(',') );
97
98
	/* update the UI */
99
	atd_show_phrases( ignored );
100
101
	/* show a nifty message to the user */
102
		jQuery( '#AtD_message' ).show();
103
}
104
105
function atd_ignore () {
106
	/* get the ignored values and update the hidden field */
107
	var ignored = jQuery( '#AtD_ignored_phrases' ).val().split( /,/g );
108
109
		jQuery.map(jQuery( '#AtD_add_ignore' ).val().split(/,\s*/g), function(value, index) { ignored.push(value); });
110
111
		jQuery( '#AtD_ignored_phrases' ).val( ignored.join(',') );
112
113
	/* update the UI */
114
	atd_show_phrases( ignored );
115
	jQuery( '#AtD_add_ignore' ).val('');
116
117
	/* show that nifteroo messaroo to the useroo */
118
		jQuery( '#AtD_message' ).show();
119
}
120
121
function atd_ignore_init() {
122
	jQuery( '#AtD_message' ).hide();
123
	jQuery( '#atd_ignores' ).on( 'click', 'a', function() {
124
		atd_unignore( jQuery(this).data( 'ignored' ) );
125
		return false;
126
	} );
127
	atd_show_phrases( jQuery( '#AtD_ignored_phrases' ).val().split( /,/g ) );
128
}
129
130
/* document.ready() does not execute in IE6 unless it's at the bottom of the page. oi! */
131
if (navigator.appName == 'Microsoft Internet Explorer')
132
	setTimeout( atd_ignore_init, 2500 );
133
else
134
	jQuery( document ).ready( atd_ignore_init );
135
</script>
136
   <input type="hidden" name="AtD_ignored_phrases" id="AtD_ignored_phrases" value="<?php echo esc_attr( $ignores ); ?>">
137
138
		  <p style="font-weight: bold"><?php _e( 'Ignored Phrases', 'jetpack' ); ?></p>
139
140
		  <p><?php _e( 'Identify words and phrases to ignore while proofreading your posts and pages:', 'jetpack' ); ?></p>
141
142
		  <p><input type="text" id="AtD_add_ignore" name="AtD_add_ignore"> <input type="button" value="<?php esc_attr_e( 'Add', 'jetpack' ); ?>" onclick="javascript:atd_ignore()"></p>
143
144
		  <div class="tagchecklist" id="atd_ignores"></div>
145
146
		  <div class="plugin-update-tr" id="AtD_message" style="display: none">
147
		  <div class="update-message"><strong><?php _e( 'Be sure to click "Update Profile" at the bottom of the screen to save your changes.', 'jetpack' ); ?></strong></div>
148
		  </div>
149
150
		 </td>
151
	  </tr>
152
   </table>
153
154
<?php
155
}
156