Conditions | 42 |
Paths | > 20000 |
Total Lines | 237 |
Code Lines | 149 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
69 | public function showform( $id = null, $public_fields = null, $label = 'Save changes' ) { |
||
70 | |||
71 | if ( Pod::$deprecated_notice ) { |
||
72 | pods_deprecated( 'Pods::showform', '2.0' ); |
||
73 | } |
||
74 | |||
75 | $public_columns =& $public_fields; |
||
76 | |||
77 | $pod = $this->obj->pod; |
||
78 | $pod_id = $this->obj->pod_id; |
||
79 | $this->obj->type_counter = array(); |
||
80 | |||
81 | if ( ! empty( $public_fields ) ) { |
||
82 | $attributes = array(); |
||
83 | |||
84 | foreach ( $public_fields as $key => $value ) { |
||
85 | if ( is_array( $public_fields[ $key ] ) ) { |
||
86 | $attributes[ $key ] = $value; |
||
87 | } else { |
||
88 | $attributes[ $value ] = array(); |
||
89 | } |
||
90 | } |
||
91 | } |
||
92 | |||
93 | $fields = $this->obj->fields; |
||
94 | |||
95 | // Re-order the fields if a public form |
||
96 | if ( ! empty( $attributes ) ) { |
||
97 | $fields = array(); |
||
98 | |||
99 | foreach ( $attributes as $key => $value ) { |
||
100 | if ( isset( $this->obj->fields[ $key ] ) ) { |
||
101 | $fields[ $key ] = $this->obj->fields[ $key ]; |
||
102 | } |
||
103 | } |
||
104 | } |
||
105 | |||
106 | do_action( 'pods_showform_pre', $pod_id, $public_fields, $label, $this ); |
||
107 | |||
108 | foreach ( $fields as $key => $field ) { |
||
109 | if ( ! is_array( $field ) || in_array( $key, array( 'created', 'modified' ), true ) ) { |
||
110 | continue; |
||
111 | } |
||
112 | |||
113 | // Pass options so they can be manipulated via form |
||
114 | $field = array_merge( $field['options'], $field ); |
||
115 | |||
116 | // Replace field attributes with public form attributes |
||
117 | if ( ! empty( $attributes ) && is_array( $attributes[ $key ] ) ) { |
||
118 | $field = array_merge( $field, $attributes[ $key ] ); |
||
119 | } |
||
120 | |||
121 | // Replace the input helper name with the helper code |
||
122 | if ( ! empty( $field['input_helper'] ) ) { |
||
123 | $helper = $this->obj->api->load_helper( array( 'name' => $field['input_helper'] ) ); |
||
124 | $field['input_helper'] = ''; |
||
125 | |||
126 | if ( ! empty( $helper ) ) { |
||
127 | $field['input_helper'] = $helper['code']; |
||
128 | } |
||
129 | } |
||
130 | |||
131 | if ( empty( $field['label'] ) ) { |
||
132 | $field['label'] = ucwords( $key ); |
||
133 | } |
||
134 | |||
135 | if ( 1 == $field['required'] ) { |
||
136 | $field['label'] .= ' <span class="red">*</span>'; |
||
137 | } |
||
138 | |||
139 | if ( ! empty( $field['pick_val'] ) ) { |
||
140 | $selected_ids = array(); |
||
141 | $pick_object = $field['pick_object']; |
||
142 | $pick_val = $field['pick_val']; |
||
143 | |||
144 | if ( 'pod' === $pick_object ) { |
||
145 | $pick_pod = $this->obj->api->load_pod( array( 'name' => $pick_val ) ); |
||
146 | $pick_object = $pick_pod['type']; |
||
147 | $pick_val = $pick_pod['name']; |
||
148 | } |
||
149 | |||
150 | $pick_table = $pick_join = $pick_where = ''; |
||
151 | |||
152 | $pick_field_id = 'id'; |
||
153 | $pick_field_name = 'name'; |
||
154 | switch ( $pick_object ) { |
||
155 | case 'pod': |
||
156 | $pick_table = "@wp_pods_{$pick_val}"; |
||
157 | $pick_field_id = 'id'; |
||
158 | $pick_field_name = 'name'; |
||
159 | break; |
||
160 | case 'post_type': |
||
161 | $pick_table = '@wp_posts'; |
||
162 | $pick_field_id = 'ID'; |
||
163 | $pick_field_name = 'post_title'; |
||
164 | $pick_where = "t.`post_type` = '{$pick_val}'"; |
||
165 | break; |
||
166 | case 'taxonomy': |
||
167 | $pick_table = '@wp_terms'; |
||
168 | $pick_field_id = 'term_id'; |
||
169 | $pick_field_name = 'name'; |
||
170 | $pick_join = '`@wp_term_taxonomy` AS tx ON tx.`term_id` = t.`term_id'; |
||
171 | $pick_where = "tx.`taxonomy` = '{$pick_val}' AND tx.`taxonomy` IS NOT NULL"; |
||
172 | break; |
||
173 | case 'user': |
||
174 | $pick_table = '@wp_users'; |
||
175 | $pick_field_id = 'ID'; |
||
176 | $pick_field_name = 'user_login'; |
||
177 | break; |
||
178 | case 'comment': |
||
179 | $pick_table = '@wp_comments'; |
||
180 | $pick_field_id = 'comment_ID'; |
||
181 | $pick_field_name = 'comment_date'; |
||
182 | $pick_where = "t.`comment_type` = '{$pick_val}'"; |
||
183 | break; |
||
184 | case 'table': |
||
185 | $pick_table = "{$pick_val}"; |
||
186 | $pick_field_id = 'id'; |
||
187 | $pick_field_name = 'name'; |
||
188 | break; |
||
189 | }//end switch |
||
190 | |||
191 | $sql = 'SELECT `related_item_id` FROM `@wp_podsrel` WHERE `item_id` = %d AND `field_id` = %d'; |
||
192 | |||
193 | $sql = array( $sql, array( $id, $field['id'] ) ); |
||
194 | |||
195 | $result = pods_query( $sql, $this ); |
||
196 | |||
197 | foreach ( $result as $row ) { |
||
198 | $selected_ids[] = $row->related_item_id; |
||
199 | } |
||
200 | |||
201 | // Use default values for public forms |
||
202 | if ( empty( $selected_ids ) && ! empty( $field['default'] ) ) { |
||
203 | $default_ids = $field['default']; |
||
204 | |||
205 | if ( ! is_array( $field['default'] ) ) { |
||
206 | $default_ids = explode( ',', $default_ids ); |
||
207 | } |
||
208 | |||
209 | foreach ( $default_ids as $default_id ) { |
||
210 | $default_id = pods_absint( $default_id ); |
||
211 | |||
212 | if ( 0 < $default_id ) { |
||
213 | $selected_ids[] = $default_id; |
||
214 | } |
||
215 | } |
||
216 | } |
||
217 | |||
218 | // If the PICK field is unique, get values already chosen |
||
219 | $exclude = false; |
||
220 | |||
221 | if ( 1 == $field['unique'] ) { |
||
222 | $unique_where = ( empty( $id ) ) ? '' : ' AND `item_id` != %d'; |
||
223 | |||
224 | $sql = "SELECT `related_item_id` FROM `@wp_podsrel` WHERE `field_id` = %d {$unique_where}"; |
||
225 | |||
226 | $sql = array( $sql, array( $field['id'] ) ); |
||
227 | |||
228 | if ( ! empty( $id ) ) { |
||
229 | $sql[1][] = $id; |
||
230 | } |
||
231 | |||
232 | $result = pods_query( $sql, $this ); |
||
233 | |||
234 | if ( ! empty( $result ) ) { |
||
235 | |||
236 | $exclude = array(); |
||
237 | |||
238 | foreach ( $result as $row ) { |
||
239 | $exclude[] = (int) $row->related_item_id; |
||
240 | } |
||
241 | |||
242 | $exclude = implode( ',', $exclude ); |
||
243 | } |
||
244 | }//end if |
||
245 | |||
246 | if ( ! empty( $field['options']['pick_filter'] ) ) { |
||
247 | $pick_where .= ' AND ' . $field['options']['pick_filter']; |
||
248 | } |
||
249 | |||
250 | $params = array( |
||
251 | 'exclude' => $exclude, |
||
252 | 'selected_ids' => $selected_ids, |
||
253 | 'table' => $pick_table, |
||
254 | 'field_id' => $pick_field_id, |
||
255 | 'field_name' => $pick_field_name, |
||
256 | 'join' => $pick_join, |
||
257 | 'orderby' => $field['options']['pick_orderby'], |
||
258 | 'where' => $pick_where, |
||
259 | ); |
||
260 | |||
261 | $this->obj->row[ $key ] = $this->get_dropdown_values( $params ); |
||
262 | } else { |
||
263 | // Set a default value if no value is entered |
||
264 | if ( ! isset( $this->obj->row[ $key ] ) || ( null === $this->obj->row[ $key ] || false === $this->obj->row[ $key ] ) ) { |
||
265 | if ( ! empty( $field['default'] ) ) { |
||
266 | $this->obj->row[ $key ] = $field['default']; |
||
267 | } else { |
||
268 | $this->obj->row[ $key ] = null; |
||
269 | } |
||
270 | } |
||
271 | }//end if |
||
272 | |||
273 | $this->obj->build_field_html( $field ); |
||
274 | }//end foreach |
||
275 | |||
276 | $uri_hash = wp_hash( $_SERVER['REQUEST_URI'] ); |
||
277 | |||
278 | $save_button_atts = array( |
||
279 | 'type' => 'button', |
||
280 | 'class' => 'button btn_save', |
||
281 | 'value' => $label, |
||
282 | 'onclick' => 'saveForm(1)', |
||
283 | ); |
||
284 | |||
285 | $save_button_atts = apply_filters( 'pods_showform_save_button_atts', $save_button_atts, $this ); |
||
286 | $atts = ''; |
||
287 | |||
288 | foreach ( $save_button_atts as $att => $value ) { |
||
289 | $atts .= ' ' . esc_attr( $att ) . '="' . esc_attr( $value ) . '"'; |
||
290 | } |
||
291 | |||
292 | $save_button = '<input ' . $atts . '/>'; |
||
293 | ?> |
||
294 | <div> |
||
295 | <input type="hidden" class="form num id" value="<?php echo esc_attr( $id ); ?>" /> |
||
296 | <input type="hidden" class="form txt pod" value="<?php echo esc_attr( $pod ); ?>" /> |
||
297 | <input type="hidden" class="form txt pod_id" value="<?php echo esc_attr( $pod_id ); ?>" /> |
||
298 | <input type="hidden" class="form txt form_count" value="1" /> |
||
299 | <input type="hidden" class="form txt token" value="<?php echo esc_attr( pods_generate_key( $pod, $uri_hash, $public_fields, 1 ) ); ?>" /> |
||
300 | <input type="hidden" class="form txt uri_hash" value="<?php echo esc_attr( $uri_hash ); ?>" /> |
||
301 | <?php echo apply_filters( 'pods_showform_save_button', $save_button, $save_button_atts, $this ); ?> |
||
302 | </div> |
||
303 | <?php |
||
304 | do_action( 'pods_showform_post', $pod_id, $public_fields, $label, $this ); |
||
305 | } |
||
306 | |||
734 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.