| Conditions | 44 |
| Paths | 124 |
| Total Lines | 300 |
| Code Lines | 150 |
| 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 | self::backward_compatibility_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-class'; |
||
| 396 | |||
| 397 | // Render function. |
||
| 398 | echo Give_Fields_API::render_tag( $value ); |
||
| 399 | |||
| 400 | break; |
||
| 401 | |||
| 402 | // Textarea. |
||
| 403 | case 'textarea': |
||
| 404 | self::backward_compatibility_1_8( $value ); |
||
| 405 | |||
| 406 | // Set field value. |
||
| 407 | $value['value'] = esc_textarea( self::get_option( $option_name, $value['name'], $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 | self::backward_compatibility_1_8( $value ); |
||
| 423 | |||
| 424 | // Set field value. |
||
| 425 | $value['value'] = give_clean( self::get_option( $option_name, $value['name'], $value['default'] ) ); |
||
| 426 | |||
| 427 | // Set layout. |
||
| 428 | $value = array_merge( $value, self::get_field_wrapper( $value, $option_name ) ); |
||
| 429 | |||
| 430 | // Update td wrapper. |
||
| 431 | $value['before_field'] = '<td class="give-forminp give-forminp-' . sanitize_title( $value['type'] ) . '"><fieldset>'; |
||
| 432 | $value['after_field'] = "{$description}</fieldset></td>"; |
||
| 433 | |||
| 434 | |||
| 435 | // Update param for radio_inline field type. |
||
| 436 | if( 'radio_inline' === $value['type'] ) { |
||
| 437 | $value['type'] = 'radio'; |
||
| 438 | $value['wrapper_attributes']['class'] = empty( $value['wrapper_attributes']['class'] ) |
||
| 439 | ? 'give-radio-inline' |
||
| 440 | : trim( $value['wrapper_attributes']['class'] ) . ' give-radio-inline'; |
||
| 441 | } |
||
| 442 | |||
| 443 | echo Give_Fields_API::render_tag( $value ); |
||
| 444 | break; |
||
| 445 | |||
| 446 | // Radio inputs. |
||
| 447 | case 'radio_inline' : |
||
| 448 | case 'radio' : |
||
| 449 | self::backward_compatibility_1_8( $value ); |
||
| 450 | |||
| 451 | // Set field value. |
||
| 452 | $value['value'] = give_clean( self::get_option( $option_name, $value['name'], $value['default'] ) ); |
||
| 453 | |||
| 454 | // Set layout. |
||
| 455 | $value = array_merge( $value, self::get_field_wrapper( $value, $option_name ) ); |
||
| 456 | |||
| 457 | // Update td wrapper. |
||
| 458 | $value['before_field'] = '<td class="give-forminp give-forminp-' . sanitize_title( $value['type'] ) . '"><fieldset>'; |
||
| 459 | $value['after_field'] = "{$description}</fieldset></td>"; |
||
| 460 | |||
| 461 | |||
| 462 | // Update param for radio_inline field type. |
||
| 463 | if( 'radio_inline' === $value['type'] ) { |
||
| 464 | $value['type'] = 'radio'; |
||
| 465 | $value['wrapper_attributes']['class'] = empty( $value['wrapper_attributes']['class'] ) |
||
| 466 | ? 'give-radio-inline' |
||
| 467 | : trim( $value['wrapper_attributes']['class'] ) . ' give-radio-inline'; |
||
| 468 | } |
||
| 469 | |||
| 470 | echo Give_Fields_API::render_tag( $value ); |
||
| 471 | break; |
||
| 472 | |||
| 473 | // Checkbox input. |
||
| 474 | case 'checkbox' : |
||
| 475 | self::backward_compatibility_1_8( $value ); |
||
| 476 | |||
| 477 | // Set field value. |
||
| 478 | $value['value'] = give_clean( self::get_option( $option_name, $value['name'], $value['default'] ) ); |
||
| 479 | |||
| 480 | // Set layout. |
||
| 481 | $value = array_merge( $value, self::get_field_wrapper( $value, $option_name ) ); |
||
| 482 | |||
| 483 | echo Give_Fields_API::render_tag( $value ); |
||
| 484 | break; |
||
| 485 | |||
| 486 | // Multi Checkbox input. |
||
| 487 | case 'multicheck' : |
||
| 488 | self::backward_compatibility_1_8( $value ); |
||
| 489 | |||
| 490 | // Set field value. |
||
| 491 | $value['value'] = self::get_option( $option_name, $value['id'], $value['default'] ); |
||
| 492 | $value['value'] = is_array( $value['value'] ) ? $value['value'] : array(); |
||
| 493 | |||
| 494 | // Set layout. |
||
| 495 | $value = array_merge( $value, self::get_field_wrapper( $value, $option_name ) ); |
||
| 496 | |||
| 497 | echo Give_Fields_API::render_tag( $value ); |
||
| 498 | break; |
||
| 499 | |||
| 500 | // File input field. |
||
| 501 | case 'file' : |
||
| 502 | $option_value = self::get_option( $option_name, $value['id'], $value['default'] ); |
||
| 503 | ?> |
||
| 504 | <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>> |
||
| 505 | <th scope="row" class="titledesc"> |
||
| 506 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label> |
||
| 507 | </th> |
||
| 508 | <td class="give-forminp"> |
||
| 509 | <div class="give-field-wrap"> |
||
| 510 | <label for="<?php echo $value['id'] ?>"> |
||
| 511 | <input |
||
| 512 | name="<?php echo esc_attr( $value['id'] ); ?>" |
||
| 513 | id="<?php echo esc_attr( $value['id'] ); ?>" |
||
| 514 | type="text" |
||
| 515 | class="give-input-field<?php echo esc_attr( isset( $value['class'] ) ? ' ' . $value['class'] : '' ); ?>" |
||
| 516 | value="<?php echo $option_value; ?>" |
||
| 517 | style="<?php echo esc_attr( $value['css'] ); ?>" |
||
| 518 | <?php echo implode( ' ', $custom_attributes ); ?> |
||
| 519 | /> <input class="give-upload-button button" type="button" |
||
| 520 | value="<?php echo esc_html__( 'Add or Upload File', 'give' ); ?>"> |
||
| 521 | <?php echo $description ?> |
||
| 522 | <div class="give-image-thumb<?php echo ! $option_value ? ' give-hidden' : ''; ?>"> |
||
| 523 | <span class="give-delete-image-thumb dashicons dashicons-no-alt"></span> |
||
| 524 | <img src="<?php echo $option_value; ?>" alt=""> |
||
| 525 | </div> |
||
| 526 | </label> |
||
| 527 | </div> |
||
| 528 | </td> |
||
| 529 | </tr><?php |
||
| 530 | break; |
||
| 531 | |||
| 532 | // WordPress Editor. |
||
| 533 | case 'wysiwyg' : |
||
| 534 | // Get option value. |
||
| 535 | $option_value = self::get_option( $option_name, $value['id'], $value['default'] ); |
||
| 536 | |||
| 537 | // Get editor settings. |
||
| 538 | $editor_settings = ! empty( $value['options'] ) ? $value['options'] : array(); |
||
| 539 | ?> |
||
| 540 | <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>> |
||
| 541 | <th scope="row" class="titledesc"> |
||
| 542 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label> |
||
| 543 | </th> |
||
| 544 | <td class="give-forminp"> |
||
| 545 | <?php wp_editor( $option_value, $value['id'], $editor_settings ); ?> |
||
| 546 | <?php echo $description; ?> |
||
| 547 | </td> |
||
| 548 | </tr><?php |
||
| 549 | break; |
||
| 550 | |||
| 551 | // Custom: Give Docs Link field type. |
||
| 552 | case 'give_docs_link' : |
||
| 553 | ?> |
||
| 554 | <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>> |
||
| 555 | <td class="give-docs-link" colspan="2"> |
||
| 556 | <?php |
||
| 557 | echo '<p class="give-docs-link"><a href="' . esc_url( $value['url'] ) |
||
| 558 | . '" target="_blank">' |
||
| 559 | . sprintf( esc_html__( 'Need Help? See docs on "%s"' ), self::get_field_title( $value ) ) |
||
| 560 | . '<span class="dashicons dashicons-editor-help"></span></a></p>'; |
||
| 561 | ?> |
||
| 562 | </td> |
||
| 563 | </tr><?php |
||
| 564 | break; |
||
| 565 | |||
| 566 | // Default: run an action |
||
| 567 | // You can add or handle your custom field action. |
||
| 568 | default: |
||
| 569 | // Get option value. |
||
| 570 | $option_value = self::get_option( $option_name, $value['id'], $value['default'] ); |
||
| 571 | do_action( 'give_admin_field_' . $value['type'], $value, $option_value ); |
||
| 572 | break; |
||
| 573 | } |
||
| 574 | } |
||
| 575 | } |
||
| 576 | |||
| 826 |
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.