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