Conditions | 13 |
Paths | 4 |
Total Lines | 259 |
Code Lines | 109 |
Lines | 8 |
Ratio | 3.09 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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 |
||
438 | public static function add_attribute() { |
||
439 | wp_enqueue_media(); |
||
440 | ?> |
||
441 | <div class="wrap woocommerce"> |
||
442 | <div class="icon32 icon32-attributes" id="icon-woocommerce"><br/></div> |
||
443 | <h1><?php _e( 'Attributes', 'woocommerce' ); ?></h1> |
||
444 | <br class="clear" /> |
||
445 | <div id="col-container"> |
||
446 | <div id="col-right"> |
||
447 | <div class="col-wrap"> |
||
448 | <table class="widefat attributes-table wp-list-table ui-sortable" style="width:100%"> |
||
449 | <thead> |
||
450 | <tr> |
||
451 | <th scope="col"><?php _e( 'Image', 'woocommerce' ); ?></th> |
||
452 | <th scope="col"><?php _e( 'Name', 'woocommerce' ); ?></th> |
||
453 | <th scope="col"><?php _e( 'Slug', 'woocommerce' ); ?></th> |
||
454 | <th scope="col"><?php _e( 'Type', 'woocommerce' ); ?></th> |
||
455 | <th scope="col"><?php _e( 'Order by', 'woocommerce' ); ?></th> |
||
456 | <th scope="col" colspan="2"><?php _e( 'Terms', 'woocommerce' ); ?></th> |
||
457 | </tr> |
||
458 | </thead> |
||
459 | <tbody> |
||
460 | <?php |
||
461 | if ( $attribute_taxonomies = wc_get_attribute_taxonomies() ) : |
||
462 | foreach ( $attribute_taxonomies as $tax ) : |
||
463 | ?><tr> |
||
464 | <td> |
||
465 | <?php |
||
466 | $thumbnail_id = $tax->attribute_thumbnail_id; |
||
467 | |||
468 | if ( $thumbnail_id ) { |
||
469 | $image = wp_get_attachment_thumb_url( $thumbnail_id ); |
||
470 | } else { |
||
471 | $image = wc_placeholder_img_src(); |
||
472 | } |
||
473 | echo '<img src="' . esc_url( $image ) . '" alt="' . esc_attr__( 'Thumbnail', 'woocommerce' ) . '" class="wp-post-image" height="48" width="48" />'; |
||
474 | ?> |
||
475 | </td> |
||
476 | <td> |
||
477 | <strong><a href="edit-tags.php?taxonomy=<?php echo esc_html( wc_attribute_taxonomy_name( $tax->attribute_name ) ); ?>&post_type=product"><?php echo esc_html( $tax->attribute_label ); ?></a></strong> |
||
478 | |||
479 | <div class="row-actions"><span class="edit"><a href="<?php echo esc_url( add_query_arg( 'edit', $tax->attribute_id, 'edit.php?post_type=product&page=product_attributes_ext' ) ); ?>"><?php _e( 'Edit', 'woocommerce' ); ?></a> | </span><span class="delete"><a class="delete" href="<?php echo esc_url( wp_nonce_url( add_query_arg( 'delete', $tax->attribute_id, 'edit.php?post_type=product&page=product_attributes_ext' ), 'woocommerce-delete-attribute_' . $tax->attribute_id ) ); ?>"><?php _e( 'Delete', 'woocommerce' ); ?></a></span></div> |
||
480 | </td> |
||
481 | <td><?php echo esc_html( $tax->attribute_name ); ?></td> |
||
482 | <td><?php echo esc_html( ucfirst( $tax->attribute_type ) ); ?> <?php echo $tax->attribute_public ? '(' . __( 'Public', 'woocommerce' ) . ')' : ''; ?></td> |
||
483 | <td><?php |
||
484 | switch ( $tax->attribute_orderby ) { |
||
485 | case 'name' : |
||
486 | _e( 'Name', 'woocommerce' ); |
||
487 | break; |
||
488 | case 'name_num' : |
||
489 | _e( 'Name (numeric)', 'woocommerce' ); |
||
490 | break; |
||
491 | case 'id' : |
||
492 | _e( 'Term ID', 'woocommerce' ); |
||
493 | break; |
||
494 | default: |
||
495 | _e( 'Custom ordering', 'woocommerce' ); |
||
496 | break; |
||
497 | } |
||
498 | ?></td> |
||
499 | <td class="attribute-terms"><?php |
||
500 | $taxonomy = wc_attribute_taxonomy_name( $tax->attribute_name ); |
||
501 | |||
502 | if ( taxonomy_exists( $taxonomy ) ) { |
||
503 | $terms = get_terms( $taxonomy, 'hide_empty=0' ); |
||
504 | |||
505 | View Code Duplication | switch ( $tax->attribute_orderby ) { |
|
506 | case 'name_num' : |
||
507 | usort( $terms, '_wc_get_product_terms_name_num_usort_callback' ); |
||
508 | break; |
||
509 | case 'parent' : |
||
510 | usort( $terms, '_wc_get_product_terms_parent_usort_callback' ); |
||
511 | break; |
||
512 | } |
||
513 | |||
514 | $terms_string = implode( ', ', wp_list_pluck( $terms, 'name' ) ); |
||
515 | if ( $terms_string ) { |
||
516 | echo $terms_string; |
||
517 | } else { |
||
518 | echo '<span class="na">–</span>'; |
||
519 | } |
||
520 | } else { |
||
521 | echo '<span class="na">–</span>'; |
||
522 | } |
||
523 | ?></td> |
||
524 | <td class="attribute-actions"><a href="edit-tags.php?taxonomy=<?php echo esc_html( wc_attribute_taxonomy_name( $tax->attribute_name ) ); ?>&post_type=product" class="button alignright tips configure-terms" data-tip="<?php esc_attr_e( 'Configure terms', 'woocommerce' ); ?>"><?php _e( 'Configure terms', 'woocommerce' ); ?></a></td> |
||
525 | </tr><?php |
||
526 | endforeach; |
||
527 | else : |
||
528 | ?><tr><td colspan="6"><?php _e( 'No attributes currently exist.', 'woocommerce' ) ?></td></tr><?php |
||
529 | endif; |
||
530 | ?> |
||
531 | </tbody> |
||
532 | </table> |
||
533 | </div> |
||
534 | </div> |
||
535 | <div id="col-left"> |
||
536 | <div class="col-wrap"> |
||
537 | <div class="form-wrap"> |
||
538 | <h2><?php _e( 'Add New Attribute', 'woocommerce' ); ?></h2> |
||
539 | <p><?php _e( 'Attributes let you define extra product data, such as size or colour. You can use these attributes in the shop sidebar using the "layered nav" widgets. Please note: you cannot rename an attribute later on.', 'woocommerce' ); ?></p> |
||
540 | <form action="edit.php?post_type=product&page=product_attributes" method="post"> |
||
541 | <div class="form-field"> |
||
542 | <label for="attribute_label"><?php _e( 'Name', 'woocommerce' ); ?></label> |
||
543 | <input name="attribute_label" id="attribute_label" type="text" value="" /> |
||
544 | <p class="description"><?php _e( 'Name for the attribute (shown on the front-end).', 'woocommerce' ); ?></p> |
||
545 | </div> |
||
546 | |||
547 | <div class="form-field"> |
||
548 | <label for="attribute_name"><?php _e( 'Slug', 'woocommerce' ); ?></label> |
||
549 | <input name="attribute_name" id="attribute_name" type="text" value="" maxlength="28" /> |
||
550 | <p class="description"><?php _e( 'Unique slug/reference for the attribute; must be shorter than 28 characters.', 'woocommerce' ); ?></p> |
||
551 | </div> |
||
552 | |||
553 | <div class="form-field"> |
||
554 | <label for="attribute_description"><?php _e( 'Description', 'woocommerce' ); ?></label> |
||
555 | <textarea name="attribute_description" id="attribute_description" value="" rows="4"></textarea> |
||
556 | <p class="description"><?php _e( 'Description for the attribute, optional.', 'woocommerce' ); ?></p> |
||
557 | </div> |
||
558 | |||
559 | <div class="form-field term-thumbnail-wrap"> |
||
560 | <label><?php _e( 'Attribute Graphic', 'woocommerce' ); ?></label> |
||
561 | <div id="attribute_thumbnail" style="float: left; margin-right: 10px;"><img src="<?php echo esc_url( wc_placeholder_img_src() ); ?>" width="60px" height="60px" /></div> |
||
562 | <div style="line-height: 60px;"> |
||
563 | <input type="hidden" id="attribute_thumbnail_id" name="attribute_thumbnail_id" /> |
||
564 | <button type="button" class="upload_image_button button"><?php _e( 'Upload/Add image', 'woocommerce' ); ?></button> |
||
565 | <button type="button" class="remove_image_button button"><?php _e( 'Remove image', 'woocommerce' ); ?></button> |
||
566 | </div> |
||
567 | <script type="text/javascript"> |
||
568 | |||
569 | // Only show the "remove image" button when needed |
||
570 | if ( ! jQuery( '#attribute_thumbnail_id' ).val() ) { |
||
571 | jQuery( '.remove_image_button' ).hide(); |
||
572 | } |
||
573 | |||
574 | // Uploading files |
||
575 | var file_frame; |
||
576 | |||
577 | jQuery( document ).on( 'click', '.upload_image_button', function( event ) { |
||
578 | |||
579 | event.preventDefault(); |
||
580 | |||
581 | // If the media frame already exists, reopen it. |
||
582 | if ( file_frame ) { |
||
583 | file_frame.open(); |
||
584 | return; |
||
585 | } |
||
586 | |||
587 | // Create the media frame. |
||
588 | file_frame = wp.media.frames.downloadable_file = wp.media({ |
||
589 | title: '<?php _e( "Choose an image", "woocommerce" ); ?>', |
||
590 | button: { |
||
591 | text: '<?php _e( "Use image", "woocommerce" ); ?>' |
||
592 | }, |
||
593 | multiple: false |
||
594 | }); |
||
595 | |||
596 | // When an image is selected, run a callback. |
||
597 | file_frame.on( 'select', function() { |
||
598 | var attachment = file_frame.state().get( 'selection' ).first().toJSON(); |
||
599 | |||
600 | jQuery( '#attribute_thumbnail_id' ).val( attachment.id ); |
||
601 | jQuery( '#attribute_thumbnail' ).find( 'img' ).attr( 'src', attachment.thumbnail.url ); |
||
602 | jQuery( '.remove_image_button' ).show(); |
||
603 | }); |
||
604 | |||
605 | // Finally, open the modal. |
||
606 | file_frame.open(); |
||
607 | }); |
||
608 | |||
609 | jQuery( document ).on( 'click', '.remove_image_button', function() { |
||
610 | jQuery( '#attribute_thumbnail' ).find( 'img' ).attr( 'src', '<?php echo esc_js( wc_placeholder_img_src() ); ?>' ); |
||
611 | jQuery( '#attribute_thumbnail_id' ).val( '' ); |
||
612 | jQuery( '.remove_image_button' ).hide(); |
||
613 | return false; |
||
614 | }); |
||
615 | |||
616 | jQuery( document ).ajaxComplete( function( event, request, options ) { |
||
617 | if ( request && 4 === request.readyState && 200 === request.status |
||
618 | && options.data && 0 <= options.data.indexOf( 'action=add-tag' ) ) { |
||
619 | |||
620 | var res = wpAjax.parseAjaxResponse( request.responseXML, 'ajax-response' ); |
||
621 | if ( ! res || res.errors ) { |
||
622 | return; |
||
623 | } |
||
624 | // Clear Thumbnail fields on submit |
||
625 | jQuery( '#attribute_thumbnail' ).find( 'img' ).attr( 'src', '<?php echo esc_js( wc_placeholder_img_src() ); ?>' ); |
||
626 | jQuery( '#attribute_thumbnail_id' ).val( '' ); |
||
627 | jQuery( '.remove_image_button' ).hide(); |
||
628 | // Clear Display type field on submit |
||
629 | jQuery( '#display_type' ).val( '' ); |
||
630 | return; |
||
631 | } |
||
632 | } ); |
||
633 | |||
634 | </script> |
||
635 | <div class="clear"></div> |
||
636 | </div> |
||
637 | |||
638 | <div class="form-field"> |
||
639 | <label for="attribute_public"><input name="attribute_public" id="attribute_public" type="checkbox" value="1" /> <?php _e( 'Enable Archives?', 'woocommerce' ); ?></label> |
||
640 | |||
641 | <p class="description"><?php _e( 'Enable this if you want this attribute to have product archives in your store.', 'woocommerce' ); ?></p> |
||
642 | </div> |
||
643 | |||
644 | <div class="form-field"> |
||
645 | <label for="attribute_type"><?php _e( 'Type', 'woocommerce' ); ?></label> |
||
646 | <select name="attribute_type" id="attribute_type"> |
||
647 | <?php foreach ( wc_get_attribute_types() as $key => $value ) : ?> |
||
648 | <option value="<?php echo esc_attr( $key ); ?>"><?php echo esc_attr( $value ); ?></option> |
||
649 | <?php endforeach; ?> |
||
650 | |||
651 | <?php |
||
652 | |||
653 | /** |
||
654 | * Deprecated action in favor of product_attributes_type_selector filter. |
||
655 | * |
||
656 | * @deprecated 2.4.0 |
||
657 | */ |
||
658 | do_action( 'woocommerce_admin_attribute_types' ); |
||
659 | ?> |
||
660 | </select> |
||
661 | <p class="description"><?php _e( 'Determines how you select attributes for products. Under admin panel -> products -> product data -> attributes -> values, <strong>Text</strong> allows manual entry whereas <strong>select</strong> allows pre-configured terms in a drop-down list.', 'woocommerce' ); ?></p> |
||
662 | </div> |
||
663 | |||
664 | <div class="form-field"> |
||
665 | <label for="attribute_orderby"><?php _e( 'Default sort order', 'woocommerce' ); ?></label> |
||
666 | <select name="attribute_orderby" id="attribute_orderby"> |
||
667 | <option value="menu_order"><?php _e( 'Custom ordering', 'woocommerce' ); ?></option> |
||
668 | <option value="name"><?php _e( 'Name', 'woocommerce' ); ?></option> |
||
669 | <option value="name_num"><?php _e( 'Name (numeric)', 'woocommerce' ); ?></option> |
||
670 | <option value="id"><?php _e( 'Term ID', 'woocommerce' ); ?></option> |
||
671 | </select> |
||
672 | <p class="description"><?php _e( 'Determines the sort order of the terms on the frontend shop product pages. If using custom ordering, you can drag and drop the terms in this attribute.', 'woocommerce' ); ?></p> |
||
673 | </div> |
||
674 | |||
675 | <p class="submit"><input type="submit" name="add_new_attribute" id="submit" class="button button-primary" value="<?php esc_attr_e( 'Add Attribute', 'woocommerce' ); ?>"></p> |
||
676 | <?php wp_nonce_field( 'woocommerce-add-new_attribute' ); ?> |
||
677 | </form> |
||
678 | </div> |
||
679 | </div> |
||
680 | </div> |
||
681 | </div> |
||
682 | <script type="text/javascript"> |
||
683 | /* <![CDATA[ */ |
||
684 | |||
685 | jQuery( 'a.delete' ).click( function() { |
||
686 | if ( window.confirm( '<?php _e( "Are you sure you want to delete this attribute?", "woocommerce" ); ?>' ) ) { |
||
687 | return true; |
||
688 | } |
||
689 | return false; |
||
690 | }); |
||
691 | |||
692 | /* ]]> */ |
||
693 | </script> |
||
694 | </div> |
||
695 | <?php |
||
696 | } |
||
697 | |||
699 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.