Conditions | 18 |
Paths | 17280 |
Total Lines | 395 |
Code Lines | 257 |
Lines | 57 |
Ratio | 14.43 % |
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 |
||
475 | public function add_my_sites_submenu( $wp_admin_bar ) { |
||
476 | $current_user = wp_get_current_user(); |
||
477 | |||
478 | $blog_name = get_bloginfo( 'name' ); |
||
479 | if ( empty( $blog_name ) ) { |
||
480 | $blog_name = $this->primary_site_slug; |
||
481 | } |
||
482 | |||
483 | if ( mb_strlen( $blog_name ) > 20 ) { |
||
484 | $blog_name = mb_substr( html_entity_decode( $blog_name, ENT_QUOTES ), 0, 20 ) . '…'; |
||
485 | } |
||
486 | |||
487 | $wp_admin_bar->add_menu( array( |
||
488 | 'id' => 'blog', |
||
489 | 'title' => __( 'My Sites', 'jetpack' ), |
||
490 | 'href' => '#', |
||
491 | 'meta' => array( |
||
492 | 'class' => 'my-sites', |
||
493 | ), |
||
494 | ) ); |
||
495 | |||
496 | $wp_admin_bar->add_menu( array( |
||
497 | 'parent' => 'blog', |
||
498 | 'id' => 'switch-site', |
||
499 | 'title' => __( 'Switch Site', 'jetpack' ), |
||
500 | 'href' => 'https://wordpress.com/sites', |
||
501 | ) ); |
||
502 | |||
503 | if ( is_user_member_of_blog( $current_user->ID ) ) { |
||
504 | $blavatar = ''; |
||
505 | $class = 'current-site'; |
||
506 | |||
507 | if ( has_site_icon() ) { |
||
508 | $src = get_site_icon_url(); |
||
509 | $blavatar = '<img class="avatar" src="'. esc_attr( $src ) . '" alt="Current site avatar">'; |
||
510 | $class = 'has-blavatar'; |
||
511 | } |
||
512 | |||
513 | $site_link = str_replace( '::', '/', $this->primary_site_slug ); |
||
514 | $blog_info = '<div class="ab-site-icon">' . $blavatar . '</div>'; |
||
515 | $blog_info .= '<span class="ab-site-title">' . esc_html( $blog_name ) . '</span>'; |
||
516 | $blog_info .= '<span class="ab-site-description">' . esc_html( $site_link ) . '</span>'; |
||
517 | |||
518 | $wp_admin_bar->add_menu( array( |
||
519 | 'parent' => 'blog', |
||
520 | 'id' => 'blog-info', |
||
521 | 'title' => $blog_info, |
||
522 | 'href' => esc_url( trailingslashit( $this->primary_site_slug ) ), |
||
523 | 'meta' => array( |
||
524 | 'class' => $class, |
||
525 | ), |
||
526 | ) ); |
||
527 | |||
528 | } |
||
529 | |||
530 | // Stats |
||
531 | View Code Duplication | if ( Jetpack::is_module_active( 'stats' ) ) { |
|
532 | $wp_admin_bar->add_menu( array( |
||
533 | 'parent' => 'blog', |
||
534 | 'id' => 'blog-stats', |
||
535 | 'title' => __( 'Stats', 'jetpack' ), |
||
536 | 'href' => 'https://wordpress.com/stats/' . esc_attr( $this->primary_site_slug ), |
||
537 | 'meta' => array( |
||
538 | 'class' => 'mb-icon', |
||
539 | ), |
||
540 | ) ); |
||
541 | } |
||
542 | |||
543 | // Add Calypso plans link and plan type indicator |
||
544 | if ( is_user_member_of_blog( $current_user->ID ) ) { |
||
545 | $plans_url = 'https://wordpress.com/plans/' . esc_attr( $this->primary_site_slug ); |
||
546 | $label = __( 'Plan', 'jetpack' ); |
||
547 | $plan = Jetpack::get_active_plan(); |
||
548 | |||
549 | $plan_title = $this->create_menu_item_pair( |
||
550 | array( |
||
551 | 'url' => $plans_url, |
||
552 | 'id' => 'wp-admin-bar-plan', |
||
553 | 'label' => $label, |
||
554 | ), |
||
555 | array( |
||
556 | 'url' => $plans_url, |
||
557 | 'id' => 'wp-admin-bar-plan-badge', |
||
558 | 'label' => $plan['product_name_short'] |
||
559 | ) |
||
560 | ); |
||
561 | |||
562 | $wp_admin_bar->add_menu( array( |
||
563 | 'parent' => 'blog', |
||
564 | 'id' => 'plan', |
||
565 | 'title' => $plan_title, |
||
566 | 'meta' => array( |
||
567 | 'class' => 'inline-action', |
||
568 | ), |
||
569 | ) ); |
||
570 | } |
||
571 | |||
572 | // Publish group |
||
573 | $wp_admin_bar->add_group( array( |
||
574 | 'parent' => 'blog', |
||
575 | 'id' => 'publish', |
||
576 | ) ); |
||
577 | |||
578 | // Publish header |
||
579 | $wp_admin_bar->add_menu( array( |
||
580 | 'parent' => 'publish', |
||
581 | 'id' => 'publish-header', |
||
582 | 'title' => _x( 'Publish', 'admin bar menu group label', 'jetpack' ), |
||
583 | 'meta' => array( |
||
584 | 'class' => 'ab-submenu-header', |
||
585 | ), |
||
586 | ) ); |
||
587 | |||
588 | // Blog Posts |
||
589 | $posts_title = $this->create_menu_item_pair( |
||
590 | array( |
||
591 | 'url' => 'https://wordpress.com/posts/' . esc_attr( $this->primary_site_slug ), |
||
592 | 'id' => 'wp-admin-bar-edit-post', |
||
593 | 'label' => __( 'Blog Posts', 'jetpack' ), |
||
594 | ), |
||
595 | array( |
||
596 | 'url' => 'https://wordpress.com/post/' . esc_attr( $this->primary_site_slug ), |
||
597 | 'id' => 'wp-admin-bar-new-post', |
||
598 | 'label' => _x( 'Add', 'admin bar menu new item label', 'jetpack' ), |
||
599 | ) |
||
600 | ); |
||
601 | |||
602 | View Code Duplication | if ( ! current_user_can( 'edit_posts' ) ) { |
|
603 | $posts_title = $this->create_menu_item_anchor( |
||
604 | 'ab-item ab-primary mb-icon', |
||
605 | 'https://wordpress.com/posts/' . esc_attr( $this->primary_site_slug ), |
||
606 | __( 'Blog Posts', 'jetpack' ), |
||
607 | 'wp-admin-bar-edit-post' |
||
608 | ); |
||
609 | } |
||
610 | |||
611 | $wp_admin_bar->add_menu( array( |
||
612 | 'parent' => 'publish', |
||
613 | 'id' => 'new-post', |
||
614 | 'title' => $posts_title, |
||
615 | 'meta' => array( |
||
616 | 'class' => 'inline-action', |
||
617 | ), |
||
618 | ) ); |
||
619 | |||
620 | // Pages |
||
621 | $pages_title = $this->create_menu_item_pair( |
||
622 | array( |
||
623 | 'url' => 'https://wordpress.com/pages/' . esc_attr( $this->primary_site_slug ), |
||
624 | 'id' => 'wp-admin-bar-edit-page', |
||
625 | 'label' => __( 'Pages', 'jetpack' ), |
||
626 | ), |
||
627 | array( |
||
628 | 'url' => 'https://wordpress.com/page/' . esc_attr( $this->primary_site_slug ), |
||
629 | 'id' => 'wp-admin-bar-new-page', |
||
630 | 'label' => _x( 'Add', 'admin bar menu new item label', 'jetpack' ), |
||
631 | ) |
||
632 | ); |
||
633 | |||
634 | View Code Duplication | if ( ! current_user_can( 'edit_pages' ) ) { |
|
635 | $pages_title = $this->create_menu_item_anchor( |
||
636 | 'ab-item ab-primary mb-icon', |
||
637 | 'https://wordpress.com/pages/' . esc_attr( $this->primary_site_slug ), |
||
638 | __( 'Pages', 'jetpack' ), |
||
639 | 'wp-admin-bar-edit-page' |
||
640 | ); |
||
641 | } |
||
642 | |||
643 | $wp_admin_bar->add_menu( array( |
||
644 | 'parent' => 'publish', |
||
645 | 'id' => 'new-page', |
||
646 | 'title' => $pages_title, |
||
647 | 'meta' => array( |
||
648 | 'class' => 'inline-action', |
||
649 | ), |
||
650 | ) ); |
||
651 | |||
652 | // Portfolio |
||
653 | $portfolios_title = $this->create_menu_item_pair( |
||
654 | array( |
||
655 | 'url' => 'https://wordpress.com/types/jetpack-portfolio/' . esc_attr( $this->primary_site_slug ), |
||
656 | 'id' => 'wp-admin-bar-edit-portfolio', |
||
657 | 'label' => __( 'Portfolio', 'jetpack' ), |
||
658 | ), |
||
659 | array( |
||
660 | 'url' => 'https://wordpress.com/edit/jetpack-portfolio/' . esc_attr( $this->primary_site_slug ), |
||
661 | 'id' => 'wp-admin-bar-new-portfolio', |
||
662 | 'label' => _x( 'Add', 'admin bar menu new item label', 'jetpack' ), |
||
663 | ) |
||
664 | ); |
||
665 | |||
666 | View Code Duplication | if ( ! current_user_can( 'edit_pages' ) ) { |
|
667 | $portfolios_title = $this->create_menu_item_anchor( |
||
668 | 'ab-item ab-primary mb-icon', |
||
669 | 'https://wordpress.com/types/jetpack-portfolio/' . esc_attr( $this->primary_site_slug ), |
||
670 | __( 'Portfolio', 'jetpack' ), |
||
671 | 'wp-admin-bar-edit-portfolio' |
||
672 | ); |
||
673 | } |
||
674 | |||
675 | $wp_admin_bar->add_menu( array( |
||
676 | 'parent' => 'publish', |
||
677 | 'id' => 'new-jetpack-portfolio', |
||
678 | 'title' => $portfolios_title, |
||
679 | 'meta' => array( |
||
680 | 'class' => 'inline-action', |
||
681 | ), |
||
682 | ) ); |
||
683 | |||
684 | if ( current_user_can( 'edit_theme_options' ) ) { |
||
685 | // Look and Feel group |
||
686 | $wp_admin_bar->add_group( array( |
||
687 | 'parent' => 'blog', |
||
688 | 'id' => 'look-and-feel', |
||
689 | ) ); |
||
690 | |||
691 | // Look and Feel header |
||
692 | $wp_admin_bar->add_menu( array( |
||
693 | 'parent' => 'look-and-feel', |
||
694 | 'id' => 'look-and-feel-header', |
||
695 | 'title' => _x( 'Personalize', 'admin bar menu group label', 'jetpack' ), |
||
696 | 'meta' => array( |
||
697 | 'class' => 'ab-submenu-header', |
||
698 | ), |
||
699 | ) ); |
||
700 | |||
701 | if ( is_admin() ) { |
||
702 | // In wp-admin the `return` query arg will return to that page after closing the Customizer |
||
703 | $customizer_url = add_query_arg( array( 'return' => urlencode( site_url( $_SERVER['REQUEST_URI'] ) ) ), wp_customize_url() ); |
||
704 | } else { |
||
705 | // On the frontend the `url` query arg will load that page in the Customizer and also return to it after closing |
||
706 | // non-home URLs won't work unless we undo domain mapping since the Customizer preview is unmapped to always have HTTPS |
||
707 | $current_page = '//' . $this->primary_site_slug . $_SERVER['REQUEST_URI']; |
||
708 | $customizer_url = add_query_arg( array( 'url' => urlencode( $current_page ) ), wp_customize_url() ); |
||
709 | } |
||
710 | |||
711 | $theme_title = $this->create_menu_item_pair( |
||
712 | array( |
||
713 | 'url' => 'https://wordpress.com/design/' . esc_attr( $this->primary_site_slug ), |
||
714 | 'id' => 'wp-admin-bar-themes', |
||
715 | 'label' => __( 'Themes', 'jetpack' ), |
||
716 | ), |
||
717 | array( |
||
718 | 'url' => $customizer_url, |
||
719 | 'id' => 'wp-admin-bar-cmz', |
||
720 | 'label' => _x( 'Customize', 'admin bar customize item label', 'jetpack' ), |
||
721 | ) |
||
722 | ); |
||
723 | $meta = array( 'class' => 'mb-icon', 'class' => 'inline-action' ); |
||
724 | $href = false; |
||
725 | |||
726 | $wp_admin_bar->add_menu( array( |
||
727 | 'parent' => 'look-and-feel', |
||
728 | 'id' => 'themes', |
||
729 | 'title' => $theme_title, |
||
730 | 'href' => $href, |
||
731 | 'meta' => $meta |
||
732 | ) ); |
||
733 | |||
734 | View Code Duplication | if ( current_theme_supports( 'menus' ) ) { |
|
735 | $wp_admin_bar->add_menu( array( |
||
736 | 'parent' => 'look-and-feel', |
||
737 | 'id' => 'menus', |
||
738 | 'title' => __( 'Menus', 'jetpack' ), |
||
739 | 'href' => 'https://wordpress.com/menus/' . esc_attr( $this->primary_site_slug ), |
||
740 | 'meta' => array( |
||
741 | 'class' => 'mb-icon', |
||
742 | ), |
||
743 | ) ); |
||
744 | } |
||
745 | } |
||
746 | |||
747 | if ( current_user_can( 'manage_options' ) ) { |
||
748 | // Configuration group |
||
749 | $wp_admin_bar->add_group( array( |
||
750 | 'parent' => 'blog', |
||
751 | 'id' => 'configuration', |
||
752 | ) ); |
||
753 | |||
754 | // Configuration header |
||
755 | $wp_admin_bar->add_menu( array( |
||
756 | 'parent' => 'configuration', |
||
757 | 'id' => 'configuration-header', |
||
758 | 'title' => __( 'Configure', 'admin bar menu group label', 'jetpack' ), |
||
759 | 'meta' => array( |
||
760 | 'class' => 'ab-submenu-header', |
||
761 | ), |
||
762 | ) ); |
||
763 | |||
764 | View Code Duplication | if ( Jetpack::is_module_active( 'publicize' ) || Jetpack::is_module_active( 'sharedaddy' ) ) { |
|
765 | $wp_admin_bar->add_menu( array( |
||
766 | 'parent' => 'configuration', |
||
767 | 'id' => 'sharing', |
||
768 | 'title' => __( 'Sharing', 'jetpack' ), |
||
769 | 'href' => 'https://wordpress.com/sharing/' . esc_attr( $this->primary_site_slug ), |
||
770 | 'meta' => array( |
||
771 | 'class' => 'mb-icon', |
||
772 | ), |
||
773 | ) ); |
||
774 | } |
||
775 | |||
776 | $people_title = $this->create_menu_item_pair( |
||
777 | array( |
||
778 | 'url' => 'https://wordpress.com/people/team/' . esc_attr( $this->primary_site_slug ), |
||
779 | 'id' => 'wp-admin-bar-people', |
||
780 | 'label' => __( 'People', 'jetpack' ), |
||
781 | ), |
||
782 | array( |
||
783 | 'url' => '//' . esc_attr( $this->primary_site_slug ) . '/wp-admin/user-new.php', |
||
784 | 'id' => 'wp-admin-bar-people-add', |
||
785 | 'label' => _x( 'Add', 'admin bar people item label', 'jetpack' ), |
||
786 | ) |
||
787 | ); |
||
788 | |||
789 | $wp_admin_bar->add_menu( array( |
||
790 | 'parent' => 'configuration', |
||
791 | 'id' => 'users-toolbar', |
||
792 | 'title' => $people_title, |
||
793 | 'href' => false, |
||
794 | 'meta' => array( |
||
795 | 'class' => 'inline-action', |
||
796 | ), |
||
797 | ) ); |
||
798 | |||
799 | $plugins_title = $this->create_menu_item_pair( |
||
800 | array( |
||
801 | 'url' => 'https://wordpress.com/plugins/' . esc_attr( $this->primary_site_slug ), |
||
802 | 'id' => 'wp-admin-bar-plugins', |
||
803 | 'label' => __( 'Plugins', 'jetpack' ), |
||
804 | ), |
||
805 | array( |
||
806 | 'url' => 'https://wordpress.com/plugins/browse/' . esc_attr( $this->primary_site_slug ), |
||
807 | 'id' => 'wp-admin-bar-plugins-add', |
||
808 | 'label' => _x( 'Add', 'Label for the button on the Masterbar to add a new plugin', 'jetpack' ), |
||
809 | ) |
||
810 | ); |
||
811 | |||
812 | $wp_admin_bar->add_menu( array( |
||
813 | 'parent' => 'configuration', |
||
814 | 'id' => 'plugins', |
||
815 | 'title' => $plugins_title, |
||
816 | 'href' => false, |
||
817 | 'meta' => array( |
||
818 | 'class' => 'inline-action', |
||
819 | ), |
||
820 | ) ); |
||
821 | |||
822 | if ( $this->isAutomatedTransferSite() ) { |
||
823 | $domain_title = $this->create_menu_item_pair( |
||
824 | array( |
||
825 | 'url' => 'https://wordpress.com/domains/' . esc_attr( $this->primary_site_slug ), |
||
826 | 'id' => 'wp-admin-bar-domains', |
||
827 | 'label' => __( 'Domains', 'jetpack' ), |
||
828 | ), |
||
829 | array( |
||
830 | 'url' => 'https://wordpress.com/domains/add/' . esc_attr( $this->primary_site_slug ), |
||
831 | 'id' => 'wp-admin-bar-domains-add', |
||
832 | 'label' => _x( 'Add', 'Label for the button on the Masterbar to add a new domain', 'jetpack' ), |
||
833 | ) |
||
834 | ); |
||
835 | } |
||
836 | |||
837 | $wp_admin_bar->add_menu( array( |
||
838 | 'parent' => 'configuration', |
||
839 | 'id' => 'domains', |
||
840 | 'title' => $domain_title, |
||
|
|||
841 | 'href' => false, |
||
842 | 'meta' => array( |
||
843 | 'class' => 'inline-action', |
||
844 | ), |
||
845 | ) ); |
||
846 | |||
847 | $wp_admin_bar->add_menu( array( |
||
848 | 'parent' => 'configuration', |
||
849 | 'id' => 'blog-settings', |
||
850 | 'title' => __( 'Settings', 'jetpack' ), |
||
851 | 'href' => 'https://wordpress.com/settings/general/' . esc_attr( $this->primary_site_slug ), |
||
852 | 'meta' => array( |
||
853 | 'class' => 'mb-icon', |
||
854 | ), |
||
855 | ) ); |
||
856 | |||
857 | if ( $this->isAutomatedTransferSite() ) { |
||
858 | $wp_admin_bar->add_menu( array( |
||
859 | 'parent' => 'configuration', |
||
860 | 'id' => 'legacy-dashboard', |
||
861 | 'title' => __( 'WP Admin', 'jetpack' ), |
||
862 | 'href' => '//' . esc_attr( $this->primary_site_slug ) . '/wp-admin/', |
||
863 | 'meta' => array( |
||
864 | 'class' => 'mb-icon', |
||
865 | ), |
||
866 | ) ); |
||
867 | } |
||
868 | } |
||
869 | } |
||
870 | } |
||
871 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: