| Conditions | 14 |
| Paths | 76 |
| Total Lines | 158 |
| Lines | 25 |
| Ratio | 15.82 % |
| 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 |
||
| 183 | function import() { |
||
| 184 | |||
| 185 | // validate |
||
| 186 | if( empty($_FILES['acf_import_file']) ) { |
||
| 187 | |||
| 188 | acf_add_admin_notice( __("No file selected", 'acf') , 'error'); |
||
| 189 | return; |
||
| 190 | |||
| 191 | } |
||
| 192 | |||
| 193 | |||
| 194 | // vars |
||
| 195 | $file = $_FILES['acf_import_file']; |
||
| 196 | |||
| 197 | |||
| 198 | // validate error |
||
| 199 | if( $file['error'] ) { |
||
| 200 | |||
| 201 | acf_add_admin_notice(__('Error uploading file. Please try again', 'acf'), 'error'); |
||
| 202 | return; |
||
| 203 | |||
| 204 | } |
||
| 205 | |||
| 206 | |||
| 207 | // validate type |
||
| 208 | if( pathinfo($file['name'], PATHINFO_EXTENSION) !== 'json' ) { |
||
| 209 | |||
| 210 | acf_add_admin_notice(__('Incorrect file type', 'acf'), 'error'); |
||
| 211 | return; |
||
| 212 | |||
| 213 | } |
||
| 214 | |||
| 215 | |||
| 216 | // read file |
||
| 217 | $json = file_get_contents( $file['tmp_name'] ); |
||
| 218 | |||
| 219 | |||
| 220 | // decode json |
||
| 221 | $json = json_decode($json, true); |
||
| 222 | |||
| 223 | |||
| 224 | // validate json |
||
| 225 | if( empty($json) ) { |
||
| 226 | |||
| 227 | acf_add_admin_notice(__('Import file empty', 'acf'), 'error'); |
||
| 228 | return; |
||
| 229 | |||
| 230 | } |
||
| 231 | |||
| 232 | |||
| 233 | // if importing an auto-json, wrap field group in array |
||
| 234 | if( isset($json['key']) ) { |
||
| 235 | |||
| 236 | $json = array( $json ); |
||
| 237 | |||
| 238 | } |
||
| 239 | |||
| 240 | |||
| 241 | // vars |
||
| 242 | $added = array(); |
||
| 243 | $ignored = array(); |
||
| 244 | $ref = array(); |
||
| 245 | $order = array(); |
||
| 246 | |||
| 247 | foreach( $json as $field_group ) { |
||
| 248 | |||
| 249 | // check if field group exists |
||
| 250 | if( acf_get_field_group($field_group['key'], true) ) { |
||
| 251 | |||
| 252 | // append to ignored |
||
| 253 | $ignored[] = $field_group['title']; |
||
| 254 | continue; |
||
| 255 | |||
| 256 | } |
||
| 257 | |||
| 258 | |||
| 259 | // remove fields |
||
| 260 | $fields = acf_extract_var($field_group, 'fields'); |
||
| 261 | |||
| 262 | |||
| 263 | // format fields |
||
| 264 | $fields = acf_prepare_fields_for_import( $fields ); |
||
| 265 | |||
| 266 | |||
| 267 | // save field group |
||
| 268 | $field_group = acf_update_field_group( $field_group ); |
||
| 269 | |||
| 270 | |||
| 271 | // add to ref |
||
| 272 | $ref[ $field_group['key'] ] = $field_group['ID']; |
||
| 273 | |||
| 274 | |||
| 275 | // add to order |
||
| 276 | $order[ $field_group['ID'] ] = 0; |
||
| 277 | |||
| 278 | |||
| 279 | // add fields |
||
| 280 | foreach( $fields as $field ) { |
||
| 281 | |||
| 282 | // add parent |
||
| 283 | View Code Duplication | if( empty($field['parent']) ) { |
|
| 284 | |||
| 285 | $field['parent'] = $field_group['ID']; |
||
| 286 | |||
| 287 | } elseif( isset($ref[ $field['parent'] ]) ) { |
||
| 288 | |||
| 289 | $field['parent'] = $ref[ $field['parent'] ]; |
||
| 290 | |||
| 291 | } |
||
| 292 | |||
| 293 | |||
| 294 | // add field menu_order |
||
| 295 | if( !isset($order[ $field['parent'] ]) ) { |
||
| 296 | |||
| 297 | $order[ $field['parent'] ] = 0; |
||
| 298 | |||
| 299 | } |
||
| 300 | |||
| 301 | $field['menu_order'] = $order[ $field['parent'] ]; |
||
| 302 | $order[ $field['parent'] ]++; |
||
| 303 | |||
| 304 | |||
| 305 | // save field |
||
| 306 | $field = acf_update_field( $field ); |
||
| 307 | |||
| 308 | |||
| 309 | // add to ref |
||
| 310 | $ref[ $field['key'] ] = $field['ID']; |
||
| 311 | |||
| 312 | } |
||
| 313 | |||
| 314 | // append to added |
||
| 315 | $added[] = '<a href="' . admin_url("post.php?post={$field_group['ID']}&action=edit") . '" target="_blank">' . $field_group['title'] . '</a>'; |
||
| 316 | |||
| 317 | } |
||
| 318 | |||
| 319 | |||
| 320 | // messages |
||
| 321 | View Code Duplication | if( !empty($added) ) { |
|
| 322 | |||
| 323 | $message = __('<b>Success</b>. Import tool added %s field groups: %s', 'acf'); |
||
| 324 | $message = sprintf( $message, count($added), implode(', ', $added) ); |
||
| 325 | |||
| 326 | acf_add_admin_notice( $message ); |
||
| 327 | |||
| 328 | } |
||
| 329 | |||
| 330 | View Code Duplication | if( !empty($ignored) ) { |
|
| 331 | |||
| 332 | $message = __('<b>Warning</b>. Import tool detected %s field groups already exist and have been ignored: %s', 'acf'); |
||
| 333 | $message = sprintf( $message, count($ignored), implode(', ', $ignored) ); |
||
| 334 | |||
| 335 | acf_add_admin_notice( $message, 'error' ); |
||
| 336 | |||
| 337 | } |
||
| 338 | |||
| 339 | |||
| 340 | } |
||
| 341 | |||
| 468 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.