Conditions | 27 |
Paths | > 20000 |
Total Lines | 481 |
Lines | 86 |
Ratio | 17.88 % |
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 |
||
557 | public function add_my_sites_submenu( $wp_admin_bar ) { |
||
558 | $current_user = wp_get_current_user(); |
||
559 | |||
560 | $blog_name = get_bloginfo( 'name' ); |
||
561 | if ( empty( $blog_name ) ) { |
||
562 | $blog_name = $this->primary_site_slug; |
||
563 | } |
||
564 | |||
565 | if ( mb_strlen( $blog_name ) > 20 ) { |
||
566 | $blog_name = mb_substr( html_entity_decode( $blog_name, ENT_QUOTES ), 0, 20 ) . '…'; |
||
567 | } |
||
568 | |||
569 | $wp_admin_bar->add_menu( array( |
||
570 | 'parent' => 'root-default', |
||
571 | 'id' => 'blog', |
||
572 | 'title' => _n( 'My Site', 'My Sites', $this->user_site_count, 'jetpack' ), |
||
573 | 'href' => '#', |
||
574 | 'meta' => array( |
||
575 | 'class' => 'my-sites mb-trackable', |
||
576 | ), |
||
577 | ) ); |
||
578 | |||
579 | if ( $this->user_site_count > 1 ) { |
||
580 | $wp_admin_bar->add_menu( array( |
||
581 | 'parent' => 'blog', |
||
582 | 'id' => 'switch-site', |
||
583 | 'title' => esc_html__( 'Switch Site', 'jetpack' ), |
||
584 | 'href' => 'https://wordpress.com/sites', |
||
585 | ) ); |
||
586 | } else { |
||
587 | $wp_admin_bar->add_menu( array( |
||
588 | 'parent' => 'blog', |
||
589 | 'id' => 'new-site', |
||
590 | 'title' => esc_html__( '+ Add New WordPress', 'jetpack' ), |
||
591 | 'href' => 'https://wordpress.com/start?ref=admin-bar-logged-in', |
||
592 | ) ); |
||
593 | } |
||
594 | |||
595 | if ( is_user_member_of_blog( $current_user->ID ) ) { |
||
596 | $blavatar = ''; |
||
597 | $class = 'current-site'; |
||
598 | |||
599 | if ( has_site_icon() ) { |
||
600 | $src = get_site_icon_url(); |
||
601 | $blavatar = '<img class="avatar" src="'. esc_attr( $src ) . '" alt="Current site avatar">'; |
||
602 | $class = 'has-blavatar'; |
||
603 | } |
||
604 | |||
605 | $blog_info = '<div class="ab-site-icon">' . $blavatar . '</div>'; |
||
606 | $blog_info .= '<span class="ab-site-title">' . esc_html( $blog_name ) . '</span>'; |
||
607 | $blog_info .= '<span class="ab-site-description">' . esc_html( $this->primary_site_url ) . '</span>'; |
||
608 | |||
609 | $wp_admin_bar->add_menu( array( |
||
610 | 'parent' => 'blog', |
||
611 | 'id' => 'blog-info', |
||
612 | 'title' => $blog_info, |
||
613 | 'href' => esc_url( trailingslashit( $this->primary_site_url ) ), |
||
614 | 'meta' => array( |
||
615 | 'class' => $class, |
||
616 | ), |
||
617 | ) ); |
||
618 | } |
||
619 | |||
620 | // Site Preview |
||
621 | if ( is_admin() ) { |
||
622 | $wp_admin_bar->add_menu( array( |
||
623 | 'parent' => 'blog', |
||
624 | 'id' => 'site-view', |
||
625 | 'title' => __( 'View Site', 'jetpack' ), |
||
626 | 'href' => home_url(), |
||
627 | 'meta' => array( |
||
628 | 'class' => 'mb-icon', |
||
629 | 'target' => '_blank', |
||
630 | ), |
||
631 | ) ); |
||
632 | } |
||
633 | |||
634 | // Stats |
||
635 | View Code Duplication | if ( Jetpack::is_module_active( 'stats' ) ) { |
|
636 | $wp_admin_bar->add_menu( array( |
||
637 | 'parent' => 'blog', |
||
638 | 'id' => 'blog-stats', |
||
639 | 'title' => esc_html__( 'Stats', 'jetpack' ), |
||
640 | 'href' => 'https://wordpress.com/stats/' . esc_attr( $this->primary_site_slug ), |
||
641 | 'meta' => array( |
||
642 | 'class' => 'mb-icon', |
||
643 | ), |
||
644 | ) ); |
||
645 | } |
||
646 | |||
647 | if ( current_user_can( 'manage_options' ) ) { |
||
648 | $wp_admin_bar->add_menu( array( |
||
649 | 'parent' => 'blog', |
||
650 | 'id' => 'activity', |
||
651 | 'title' => esc_html__( 'Activity', 'jetpack' ), |
||
652 | 'href' => 'https://wordpress.com/activity-log/' . esc_attr( $this->primary_site_slug ), |
||
653 | 'meta' => array( |
||
654 | 'class' => 'mb-icon', |
||
655 | ), |
||
656 | ) ); |
||
657 | } |
||
658 | |||
659 | // Add Calypso plans link and plan type indicator |
||
660 | if ( is_user_member_of_blog( $current_user->ID ) ) { |
||
661 | $plans_url = 'https://wordpress.com/plans/' . esc_attr( $this->primary_site_slug ); |
||
662 | $label = esc_html__( 'Plan', 'jetpack' ); |
||
663 | $plan = Jetpack::get_active_plan(); |
||
664 | |||
665 | $plan_title = $this->create_menu_item_pair( |
||
666 | array( |
||
667 | 'url' => $plans_url, |
||
668 | 'id' => 'wp-admin-bar-plan', |
||
669 | 'label' => $label, |
||
670 | ), |
||
671 | array( |
||
672 | 'url' => $plans_url, |
||
673 | 'id' => 'wp-admin-bar-plan-badge', |
||
674 | 'label' => $plan['product_name_short'] |
||
675 | ) |
||
676 | ); |
||
677 | |||
678 | $wp_admin_bar->add_menu( array( |
||
679 | 'parent' => 'blog', |
||
680 | 'id' => 'plan', |
||
681 | 'title' => $plan_title, |
||
682 | 'meta' => array( |
||
683 | 'class' => 'inline-action', |
||
684 | ), |
||
685 | ) ); |
||
686 | } |
||
687 | |||
688 | // Publish group |
||
689 | $wp_admin_bar->add_group( array( |
||
690 | 'parent' => 'blog', |
||
691 | 'id' => 'publish', |
||
692 | ) ); |
||
693 | |||
694 | // Publish header |
||
695 | $wp_admin_bar->add_menu( array( |
||
696 | 'parent' => 'publish', |
||
697 | 'id' => 'publish-header', |
||
698 | 'title' => esc_html_x( 'Manage', 'admin bar menu group label', 'jetpack' ), |
||
699 | 'meta' => array( |
||
700 | 'class' => 'ab-submenu-header', |
||
701 | ), |
||
702 | ) ); |
||
703 | |||
704 | // Pages |
||
705 | $pages_title = $this->create_menu_item_pair( |
||
706 | array( |
||
707 | 'url' => 'https://wordpress.com/pages/' . esc_attr( $this->primary_site_slug ), |
||
708 | 'id' => 'wp-admin-bar-edit-page', |
||
709 | 'label' => esc_html__( 'Site Pages', 'jetpack' ), |
||
710 | ), |
||
711 | array( |
||
712 | 'url' => 'https://wordpress.com/page/' . esc_attr( $this->primary_site_slug ), |
||
713 | 'id' => 'wp-admin-bar-new-page-badge', |
||
714 | 'label' => esc_html_x( 'Add', 'admin bar menu new item label', 'jetpack' ), |
||
715 | ) |
||
716 | ); |
||
717 | |||
718 | if ( ! current_user_can( 'edit_pages' ) ) { |
||
719 | $pages_title = $this->create_menu_item_anchor( |
||
720 | 'ab-item ab-primary mb-icon', |
||
721 | 'https://wordpress.com/pages/' . esc_attr( $this->primary_site_slug ), |
||
722 | esc_html__( 'Site Pages', 'jetpack' ), |
||
723 | 'wp-admin-bar-edit-page' |
||
724 | ); |
||
725 | } |
||
726 | |||
727 | $wp_admin_bar->add_menu( array( |
||
728 | 'parent' => 'publish', |
||
729 | 'id' => 'new-page', |
||
730 | 'title' => $pages_title, |
||
731 | 'meta' => array( |
||
732 | 'class' => 'inline-action', |
||
733 | ), |
||
734 | ) ); |
||
735 | |||
736 | // Blog Posts |
||
737 | $posts_title = $this->create_menu_item_pair( |
||
738 | array( |
||
739 | 'url' => 'https://wordpress.com/posts/' . esc_attr( $this->primary_site_slug ), |
||
740 | 'id' => 'wp-admin-bar-edit-post', |
||
741 | 'label' => esc_html__( 'Blog Posts', 'jetpack' ), |
||
742 | ), |
||
743 | array( |
||
744 | 'url' => 'https://wordpress.com/post/' . esc_attr( $this->primary_site_slug ), |
||
745 | 'id' => 'wp-admin-bar-new-post-badge', |
||
746 | 'label' => esc_html_x( 'Add', 'admin bar menu new item label', 'jetpack' ), |
||
747 | ) |
||
748 | ); |
||
749 | |||
750 | if ( ! current_user_can( 'edit_posts' ) ) { |
||
751 | $posts_title = $this->create_menu_item_anchor( |
||
752 | 'ab-item ab-primary mb-icon', |
||
753 | 'https://wordpress.com/posts/' . esc_attr( $this->primary_site_slug ), |
||
754 | esc_html__( 'Blog Posts', 'jetpack' ), |
||
755 | 'wp-admin-bar-edit-post' |
||
756 | ); |
||
757 | } |
||
758 | |||
759 | $wp_admin_bar->add_menu( array( |
||
760 | 'parent' => 'publish', |
||
761 | 'id' => 'new-post', |
||
762 | 'title' => $posts_title, |
||
763 | 'meta' => array( |
||
764 | 'class' => 'inline-action mb-trackable', |
||
765 | ), |
||
766 | ) ); |
||
767 | |||
768 | // Comments |
||
769 | if ( current_user_can( 'moderate_comments' ) ) { |
||
770 | $wp_admin_bar->add_menu( array( |
||
771 | 'parent' => 'publish', |
||
772 | 'id' => 'comments', |
||
773 | 'title' => __( 'Comments' ), |
||
774 | 'href' => 'https://wordpress.com/comments/' . esc_attr( $this->primary_site_slug ), |
||
775 | 'meta' => array( |
||
776 | 'class' => 'mb-icon', |
||
777 | ), |
||
778 | ) ); |
||
779 | } |
||
780 | |||
781 | // Testimonials |
||
782 | View Code Duplication | if ( Jetpack::is_module_active( 'custom-content-types' ) && get_option( 'jetpack_testimonial' ) ) { |
|
783 | $testimonials_title = $this->create_menu_item_pair( |
||
784 | array( |
||
785 | 'url' => 'https://wordpress.com/types/jetpack-testimonial/' . esc_attr( $this->primary_site_slug ), |
||
786 | 'id' => 'wp-admin-bar-edit-testimonial', |
||
787 | 'label' => esc_html__( 'Testimonials', 'jetpack' ), |
||
788 | ), |
||
789 | array( |
||
790 | 'url' => 'https://wordpress.com/edit/jetpack-testimonial/' . esc_attr( $this->primary_site_slug ), |
||
791 | 'id' => 'wp-admin-bar-new-testimonial', |
||
792 | 'label' => esc_html_x( 'Add', 'Button label for adding a new item via the toolbar menu', 'jetpack' ), |
||
793 | ) |
||
794 | ); |
||
795 | |||
796 | if ( ! current_user_can( 'edit_pages' ) ) { |
||
797 | $testimonials_title = $this->create_menu_item_anchor( |
||
798 | 'ab-item ab-primary mb-icon', |
||
799 | 'https://wordpress.com/types/jetpack-testimonial/' . esc_attr( $this->primary_site_slug ), |
||
800 | esc_html__( 'Testimonials', 'jetpack' ), |
||
801 | 'wp-admin-bar-edit-testimonial' |
||
802 | ); |
||
803 | } |
||
804 | |||
805 | $wp_admin_bar->add_menu( array( |
||
806 | 'parent' => 'publish', |
||
807 | 'id' => 'new-jetpack-testimonial', |
||
808 | 'title' => $testimonials_title, |
||
809 | 'meta' => array( |
||
810 | 'class' => 'inline-action', |
||
811 | ), |
||
812 | ) ); |
||
813 | } |
||
814 | |||
815 | // Portfolio |
||
816 | View Code Duplication | if ( Jetpack::is_module_active( 'custom-content-types' ) && get_option( 'jetpack_portfolio' ) ) { |
|
817 | $portfolios_title = $this->create_menu_item_pair( |
||
818 | array( |
||
819 | 'url' => 'https://wordpress.com/types/jetpack-portfolio/' . esc_attr( $this->primary_site_slug ), |
||
820 | 'id' => 'wp-admin-bar-edit-portfolio', |
||
821 | 'label' => esc_html__( 'Portfolio', 'jetpack' ), |
||
822 | ), |
||
823 | array( |
||
824 | 'url' => 'https://wordpress.com/edit/jetpack-portfolio/' . esc_attr( $this->primary_site_slug ), |
||
825 | 'id' => 'wp-admin-bar-new-portfolio', |
||
826 | 'label' => esc_html_x( 'Add', 'Button label for adding a new item via the toolbar menu', 'jetpack' ), |
||
827 | ) |
||
828 | ); |
||
829 | |||
830 | if ( ! current_user_can( 'edit_pages' ) ) { |
||
831 | $portfolios_title = $this->create_menu_item_anchor( |
||
832 | 'ab-item ab-primary mb-icon', |
||
833 | 'https://wordpress.com/types/jetpack-portfolio/' . esc_attr( $this->primary_site_slug ), |
||
834 | esc_html__( 'Portfolio', 'jetpack' ), |
||
835 | 'wp-admin-bar-edit-portfolio' |
||
836 | ); |
||
837 | } |
||
838 | |||
839 | $wp_admin_bar->add_menu( array( |
||
840 | 'parent' => 'publish', |
||
841 | 'id' => 'new-jetpack-portfolio', |
||
842 | 'title' => $portfolios_title, |
||
843 | 'meta' => array( |
||
844 | 'class' => 'inline-action', |
||
845 | ), |
||
846 | ) ); |
||
847 | } |
||
848 | |||
849 | if ( current_user_can( 'edit_theme_options' ) ) { |
||
850 | // Look and Feel group |
||
851 | $wp_admin_bar->add_group( array( |
||
852 | 'parent' => 'blog', |
||
853 | 'id' => 'look-and-feel', |
||
854 | ) ); |
||
855 | |||
856 | // Look and Feel header |
||
857 | $wp_admin_bar->add_menu( array( |
||
858 | 'parent' => 'look-and-feel', |
||
859 | 'id' => 'look-and-feel-header', |
||
860 | 'title' => esc_html_x( 'Personalize', 'admin bar menu group label', 'jetpack' ), |
||
861 | 'meta' => array( |
||
862 | 'class' => 'ab-submenu-header', |
||
863 | ), |
||
864 | ) ); |
||
865 | |||
866 | if ( is_admin() ) { |
||
867 | // In wp-admin the `return` query arg will return to that page after closing the Customizer |
||
868 | $customizer_url = add_query_arg( array( 'return' => urlencode( site_url( $_SERVER['REQUEST_URI'] ) ) ), wp_customize_url() ); |
||
869 | } else { |
||
870 | // On the frontend the `url` query arg will load that page in the Customizer and also return to it after closing |
||
871 | // non-home URLs won't work unless we undo domain mapping since the Customizer preview is unmapped to always have HTTPS |
||
872 | $current_page = '//' . $this->primary_site_slug . $_SERVER['REQUEST_URI']; |
||
873 | $customizer_url = add_query_arg( array( 'url' => urlencode( $current_page ) ), wp_customize_url() ); |
||
874 | } |
||
875 | |||
876 | $theme_title = $this->create_menu_item_pair( |
||
877 | array( |
||
878 | 'url' => $customizer_url, |
||
879 | 'id' => 'wp-admin-bar-cmz', |
||
880 | 'label' => esc_html_x( 'Customize', 'admin bar customize item label', 'jetpack' ), |
||
881 | ), |
||
882 | array( |
||
883 | 'url' => 'https://wordpress.com/themes/' . esc_attr( $this->primary_site_slug ), |
||
884 | 'id' => 'wp-admin-bar-themes', |
||
885 | 'label' => esc_html__( 'Themes', 'jetpack' ), |
||
886 | ) |
||
887 | ); |
||
888 | $meta = array( 'class' => 'mb-icon', 'class' => 'inline-action' ); |
||
889 | $href = false; |
||
890 | |||
891 | $wp_admin_bar->add_menu( array( |
||
892 | 'parent' => 'look-and-feel', |
||
893 | 'id' => 'themes', |
||
894 | 'title' => $theme_title, |
||
895 | 'href' => $href, |
||
896 | 'meta' => $meta |
||
897 | ) ); |
||
898 | } |
||
899 | |||
900 | if ( current_user_can( 'manage_options' ) ) { |
||
901 | // Configuration group |
||
902 | $wp_admin_bar->add_group( array( |
||
903 | 'parent' => 'blog', |
||
904 | 'id' => 'configuration', |
||
905 | ) ); |
||
906 | |||
907 | // Configuration header |
||
908 | $wp_admin_bar->add_menu( array( |
||
909 | 'parent' => 'configuration', |
||
910 | 'id' => 'configuration-header', |
||
911 | 'title' => esc_html__( 'Configure', 'admin bar menu group label', 'jetpack' ), |
||
912 | 'meta' => array( |
||
913 | 'class' => 'ab-submenu-header', |
||
914 | ), |
||
915 | ) ); |
||
916 | |||
917 | View Code Duplication | if ( Jetpack::is_module_active( 'publicize' ) || Jetpack::is_module_active( 'sharedaddy' ) ) { |
|
918 | $wp_admin_bar->add_menu( array( |
||
919 | 'parent' => 'configuration', |
||
920 | 'id' => 'sharing', |
||
921 | 'title' => esc_html__( 'Sharing', 'jetpack' ), |
||
922 | 'href' => 'https://wordpress.com/sharing/' . esc_attr( $this->primary_site_slug ), |
||
923 | 'meta' => array( |
||
924 | 'class' => 'mb-icon', |
||
925 | ), |
||
926 | ) ); |
||
927 | } |
||
928 | |||
929 | $people_title = $this->create_menu_item_pair( |
||
930 | array( |
||
931 | 'url' => 'https://wordpress.com/people/team/' . esc_attr( $this->primary_site_slug ), |
||
932 | 'id' => 'wp-admin-bar-people', |
||
933 | 'label' => esc_html__( 'People', 'jetpack' ), |
||
934 | ), |
||
935 | array( |
||
936 | 'url' => admin_url( 'user-new.php' ), |
||
937 | 'id' => 'wp-admin-bar-people-add', |
||
938 | 'label' => esc_html_x( 'Add', 'admin bar people item label', 'jetpack' ), |
||
939 | ) |
||
940 | ); |
||
941 | |||
942 | $wp_admin_bar->add_menu( array( |
||
943 | 'parent' => 'configuration', |
||
944 | 'id' => 'users-toolbar', |
||
945 | 'title' => $people_title, |
||
946 | 'href' => false, |
||
947 | 'meta' => array( |
||
948 | 'class' => 'inline-action', |
||
949 | ), |
||
950 | ) ); |
||
951 | |||
952 | $plugins_title = $this->create_menu_item_pair( |
||
953 | array( |
||
954 | 'url' => 'https://wordpress.com/plugins/' . esc_attr( $this->primary_site_slug ), |
||
955 | 'id' => 'wp-admin-bar-plugins', |
||
956 | 'label' => esc_html__( 'Plugins', 'jetpack' ), |
||
957 | ), |
||
958 | array( |
||
959 | 'url' => 'https://wordpress.com/plugins/manage/' . esc_attr( $this->primary_site_slug ), |
||
960 | 'id' => 'wp-admin-bar-plugins-add', |
||
961 | 'label' => esc_html_x( 'Manage', 'Label for the button on the Masterbar to manage plugins', 'jetpack' ), |
||
962 | ) |
||
963 | ); |
||
964 | |||
965 | $wp_admin_bar->add_menu( array( |
||
966 | 'parent' => 'configuration', |
||
967 | 'id' => 'plugins', |
||
968 | 'title' => $plugins_title, |
||
969 | 'href' => false, |
||
970 | 'meta' => array( |
||
971 | 'class' => 'inline-action', |
||
972 | ), |
||
973 | ) ); |
||
974 | |||
975 | if ( jetpack_is_atomic_site() ) { |
||
976 | $domain_title = $this->create_menu_item_pair( |
||
977 | array( |
||
978 | 'url' => 'https://wordpress.com/domains/' . esc_attr( $this->primary_site_slug ), |
||
979 | 'id' => 'wp-admin-bar-domains', |
||
980 | 'label' => esc_html__( 'Domains', 'jetpack' ), |
||
981 | ), |
||
982 | array( |
||
983 | 'url' => 'https://wordpress.com/domains/add/' . esc_attr( $this->primary_site_slug ), |
||
984 | 'id' => 'wp-admin-bar-domains-add', |
||
985 | 'label' => esc_html_x( 'Add', 'Label for the button on the Masterbar to add a new domain', 'jetpack' ), |
||
986 | ) |
||
987 | ); |
||
988 | $wp_admin_bar->add_menu( array( |
||
989 | 'parent' => 'configuration', |
||
990 | 'id' => 'domains', |
||
991 | 'title' => $domain_title, |
||
992 | 'href' => false, |
||
993 | 'meta' => array( |
||
994 | 'class' => 'inline-action', |
||
995 | ), |
||
996 | ) ); |
||
997 | } |
||
998 | |||
999 | $wp_admin_bar->add_menu( array( |
||
1000 | 'parent' => 'configuration', |
||
1001 | 'id' => 'blog-settings', |
||
1002 | 'title' => esc_html__( 'Settings', 'jetpack' ), |
||
1003 | 'href' => 'https://wordpress.com/settings/general/' . esc_attr( $this->primary_site_slug ), |
||
1004 | 'meta' => array( |
||
1005 | 'class' => 'mb-icon', |
||
1006 | ), |
||
1007 | ) ); |
||
1008 | |||
1009 | if ( ! is_admin() ) { |
||
1010 | $wp_admin_bar->add_menu( array( |
||
1011 | 'parent' => 'configuration', |
||
1012 | 'id' => 'legacy-dashboard', |
||
1013 | 'title' => esc_html__( 'Dashboard', 'jetpack' ), |
||
1014 | 'href' => admin_url(), |
||
1015 | 'meta' => array( |
||
1016 | 'class' => 'mb-icon', |
||
1017 | ), |
||
1018 | ) ); |
||
1019 | } |
||
1020 | |||
1021 | // Restore dashboard menu toggle that is needed on mobile views. |
||
1022 | if ( is_admin() ) { |
||
1023 | $wp_admin_bar->add_menu( array( |
||
1024 | 'id' => 'menu-toggle', |
||
1025 | 'title' => '<span class="ab-icon"></span><span class="screen-reader-text">' . esc_html__( 'Menu', 'jetpack' ) . '</span>', |
||
1026 | 'href' => '#', |
||
1027 | ) ); |
||
1028 | } |
||
1029 | |||
1030 | /** |
||
1031 | * Fires when menu items are added to the masterbar "My Sites" menu. |
||
1032 | * |
||
1033 | * @since 5.4.0 |
||
1034 | */ |
||
1035 | do_action( 'jetpack_masterbar' ); |
||
1036 | } |
||
1037 | } |
||
1038 | } |
||
1039 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: