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