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

config-options.php ➔ AtD_display_options_form()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 75

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 0
dl 0
loc 75
rs 8.5454
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
 *   Display the configuration options for AtD
4
 */
5
6
/*
7
 *   A convienence function to display the HTML for an AtD option
8
 */
9
function AtD_print_option( $name, $value, $options ) {
10
	// Attribute-safe version of $name
11
	$attr_name = sanitize_title( $name ); // Using sanitize_title since there's no comparable function for attributes
12
?>
13
   <input type="checkbox" id="atd_<?php echo esc_attr( $attr_name ); ?>" name="<?php echo esc_attr( $options['name'] ); ?>[<?php echo esc_attr( $name ); ?>]" value="1" <?php checked( '1', isset( $options[ $name ] ) ? $options[ $name ] : false ); ?>> <label for="atd_<?php echo esc_attr( $attr_name ); ?>"><?php echo esc_html( $value ); ?></label>
14
<?php
15
}
16
17
/*
18
 *  Save AtD options
19
 */
20
function AtD_process_options_update() {
21
22
	$user = wp_get_current_user();
23
24
	if ( ! $user || $user->ID == 0 ) {
25
		return;
26
	}
27
28
	AtD_update_options( $user->ID, 'AtD_options' );
29
	AtD_update_options( $user->ID, 'AtD_check_when' );
30
	AtD_update_options( $user->ID, 'AtD_guess_lang' );
31
}
32
33
/*
34
 *  Display the various AtD options
35
 */
36
function AtD_display_options_form() {
37
38
	/* grab our user and validate their existence */
39
	$user = wp_get_current_user();
40
	if ( ! $user || $user->ID == 0 ) {
41
		return;
42
	}
43
44
	$options_show_types = AtD_get_options( $user->ID, 'AtD_options' );
45
	$options_check_when = AtD_get_options( $user->ID, 'AtD_check_when' );
46
	$options_guess_lang = AtD_get_options( $user->ID, 'AtD_guess_lang' );
47
?>
48
   <table class="form-table">
49
	  <tr valign="top">
50
		 <th scope="row"> <a id="atd"></a> <?php _e( 'Proofreading', 'jetpack' ); ?></th>
51
		 <td>
52
   <p><?php _e( 'Automatically proofread content when:', 'jetpack' ); ?>
53
54
   <p>
55
	<?php
56
		AtD_print_option( 'onpublish', __( 'a post or page is first published', 'jetpack' ), $options_check_when );
57
		echo '<br />';
58
		AtD_print_option( 'onupdate', __( 'a post or page is updated', 'jetpack' ), $options_check_when );
59
	?>
60
   </p>
61
62
   <p style="font-weight: bold"><?php _e( 'English Options', 'jetpack' ); ?></p>
63
64
   <p><?php _e( 'Enable proofreading for the following grammar and style rules when writing posts and pages:', 'jetpack' ); ?></p>
65
66
   <p>
67
	<?php
68
		AtD_print_option( 'Bias Language', __( 'Bias Language', 'jetpack' ), $options_show_types );
69
		echo '<br />';
70
		AtD_print_option( 'Cliches', __( 'Clich&eacute;s', 'jetpack' ), $options_show_types );
71
		echo '<br />';
72
		AtD_print_option( 'Complex Expression', __( 'Complex Phrases', 'jetpack' ), $options_show_types );
73
		echo '<br />';
74
		AtD_print_option( 'Diacritical Marks', __( 'Diacritical Marks', 'jetpack' ), $options_show_types );
75
		echo '<br />';
76
		AtD_print_option( 'Double Negative', __( 'Double Negatives', 'jetpack' ), $options_show_types );
77
		echo '<br />';
78
		AtD_print_option( 'Hidden Verbs', __( 'Hidden Verbs', 'jetpack' ), $options_show_types );
79
		echo '<br />';
80
		AtD_print_option( 'Jargon Language', __( 'Jargon', 'jetpack' ), $options_show_types );
81
		echo '<br />';
82
		AtD_print_option( 'Passive voice', __( 'Passive Voice', 'jetpack' ), $options_show_types );
83
		echo '<br />';
84
		AtD_print_option( 'Phrases to Avoid', __( 'Phrases to Avoid', 'jetpack' ), $options_show_types );
85
		echo '<br />';
86
		AtD_print_option( 'Redundant Expression', __( 'Redundant Phrases', 'jetpack' ), $options_show_types );
87
	?>
88
   </p>
89
   <p>
90
	<?php
91
	printf( __( '<a href="%s" rel="noopener noreferrer" target="_blank">Learn more</a> about these options.', 'jetpack' ), 'http://support.wordpress.com/proofreading/' );
92
?>
93
</p>
94
95
   <p style="font-weight: bold"><?php _e( 'Language', 'jetpack' ); ?></p>
96
97
   <p>
98
	<?php
99
	_e( 'The proofreader supports English, French, German, Portuguese, and Spanish. Your user interface language (see above) is the default proofreading language.', 'jetpack' );
100
		?>
101
	 </p>
102
103
   <p>
104
	<?php
105
	AtD_print_option( 'true', __( 'Use automatically detected language to proofread posts and pages', 'jetpack' ), $options_guess_lang );
106
	?>
107
   </p>
108
109
<?php
110
}
111
112
/*
113
 *  Returns an array of AtD user options specified by $name
114
 */
115
function AtD_get_options( $user_id, $name ) {
116
	$options_raw = AtD_get_setting( $user_id, $name, 'single' );
0 ignored issues
show
Documentation introduced by
'single' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
117
118
	$options         = array();
119
	$options['name'] = $name;
120
121
	if ( $options_raw ) {
122
		foreach ( explode( ',', $options_raw ) as $option ) {
123
			$options[ $option ] = 1;
124
		}
125
	}
126
127
	return $options;
128
}
129
130
/*
131
 *  Saves set of user options specified by $name from POST data
132
 */
133
function AtD_update_options( $user_id, $name ) {
134
	/* We should probably run $_POST[name] through an esc_*() function... */
135
	if ( isset( $_POST[ $name ] ) && is_array( $_POST[ $name ] ) ) {
136
		$copy = array_map( 'strip_tags', array_keys( $_POST[ $name ] ) );
137
		AtD_update_setting( $user_id, AtD_sanitize( $name ), implode( ',', $copy ) );
138
	} else {
139
		AtD_update_setting( $user_id, AtD_sanitize( $name ), '' );
140
	}
141
142
	return;
143
}
144