| Conditions | 23 |
| Paths | > 20000 |
| Total Lines | 426 |
| Code Lines | 279 |
| Lines | 86 |
| Ratio | 20.19 % |
| 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 | // Stats |
||
| 567 | View Code Duplication | if ( Jetpack::is_module_active( 'stats' ) ) { |
|
| 568 | $wp_admin_bar->add_menu( array( |
||
| 569 | 'parent' => 'blog', |
||
| 570 | 'id' => 'blog-stats', |
||
| 571 | 'title' => __( 'Stats', 'jetpack' ), |
||
| 572 | 'href' => 'https://wordpress.com/stats/' . esc_attr( $this->primary_site_slug ), |
||
| 573 | 'meta' => array( |
||
| 574 | 'class' => 'mb-icon', |
||
| 575 | ), |
||
| 576 | ) ); |
||
| 577 | } |
||
| 578 | |||
| 579 | // Add Calypso plans link and plan type indicator |
||
| 580 | if ( is_user_member_of_blog( $current_user->ID ) ) { |
||
| 581 | $plans_url = 'https://wordpress.com/plans/' . esc_attr( $this->primary_site_slug ); |
||
| 582 | $label = __( 'Plan', 'jetpack' ); |
||
| 583 | $plan = Jetpack::get_active_plan(); |
||
| 584 | |||
| 585 | $plan_title = $this->create_menu_item_pair( |
||
| 586 | array( |
||
| 587 | 'url' => $plans_url, |
||
| 588 | 'id' => 'wp-admin-bar-plan', |
||
| 589 | 'label' => $label, |
||
| 590 | ), |
||
| 591 | array( |
||
| 592 | 'url' => $plans_url, |
||
| 593 | 'id' => 'wp-admin-bar-plan-badge', |
||
| 594 | 'label' => $plan['product_name_short'] |
||
| 595 | ) |
||
| 596 | ); |
||
| 597 | |||
| 598 | $wp_admin_bar->add_menu( array( |
||
| 599 | 'parent' => 'blog', |
||
| 600 | 'id' => 'plan', |
||
| 601 | 'title' => $plan_title, |
||
| 602 | 'meta' => array( |
||
| 603 | 'class' => 'inline-action', |
||
| 604 | ), |
||
| 605 | ) ); |
||
| 606 | } |
||
| 607 | |||
| 608 | // Publish group |
||
| 609 | $wp_admin_bar->add_group( array( |
||
| 610 | 'parent' => 'blog', |
||
| 611 | 'id' => 'publish', |
||
| 612 | ) ); |
||
| 613 | |||
| 614 | // Publish header |
||
| 615 | $wp_admin_bar->add_menu( array( |
||
| 616 | 'parent' => 'publish', |
||
| 617 | 'id' => 'publish-header', |
||
| 618 | 'title' => _x( 'Publish', 'admin bar menu group label', 'jetpack' ), |
||
| 619 | 'meta' => array( |
||
| 620 | 'class' => 'ab-submenu-header', |
||
| 621 | ), |
||
| 622 | ) ); |
||
| 623 | |||
| 624 | // Blog Posts |
||
| 625 | $posts_title = $this->create_menu_item_pair( |
||
| 626 | array( |
||
| 627 | 'url' => 'https://wordpress.com/posts/' . esc_attr( $this->primary_site_slug ), |
||
| 628 | 'id' => 'wp-admin-bar-edit-post', |
||
| 629 | 'label' => __( 'Blog Posts', 'jetpack' ), |
||
| 630 | ), |
||
| 631 | array( |
||
| 632 | 'url' => 'https://wordpress.com/post/' . esc_attr( $this->primary_site_slug ), |
||
| 633 | 'id' => 'wp-admin-bar-new-post', |
||
| 634 | 'label' => _x( 'Add', 'admin bar menu new item label', 'jetpack' ), |
||
| 635 | ) |
||
| 636 | ); |
||
| 637 | |||
| 638 | if ( ! current_user_can( 'edit_posts' ) ) { |
||
| 639 | $posts_title = $this->create_menu_item_anchor( |
||
| 640 | 'ab-item ab-primary mb-icon', |
||
| 641 | 'https://wordpress.com/posts/' . esc_attr( $this->primary_site_slug ), |
||
| 642 | __( 'Blog Posts', 'jetpack' ), |
||
| 643 | 'wp-admin-bar-edit-post' |
||
| 644 | ); |
||
| 645 | } |
||
| 646 | |||
| 647 | $wp_admin_bar->add_menu( array( |
||
| 648 | 'parent' => 'publish', |
||
| 649 | 'id' => 'new-post', |
||
| 650 | 'title' => $posts_title, |
||
| 651 | 'meta' => array( |
||
| 652 | 'class' => 'inline-action', |
||
| 653 | ), |
||
| 654 | ) ); |
||
| 655 | |||
| 656 | // Pages |
||
| 657 | $pages_title = $this->create_menu_item_pair( |
||
| 658 | array( |
||
| 659 | 'url' => 'https://wordpress.com/pages/' . esc_attr( $this->primary_site_slug ), |
||
| 660 | 'id' => 'wp-admin-bar-edit-page', |
||
| 661 | 'label' => __( 'Pages', 'jetpack' ), |
||
| 662 | ), |
||
| 663 | array( |
||
| 664 | 'url' => 'https://wordpress.com/page/' . esc_attr( $this->primary_site_slug ), |
||
| 665 | 'id' => 'wp-admin-bar-new-page', |
||
| 666 | 'label' => _x( 'Add', 'admin bar menu new item label', 'jetpack' ), |
||
| 667 | ) |
||
| 668 | ); |
||
| 669 | |||
| 670 | if ( ! current_user_can( 'edit_pages' ) ) { |
||
| 671 | $pages_title = $this->create_menu_item_anchor( |
||
| 672 | 'ab-item ab-primary mb-icon', |
||
| 673 | 'https://wordpress.com/pages/' . esc_attr( $this->primary_site_slug ), |
||
| 674 | __( 'Pages', 'jetpack' ), |
||
| 675 | 'wp-admin-bar-edit-page' |
||
| 676 | ); |
||
| 677 | } |
||
| 678 | |||
| 679 | $wp_admin_bar->add_menu( array( |
||
| 680 | 'parent' => 'publish', |
||
| 681 | 'id' => 'new-page', |
||
| 682 | 'title' => $pages_title, |
||
| 683 | 'meta' => array( |
||
| 684 | 'class' => 'inline-action', |
||
| 685 | ), |
||
| 686 | ) ); |
||
| 687 | |||
| 688 | // Testimonials |
||
| 689 | View Code Duplication | if ( Jetpack::is_module_active( 'custom-content-types' ) && get_option( 'jetpack_testimonial' ) ) { |
|
| 690 | $testimonials_title = $this->create_menu_item_pair( |
||
| 691 | array( |
||
| 692 | 'url' => 'https://wordpress.com/types/jetpack-testimonial/' . esc_attr( $this->primary_site_slug ), |
||
| 693 | 'id' => 'wp-admin-bar-edit-testimonial', |
||
| 694 | 'label' => __( 'Testimonials', 'jetpack' ), |
||
| 695 | ), |
||
| 696 | array( |
||
| 697 | 'url' => 'https://wordpress.com/edit/jetpack-testimonial/' . esc_attr( $this->primary_site_slug ), |
||
| 698 | 'id' => 'wp-admin-bar-new-testimonial', |
||
| 699 | 'label' => _x( 'Add', 'Button label for adding a new item via the toolbar menu', 'jetpack' ), |
||
| 700 | ) |
||
| 701 | ); |
||
| 702 | |||
| 703 | if ( ! current_user_can( 'edit_pages' ) ) { |
||
| 704 | $testimonials_title = $this->create_menu_item_anchor( |
||
| 705 | 'ab-item ab-primary mb-icon', |
||
| 706 | 'https://wordpress.com/types/jetpack-testimonial/' . esc_attr( $this->primary_site_slug ), |
||
| 707 | __( 'Testimonials', 'jetpack' ), |
||
| 708 | 'wp-admin-bar-edit-testimonial' |
||
| 709 | ); |
||
| 710 | } |
||
| 711 | |||
| 712 | $wp_admin_bar->add_menu( array( |
||
| 713 | 'parent' => 'publish', |
||
| 714 | 'id' => 'new-jetpack-testimonial', |
||
| 715 | 'title' => $testimonials_title, |
||
| 716 | 'meta' => array( |
||
| 717 | 'class' => 'inline-action', |
||
| 718 | ), |
||
| 719 | ) ); |
||
| 720 | } |
||
| 721 | |||
| 722 | // Portfolio |
||
| 723 | View Code Duplication | if ( Jetpack::is_module_active( 'custom-content-types' ) && get_option( 'jetpack_portfolio' ) ) { |
|
| 724 | $portfolios_title = $this->create_menu_item_pair( |
||
| 725 | array( |
||
| 726 | 'url' => 'https://wordpress.com/types/jetpack-portfolio/' . esc_attr( $this->primary_site_slug ), |
||
| 727 | 'id' => 'wp-admin-bar-edit-portfolio', |
||
| 728 | 'label' => __( 'Portfolio', 'jetpack' ), |
||
| 729 | ), |
||
| 730 | array( |
||
| 731 | 'url' => 'https://wordpress.com/edit/jetpack-portfolio/' . esc_attr( $this->primary_site_slug ), |
||
| 732 | 'id' => 'wp-admin-bar-new-portfolio', |
||
| 733 | 'label' => _x( 'Add', 'Button label for adding a new item via the toolbar menu', 'jetpack' ), |
||
| 734 | ) |
||
| 735 | ); |
||
| 736 | |||
| 737 | if ( ! current_user_can( 'edit_pages' ) ) { |
||
| 738 | $portfolios_title = $this->create_menu_item_anchor( |
||
| 739 | 'ab-item ab-primary mb-icon', |
||
| 740 | 'https://wordpress.com/types/jetpack-portfolio/' . esc_attr( $this->primary_site_slug ), |
||
| 741 | __( 'Portfolio', 'jetpack' ), |
||
| 742 | 'wp-admin-bar-edit-portfolio' |
||
| 743 | ); |
||
| 744 | } |
||
| 745 | |||
| 746 | $wp_admin_bar->add_menu( array( |
||
| 747 | 'parent' => 'publish', |
||
| 748 | 'id' => 'new-jetpack-portfolio', |
||
| 749 | 'title' => $portfolios_title, |
||
| 750 | 'meta' => array( |
||
| 751 | 'class' => 'inline-action', |
||
| 752 | ), |
||
| 753 | ) ); |
||
| 754 | } |
||
| 755 | |||
| 756 | if ( current_user_can( 'edit_theme_options' ) ) { |
||
| 757 | // Look and Feel group |
||
| 758 | $wp_admin_bar->add_group( array( |
||
| 759 | 'parent' => 'blog', |
||
| 760 | 'id' => 'look-and-feel', |
||
| 761 | ) ); |
||
| 762 | |||
| 763 | // Look and Feel header |
||
| 764 | $wp_admin_bar->add_menu( array( |
||
| 765 | 'parent' => 'look-and-feel', |
||
| 766 | 'id' => 'look-and-feel-header', |
||
| 767 | 'title' => _x( 'Personalize', 'admin bar menu group label', 'jetpack' ), |
||
| 768 | 'meta' => array( |
||
| 769 | 'class' => 'ab-submenu-header', |
||
| 770 | ), |
||
| 771 | ) ); |
||
| 772 | |||
| 773 | if ( is_admin() ) { |
||
| 774 | // In wp-admin the `return` query arg will return to that page after closing the Customizer |
||
| 775 | $customizer_url = add_query_arg( array( 'return' => urlencode( site_url( $_SERVER['REQUEST_URI'] ) ) ), wp_customize_url() ); |
||
| 776 | } else { |
||
| 777 | // On the frontend the `url` query arg will load that page in the Customizer and also return to it after closing |
||
| 778 | // non-home URLs won't work unless we undo domain mapping since the Customizer preview is unmapped to always have HTTPS |
||
| 779 | $current_page = '//' . $this->primary_site_slug . $_SERVER['REQUEST_URI']; |
||
| 780 | $customizer_url = add_query_arg( array( 'url' => urlencode( $current_page ) ), wp_customize_url() ); |
||
| 781 | } |
||
| 782 | |||
| 783 | $theme_title = $this->create_menu_item_pair( |
||
| 784 | array( |
||
| 785 | 'url' => 'https://wordpress.com/design/' . esc_attr( $this->primary_site_slug ), |
||
| 786 | 'id' => 'wp-admin-bar-themes', |
||
| 787 | 'label' => __( 'Themes', 'jetpack' ), |
||
| 788 | ), |
||
| 789 | array( |
||
| 790 | 'url' => $customizer_url, |
||
| 791 | 'id' => 'wp-admin-bar-cmz', |
||
| 792 | 'label' => _x( 'Customize', 'admin bar customize item label', 'jetpack' ), |
||
| 793 | ) |
||
| 794 | ); |
||
| 795 | $meta = array( 'class' => 'mb-icon', 'class' => 'inline-action' ); |
||
| 796 | $href = false; |
||
| 797 | |||
| 798 | $wp_admin_bar->add_menu( array( |
||
| 799 | 'parent' => 'look-and-feel', |
||
| 800 | 'id' => 'themes', |
||
| 801 | 'title' => $theme_title, |
||
| 802 | 'href' => $href, |
||
| 803 | 'meta' => $meta |
||
| 804 | ) ); |
||
| 805 | } |
||
| 806 | |||
| 807 | if ( current_user_can( 'manage_options' ) ) { |
||
| 808 | // Configuration group |
||
| 809 | $wp_admin_bar->add_group( array( |
||
| 810 | 'parent' => 'blog', |
||
| 811 | 'id' => 'configuration', |
||
| 812 | ) ); |
||
| 813 | |||
| 814 | // Configuration header |
||
| 815 | $wp_admin_bar->add_menu( array( |
||
| 816 | 'parent' => 'configuration', |
||
| 817 | 'id' => 'configuration-header', |
||
| 818 | 'title' => __( 'Configure', 'admin bar menu group label', 'jetpack' ), |
||
| 819 | 'meta' => array( |
||
| 820 | 'class' => 'ab-submenu-header', |
||
| 821 | ), |
||
| 822 | ) ); |
||
| 823 | |||
| 824 | View Code Duplication | if ( Jetpack::is_module_active( 'publicize' ) || Jetpack::is_module_active( 'sharedaddy' ) ) { |
|
| 825 | $wp_admin_bar->add_menu( array( |
||
| 826 | 'parent' => 'configuration', |
||
| 827 | 'id' => 'sharing', |
||
| 828 | 'title' => __( 'Sharing', 'jetpack' ), |
||
| 829 | 'href' => 'https://wordpress.com/sharing/' . esc_attr( $this->primary_site_slug ), |
||
| 830 | 'meta' => array( |
||
| 831 | 'class' => 'mb-icon', |
||
| 832 | ), |
||
| 833 | ) ); |
||
| 834 | } |
||
| 835 | |||
| 836 | $people_title = $this->create_menu_item_pair( |
||
| 837 | array( |
||
| 838 | 'url' => 'https://wordpress.com/people/team/' . esc_attr( $this->primary_site_slug ), |
||
| 839 | 'id' => 'wp-admin-bar-people', |
||
| 840 | 'label' => __( 'People', 'jetpack' ), |
||
| 841 | ), |
||
| 842 | array( |
||
| 843 | 'url' => admin_url( 'user-new.php' ), |
||
| 844 | 'id' => 'wp-admin-bar-people-add', |
||
| 845 | 'label' => _x( 'Add', 'admin bar people item label', 'jetpack' ), |
||
| 846 | ) |
||
| 847 | ); |
||
| 848 | |||
| 849 | $wp_admin_bar->add_menu( array( |
||
| 850 | 'parent' => 'configuration', |
||
| 851 | 'id' => 'users-toolbar', |
||
| 852 | 'title' => $people_title, |
||
| 853 | 'href' => false, |
||
| 854 | 'meta' => array( |
||
| 855 | 'class' => 'inline-action', |
||
| 856 | ), |
||
| 857 | ) ); |
||
| 858 | |||
| 859 | $plugins_title = $this->create_menu_item_pair( |
||
| 860 | array( |
||
| 861 | 'url' => 'https://wordpress.com/plugins/' . esc_attr( $this->primary_site_slug ), |
||
| 862 | 'id' => 'wp-admin-bar-plugins', |
||
| 863 | 'label' => __( 'Plugins', 'jetpack' ), |
||
| 864 | ), |
||
| 865 | array( |
||
| 866 | 'url' => 'https://wordpress.com/plugins/browse/' . esc_attr( $this->primary_site_slug ), |
||
| 867 | 'id' => 'wp-admin-bar-plugins-add', |
||
| 868 | 'label' => _x( 'Add', 'Label for the button on the Masterbar to add a new plugin', 'jetpack' ), |
||
| 869 | ) |
||
| 870 | ); |
||
| 871 | |||
| 872 | $wp_admin_bar->add_menu( array( |
||
| 873 | 'parent' => 'configuration', |
||
| 874 | 'id' => 'plugins', |
||
| 875 | 'title' => $plugins_title, |
||
| 876 | 'href' => false, |
||
| 877 | 'meta' => array( |
||
| 878 | 'class' => 'inline-action', |
||
| 879 | ), |
||
| 880 | ) ); |
||
| 881 | |||
| 882 | if ( $this->is_automated_transfer_site() ) { |
||
| 883 | $domain_title = $this->create_menu_item_pair( |
||
| 884 | array( |
||
| 885 | 'url' => 'https://wordpress.com/domains/' . esc_attr( $this->primary_site_slug ), |
||
| 886 | 'id' => 'wp-admin-bar-domains', |
||
| 887 | 'label' => __( 'Domains', 'jetpack' ), |
||
| 888 | ), |
||
| 889 | array( |
||
| 890 | 'url' => 'https://wordpress.com/domains/add/' . esc_attr( $this->primary_site_slug ), |
||
| 891 | 'id' => 'wp-admin-bar-domains-add', |
||
| 892 | 'label' => _x( 'Add', 'Label for the button on the Masterbar to add a new domain', 'jetpack' ), |
||
| 893 | ) |
||
| 894 | ); |
||
| 895 | $wp_admin_bar->add_menu( array( |
||
| 896 | 'parent' => 'configuration', |
||
| 897 | 'id' => 'domains', |
||
| 898 | 'title' => $domain_title, |
||
| 899 | 'href' => false, |
||
| 900 | 'meta' => array( |
||
| 901 | 'class' => 'inline-action', |
||
| 902 | ), |
||
| 903 | ) ); |
||
| 904 | } |
||
| 905 | |||
| 906 | $wp_admin_bar->add_menu( array( |
||
| 907 | 'parent' => 'configuration', |
||
| 908 | 'id' => 'blog-settings', |
||
| 909 | 'title' => __( 'Settings', 'jetpack' ), |
||
| 910 | 'href' => 'https://wordpress.com/settings/general/' . esc_attr( $this->primary_site_slug ), |
||
| 911 | 'meta' => array( |
||
| 912 | 'class' => 'mb-icon', |
||
| 913 | ), |
||
| 914 | ) ); |
||
| 915 | |||
| 916 | if ( ! is_admin() ) { |
||
| 917 | $wp_admin_bar->add_menu( array( |
||
| 918 | 'parent' => 'configuration', |
||
| 919 | 'id' => 'legacy-dashboard', |
||
| 920 | 'title' => __( 'Dashboard', 'jetpack' ), |
||
| 921 | 'href' => admin_url(), |
||
| 922 | 'meta' => array( |
||
| 923 | 'class' => 'mb-icon', |
||
| 924 | ), |
||
| 925 | ) ); |
||
| 926 | } |
||
| 927 | } |
||
| 928 | } |
||
| 929 | } |
||
| 930 |
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: