GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — feature/gallery-template-clien... ( 6eed57...a5e10a )
by Brad
02:39
created

FooGallery_Admin_Gallery_MetaBox_Fields   F

Complexity

Total Complexity 66

Size/Duplication

Total Lines 306
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 306
rs 3.1913
c 0
b 0
f 0
wmc 66
lcom 0
cbo 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
F render_gallery_template_field() 0 155 39
C alter_gallery_template_field() 0 53 10
B get_thumb_size_choices() 0 14 5
A get_thumb_link_field_choices() 0 8 1
A get_lightbox_field_choices() 0 5 1
A check_lightbox_value() 0 12 4
B add_section_icons() 0 16 5

How to fix   Complexity   

Complex Class

Complex classes like FooGallery_Admin_Gallery_MetaBox_Fields often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use FooGallery_Admin_Gallery_MetaBox_Fields, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
if ( ! class_exists( 'FooGallery_Admin_Gallery_MetaBox_Fields' ) ) {
4
5
	class FooGallery_Admin_Gallery_MetaBox_Fields {
6
7
		function __construct() {
8
			//handle some default field types that all templates can reuse
9
			add_filter( 'foogallery_alter_gallery_template_field', array( $this, 'alter_gallery_template_field' ), 10, 2 );
10
11
			//render the different types of fields for our gallery settings
12
			add_action( 'foogallery_render_gallery_template_field', array( $this, 'render_gallery_template_field' ), 10, 3 );
13
14
			//allow changing of field values
15
			add_filter( 'foogallery_render_gallery_template_field_value', array( $this, 'check_lightbox_value' ), 10, 4 );
16
17
			add_filter( 'foogallery_gallery_settings_metabox_section_icon', array( $this, 'add_section_icons') );
18
		}
19
20
		/**
21
		 * Renders a gallery template field into the gallery settings metabox for a FooGallery
22
		 *
23
		 * @param array $field
24
		 * @param       $gallery FooGallery
25
		 * @param       $template
26
		 */
27
		function render_gallery_template_field( $field = array(), $gallery, $template ) {
28
			$template_slug = $template['slug'];
29
30
			//only declare up front so no debug warnings are shown
31
			$type = $id = $desc = $default = $placeholder = $choices = $class = $spacer = $opactiy = null;
32
33
			extract( $field );
34
35
			$id = $template_slug . '_' . $id;
36
37
			$field['value'] = apply_filters( 'foogallery_render_gallery_template_field_value', $gallery->get_meta( $id, $default ), $field, $gallery, $template );
38
39
			$field_class = empty($class) ? '' : ' class="' . $class . '"';
40
41
			$field['choices'] = apply_filters( 'foogallery_render_gallery_template_field_choices', $choices, $field, $gallery );
42
43
			//allow for UI customization
44
			do_action( 'foogallery_render_gallery_template_field_before', $field, $gallery );
45
46
			echo '<div class="foogallery_metabox_field-' . $type . '">';
47
48
			switch ( $type ) {
49
50
				case 'html':
51
					echo $desc;
52
					$desc = '';
0 ignored issues
show
Unused Code introduced by
$desc is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
53
					break;
54
55
				case 'checkbox':
56
					if ( isset($gallery->settings[$id]) && $gallery->settings[$id] == 'on' ) {
57
						$field['value'] = 'on';
58
					} else if ( ! isset($gallery->settings) && $default == 'on' ) {
59
						$field['value'] = 'on';
60
					} else {
61
						$field['value'] = '';
62
					}
63
64
					$checked = 'on' === $field['value'] ? ' checked="checked"' : '';
65
					echo '<input' . $field_class . ' type="checkbox" id="FooGallerySettings_' . $id . '" name="' . FOOGALLERY_META_SETTINGS . '[' . $id . ']" value="on"' . $checked . ' />';
66
					break;
67
68
				case 'select':
69
					echo '<select' . $field_class . ' id="FooGallerySettings_' . $id . '" name="' . FOOGALLERY_META_SETTINGS . '[' . $id . ']">';
70
					foreach ( $choices as $value => $label ) {
0 ignored issues
show
Bug introduced by
The expression $choices of type null is not traversable.
Loading history...
71
						$selected = '';
72
						if ( $field['value'] == $value ) {
73
							$selected = ' selected="selected"';
74
						}
75
						echo '<option ' . $selected . ' value="' . $value . '">' . $label . '</option>';
76
					}
77
78
					echo '</select>';
79
					break;
80
81
				case 'radio':
82
					$i = 0;
83
					$spacer = isset($spacer) ? $spacer : '<br />';
84
					foreach ( $choices as $value => $label ) {
0 ignored issues
show
Bug introduced by
The expression $choices of type null is not traversable.
Loading history...
85
						$selected = '';
86
						if ( $field['value'] == $value ) {
87
							$selected = ' checked="checked"';
88
						}
89
						echo '<input' . $field_class . $selected . ' type="radio" name="' . FOOGALLERY_META_SETTINGS . '[' . $id . ']"  id="FooGallerySettings_' . $id . $i . '" value="' . $value . '"> <label for="FooGallerySettings_' . $id . $i . '">' . $label . '</label>';
90
						if ( $i < count( $choices ) - 1 ) {
91
							echo $spacer;
92
						}
93
						$i++;
94
					}
95
					break;
96
97
				case 'textarea':
98
					echo '<textarea' . $field_class . ' id="FooGallerySettings_' . $id . '" name="' . FOOGALLERY_META_SETTINGS . '[' . $id . ']" placeholder="' . $placeholder . '">' . esc_attr( $field['value'] ) . '</textarea>';
99
100
					break;
101
102
				case 'text':
103
					echo '<input' . $field_class . ' type="text" id="FooGallerySettings_' . $id . '" name="' . FOOGALLERY_META_SETTINGS . '[' . $id . ']" value="' . esc_attr( $field['value'] ) . '" />';
104
105
					break;
106
107
				case 'colorpicker':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
108
109
					$opacity_attribute = empty($opacity) ? '' : ' data-show-alpha="true"';
110
111
					echo '<input ' . $opacity_attribute . ' class="colorpicker" type="text" id="FooGallerySettings_' . $id . '" name="' . FOOGALLERY_META_SETTINGS . '[' . $id . ']" value="' . esc_attr( $field['value'] ) . '" />';
112
113
					break;
114
115
				case 'number':
116
					$min = isset($min) ? $min : 0;
117
					$step = isset($step) ? $step : 1;
118
					echo '<input class="regular-text ' . $class . '" type="number" step="' . $step . '" min="' . $min .'" id="FooGallerySettings_' . $id . '" name="' . FOOGALLERY_META_SETTINGS . '[' . $id . ']" placeholder="' . $placeholder . '" value="' . esc_attr( $field['value'] ) . '" />';
119
120
					break;
121
122
				case 'checkboxlist':
123
					$i = 0;
124
					foreach ( $choices as $value => $label ) {
125
126
						$checked = '';
127
						if ( isset($field['value'][$value]) && $field['value'][$value] == 'true' ) {
128
							$checked = 'checked="checked"';
129
						}
130
131
						echo '<input' . $field_class . ' ' . $checked . ' type="checkbox" name="' . FOOGALLERY_META_SETTINGS . '[' . $id . '|' . $value . ']" id="FooGallerySettings_' . $id . $i . '" value="on"> <label for="FooGallerySettings_' . $id . $i . '">' . $label . '</label>';
132
						if ( $i < count( $choices ) - 1 ) {
133
							echo '<br />';
134
						}
135
						$i++;
136
					}
137
138
					break;
139
				case 'icon':
140
					$i = 0;
141
					$input_name = FOOGALLERY_META_SETTINGS . '[' . $id . ']';
142
					$icon_html = '';
143
					foreach ( $choices as $value => $icon ) {
144
						$selected = ( $field['value'] == $value ) ? ' checked="checked"' : '';
145
						$icon_html .= '<input style="display:none" name="' . $input_name. '" id="FooGallerySettings_' . $id . $i . '" ' . $selected . ' type="radio" value="' . $value . '" tabindex="' . $i . '"/>';
146
						$title = $icon['label'];
147
						$img = $icon['img'];
148
						$icon_html .= '<label for="FooGallerySettings_' . $id . $i . '" title="' . $title . '"><img src="' . $img . '" /></label>';
149
						$i++;
150
					}
151
					echo $icon_html;
152
					break;
153
154
				case 'thumb_size':
155
					$width = is_array( $field['value'] ) ? $field['value']['width'] : 150;
156
					$height = is_array( $field['value'] ) ? $field['value']['height'] : 150;
157
					$crop = is_array( $field['value'] ) && array_key_exists( 'crop', $field['value'] ) ? $field['value']['crop'] : 0;
158
					$crop_checked = ( $crop == 1 ) ? ' checked="checked"' : '';
159
					echo '<label for="FooGallerySettings_' . $id . '_width">' . __( 'Width', 'foogallery' ) . '</label>';
160
					echo '<input class="small-text" type="number" step="1" min="0" id="FooGallerySettings_' . $id . '_width" name="' . FOOGALLERY_META_SETTINGS . '[' . $id . '][width]" value="' . esc_attr( $width ) . '" />';
161
					echo '<label for="FooGallerySettings_' . $id . '_width">' . __( 'Height', 'foogallery' ) . '</label>';
162
					echo '<input class="small-text" type="number" step="1" min="0" id="FooGallerySettings_' . $id . '_height" name="' . FOOGALLERY_META_SETTINGS . '[' . $id . '][height]" value="' . esc_attr( $height ) . '" />';
163
					echo '<div class="foogallery-thumbsize-crop"><input name="' . FOOGALLERY_META_SETTINGS . '[' . $id . '][crop]" type="hidden" id="FooGallerySettings_' . $id . '_nocrop" value="0" />';
164
					echo '<input name="' . FOOGALLERY_META_SETTINGS . '[' . $id . '][crop]" type="checkbox" id="FooGallerySettings_' . $id . '_crop" value="1"' . $crop_checked . '>';
165
					echo '<label for="FooGallerySettings_' . $id . '_crop">' . __( 'Crop thumbnail to exact dimensions', 'foogallery' ) . '</label></div>';
166
					break;
167
168
				default:
169
					do_action( 'foogallery_render_gallery_template_field_custom', $field, $gallery, $template );
170
					break;
171
			}
172
173
			if (!empty($suffix)) {
174
				echo $suffix;
175
			}
176
177
			echo '</div>';
178
179
			//allow for more customization
180
			do_action( 'foogallery_render_gallery_template_field_after', $field, $gallery );
181
		}
182
183
		function alter_gallery_template_field( $field, $gallery ) {
184
			if ( $field ) {
185
				switch ( $field['type'] ) {
186
					case 'thumb_link':
187
						$field['type'] = 'radio';
188
						$field['choices'] = $this->get_thumb_link_field_choices();
189
						break;
190
					case 'lightbox':
191
						$field['lightbox'] = true;
192
						$lightboxes = $this->get_lightbox_field_choices();
193
						if ( 1 === count( $lightboxes ) && array_key_exists( 'none', $lightboxes ) ) {
194
							$field['type'] = 'html';
195
							$field['desc'] = '<strong>' . __( 'You have no lightbox extensions activated!', 'foogallery' ) . '</strong><br />';
196
							$api = new FooGallery_Extensions_API();
197
							if ( $api->is_downloaded( false, FOOGALLERY_FOOBOX_PRO_EXTENSION_SLUG ) ) {
0 ignored issues
show
Documentation introduced by
FOOGALLERY_FOOBOX_PRO_EXTENSION_SLUG 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...
198
								//just need to activate it
199
								$foobox_install_link = foogallery_build_admin_menu_url( array(
200
									'page' => 'foogallery-extensions',
201
									'extension' => FOOGALLERY_FOOBOX_PRO_EXTENSION_SLUG,
202
									'action' => 'activate',
203
								));
204
								$field['desc'] .= '<a target="_blank" href="' . esc_url( $foobox_install_link ). '">' . __( 'Activate FooBox right now!', 'foogallery' ) . '</a>';
205
							} else if ( $api->is_downloaded( false, FOOGALLERY_FOOBOX_FREE_EXTENSION_SLUG ) ) {
0 ignored issues
show
Documentation introduced by
FOOGALLERY_FOOBOX_FREE_EXTENSION_SLUG 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...
206
								//just need to activate it
207
								$foobox_install_link = foogallery_build_admin_menu_url( array(
208
									'page' => 'foogallery-extensions',
209
									'extension' => FOOGALLERY_FOOBOX_FREE_EXTENSION_SLUG,
210
									'action' => 'activate',
211
								));
212
								$field['desc'] .= '<a target="_blank" href="' . esc_url( $foobox_install_link ). '">' . __( 'Activate FooBox FREE right now!', 'foogallery' ) . '</a>';
213
							} else {
214
								//we need to download it
215
								$foobox_install_link = foogallery_build_admin_menu_url( array(
216
									'page' => 'foogallery-extensions',
217
									'extension' => FOOGALLERY_FOOBOX_FREE_EXTENSION_SLUG,
218
									'action' => 'download',
219
								));
220
								$foobox_install_html = '<a target="_blank" href="' . esc_url( $foobox_install_link ) . '">' . __( 'Download and activate FooBox FREE', 'foogallery' ) . '</a>';
221
								$field['desc'] .= sprintf( __( '%s which works flawlessly with %s.', 'foogallery' ), $foobox_install_html, foogallery_plugin_name() );
222
							}
223
						} else {
224
							$field['type'] = 'select';
225
							$field['choices'] = $lightboxes;
226
						}
227
						break;
228
				}
229
230
				if ( isset($field['help']) && $field['help'] ) {
231
					$field['type'] = 'help';
232
				}
233
			}
234
			return $field;
235
		}
236
237
		function get_thumb_size_choices() {
238
			global $_wp_additional_image_sizes;
239
			$sizes = array();
240
			foreach( get_intermediate_image_sizes() as $s ){
241
				$sizes[ $s ] = array( 0, 0 );
242
				if ( in_array( $s, array( 'thumbnail', 'medium', 'large', ) ) ){
243
					$sizes[ $s ] = $s . ' (' . get_option( $s . '_size_w' ) . 'x' . get_option( $s . '_size_h' ) . ')';
244
				} else {
245
					if ( isset( $_wp_additional_image_sizes ) && isset( $_wp_additional_image_sizes[ $s ] ) )
246
						$sizes[ $s ] = $s . ' (' . $_wp_additional_image_sizes[ $s ]['width'] . 'x' . $_wp_additional_image_sizes[ $s ]['height'] . ')';
247
				}
248
			}
249
			return $sizes;
250
		}
251
252
		function get_thumb_link_field_choices() {
253
			return apply_filters( 'foogallery_gallery_template_field_thumb_links', array(
254
				'image'  => __( 'Full Size Image', 'foogallery' ),
255
				'page'   => __( 'Image Attachment Page', 'foogallery' ),
256
				'custom' => __( 'Custom URL', 'foogallery' ),
257
				'none'   => __( 'Not linked', 'foogallery' ),
258
			) );
259
		}
260
261
		function get_lightbox_field_choices() {
262
			$lightboxes = apply_filters( 'foogallery_gallery_template_field_lightboxes', array() );
263
			$lightboxes['none'] = __( 'None', 'foogallery' );
264
			return $lightboxes;
265
		}
266
267
		/***
268
		 * Check if we have a lightbox value from FooBox free and change it if foobox free is no longer active
269
		 * @param $value
270
		 * @param $field
271
		 * @param $gallery
272
		 * @param $template
273
		 *
274
		 * @return string
275
		 */
276
		function check_lightbox_value($value, $field, $gallery, $template) {
277
278
			if ( isset( $field['lightbox'] ) ) {
279
				if ( 'foobox-free' === $value ) {
280
					if ( !class_exists( 'Foobox_Free' ) ) {
281
						return 'foobox';
282
					}
283
				}
284
			}
285
286
			return $value;
287
		}
288
289
        /**
290
         * Returns the Dashicon that can be used in the settings tabs
291
         * @param $section_slug
292
         * @return string
293
         */
294
		function add_section_icons( $section_slug ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
295
		    switch ( $section_slug ) {
296
                case 'general':
297
                    return 'dashicons-admin-tools';
298
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
299
                case 'advanced':
300
                    return 'dashicons-admin-generic';
301
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
302
                case 'look &amp; feel':
303
                    return 'dashicons-images-alt2';
304
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
305
                case 'video':
306
                    return 'dashicons-format-video';
307
            }
308
		    return 'dashicons-admin-tools';
309
        }
310
	}
311
}
312