Conditions | 67 |
Paths | 250 |
Total Lines | 447 |
Code Lines | 268 |
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 |
||
275 | public static function output_fields( $options, $option_name = '' ) { |
||
276 | $current_tab = give_get_current_setting_tab(); |
||
277 | |||
278 | // Field Default values. |
||
279 | $defaults = array( |
||
280 | 'id' => '', |
||
281 | 'class' => '', |
||
282 | 'css' => '', |
||
283 | 'default' => '', |
||
284 | 'desc' => '', |
||
285 | 'table_html' => true, |
||
286 | ); |
||
287 | |||
288 | foreach ( $options as $value ) { |
||
289 | if ( ! isset( $value['type'] ) ) { |
||
290 | continue; |
||
291 | } |
||
292 | |||
293 | // Set title. |
||
294 | $defaults['title'] = isset( $value['name'] ) ? $value['name'] : ''; |
||
295 | |||
296 | $value = wp_parse_args( $value, $defaults ); |
||
297 | |||
298 | // Custom attribute handling. |
||
299 | $custom_attributes = array(); |
||
300 | |||
301 | if ( ! empty( $value['attributes'] ) && is_array( $value['attributes'] ) ) { |
||
302 | foreach ( $value['attributes'] as $attribute => $attribute_value ) { |
||
303 | $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"'; |
||
304 | } |
||
305 | } |
||
306 | |||
307 | // Description handling. |
||
308 | $description = self::get_field_description( $value ); |
||
309 | |||
310 | // Switch based on type. |
||
311 | switch ( $value['type'] ) { |
||
312 | |||
313 | // Section Titles |
||
314 | case 'title': |
||
315 | if ( ! empty( $value['title'] ) ) { |
||
316 | echo '<div class="give-setting-tab-header give-setting-tab-header-' . $current_tab . '"><h2>' . self::get_field_title( $value ) . '</h2><hr></div>'; |
||
317 | } |
||
318 | |||
319 | if ( ! empty( $value['desc'] ) ) { |
||
320 | echo wpautop( wptexturize( wp_kses_post( $value['desc'] ) ) ); |
||
321 | } |
||
322 | |||
323 | if ( $value['table_html'] ) { |
||
324 | echo '<table class="form-table give-setting-tab-body give-setting-tab-body-' . $current_tab . '">' . "\n\n"; |
||
325 | } |
||
326 | |||
327 | if ( ! empty( $value['id'] ) ) { |
||
328 | |||
329 | /** |
||
330 | * Trigger Action. |
||
331 | * |
||
332 | * Note: action dynamically fire on basis of field id. |
||
333 | * |
||
334 | * @since 1.8 |
||
335 | */ |
||
336 | do_action( 'give_settings_' . sanitize_title( $value['id'] ) ); |
||
337 | } |
||
338 | |||
339 | break; |
||
340 | |||
341 | // Section Ends. |
||
342 | case 'sectionend': |
||
343 | if ( ! empty( $value['id'] ) ) { |
||
344 | |||
345 | /** |
||
346 | * Trigger Action. |
||
347 | * |
||
348 | * Note: action dynamically fire on basis of field id. |
||
349 | * |
||
350 | * @since 1.8 |
||
351 | */ |
||
352 | do_action( 'give_settings_' . sanitize_title( $value['id'] ) . '_end' ); |
||
353 | } |
||
354 | |||
355 | if ( $value['table_html'] ) { |
||
356 | echo '</table>'; |
||
357 | } |
||
358 | |||
359 | if ( ! empty( $value['id'] ) ) { |
||
360 | |||
361 | /** |
||
362 | * Trigger Action. |
||
363 | * |
||
364 | * Note: action dynamically fire on basis of field id. |
||
365 | * |
||
366 | * @since 1.8 |
||
367 | */ |
||
368 | do_action( 'give_settings_' . sanitize_title( $value['id'] ) . '_after' ); |
||
369 | } |
||
370 | |||
371 | break; |
||
372 | |||
373 | // Standard text inputs and subtypes like 'number'. |
||
374 | case 'text': |
||
375 | case 'email': |
||
376 | case 'number': |
||
377 | case 'password' : |
||
378 | |||
379 | $type = $value['type']; |
||
380 | $option_value = self::get_option( $option_name, $value['id'], $value['default'] ); |
||
381 | |||
382 | ?> |
||
383 | <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>> |
||
384 | <th scope="row" class="titledesc"> |
||
385 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label> |
||
386 | </th> |
||
387 | <td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ) ?>"> |
||
388 | <input |
||
389 | name="<?php echo esc_attr( $value['id'] ); ?>" |
||
390 | id="<?php echo esc_attr( $value['id'] ); ?>" |
||
391 | type="<?php echo esc_attr( $type ); ?>" |
||
392 | style="<?php echo esc_attr( $value['css'] ); ?>" |
||
393 | value="<?php echo esc_attr( $option_value ); ?>" |
||
394 | class="give-input-field<?php echo( empty( $value['class'] ) ? '' : ' ' . esc_attr( $value['class'] ) ); ?>" |
||
395 | <?php echo implode( ' ', $custom_attributes ); ?> |
||
396 | /> <?php echo $description; ?> |
||
397 | </td> |
||
398 | </tr><?php |
||
399 | break; |
||
400 | |||
401 | // Textarea. |
||
402 | case 'textarea': |
||
403 | |||
404 | $option_value = self::get_option( $option_name, $value['id'], $value['default'] ); |
||
405 | |||
406 | ?> |
||
407 | <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>> |
||
408 | <th scope="row" class="titledesc"> |
||
409 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label> |
||
410 | </th> |
||
411 | <td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ) ?>"> |
||
412 | <textarea |
||
413 | name="<?php echo esc_attr( $value['id'] ); ?>" |
||
414 | id="<?php echo esc_attr( $value['id'] ); ?>" |
||
415 | style="<?php echo esc_attr( $value['css'] ); ?>" |
||
416 | class="<?php echo esc_attr( $value['class'] ); ?>" |
||
417 | rows="10" |
||
418 | cols="60" |
||
419 | <?php echo implode( ' ', $custom_attributes ); ?> |
||
420 | ><?php echo esc_textarea( $option_value ); ?></textarea> |
||
421 | <?php echo $description; ?> |
||
422 | </td> |
||
423 | </tr><?php |
||
424 | break; |
||
425 | |||
426 | // Select boxes. |
||
427 | case 'select' : |
||
428 | case 'multiselect' : |
||
429 | |||
430 | $option_value = self::get_option( $option_name, $value['id'], $value['default'] ); |
||
431 | |||
432 | ?> |
||
433 | <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>> |
||
434 | <th scope="row" class="titledesc"> |
||
435 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label> |
||
436 | </th> |
||
437 | <td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ) ?>"> |
||
438 | <select |
||
439 | name="<?php echo esc_attr( $value['id'] ); ?><?php if ( $value['type'] == 'multiselect' ) { |
||
440 | echo '[]'; |
||
441 | } ?>" |
||
442 | id="<?php echo esc_attr( $value['id'] ); ?>" |
||
443 | style="<?php echo esc_attr( $value['css'] ); ?>" |
||
444 | class="<?php echo esc_attr( $value['class'] ); ?>" |
||
445 | <?php echo implode( ' ', $custom_attributes ); ?> |
||
446 | <?php echo ( 'multiselect' == $value['type'] ) ? 'multiple="multiple"' : ''; ?> |
||
447 | > |
||
448 | |||
449 | <?php |
||
450 | if ( ! empty( $value['options'] ) ) { |
||
451 | foreach ( $value['options'] as $key => $val ) { |
||
452 | ?> |
||
453 | <option value="<?php echo esc_attr( $key ); ?>" <?php |
||
454 | |||
455 | if ( is_array( $option_value ) ) { |
||
456 | selected( in_array( $key, $option_value ), true ); |
||
457 | } else { |
||
458 | selected( $option_value, $key ); |
||
459 | } |
||
460 | |||
461 | ?>><?php echo $val ?></option> |
||
462 | <?php |
||
463 | } |
||
464 | } |
||
465 | ?> |
||
466 | |||
467 | </select> <?php echo $description; ?> |
||
468 | </td> |
||
469 | </tr><?php |
||
470 | break; |
||
471 | |||
472 | // Radio inputs. |
||
473 | case 'radio_inline' : |
||
474 | $value['class'] = empty( $value['class'] ) ? 'give-radio-inline' : $value['class'] . ' give-radio-inline'; |
||
475 | case 'radio' : |
||
476 | $option_value = self::get_option( $option_name, $value['id'], $value['default'] ); |
||
477 | ?> |
||
478 | <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>> |
||
479 | <th scope="row" class="titledesc"> |
||
480 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label> |
||
481 | </th> |
||
482 | <td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ) ?> <?php echo( ! empty( $value['class'] ) ? $value['class'] : '' ); ?>"> |
||
483 | <fieldset> |
||
484 | <ul> |
||
485 | <?php |
||
486 | foreach ( $value['options'] as $key => $val ) { |
||
487 | ?> |
||
488 | <li> |
||
489 | <label><input |
||
490 | name="<?php echo esc_attr( $value['id'] ); ?>" |
||
491 | value="<?php echo $key; ?>" |
||
492 | type="radio" |
||
493 | style="<?php echo esc_attr( $value['css'] ); ?>" |
||
494 | <?php echo implode( ' ', $custom_attributes ); ?> |
||
495 | <?php checked( $key, $option_value ); ?> |
||
496 | /> <?php echo $val ?></label> |
||
497 | </li> |
||
498 | <?php |
||
499 | } |
||
500 | ?> |
||
501 | <?php echo $description; ?> |
||
502 | </fieldset> |
||
503 | </td> |
||
504 | </tr><?php |
||
505 | break; |
||
506 | |||
507 | // Checkbox input. |
||
508 | case 'checkbox' : |
||
509 | $option_value = self::get_option( $option_name, $value['id'], $value['default'] ); |
||
510 | ?> |
||
511 | <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>> |
||
512 | <th scope="row" class="titledesc"> |
||
513 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label> |
||
514 | </th> |
||
515 | <td class="give-forminp"> |
||
516 | <input |
||
517 | name="<?php echo esc_attr( $value['id'] ); ?>" |
||
518 | id="<?php echo esc_attr( $value['id'] ); ?>" |
||
519 | type="checkbox" |
||
520 | class="<?php echo esc_attr( isset( $value['class'] ) ? $value['class'] : '' ); ?>" |
||
521 | value="1" |
||
522 | <?php checked( $option_value, 'on' ); ?> |
||
523 | <?php echo implode( ' ', $custom_attributes ); ?> |
||
524 | /> |
||
525 | <?php echo $description; ?> |
||
526 | </td> |
||
527 | </tr> |
||
528 | <?php |
||
529 | break; |
||
530 | |||
531 | // Multi Checkbox input. |
||
532 | case 'multicheck' : |
||
533 | $option_value = self::get_option( $option_name, $value['id'], $value['default'] ); |
||
534 | $option_value = is_array( $option_value ) ? $option_value : array(); |
||
535 | ?> |
||
536 | <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>> |
||
537 | <th scope="row" class="titledesc"> |
||
538 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label> |
||
539 | </th> |
||
540 | <td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ) ?> <?php echo( ! empty( $value['class'] ) ? $value['class'] : '' ); ?>"> |
||
541 | <fieldset> |
||
542 | <ul> |
||
543 | <?php |
||
544 | foreach ( $value['options'] as $key => $val ) { |
||
545 | ?> |
||
546 | <li> |
||
547 | <label> |
||
548 | <input |
||
549 | name="<?php echo esc_attr( $value['id'] ); ?>[]" |
||
550 | value="<?php echo $key; ?>" |
||
551 | type="checkbox" |
||
552 | style="<?php echo esc_attr( $value['css'] ); ?>" |
||
553 | <?php echo implode( ' ', $custom_attributes ); ?> |
||
554 | <?php if ( in_array( $key, $option_value ) ) { |
||
555 | echo 'checked="checked"'; |
||
556 | } ?> |
||
557 | /> <?php echo $val ?> |
||
558 | </label> |
||
559 | </li> |
||
560 | <?php |
||
561 | } |
||
562 | ?> |
||
563 | <?php echo $description; ?> |
||
564 | </fieldset> |
||
565 | </td> |
||
566 | </tr> |
||
567 | <?php |
||
568 | break; |
||
569 | |||
570 | // File input field. |
||
571 | case 'file' : |
||
572 | $option_value = self::get_option( $option_name, $value['id'], $value['default'] ); |
||
573 | ?> |
||
574 | <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>> |
||
575 | <th scope="row" class="titledesc"> |
||
576 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label> |
||
577 | </th> |
||
578 | <td class="give-forminp"> |
||
579 | <div class="give-field-wrap"> |
||
580 | <label for="<?php echo $value['id'] ?>"> |
||
581 | <input |
||
582 | name="<?php echo esc_attr( $value['id'] ); ?>" |
||
583 | id="<?php echo esc_attr( $value['id'] ); ?>" |
||
584 | type="text" |
||
585 | class="give-input-field<?php echo esc_attr( isset( $value['class'] ) ? ' ' . $value['class'] : '' ); ?>" |
||
586 | value="<?php echo $option_value; ?>" |
||
587 | style="<?php echo esc_attr( $value['css'] ); ?>" |
||
588 | <?php echo implode( ' ', $custom_attributes ); ?> |
||
589 | /> <input class="give-upload-button button" type="button" value="<?php echo esc_html__( 'Add or Upload File', 'give' ); ?>"> |
||
590 | <?php echo $description ?> |
||
591 | <div class="give-image-thumb<?php echo ! $option_value ? ' give-hidden' : ''; ?>"> |
||
592 | <span class="give-delete-image-thumb dashicons dashicons-no-alt"></span> |
||
593 | <img src="<?php echo $option_value; ?>" alt=""> |
||
594 | </div> |
||
595 | </label> |
||
596 | </div> |
||
597 | </td> |
||
598 | </tr><?php |
||
599 | break; |
||
600 | |||
601 | // WordPress Editor. |
||
602 | case 'wysiwyg' : |
||
603 | // Get option value. |
||
604 | $option_value = self::get_option( $option_name, $value['id'], $value['default'] ); |
||
605 | |||
606 | // Get editor settings. |
||
607 | $editor_settings = ! empty( $value['options'] ) ? $value['options'] : array(); |
||
608 | ?> |
||
609 | <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>> |
||
610 | <th scope="row" class="titledesc"> |
||
611 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label> |
||
612 | </th> |
||
613 | <td class="give-forminp"> |
||
614 | <?php wp_editor( $option_value, $value['id'], $editor_settings ); ?> |
||
615 | <?php echo $description; ?> |
||
616 | </td> |
||
617 | </tr><?php |
||
618 | break; |
||
619 | |||
620 | // Custom: System setting field. |
||
621 | case 'system_info' : |
||
622 | ?> |
||
623 | <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>> |
||
624 | <th scope="row" class="titledesc"> |
||
625 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label> |
||
626 | </th> |
||
627 | <td class="give-forminp"> |
||
628 | <?php give_system_info_callback(); ?> |
||
629 | <?php echo $description; ?> |
||
630 | </td> |
||
631 | </tr><?php |
||
632 | break; |
||
633 | |||
634 | // Custom: Default gateways setting field. |
||
635 | case 'default_gateway' : |
||
636 | $option_value = self::get_option( $option_name, $value['id'], $value['default'] ); |
||
637 | ?> |
||
638 | <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>> |
||
639 | <th scope="row" class="titledesc"> |
||
640 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label> |
||
641 | </th> |
||
642 | <td class="give-forminp"> |
||
643 | <?php give_default_gateway_callback( $value, $option_value ); ?> |
||
644 | <?php echo $description; ?> |
||
645 | </td> |
||
646 | </tr><?php |
||
647 | break; |
||
648 | |||
649 | // Custom: Enable gateways setting field. |
||
650 | case 'enabled_gateways' : |
||
651 | $option_value = self::get_option( $option_name, $value['id'], $value['default'] ); |
||
652 | ?> |
||
653 | <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>> |
||
654 | <th scope="row" class="titledesc"> |
||
655 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label> |
||
656 | </th> |
||
657 | <td class="give-forminp"> |
||
658 | <?php give_enabled_gateways_callback( $value, $option_value ); ?> |
||
659 | <?php echo $description; ?> |
||
660 | </td> |
||
661 | </tr><?php |
||
662 | break; |
||
663 | |||
664 | // Custom: Email preview buttons field. |
||
665 | case 'email_preview_buttons' : |
||
666 | ?> |
||
667 | <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>> |
||
668 | <th scope="row" class="titledesc"> |
||
669 | <label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label> |
||
670 | </th> |
||
671 | <td class="give-forminp"> |
||
672 | <?php give_email_preview_buttons_callback(); ?> |
||
673 | <?php echo $description; ?> |
||
674 | </td> |
||
675 | </tr><?php |
||
676 | break; |
||
677 | |||
678 | // Custom: API field. |
||
679 | case 'api' : |
||
680 | give_api_callback(); |
||
681 | echo $description; |
||
682 | break; |
||
683 | |||
684 | // Custom: Log field. |
||
685 | case 'logs' : |
||
686 | // Note: there are no need to check for html field param because we want custom html to this field. |
||
687 | give_reports_tab_logs(); |
||
688 | echo $description; |
||
689 | break; |
||
690 | |||
691 | // Custom: API field. |
||
692 | case 'data' : |
||
693 | give_tools_recount_stats_display(); |
||
694 | echo $description; |
||
695 | break; |
||
696 | |||
697 | // Custom: Give Docs Link field type. |
||
698 | case 'give_docs_link' : |
||
699 | ?> |
||
700 | <tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>> |
||
701 | <td class="give-docs-link" colspan="2"> |
||
702 | <?php |
||
703 | echo '<p class="give-docs-link"><a href="' . esc_url( $value['url'] ) |
||
704 | . '" target="_blank">' |
||
705 | . sprintf( esc_html__( 'Need Help? See docs on "%s"' ), $value['title'] ) |
||
706 | . '<span class="dashicons dashicons-editor-help"></span></a></p>'; |
||
707 | ?> |
||
708 | </td> |
||
709 | </tr><?php |
||
710 | break; |
||
711 | |||
712 | // Default: run an action |
||
713 | // You can add or handle your custom field action. |
||
714 | default: |
||
715 | // Get option value. |
||
716 | $option_value = self::get_option( $option_name, $value['id'], $value['default'] ); |
||
717 | do_action( 'give_admin_field_' . $value['type'], $value, $option_value ); |
||
718 | break; |
||
719 | } |
||
720 | } |
||
721 | } |
||
722 | |||
887 |
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.