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