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
Pull Request — develop (#63)
by David
02:31
created

FooGallery_Attachment_Fields::save_fields()   C

Complexity

Conditions 11
Paths 2

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 38
rs 5.2653
cc 11
eloc 21
nc 2
nop 2

How to fix   Complexity   

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
 * class FooGallery_Attachment_Fields
4
 *
5
 * Add custom fields to media attachments
6
 */
7
if (!class_exists('FooGallery_Attachment_Fields')) {
8
9
    class FooGallery_Attachment_Fields {
10
11
        function __construct() {
12
	        add_filter( 'attachment_fields_to_edit', array( $this, 'add_fields' ), 9, 2 );
13
	        add_filter( 'attachment_fields_to_save', array( $this, 'save_fields' ), 11, 2 );
14
        }
15
16
	    public function get_custom_fields() {
17
18
		    $target_options = apply_filters( 'foogallery_attachment_field_custom_target_options',  array(
19
			    'default' => __( 'Default', 'foogallery' ),
20
			    '_blank' => __( 'New tab (_blank)', 'foogallery' ),
21
			    '_self' => __( 'Same tab (_self)', 'foogallery' ),
22
			    'foobox' => __( 'FooBox', 'foogallery' )
23
		    ) );
24
25
		    $fields = array(
26
			    'foogallery_custom_url' => array(
27
				    'label'       =>  sprintf( __( '%s Custom URL', 'foogallery' ), foogallery_plugin_name() ),
28
				    'input'       => 'text', //other types are 'textarea', 'checkbox', 'radio', 'select',
0 ignored issues
show
Unused Code Comprehensibility introduced by
48% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
29
				    'helps'       => __( 'Point your attachment to a custom URL', 'foogallery' ),
30
				    'exclusions'  => array( 'audio', 'video' ),
31
			    ),
32
33
			    'foogallery_custom_target' => array(
34
				    'label'       =>  sprintf( __( '%s Custom Target', 'foogallery' ), foogallery_plugin_name() ),
35
				    'input'       => 'select',
36
				    'helps'       => __( 'Set a custom target for your attachment', 'foogallery' ),
37
				    'exclusions'  => array( 'audio', 'video' ),
38
				    'options'     => $target_options
39
			    )
40
		    );
41
42
		    return apply_filters( 'foogallery_attachment_custom_fields', $fields );
43
	    }
44
45
	    /**
46
	     * @param      $form_fields
47
	     * @param WP_Post $post
48
	     *
49
	     * @return mixed
50
	     */
51
	    public function add_fields( $form_fields, $post = null ) {
52
		    $custom_fields = $this->get_custom_fields();
53
54
		    // If our fields array is not empty
55
		    if ( ! empty( $custom_fields ) ) {
56
			    // We browse our set of options
57
			    foreach ( $custom_fields as $field => $values ) {
58
				    // If the field matches the current attachment mime type
59
				    // and is not one of the exclusions
60
				    if ( !in_array( $post->post_mime_type, $values['exclusions'] ) ) {
61
					    // We get the already saved field meta value
62
					    $meta = apply_filters( 'foogallery_attachment_custom_field_value', get_post_meta( $post->ID, '_' . $field, true ), $post->ID, $field, $values );
63
64
					    switch ( $values['input'] ) {
65
						    default:
66
						    case 'text':
67
							    $values['input'] = 'text';
68
							    break;
69
70
						    case 'textarea':
71
							    $values['input'] = 'textarea';
72
							    break;
73
74
						    case 'select':
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...
75
76
							    // Select type doesn't exist, so we will create the html manually
77
							    // For this, we have to set the input type to 'html'
78
							    $values['input'] = 'html';
79
80
							    // Create the select element with the right name (matches the one that wordpress creates for custom fields)
81
							    $html = '<select name="attachments[' . $post->ID . '][' . $field . ']">';
82
83
							    // If options array is passed
84
							    if ( isset( $values['options'] ) ) {
85
								    // Browse and add the options
86
								    foreach ( $values['options'] as $k => $v ) {
87
									    // Set the option selected or not
88
									    if ( $meta == $k )
89
										    $selected = ' selected="selected"';
90
									    else
91
										    $selected = '';
92
93
									    $html .= '<option' . $selected . ' value="' . $k . '">' . $v . '</option>';
94
								    }
95
							    }
96
97
							    $html .= '</select>';
98
99
							    // Set the html content
100
							    $values['html'] = $html;
101
102
							    break;
103
104
						    case 'checkbox':
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...
105
106
							    // Checkbox type doesn't exist either
107
							    $values['input'] = 'html';
108
109
							    // Set the checkbox checked or not
110
							    if ( $meta == 'on' )
111
								    $checked = ' checked="checked"';
112
							    else
113
								    $checked = '';
114
115
							    $html = '<input' . $checked . ' type="checkbox" name="attachments[' . $post->ID . '][' . $field . ']" id="attachments-' . $post->ID . '-' . $field . '" />';
116
117
							    $values['html'] = $html;
118
119
							    break;
120
121
						    case 'radio':
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...
122
123
							    // radio type doesn't exist either
124
							    $values['input'] = 'html';
125
126
							    $html = '';
127
128
							    if ( ! empty( $values['options'] ) ) {
129
								    $i = 0;
130
131
								    foreach ( $values['options'] as $k => $v ) {
132
									    if ( $meta == $k )
133
										    $checked = ' checked="checked"';
134
									    else
135
										    $checked = '';
136
137
									    $html .= '<input' . $checked . ' value="' . $k . '" type="radio" name="attachments[' . $post->ID . '][' . $field . ']" id="' . sanitize_key( $field . '_' . $post->ID . '_' . $i ) . '" /> <label for="' . sanitize_key( $field . '_' . $post->ID . '_' . $i ) . '">' . $v . '</label><br />';
138
									    $i++;
139
								    }
140
							    }
141
142
							    $values['html'] = $html;
143
144
							    break;
145
					    }
146
147
					    // And set it to the field before building it
148
					    $values['value'] = $meta;
149
150
					    // We add our field into the $form_fields array
151
					    $form_fields[$field] = apply_filters( 'foogallery_attachment_field_' . $field, $values, $post->ID );
152
				    }
153
			    }
154
		    }
155
156
		    // We return the completed $form_fields array
157
		    return apply_filters( 'foogallery_attachment_add_fields', $form_fields );
158
	    }
159
160
	    function save_fields( $post, $attachment ) {
161
		    $custom_fields = $this->get_custom_fields();
162
163
		    // If our fields array is not empty
164
		    if ( ! empty( $custom_fields ) ) {
165
			    // We browse our set of options
166
			    foreach ( $custom_fields as $field => $values ) {
167
				    switch ( $values['input'] ) {
168
					    case 'text':
169
					    case 'textarea':
170
					    case 'select':
171
					    case 'radio':
172
					    case 'checkbox':
173
						    // If this field has been submitted (is present in the $attachment variable)
174
						    if ( isset( $attachment[$field] ) ) {
175
							    // If submitted field is empty
176
							    // We add errors to the post object with the "error_text" parameter if set in the options
177
							    if ( strlen( trim( $attachment[$field] ) ) == 0 && isset( $values['error_text'] ) ) {
178
								    $post['errors'][ $field ]['errors'][] = __( $values['error_text'] );
179
								    // Otherwise we update the custom field
180
							    } else {
181
								    update_post_meta( $post['ID'], '_' . $field, $attachment[ $field ] );
182
							    }
183
						    }
184
						    // Otherwise, we delete it if it already existed
185
						    else {
186
							    delete_post_meta( $post['ID'], $field );
187
						    }
188
					        break;
189
190
					    default:
191
						    do_action( 'foogallery_attachment_save_field_' . $values['input'], $field, $values, $post, $attachment);
192
				    }
193
			    }
194
		    }
195
196
		    return $post;
197
	    }
198
    }
199
}