| Conditions | 38 |
| Paths | 122 |
| Total Lines | 269 |
| Code Lines | 127 |
| 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 |
||
| 276 | public static function output_fields( $options, $option_name = '' ) { |
||
| 277 | $current_tab = give_get_current_setting_tab(); |
||
| 278 | |||
| 279 | // Field Default values. |
||
| 280 | $defaults = array( |
||
| 281 | 'id' => '', |
||
| 282 | 'name' => '', |
||
| 283 | 'class' => '', |
||
| 284 | 'css' => '', |
||
| 285 | 'default' => '', |
||
| 286 | 'desc' => '', |
||
| 287 | 'table_html' => true, |
||
| 288 | ); |
||
| 289 | |||
| 290 | foreach ( $options as $value ) { |
||
| 291 | if ( ! isset( $value['type'] ) ) { |
||
| 292 | continue; |
||
| 293 | } |
||
| 294 | |||
| 295 | // Set default setting. |
||
| 296 | $value = wp_parse_args( $value, $defaults ); |
||
| 297 | |||
| 298 | |||
| 299 | // Custom attribute handling. |
||
| 300 | $custom_attributes = array(); |
||
| 301 | |||
| 302 | if ( ! empty( $value['attributes'] ) && is_array( $value['attributes'] ) ) { |
||
| 303 | foreach ( $value['attributes'] as $attribute => $attribute_value ) { |
||
| 304 | $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"'; |
||
| 305 | } |
||
| 306 | } |
||
| 307 | |||
| 308 | // Description handling. |
||
| 309 | $description = self::get_field_description( $value ); |
||
| 310 | |||
| 311 | // Switch based on type. |
||
| 312 | switch ( $value['type'] ) { |
||
| 313 | |||
| 314 | // Section Titles |
||
| 315 | case 'title': |
||
| 316 | if ( self::get_field_title( $value ) ) { |
||
| 317 | echo '<div class="give-setting-tab-header give-setting-tab-header-' . $current_tab . '"><h2>' . self::get_field_title( $value ) . '</h2><hr></div>'; |
||
| 318 | } |
||
| 319 | |||
| 320 | if ( ! empty( $value['desc'] ) ) { |
||
| 321 | echo wpautop( wptexturize( wp_kses_post( $value['desc'] ) ) ); |
||
| 322 | } |
||
| 323 | |||
| 324 | if ( $value['table_html'] ) { |
||
| 325 | echo '<table class="form-table give-setting-tab-body give-setting-tab-body-' . $current_tab . '">' . "\n\n"; |
||
| 326 | } |
||
| 327 | |||
| 328 | if ( ! empty( $value['id'] ) ) { |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Trigger Action. |
||
| 332 | * |
||
| 333 | * Note: action dynamically fire on basis of field id. |
||
| 334 | * |
||
| 335 | * @since 1.8 |
||
| 336 | */ |
||
| 337 | do_action( 'give_settings_' . sanitize_title( $value['id'] ) ); |
||
| 338 | } |
||
| 339 | |||
| 340 | break; |
||
| 341 | |||
| 342 | // Section Ends. |
||
| 343 | case 'sectionend': |
||
| 344 | if ( ! empty( $value['id'] ) ) { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Trigger Action. |
||
| 348 | * |
||
| 349 | * Note: action dynamically fire on basis of field id. |
||
| 350 | * |
||
| 351 | * @since 1.8 |
||
| 352 | */ |
||
| 353 | do_action( 'give_settings_' . sanitize_title( $value['id'] ) . '_end' ); |
||
| 354 | } |
||
| 355 | |||
| 356 | if ( $value['table_html'] ) { |
||
| 357 | echo '</table>'; |
||
| 358 | } |
||
| 359 | |||
| 360 | if ( ! empty( $value['id'] ) ) { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Trigger Action. |
||
| 364 | * |
||
| 365 | * Note: action dynamically fire on basis of field id. |
||
| 366 | * |
||
| 367 | * @since 1.8 |
||
| 368 | */ |
||
| 369 | do_action( 'give_settings_' . sanitize_title( $value['id'] ) . '_after' ); |
||
| 370 | } |
||
| 371 | |||
| 372 | break; |
||
| 373 | |||
| 374 | // Standard text inputs and subtypes like 'number'. |
||
| 375 | case 'colorpicker': |
||
| 376 | $value['field_attributes']['class'] = trim( $value['class'] ) . ' give-colorpicker'; |
||
| 377 | $value['type'] = 'text'; |
||
| 378 | |||
| 379 | case 'api_key' : |
||
| 380 | $value['value'] = self::get_option( $option_name, $value['id'], $value['default'] ); |
||
| 381 | $value['type'] = ! empty( $value['value'] ) ? 'password' : 'text'; |
||
| 382 | |||
| 383 | case 'text': |
||
| 384 | case 'email': |
||
| 385 | case 'number': |
||
| 386 | case 'password' : |
||
| 387 | $value = give_backward_compatibility_setting_api_1_8( $value ); |
||
| 388 | |||
| 389 | // Set layout. |
||
| 390 | $value = array_merge( $value, self::get_field_wrapper( $value, $option_name ) ); |
||
| 391 | |||
| 392 | // Add input specific class. |
||
| 393 | $value['field_attributes']['class'] = empty( $value['field_attributes']['class'] ) |
||
| 394 | ? 'give-input-field' |
||
| 395 | : trim( $value['field_attributes']['class'] ) . ' give-input-field'; |
||
| 396 | |||
| 397 | // Render function. |
||
| 398 | echo Give_Fields_API::render_tag( $value ); |
||
| 399 | |||
| 400 | break; |
||
| 401 | |||
| 402 | // Textarea. |
||
| 403 | case 'textarea': |
||
| 404 | $value = give_backward_compatibility_setting_api_1_8( $value ); |
||
| 405 | |||
| 406 | // Set field value. |
||
| 407 | $value['value'] = esc_textarea( self::get_option( $option_name, $value['id'], $value['default'] ) ); |
||
| 408 | |||
| 409 | // Set layout. |
||
| 410 | $value = array_merge( $value, self::get_field_wrapper( $value, $option_name ) ); |
||
| 411 | |||
| 412 | // Add rows and cols for textarea. |
||
| 413 | $value['field_attributes']['rows'] = 10; |
||
| 414 | $value['field_attributes']['cols'] = 60; |
||
| 415 | |||
| 416 | echo Give_Fields_API::render_tag( $value ); |
||
| 417 | break; |
||
| 418 | |||
| 419 | // Select boxes. |
||
| 420 | case 'select' : |
||
| 421 | case 'multiselect' : |
||
| 422 | $value = give_backward_compatibility_setting_api_1_8( $value ); |
||
| 423 | |||
| 424 | // Set layout. |
||
| 425 | $value = array_merge( $value, self::get_field_wrapper( $value, $option_name ) ); |
||
| 426 | |||
| 427 | // Update td wrapper. |
||
| 428 | $value['before_field'] = '<td class="give-forminp give-forminp-' . sanitize_title( $value['type'] ) . '"><fieldset>'; |
||
| 429 | $value['after_field'] = "{$description}</fieldset></td>"; |
||
| 430 | |||
| 431 | |||
| 432 | // Update param for radio_inline field type. |
||
| 433 | if( 'radio_inline' === $value['type'] ) { |
||
| 434 | $value['type'] = 'radio'; |
||
| 435 | $value['wrapper_attributes']['class'] = empty( $value['wrapper_attributes']['class'] ) |
||
| 436 | ? 'give-radio-inline' |
||
| 437 | : trim( $value['wrapper_attributes']['class'] ) . ' give-radio-inline'; |
||
| 438 | } |
||
| 439 | |||
| 440 | echo Give_Fields_API::render_tag( $value ); |
||
| 441 | break; |
||
| 442 | |||
| 443 | // Radio inputs. |
||
| 444 | case 'radio_inline' : |
||
| 445 | case 'radio' : |
||
| 446 | $value = give_backward_compatibility_setting_api_1_8( $value ); |
||
| 447 | |||
| 448 | // Set layout. |
||
| 449 | $value = array_merge( $value, self::get_field_wrapper( $value, $option_name ) ); |
||
| 450 | |||
| 451 | // Update td wrapper. |
||
| 452 | $value['before_field'] = '<td class="give-forminp give-forminp-' . sanitize_title( $value['type'] ) . '"><fieldset>'; |
||
| 453 | $value['after_field'] = "{$description}</fieldset></td>"; |
||
| 454 | |||
| 455 | |||
| 456 | // Update param for radio_inline field type. |
||
| 457 | if( 'radio_inline' === $value['type'] ) { |
||
| 458 | $value['type'] = 'radio'; |
||
| 459 | $value['wrapper_attributes']['class'] = empty( $value['wrapper_attributes']['class'] ) |
||
| 460 | ? 'give-radio-inline' |
||
| 461 | : trim( $value['wrapper_attributes']['class'] ) . ' give-radio-inline'; |
||
| 462 | } |
||
| 463 | |||
| 464 | echo Give_Fields_API::render_tag( $value ); |
||
| 465 | break; |
||
| 466 | |||
| 467 | // Checkbox input. |
||
| 468 | case 'checkbox' : |
||
| 469 | $value = give_backward_compatibility_setting_api_1_8( $value ); |
||
| 470 | |||
| 471 | // Set layout. |
||
| 472 | $value = array_merge( $value, self::get_field_wrapper( $value, $option_name ) ); |
||
| 473 | |||
| 474 | echo Give_Fields_API::render_tag( $value ); |
||
| 475 | break; |
||
| 476 | |||
| 477 | // Multi Checkbox input. |
||
| 478 | case 'multicheck' : |
||
| 479 | $value = give_backward_compatibility_setting_api_1_8( $value ); |
||
| 480 | |||
| 481 | // Set layout. |
||
| 482 | $value = array_merge( $value, self::get_field_wrapper( $value, $option_name ) ); |
||
| 483 | |||
| 484 | echo Give_Fields_API::render_tag( $value ); |
||
| 485 | break; |
||
| 486 | |||
| 487 | // File input field. |
||
| 488 | case 'file' : |
||
| 489 | $value = give_backward_compatibility_setting_api_1_8( $value ); |
||
| 490 | |||
| 491 | // Set layout. |
||
| 492 | $value = array_merge( $value, self::get_field_wrapper( $value, $option_name ) ); |
||
| 493 | |||
| 494 | echo Give_Fields_API::render_tag( $value ); |
||
| 495 | break; |
||
| 496 | |||
| 497 | // WordPress Editor. |
||
| 498 | case 'wysiwyg' : |
||
| 499 | $value = give_backward_compatibility_setting_api_1_8( $value ); |
||
| 500 | |||
| 501 | // Set field value. |
||
| 502 | $value['value'] = wp_kses_post( self::get_option( $option_name, $value['id'], $value['default'] ) ); |
||
| 503 | |||
| 504 | // Set layout. |
||
| 505 | $value = array_merge( $value, self::get_field_wrapper( $value, $option_name ) ); |
||
| 506 | |||
| 507 | echo Give_Fields_API::render_tag( $value ); |
||
| 508 | break; |
||
| 509 | |||
| 510 | // Custom: Give Docs Link field type. |
||
| 511 | case 'give_docs_link' : |
||
| 512 | $value = give_backward_compatibility_setting_api_1_8( $value ); |
||
| 513 | |||
| 514 | // Set layout. |
||
| 515 | $value = array_merge( $value, self::get_field_wrapper( $value, $option_name ) ); |
||
| 516 | $value['before_field'] = '<td class="give-forminp give-forminp-' . sanitize_title( $value['type'] ) . ' give-docs-link" colspan="2">'; |
||
| 517 | |||
| 518 | echo Give_Fields_API::render_tag( $value ); |
||
| 519 | break; |
||
| 520 | |||
| 521 | case 'group': |
||
| 522 | $value = give_backward_compatibility_setting_api_1_8( $value ); |
||
| 523 | |||
| 524 | // Set field value. |
||
| 525 | $value['value'] = self::get_option( $option_name, $value['id'], $value['default'] ); |
||
| 526 | |||
| 527 | // Set layout. |
||
| 528 | $value = array_merge( $value, self::get_field_wrapper( $value, $option_name ) ); |
||
| 529 | |||
| 530 | echo Give_Fields_API::render_tag( $value ); |
||
| 531 | break; |
||
| 532 | |||
| 533 | // Default: run an action |
||
| 534 | // You can add or handle your custom field action. |
||
| 535 | default: |
||
| 536 | $value = give_backward_compatibility_setting_api_1_8( $value ); |
||
| 537 | |||
| 538 | // Get option value. |
||
| 539 | $option_value = self::get_option( $option_name, $value['id'], $value['default'] ); |
||
| 540 | do_action( 'give_admin_field_' . $value['type'], $value, $option_value ); |
||
| 541 | break; |
||
| 542 | } |
||
| 543 | } |
||
| 544 | } |
||
| 545 | |||
| 785 |
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.