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