Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like A8C_WPCOM_Masterbar often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use A8C_WPCOM_Masterbar, and based on these observations, apply Extract Interface, too.
1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
||
11 | class A8C_WPCOM_Masterbar { |
||
12 | /** |
||
13 | * Use for testing changes made to remotely enqueued scripts and styles on your sandbox. |
||
14 | * If not set it will default to loading the ones from WordPress.com. |
||
15 | * |
||
16 | * @var string $sandbox_url |
||
17 | */ |
||
18 | private $sandbox_url = ''; |
||
19 | |||
20 | /** |
||
21 | * Current locale. |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | private $locale; |
||
26 | |||
27 | /** |
||
28 | * Current User ID. |
||
29 | * |
||
30 | * @var int |
||
31 | */ |
||
32 | private $user_id; |
||
33 | /** |
||
34 | * WordPress.com user data of the connected user. |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | private $user_data; |
||
39 | /** |
||
40 | * WordPress.com username for the connected user. |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | private $user_login; |
||
45 | /** |
||
46 | * WordPress.com email address for the connected user. |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | private $user_email; |
||
51 | /** |
||
52 | * WordPress.com display name for the connected user. |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | private $display_name; |
||
57 | /** |
||
58 | * Site URL sanitized for usage in WordPress.com slugs. |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | private $primary_site_slug; |
||
63 | /** |
||
64 | * Text direction (ltr or rtl) based on connected WordPress.com user's interface settings. |
||
65 | * |
||
66 | * @var string |
||
67 | */ |
||
68 | private $user_text_direction; |
||
69 | /** |
||
70 | * Number of sites owned by connected WordPress.com user. |
||
71 | * |
||
72 | * @var int |
||
73 | */ |
||
74 | private $user_site_count; |
||
75 | |||
76 | /** |
||
77 | * Constructor |
||
78 | */ |
||
79 | public function __construct() { |
||
80 | add_action( 'admin_bar_init', array( $this, 'init' ) ); |
||
81 | |||
82 | // Post logout on the site, also log the user out of WordPress.com. |
||
83 | add_action( 'wp_logout', array( $this, 'maybe_logout_user_from_wpcom' ) ); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Initialize our masterbar. |
||
88 | */ |
||
89 | public function init() { |
||
90 | $this->locale = $this->get_locale(); |
||
91 | $this->user_id = get_current_user_id(); |
||
92 | |||
93 | // Limit the masterbar to be shown only to connected Jetpack users. |
||
94 | if ( ! Jetpack::is_user_connected( $this->user_id ) ) { |
||
95 | return; |
||
96 | } |
||
97 | |||
98 | // Don't show the masterbar on WordPress mobile apps. |
||
99 | if ( Jetpack_User_Agent_Info::is_mobile_app() ) { |
||
100 | add_filter( 'show_admin_bar', '__return_false' ); |
||
101 | return; |
||
102 | } |
||
103 | |||
104 | // Disable the Masterbar on AMP views. |
||
105 | if ( |
||
106 | class_exists( 'Jetpack_AMP_Support' ) |
||
107 | && Jetpack_AMP_Support::is_amp_request() |
||
108 | ) { |
||
109 | return; |
||
110 | } |
||
111 | |||
112 | Jetpack::dns_prefetch( |
||
113 | array( |
||
114 | '//s0.wp.com', |
||
115 | '//s1.wp.com', |
||
116 | '//s2.wp.com', |
||
117 | '//0.gravatar.com', |
||
118 | '//1.gravatar.com', |
||
119 | '//2.gravatar.com', |
||
120 | ) |
||
121 | ); |
||
122 | |||
123 | // Atomic only. |
||
124 | if ( jetpack_is_atomic_site() ) { |
||
125 | /* |
||
126 | * override user setting that hides masterbar from site's front. |
||
127 | * https://github.com/Automattic/jetpack/issues/7667 |
||
128 | */ |
||
129 | add_filter( 'show_admin_bar', '__return_true' ); |
||
130 | } |
||
131 | |||
132 | $this->user_data = Jetpack::get_connected_user_data( $this->user_id ); |
||
133 | $this->user_login = $this->user_data['login']; |
||
134 | $this->user_email = $this->user_data['email']; |
||
135 | $this->display_name = $this->user_data['display_name']; |
||
136 | $this->user_site_count = $this->user_data['site_count']; |
||
137 | |||
138 | // Used to build menu links that point directly to Calypso. |
||
139 | $this->primary_site_slug = Jetpack::build_raw_urls( get_home_url() ); |
||
140 | |||
141 | // Used for display purposes and for building WP Admin links. |
||
142 | $this->primary_site_url = str_replace( '::', '/', $this->primary_site_slug ); |
||
|
|||
143 | |||
144 | // We need to use user's setting here, instead of relying on current blog's text direction. |
||
145 | $this->user_text_direction = $this->user_data['text_direction']; |
||
146 | |||
147 | if ( $this->is_rtl() ) { |
||
148 | // Extend core WP_Admin_Bar class in order to add rtl styles. |
||
149 | add_filter( 'wp_admin_bar_class', array( $this, 'get_rtl_admin_bar_class' ) ); |
||
150 | } |
||
151 | add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) ); |
||
152 | |||
153 | add_action( 'wp_before_admin_bar_render', array( $this, 'replace_core_masterbar' ), 99999 ); |
||
154 | |||
155 | add_action( 'wp_enqueue_scripts', array( $this, 'add_styles_and_scripts' ) ); |
||
156 | add_action( 'admin_enqueue_scripts', array( $this, 'add_styles_and_scripts' ) ); |
||
157 | |||
158 | add_action( 'wp_enqueue_scripts', array( $this, 'remove_core_styles' ) ); |
||
159 | add_action( 'admin_enqueue_scripts', array( $this, 'remove_core_styles' ) ); |
||
160 | |||
161 | if ( Jetpack::is_module_active( 'notes' ) && $this->is_rtl() ) { |
||
162 | // Override Notification module to include RTL styles. |
||
163 | add_action( 'a8c_wpcom_masterbar_enqueue_rtl_notification_styles', '__return_true' ); |
||
164 | } |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Log out from WordPress.com when logging out of the local site. |
||
169 | */ |
||
170 | public function maybe_logout_user_from_wpcom() { |
||
171 | /** |
||
172 | * Whether we should sign out from wpcom too when signing out from the masterbar. |
||
173 | * |
||
174 | * @since 5.9.0 |
||
175 | * |
||
176 | * @param bool $masterbar_should_logout_from_wpcom True by default. |
||
177 | */ |
||
178 | $masterbar_should_logout_from_wpcom = apply_filters( 'jetpack_masterbar_should_logout_from_wpcom', true ); |
||
179 | if ( |
||
180 | // No need to check for a nonce here, it happens further up. |
||
181 | isset( $_GET['context'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
||
182 | && 'masterbar' === $_GET['context'] // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
||
183 | && $masterbar_should_logout_from_wpcom |
||
184 | ) { |
||
185 | do_action( 'wp_masterbar_logout' ); |
||
186 | } |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Get class name for RTL sites. |
||
191 | */ |
||
192 | public function get_rtl_admin_bar_class() { |
||
193 | return 'RTL_Admin_Bar'; |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Adds CSS classes to admin body tag. |
||
198 | * |
||
199 | * @since 5.1 |
||
200 | * |
||
201 | * @param string $admin_body_classes CSS classes that will be added. |
||
202 | * |
||
203 | * @return string |
||
204 | */ |
||
205 | public function admin_body_class( $admin_body_classes ) { |
||
206 | return "$admin_body_classes jetpack-masterbar"; |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Remove the default Admin Bar CSS. |
||
211 | */ |
||
212 | public function remove_core_styles() { |
||
213 | /* |
||
214 | * Notifications need the admin bar styles, |
||
215 | * so let's not remove them when the module is active. |
||
216 | */ |
||
217 | if ( ! Jetpack::is_module_active( 'notes' ) ) { |
||
218 | wp_dequeue_style( 'admin-bar' ); |
||
219 | } |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Check if the user settings are for an RTL language or not. |
||
224 | */ |
||
225 | public function is_rtl() { |
||
228 | |||
229 | /** |
||
230 | * Enqueue our own CSS and JS to display our custom admin bar. |
||
231 | */ |
||
232 | public function add_styles_and_scripts() { |
||
276 | |||
277 | /** |
||
278 | * Get base URL where our CSS and JS will come from. |
||
279 | * |
||
280 | * @param string $file File path for a static resource. |
||
281 | */ |
||
282 | private function wpcom_static_url( $file ) { |
||
293 | |||
294 | /** |
||
295 | * Remove the default admin bar items and replace it with our own admin bar. |
||
296 | */ |
||
297 | public function replace_core_masterbar() { |
||
307 | |||
308 | /** |
||
309 | * Remove all existing toolbar entries from core Masterbar |
||
310 | * |
||
311 | * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance. |
||
312 | */ |
||
313 | public function clear_core_masterbar( $wp_admin_bar ) { |
||
318 | |||
319 | /** |
||
320 | * Add entries corresponding to WordPress.com Masterbar |
||
321 | * |
||
322 | * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance. |
||
323 | */ |
||
324 | public function build_wpcom_masterbar( $wp_admin_bar ) { |
||
345 | |||
346 | /** |
||
347 | * Get WordPress.com current locale name. |
||
348 | */ |
||
349 | public function get_locale() { |
||
367 | |||
368 | /** |
||
369 | * Add the Notifications menu item. |
||
370 | * |
||
371 | * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance. |
||
372 | */ |
||
373 | public function add_notifications( $wp_admin_bar ) { |
||
396 | |||
397 | /** |
||
398 | * Add the "Reader" menu item in the root default group. |
||
399 | * |
||
400 | * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance. |
||
401 | */ |
||
402 | public function add_reader_submenu( $wp_admin_bar ) { |
||
501 | |||
502 | /** |
||
503 | * Merge 2 menu items together into 2 link tags. |
||
504 | * |
||
505 | * @param array $primary Array of menu information. |
||
506 | * @param array $secondary Array of menu information. |
||
507 | */ |
||
508 | public function create_menu_item_pair( $primary, $secondary ) { |
||
517 | |||
518 | /** |
||
519 | * Create a link tag based on information about a menu item. |
||
520 | * |
||
521 | * @param string $class Menu item CSS class. |
||
522 | * @param string $url URL you go to when clicking on the menu item. |
||
523 | * @param string $label Menu item title. |
||
524 | * @param string $id Menu item slug. |
||
525 | */ |
||
526 | public function create_menu_item_anchor( $class, $url, $label, $id ) { |
||
529 | |||
530 | /** |
||
531 | * Add Secondary groups for submenu items. |
||
532 | * |
||
533 | * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance. |
||
534 | */ |
||
535 | public function wpcom_adminbar_add_secondary_groups( $wp_admin_bar ) { |
||
564 | |||
565 | /** |
||
566 | * Add User info menu item. |
||
567 | * |
||
568 | * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance. |
||
569 | */ |
||
570 | public function add_me_submenu( $wp_admin_bar ) { |
||
743 | |||
744 | /** |
||
745 | * Add Write Menu item. |
||
746 | * |
||
747 | * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance. |
||
748 | */ |
||
749 | public function add_write_button( $wp_admin_bar ) { |
||
777 | |||
778 | /** |
||
779 | * Add the "My Site" menu item in the root default group. |
||
780 | * |
||
781 | * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance. |
||
782 | */ |
||
783 | public function add_my_sites_submenu( $wp_admin_bar ) { |
||
784 | $current_user = wp_get_current_user(); |
||
785 | |||
786 | $blog_name = get_bloginfo( 'name' ); |
||
787 | if ( empty( $blog_name ) ) { |
||
788 | $blog_name = $this->primary_site_slug; |
||
789 | } |
||
790 | |||
791 | if ( mb_strlen( $blog_name ) > 20 ) { |
||
792 | $blog_name = mb_substr( html_entity_decode( $blog_name, ENT_QUOTES ), 0, 20 ) . '…'; |
||
793 | } |
||
794 | |||
795 | $wp_admin_bar->add_menu( |
||
796 | array( |
||
797 | 'parent' => 'root-default', |
||
798 | 'id' => 'blog', |
||
799 | 'title' => _n( 'My Site', 'My Sites', $this->user_site_count, 'jetpack' ), |
||
800 | 'href' => '#', |
||
801 | 'meta' => array( |
||
802 | 'class' => 'my-sites mb-trackable', |
||
803 | ), |
||
804 | ) |
||
805 | ); |
||
806 | |||
807 | if ( $this->user_site_count > 1 ) { |
||
808 | $wp_admin_bar->add_menu( |
||
809 | array( |
||
810 | 'parent' => 'blog', |
||
811 | 'id' => 'switch-site', |
||
812 | 'title' => esc_html__( 'Switch Site', 'jetpack' ), |
||
813 | 'href' => 'https://wordpress.com/sites', |
||
814 | ) |
||
815 | ); |
||
816 | } else { |
||
817 | $wp_admin_bar->add_menu( |
||
818 | array( |
||
819 | 'parent' => 'blog', |
||
820 | 'id' => 'new-site', |
||
821 | 'title' => esc_html__( '+ Add New WordPress', 'jetpack' ), |
||
822 | 'href' => 'https://wordpress.com/start?ref=admin-bar-logged-in', |
||
823 | ) |
||
824 | ); |
||
825 | } |
||
826 | |||
827 | if ( is_user_member_of_blog( $current_user->ID ) ) { |
||
828 | $blavatar = ''; |
||
829 | $class = 'current-site'; |
||
830 | |||
831 | if ( has_site_icon() ) { |
||
832 | $src = get_site_icon_url(); |
||
833 | $blavatar = '<img class="avatar" src="' . esc_attr( $src ) . '" alt="Current site avatar">'; |
||
834 | $class = 'has-blavatar'; |
||
835 | } |
||
836 | |||
837 | $blog_info = '<div class="ab-site-icon">' . $blavatar . '</div>'; |
||
838 | $blog_info .= '<span class="ab-site-title">' . esc_html( $blog_name ) . '</span>'; |
||
839 | $blog_info .= '<span class="ab-site-description">' . esc_html( $this->primary_site_url ) . '</span>'; |
||
840 | |||
841 | $wp_admin_bar->add_menu( |
||
842 | array( |
||
843 | 'parent' => 'blog', |
||
844 | 'id' => 'blog-info', |
||
845 | 'title' => $blog_info, |
||
846 | 'href' => esc_url( trailingslashit( $this->primary_site_url ) ), |
||
847 | 'meta' => array( |
||
848 | 'class' => $class, |
||
849 | ), |
||
850 | ) |
||
851 | ); |
||
852 | } |
||
853 | |||
854 | // Site Preview. |
||
855 | if ( is_admin() ) { |
||
856 | $wp_admin_bar->add_menu( |
||
857 | array( |
||
858 | 'parent' => 'blog', |
||
859 | 'id' => 'site-view', |
||
860 | 'title' => __( 'View Site', 'jetpack' ), |
||
861 | 'href' => home_url(), |
||
862 | 'meta' => array( |
||
863 | 'class' => 'mb-icon', |
||
864 | 'target' => '_blank', |
||
865 | ), |
||
866 | ) |
||
867 | ); |
||
868 | } |
||
869 | |||
870 | $this->add_my_home_submenu_item( $wp_admin_bar ); |
||
871 | |||
872 | // Stats. |
||
873 | View Code Duplication | if ( Jetpack::is_module_active( 'stats' ) && current_user_can( 'view_stats' ) ) { |
|
874 | $wp_admin_bar->add_menu( |
||
875 | array( |
||
876 | 'parent' => 'blog', |
||
877 | 'id' => 'blog-stats', |
||
878 | 'title' => esc_html__( 'Stats', 'jetpack' ), |
||
879 | 'href' => 'https://wordpress.com/stats/' . esc_attr( $this->primary_site_slug ), |
||
880 | 'meta' => array( |
||
881 | 'class' => 'mb-icon', |
||
882 | ), |
||
883 | ) |
||
884 | ); |
||
885 | } |
||
886 | |||
887 | View Code Duplication | if ( current_user_can( 'manage_options' ) ) { |
|
888 | $wp_admin_bar->add_menu( |
||
889 | array( |
||
890 | 'parent' => 'blog', |
||
891 | 'id' => 'activity', |
||
892 | 'title' => esc_html__( 'Activity', 'jetpack' ), |
||
893 | 'href' => 'https://wordpress.com/activity-log/' . esc_attr( $this->primary_site_slug ), |
||
894 | 'meta' => array( |
||
895 | 'class' => 'mb-icon', |
||
896 | ), |
||
897 | ) |
||
898 | ); |
||
899 | } |
||
900 | |||
901 | // Add Calypso plans link and plan type indicator. |
||
902 | if ( is_user_member_of_blog( $current_user->ID ) && current_user_can( 'manage_options' ) ) { |
||
903 | $plans_url = 'https://wordpress.com/plans/' . esc_attr( $this->primary_site_slug ); |
||
904 | $label = esc_html__( 'Plan', 'jetpack' ); |
||
905 | $plan = Jetpack_Plan::get(); |
||
906 | |||
907 | $plan_title = $this->create_menu_item_pair( |
||
908 | array( |
||
909 | 'url' => $plans_url, |
||
910 | 'id' => 'wp-admin-bar-plan', |
||
911 | 'label' => $label, |
||
912 | ), |
||
913 | array( |
||
914 | 'url' => $plans_url, |
||
915 | 'id' => 'wp-admin-bar-plan-badge', |
||
916 | 'label' => $plan['product_name_short'], |
||
917 | ) |
||
918 | ); |
||
919 | |||
920 | $wp_admin_bar->add_menu( |
||
921 | array( |
||
922 | 'parent' => 'blog', |
||
923 | 'id' => 'plan', |
||
924 | 'title' => $plan_title, |
||
925 | 'meta' => array( |
||
926 | 'class' => 'inline-action', |
||
927 | ), |
||
928 | ) |
||
929 | ); |
||
930 | } |
||
931 | |||
932 | // Publish group. |
||
933 | $wp_admin_bar->add_group( |
||
934 | array( |
||
935 | 'parent' => 'blog', |
||
936 | 'id' => 'publish', |
||
937 | ) |
||
938 | ); |
||
939 | |||
940 | // Publish header. |
||
941 | $wp_admin_bar->add_menu( |
||
942 | array( |
||
943 | 'parent' => 'publish', |
||
944 | 'id' => 'publish-header', |
||
945 | 'title' => esc_html_x( 'Manage', 'admin bar menu group label', 'jetpack' ), |
||
946 | 'meta' => array( |
||
947 | 'class' => 'ab-submenu-header', |
||
948 | ), |
||
949 | ) |
||
950 | ); |
||
951 | |||
952 | // Pages. |
||
953 | $pages_title = $this->create_menu_item_pair( |
||
954 | array( |
||
955 | 'url' => 'https://wordpress.com/pages/' . esc_attr( $this->primary_site_slug ), |
||
956 | 'id' => 'wp-admin-bar-edit-page', |
||
957 | 'label' => esc_html__( 'Site Pages', 'jetpack' ), |
||
958 | ), |
||
959 | array( |
||
960 | 'url' => 'https://wordpress.com/page/' . esc_attr( $this->primary_site_slug ), |
||
961 | 'id' => 'wp-admin-bar-new-page-badge', |
||
962 | 'label' => esc_html_x( 'Add', 'admin bar menu new item label', 'jetpack' ), |
||
963 | ) |
||
964 | ); |
||
965 | |||
966 | if ( ! current_user_can( 'edit_pages' ) ) { |
||
967 | $pages_title = $this->create_menu_item_anchor( |
||
968 | 'ab-item ab-primary mb-icon', |
||
969 | 'https://wordpress.com/pages/' . esc_attr( $this->primary_site_slug ), |
||
970 | esc_html__( 'Site Pages', 'jetpack' ), |
||
971 | 'wp-admin-bar-edit-page' |
||
972 | ); |
||
973 | } |
||
974 | |||
975 | $wp_admin_bar->add_menu( |
||
976 | array( |
||
977 | 'parent' => 'publish', |
||
978 | 'id' => 'new-page', |
||
979 | 'title' => $pages_title, |
||
980 | 'meta' => array( |
||
981 | 'class' => 'inline-action', |
||
982 | ), |
||
983 | ) |
||
984 | ); |
||
985 | |||
986 | // Blog Posts. |
||
987 | $posts_title = $this->create_menu_item_pair( |
||
988 | array( |
||
989 | 'url' => 'https://wordpress.com/posts/' . esc_attr( $this->primary_site_slug ), |
||
990 | 'id' => 'wp-admin-bar-edit-post', |
||
991 | 'label' => esc_html__( 'Blog Posts', 'jetpack' ), |
||
992 | ), |
||
993 | array( |
||
994 | 'url' => 'https://wordpress.com/post/' . esc_attr( $this->primary_site_slug ), |
||
995 | 'id' => 'wp-admin-bar-new-post-badge', |
||
996 | 'label' => esc_html_x( 'Add', 'admin bar menu new item label', 'jetpack' ), |
||
997 | ) |
||
998 | ); |
||
999 | |||
1000 | if ( ! current_user_can( 'edit_posts' ) ) { |
||
1001 | $posts_title = $this->create_menu_item_anchor( |
||
1002 | 'ab-item ab-primary mb-icon', |
||
1003 | 'https://wordpress.com/posts/' . esc_attr( $this->primary_site_slug ), |
||
1004 | esc_html__( 'Blog Posts', 'jetpack' ), |
||
1005 | 'wp-admin-bar-edit-post' |
||
1006 | ); |
||
1007 | } |
||
1008 | |||
1009 | $wp_admin_bar->add_menu( |
||
1010 | array( |
||
1011 | 'parent' => 'publish', |
||
1012 | 'id' => 'new-post', |
||
1013 | 'title' => $posts_title, |
||
1014 | 'meta' => array( |
||
1015 | 'class' => 'inline-action mb-trackable', |
||
1016 | ), |
||
1017 | ) |
||
1018 | ); |
||
1019 | |||
1020 | // Comments. |
||
1021 | View Code Duplication | if ( current_user_can( 'moderate_comments' ) ) { |
|
1022 | $wp_admin_bar->add_menu( |
||
1023 | array( |
||
1024 | 'parent' => 'publish', |
||
1025 | 'id' => 'comments', |
||
1026 | 'title' => __( 'Comments', 'jetpack' ), |
||
1027 | 'href' => 'https://wordpress.com/comments/' . esc_attr( $this->primary_site_slug ), |
||
1028 | 'meta' => array( |
||
1029 | 'class' => 'mb-icon', |
||
1030 | ), |
||
1031 | ) |
||
1032 | ); |
||
1033 | } |
||
1034 | |||
1035 | // Testimonials. |
||
1036 | View Code Duplication | if ( Jetpack::is_module_active( 'custom-content-types' ) && get_option( 'jetpack_testimonial' ) ) { |
|
1037 | $testimonials_title = $this->create_menu_item_pair( |
||
1038 | array( |
||
1039 | 'url' => 'https://wordpress.com/types/jetpack-testimonial/' . esc_attr( $this->primary_site_slug ), |
||
1040 | 'id' => 'wp-admin-bar-edit-testimonial', |
||
1041 | 'label' => esc_html__( 'Testimonials', 'jetpack' ), |
||
1042 | ), |
||
1043 | array( |
||
1044 | 'url' => 'https://wordpress.com/edit/jetpack-testimonial/' . esc_attr( $this->primary_site_slug ), |
||
1045 | 'id' => 'wp-admin-bar-new-testimonial', |
||
1046 | 'label' => esc_html_x( 'Add', 'Button label for adding a new item via the toolbar menu', 'jetpack' ), |
||
1047 | ) |
||
1048 | ); |
||
1049 | |||
1050 | if ( ! current_user_can( 'edit_pages' ) ) { |
||
1051 | $testimonials_title = $this->create_menu_item_anchor( |
||
1052 | 'ab-item ab-primary mb-icon', |
||
1053 | 'https://wordpress.com/types/jetpack-testimonial/' . esc_attr( $this->primary_site_slug ), |
||
1054 | esc_html__( 'Testimonials', 'jetpack' ), |
||
1055 | 'wp-admin-bar-edit-testimonial' |
||
1056 | ); |
||
1057 | } |
||
1058 | |||
1059 | $wp_admin_bar->add_menu( |
||
1060 | array( |
||
1061 | 'parent' => 'publish', |
||
1062 | 'id' => 'new-jetpack-testimonial', |
||
1063 | 'title' => $testimonials_title, |
||
1064 | 'meta' => array( |
||
1065 | 'class' => 'inline-action', |
||
1066 | ), |
||
1067 | ) |
||
1068 | ); |
||
1069 | } |
||
1070 | |||
1071 | // Portfolio. |
||
1072 | View Code Duplication | if ( Jetpack::is_module_active( 'custom-content-types' ) && get_option( 'jetpack_portfolio' ) ) { |
|
1073 | $portfolios_title = $this->create_menu_item_pair( |
||
1074 | array( |
||
1075 | 'url' => 'https://wordpress.com/types/jetpack-portfolio/' . esc_attr( $this->primary_site_slug ), |
||
1076 | 'id' => 'wp-admin-bar-edit-portfolio', |
||
1077 | 'label' => esc_html__( 'Portfolio', 'jetpack' ), |
||
1078 | ), |
||
1079 | array( |
||
1080 | 'url' => 'https://wordpress.com/edit/jetpack-portfolio/' . esc_attr( $this->primary_site_slug ), |
||
1081 | 'id' => 'wp-admin-bar-new-portfolio', |
||
1082 | 'label' => esc_html_x( 'Add', 'Button label for adding a new item via the toolbar menu', 'jetpack' ), |
||
1083 | ) |
||
1084 | ); |
||
1085 | |||
1086 | if ( ! current_user_can( 'edit_pages' ) ) { |
||
1087 | $portfolios_title = $this->create_menu_item_anchor( |
||
1088 | 'ab-item ab-primary mb-icon', |
||
1089 | 'https://wordpress.com/types/jetpack-portfolio/' . esc_attr( $this->primary_site_slug ), |
||
1090 | esc_html__( 'Portfolio', 'jetpack' ), |
||
1091 | 'wp-admin-bar-edit-portfolio' |
||
1092 | ); |
||
1093 | } |
||
1094 | |||
1095 | $wp_admin_bar->add_menu( |
||
1096 | array( |
||
1097 | 'parent' => 'publish', |
||
1098 | 'id' => 'new-jetpack-portfolio', |
||
1099 | 'title' => $portfolios_title, |
||
1100 | 'meta' => array( |
||
1101 | 'class' => 'inline-action', |
||
1102 | ), |
||
1103 | ) |
||
1104 | ); |
||
1105 | } |
||
1106 | |||
1107 | if ( current_user_can( 'edit_theme_options' ) ) { |
||
1108 | // Look and Feel group. |
||
1109 | $wp_admin_bar->add_group( |
||
1110 | array( |
||
1111 | 'parent' => 'blog', |
||
1112 | 'id' => 'look-and-feel', |
||
1113 | ) |
||
1114 | ); |
||
1115 | |||
1116 | // Look and Feel header. |
||
1117 | $wp_admin_bar->add_menu( |
||
1118 | array( |
||
1119 | 'parent' => 'look-and-feel', |
||
1120 | 'id' => 'look-and-feel-header', |
||
1121 | 'title' => esc_html_x( 'Personalize', 'admin bar menu group label', 'jetpack' ), |
||
1122 | 'meta' => array( |
||
1123 | 'class' => 'ab-submenu-header', |
||
1124 | ), |
||
1125 | ) |
||
1126 | ); |
||
1127 | |||
1128 | if ( is_admin() ) { |
||
1129 | // In wp-admin the `return` query arg will return to that page after closing the Customizer. |
||
1130 | $customizer_url = add_query_arg( |
||
1131 | array( |
||
1132 | 'return' => rawurlencode( site_url( $_SERVER['REQUEST_URI'] ) ), |
||
1133 | ), |
||
1134 | wp_customize_url() |
||
1135 | ); |
||
1136 | } else { |
||
1137 | /* |
||
1138 | * On the frontend the `url` query arg will load that page in the Customizer |
||
1139 | * and also return to it after closing |
||
1140 | * non-home URLs won't work unless we undo domain mapping |
||
1141 | * since the Customizer preview is unmapped to always have HTTPS. |
||
1142 | */ |
||
1143 | $current_page = '//' . $this->primary_site_slug . $_SERVER['REQUEST_URI']; |
||
1144 | $customizer_url = add_query_arg( array( 'url' => rawurlencode( $current_page ) ), wp_customize_url() ); |
||
1145 | } |
||
1146 | |||
1147 | $theme_title = $this->create_menu_item_pair( |
||
1148 | array( |
||
1149 | 'url' => $customizer_url, |
||
1150 | 'id' => 'wp-admin-bar-cmz', |
||
1151 | 'label' => esc_html_x( 'Customize', 'admin bar customize item label', 'jetpack' ), |
||
1152 | ), |
||
1153 | array( |
||
1154 | 'url' => 'https://wordpress.com/themes/' . esc_attr( $this->primary_site_slug ), |
||
1155 | 'id' => 'wp-admin-bar-themes', |
||
1156 | 'label' => esc_html__( 'Themes', 'jetpack' ), |
||
1157 | ) |
||
1158 | ); |
||
1159 | $meta = array( |
||
1160 | 'class' => 'mb-icon', |
||
1161 | 'class' => 'inline-action', |
||
1162 | ); |
||
1163 | $href = false; |
||
1164 | |||
1165 | $wp_admin_bar->add_menu( |
||
1166 | array( |
||
1167 | 'parent' => 'look-and-feel', |
||
1168 | 'id' => 'themes', |
||
1169 | 'title' => $theme_title, |
||
1170 | 'href' => $href, |
||
1171 | 'meta' => $meta, |
||
1172 | ) |
||
1173 | ); |
||
1174 | } |
||
1175 | |||
1176 | if ( current_user_can( 'manage_options' ) ) { |
||
1177 | // Configuration group. |
||
1178 | $wp_admin_bar->add_group( |
||
1179 | array( |
||
1180 | 'parent' => 'blog', |
||
1181 | 'id' => 'configuration', |
||
1182 | ) |
||
1183 | ); |
||
1184 | |||
1185 | // Configuration header. |
||
1186 | $wp_admin_bar->add_menu( |
||
1187 | array( |
||
1188 | 'parent' => 'configuration', |
||
1189 | 'id' => 'configuration-header', |
||
1190 | 'title' => esc_html_x( 'Configure', 'admin bar menu group label', 'jetpack' ), |
||
1191 | 'meta' => array( |
||
1192 | 'class' => 'ab-submenu-header', |
||
1193 | ), |
||
1194 | ) |
||
1195 | ); |
||
1196 | |||
1197 | View Code Duplication | if ( Jetpack::is_module_active( 'publicize' ) || Jetpack::is_module_active( 'sharedaddy' ) ) { |
|
1198 | $wp_admin_bar->add_menu( |
||
1199 | array( |
||
1200 | 'parent' => 'configuration', |
||
1201 | 'id' => 'sharing', |
||
1202 | 'title' => esc_html__( 'Sharing', 'jetpack' ), |
||
1203 | 'href' => 'https://wordpress.com/sharing/' . esc_attr( $this->primary_site_slug ), |
||
1204 | 'meta' => array( |
||
1205 | 'class' => 'mb-icon', |
||
1206 | ), |
||
1207 | ) |
||
1208 | ); |
||
1209 | } |
||
1210 | |||
1211 | $people_title = $this->create_menu_item_pair( |
||
1212 | array( |
||
1213 | 'url' => 'https://wordpress.com/people/team/' . esc_attr( $this->primary_site_slug ), |
||
1214 | 'id' => 'wp-admin-bar-people', |
||
1215 | 'label' => esc_html__( 'People', 'jetpack' ), |
||
1216 | ), |
||
1217 | array( |
||
1218 | 'url' => admin_url( 'user-new.php' ), |
||
1219 | 'id' => 'wp-admin-bar-people-add', |
||
1220 | 'label' => esc_html_x( 'Add', 'admin bar people item label', 'jetpack' ), |
||
1221 | ) |
||
1222 | ); |
||
1223 | |||
1224 | $wp_admin_bar->add_menu( |
||
1225 | array( |
||
1226 | 'parent' => 'configuration', |
||
1227 | 'id' => 'users-toolbar', |
||
1228 | 'title' => $people_title, |
||
1229 | 'href' => false, |
||
1230 | 'meta' => array( |
||
1231 | 'class' => 'inline-action', |
||
1232 | ), |
||
1233 | ) |
||
1234 | ); |
||
1235 | |||
1236 | $plugins_title = $this->create_menu_item_pair( |
||
1237 | array( |
||
1238 | 'url' => 'https://wordpress.com/plugins/' . esc_attr( $this->primary_site_slug ), |
||
1239 | 'id' => 'wp-admin-bar-plugins', |
||
1240 | 'label' => esc_html__( 'Plugins', 'jetpack' ), |
||
1241 | ), |
||
1242 | array( |
||
1243 | 'url' => 'https://wordpress.com/plugins/manage/' . esc_attr( $this->primary_site_slug ), |
||
1244 | 'id' => 'wp-admin-bar-plugins-add', |
||
1245 | 'label' => esc_html_x( 'Manage', 'Label for the button on the Masterbar to manage plugins', 'jetpack' ), |
||
1246 | ) |
||
1247 | ); |
||
1248 | |||
1249 | $wp_admin_bar->add_menu( |
||
1250 | array( |
||
1251 | 'parent' => 'configuration', |
||
1252 | 'id' => 'plugins', |
||
1253 | 'title' => $plugins_title, |
||
1254 | 'href' => false, |
||
1255 | 'meta' => array( |
||
1256 | 'class' => 'inline-action', |
||
1257 | ), |
||
1258 | ) |
||
1259 | ); |
||
1260 | |||
1261 | if ( jetpack_is_atomic_site() ) { |
||
1262 | $domain_title = $this->create_menu_item_pair( |
||
1263 | array( |
||
1264 | 'url' => 'https://wordpress.com/domains/' . esc_attr( $this->primary_site_slug ), |
||
1265 | 'id' => 'wp-admin-bar-domains', |
||
1266 | 'label' => esc_html__( 'Domains', 'jetpack' ), |
||
1267 | ), |
||
1268 | array( |
||
1269 | 'url' => 'https://wordpress.com/domains/add/' . esc_attr( $this->primary_site_slug ), |
||
1270 | 'id' => 'wp-admin-bar-domains-add', |
||
1271 | 'label' => esc_html_x( 'Add', 'Label for the button on the Masterbar to add a new domain', 'jetpack' ), |
||
1272 | ) |
||
1273 | ); |
||
1274 | $wp_admin_bar->add_menu( |
||
1275 | array( |
||
1276 | 'parent' => 'configuration', |
||
1277 | 'id' => 'domains', |
||
1278 | 'title' => $domain_title, |
||
1279 | 'href' => false, |
||
1280 | 'meta' => array( |
||
1281 | 'class' => 'inline-action', |
||
1282 | ), |
||
1283 | ) |
||
1284 | ); |
||
1285 | } |
||
1286 | |||
1287 | $wp_admin_bar->add_menu( |
||
1288 | array( |
||
1289 | 'parent' => 'configuration', |
||
1290 | 'id' => 'blog-settings', |
||
1291 | 'title' => esc_html__( 'Settings', 'jetpack' ), |
||
1292 | 'href' => 'https://wordpress.com/settings/general/' . esc_attr( $this->primary_site_slug ), |
||
1293 | 'meta' => array( |
||
1294 | 'class' => 'mb-icon', |
||
1295 | ), |
||
1296 | ) |
||
1297 | ); |
||
1298 | |||
1299 | if ( ! is_admin() ) { |
||
1300 | $wp_admin_bar->add_menu( |
||
1301 | array( |
||
1302 | 'parent' => 'configuration', |
||
1303 | 'id' => 'legacy-dashboard', |
||
1304 | 'title' => esc_html__( 'Dashboard', 'jetpack' ), |
||
1305 | 'href' => admin_url(), |
||
1306 | 'meta' => array( |
||
1307 | 'class' => 'mb-icon', |
||
1308 | ), |
||
1309 | ) |
||
1310 | ); |
||
1311 | } |
||
1312 | |||
1313 | // Restore dashboard menu toggle that is needed on mobile views. |
||
1314 | if ( is_admin() ) { |
||
1315 | $wp_admin_bar->add_menu( |
||
1316 | array( |
||
1317 | 'id' => 'menu-toggle', |
||
1318 | 'title' => '<span class="ab-icon"></span><span class="screen-reader-text">' . esc_html__( 'Menu', 'jetpack' ) . '</span>', |
||
1319 | 'href' => '#', |
||
1320 | ) |
||
1321 | ); |
||
1322 | } |
||
1323 | |||
1324 | /** |
||
1325 | * Fires when menu items are added to the masterbar "My Sites" menu. |
||
1326 | * |
||
1327 | * @since 5.4.0 |
||
1328 | */ |
||
1329 | do_action( 'jetpack_masterbar' ); |
||
1330 | } |
||
1331 | } |
||
1332 | |||
1333 | /** |
||
1334 | * Calls the wpcom API to get the creation date of the site |
||
1335 | * and determine if it's eligible for the 'My Home' page. |
||
1336 | * |
||
1337 | * @return bool Whether the site has 'My Home' enabled. |
||
1338 | */ |
||
1339 | private function is_my_home_enabled() { |
||
1366 | |||
1367 | /** |
||
1368 | * Adds "My Home" submenu item to sites that are eligible. |
||
1369 | * |
||
1370 | * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance. |
||
1371 | * @return void |
||
1372 | */ |
||
1373 | private function add_my_home_submenu_item( &$wp_admin_bar ) { |
||
1392 | } |
||
1393 |
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: