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 |
||
5 | class A8C_WPCOM_Masterbar { |
||
6 | private $locale; |
||
7 | |||
8 | private $user_id; |
||
9 | private $user_data; |
||
10 | private $user_login; |
||
11 | private $user_email; |
||
12 | private $display_name; |
||
13 | private $primary_site_slug; |
||
14 | private $user_text_direction; |
||
15 | |||
16 | function __construct() { |
||
17 | $this->locale = $this->get_locale(); |
||
18 | $this->user_id = get_current_user_id(); |
||
19 | |||
20 | // Limit the masterbar to be shown only to connected Jetpack users. |
||
21 | if ( ! Jetpack::is_user_connected( $this->user_id ) ) { |
||
22 | return; |
||
23 | } |
||
24 | |||
25 | $this->user_data = Jetpack::get_connected_user_data( $this->user_id ); |
||
26 | $this->user_login = $this->user_data['login']; |
||
27 | $this->user_email = $this->user_data['email']; |
||
28 | $this->display_name = $this->user_data['display_name']; |
||
29 | $this->primary_site_slug = Jetpack::build_raw_urls( get_home_url() ); |
||
30 | // We need to use user's setting here, instead of relying on current blog's text direction |
||
31 | $this->user_text_direction = $this->user_data['text_direction']; |
||
32 | |||
33 | if ( $this->is_rtl() ) { |
||
34 | // Extend core WP_Admin_Bar class in order to add rtl styles |
||
35 | add_filter( 'wp_admin_bar_class', array( $this, 'get_rtl_admin_bar_class' ) ); |
||
36 | } |
||
37 | |||
38 | add_action( 'wp_before_admin_bar_render', array( $this, 'replace_core_masterbar' ), 99999 ); |
||
39 | |||
40 | add_action( 'wp_head', array( $this, 'add_styles_and_scripts' ) ); |
||
41 | add_action( 'admin_head', array( $this, 'add_styles_and_scripts' ) ); |
||
42 | |||
43 | add_action( 'wp_enqueue_scripts', array( $this, 'remove_core_styles' ) ); |
||
44 | add_action( 'admin_enqueue_scripts', array( $this, 'remove_core_styles' ) ); |
||
45 | |||
46 | if ( Jetpack::is_module_active( 'notes' ) && $this->is_rtl() ) { |
||
47 | // Override Notification module to include RTL styles |
||
48 | add_action( 'a8c_wpcom_masterbar_enqueue_rtl_notification_styles', '__return_true' ); |
||
49 | } |
||
50 | } |
||
51 | |||
52 | public function get_rtl_admin_bar_class() { |
||
53 | return 'RTL_Admin_Bar'; |
||
54 | } |
||
55 | |||
56 | public function remove_core_styles() { |
||
57 | wp_dequeue_style( 'admin-bar' ); |
||
58 | } |
||
59 | |||
60 | public function is_rtl() { |
||
61 | return $this->user_text_direction === 'rtl' ? true : false; |
||
62 | } |
||
63 | |||
64 | public function add_styles_and_scripts() { |
||
65 | |||
66 | if ( $this->is_rtl() ) { |
||
67 | wp_enqueue_style( 'a8c-wpcom-masterbar-rtl', $this->wpcom_static_url( '/wp-content/mu-plugins/admin-bar/rtl/wpcom-admin-bar-rtl.css' ) ); |
||
68 | wp_enqueue_style( 'a8c-wpcom-masterbar-overrides-rtl', $this->wpcom_static_url( '/wp-content/mu-plugins/admin-bar/masterbar-overrides/rtl/masterbar-rtl.css' ) ); |
||
69 | } else { |
||
70 | wp_enqueue_style( 'a8c-wpcom-masterbar', $this->wpcom_static_url( '/wp-content/mu-plugins/admin-bar/wpcom-admin-bar.css' ) ); |
||
71 | wp_enqueue_style( 'a8c-wpcom-masterbar-overrides', $this->wpcom_static_url( '/wp-content/mu-plugins/admin-bar/masterbar-overrides/masterbar.css' ) ); |
||
72 | } |
||
73 | |||
74 | // Local overrides |
||
75 | wp_enqueue_style( 'a8c_wpcom_css_override', plugins_url( 'overrides.css', __FILE__ ) ); |
||
76 | |||
77 | if ( ! Jetpack::is_module_active( 'notes ' ) ) { |
||
78 | // Masterbar is relying on some icons from noticons.css |
||
79 | wp_enqueue_style( 'noticons', $this->wpcom_static_url( '/i/noticons/noticons.css' ), array(), JETPACK_NOTES__CACHE_BUSTER ); |
||
80 | } |
||
81 | |||
82 | wp_enqueue_script( 'a8c_wpcom_masterbar_overrides', $this->wpcom_static_url( '/wp-content/mu-plugins/admin-bar/masterbar-overrides/masterbar.js' ) ); |
||
83 | } |
||
84 | |||
85 | function wpcom_static_url( $file ) { |
||
86 | $i = hexdec( substr( md5( $file ), - 1 ) ) % 2; |
||
87 | $url = 'https://s' . $i . '.wp.com' . $file; |
||
88 | |||
89 | return set_url_scheme( $url, 'https'); |
||
90 | } |
||
91 | |||
92 | public function replace_core_masterbar() { |
||
93 | global $wp_admin_bar; |
||
94 | |||
95 | if ( ! is_object( $wp_admin_bar ) ) { |
||
96 | return false; |
||
97 | } |
||
98 | |||
99 | $this->clear_core_masterbar( $wp_admin_bar ); |
||
100 | $this->build_wpcom_masterbar( $wp_admin_bar ); |
||
101 | } |
||
102 | |||
103 | // Remove all existing toolbar entries from core Masterbar |
||
104 | public function clear_core_masterbar( $wp_admin_bar ) { |
||
105 | foreach ( $wp_admin_bar->get_nodes() as $node ) { |
||
106 | $wp_admin_bar->remove_node( $node->id ); |
||
107 | } |
||
108 | } |
||
109 | |||
110 | // Add entries corresponding to WordPress.com Masterbar |
||
111 | public function build_wpcom_masterbar( $wp_admin_bar ) { |
||
112 | // Menu groups |
||
113 | $this->wpcom_adminbar_add_secondary_groups( $wp_admin_bar ); |
||
114 | |||
115 | // Left part |
||
116 | $this->add_my_sites_submenu( $wp_admin_bar ); |
||
117 | $this->add_reader_submenu( $wp_admin_bar ); |
||
118 | |||
119 | // Right part |
||
120 | if ( Jetpack::is_module_active( 'notes' ) ) { |
||
121 | $this->add_notifications( $wp_admin_bar ); |
||
122 | } |
||
123 | |||
124 | $this->add_me_submenu( $wp_admin_bar ); |
||
125 | $this->add_write_button( $wp_admin_bar ); |
||
126 | } |
||
127 | |||
128 | public function get_locale() { |
||
129 | $wpcom_locale = get_locale(); |
||
130 | |||
131 | if ( ! class_exists( 'GP_Locales' ) ) { |
||
132 | if ( defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) && file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) { |
||
133 | require JETPACK__GLOTPRESS_LOCALES_PATH; |
||
134 | } |
||
135 | } |
||
136 | |||
137 | View Code Duplication | if ( class_exists( 'GP_Locales' ) ) { |
|
138 | $wpcom_locale_object = GP_Locales::by_field( 'wp_locale', get_locale() ); |
||
139 | if ( $wpcom_locale_object instanceof GP_Locale ) { |
||
140 | $wpcom_locale = $wpcom_locale_object->slug; |
||
141 | } |
||
142 | } |
||
143 | |||
144 | return $wpcom_locale; |
||
145 | } |
||
146 | |||
147 | public function add_notifications( $wp_admin_bar ) { |
||
148 | $wp_admin_bar->add_node( array( |
||
149 | 'id' => 'notes', |
||
150 | 'title' => '<span id="wpnt-notes-unread-count" class="wpnt-loading wpn-read"></span> |
||
151 | <span class="noticon noticon-bell"></span>', |
||
152 | 'meta' => array( |
||
153 | 'html' => '<div id="wpnt-notes-panel2" style="display:none" lang="'. esc_attr( $this->locale ) . '" dir="' . ( $this->is_rtl() ? 'rtl' : 'ltr' ) . '">' . |
||
154 | '<div class="wpnt-notes-panel-header">' . |
||
155 | '<span class="wpnt-notes-header">' . |
||
156 | __( 'Notifications', 'jetpack' ) . |
||
157 | '</span>' . |
||
158 | '<span class="wpnt-notes-panel-link">' . |
||
159 | '</span>' . |
||
160 | '</div>' . |
||
161 | '</div>', |
||
162 | 'class' => 'menupop', |
||
163 | ), |
||
164 | 'parent' => 'top-secondary', |
||
165 | ) ); |
||
166 | } |
||
167 | |||
168 | public function add_reader_submenu( $wp_admin_bar ) { |
||
169 | $wp_admin_bar->add_menu( array( |
||
170 | 'id' => 'newdash', |
||
171 | 'title' => __( 'Reader', 'jetpack' ), |
||
172 | 'href' => '#', |
||
173 | ) ); |
||
174 | |||
175 | $wp_admin_bar->add_menu( array( |
||
176 | 'parent' => 'newdash', |
||
177 | 'id' => 'streams-header', |
||
178 | 'title' => _x( |
||
179 | 'Streams', |
||
180 | 'Title for Reader sub-menu that contains followed sites, likes, and recommendations', |
||
181 | 'jetpack' |
||
182 | ), |
||
183 | 'meta' => array( |
||
184 | 'class' => 'ab-submenu-header', |
||
185 | ) |
||
186 | ) ); |
||
187 | |||
188 | $following_title = $this->create_menu_item_pair( |
||
189 | array( |
||
190 | 'url' => 'https://wordpress.com/', |
||
191 | 'id' => 'wp-admin-bar-followed-sites', |
||
192 | 'label' => __( 'Followed Sites', 'jetpack' ), |
||
193 | ), |
||
194 | array( |
||
195 | 'url' => 'https://wordpress.com/following/edit', |
||
196 | 'id' => 'wp-admin-bar-reader-followed-sites-manage', |
||
197 | 'label' => __( 'Manage', 'jetpack' ), |
||
198 | ) |
||
199 | ); |
||
200 | |||
201 | $wp_admin_bar->add_menu( array( |
||
202 | 'parent' => 'newdash', |
||
203 | 'id' => 'following', |
||
204 | 'title' => $following_title, |
||
205 | 'meta' => array( 'class' => 'inline-action' ) |
||
206 | ) ); |
||
207 | |||
208 | $wp_admin_bar->add_menu( array( |
||
209 | 'parent' => 'newdash', |
||
210 | 'id' => 'discover-discover', |
||
211 | 'title' => __( 'Discover', 'jetpack' ), |
||
212 | 'href' => 'https://wordpress.com/discover', |
||
213 | 'meta' => array( |
||
214 | 'class' => 'mb-icon-spacer', |
||
215 | ) |
||
216 | ) ); |
||
217 | |||
218 | $wp_admin_bar->add_menu( array( |
||
219 | 'parent' => 'newdash', |
||
220 | 'id' => 'discover-search', |
||
221 | 'title' => __( 'Search', 'jetpack' ), |
||
222 | 'href' => 'https://wordpress.com/read/search', |
||
223 | 'meta' => array( |
||
224 | 'class' => 'mb-icon-spacer', |
||
225 | ) |
||
226 | ) ); |
||
227 | |||
228 | $wp_admin_bar->add_menu( array( |
||
229 | 'parent' => 'newdash', |
||
230 | 'id' => 'discover-recommended-blogs', |
||
231 | 'title' => __( 'Recommendations', 'jetpack' ), |
||
232 | 'href' => 'https://wordpress.com/recommendations', |
||
233 | 'meta' => array( |
||
234 | 'class' => 'mb-icon-spacer', |
||
235 | ) |
||
236 | ) ); |
||
237 | |||
238 | $wp_admin_bar->add_menu( array( |
||
239 | 'parent' => 'newdash', |
||
240 | 'id' => 'my-activity-my-likes', |
||
241 | 'title' => __( 'My Likes', 'jetpack' ), |
||
242 | 'href' => 'https://wordpress.com/activities/likes', |
||
243 | 'meta' => array( |
||
244 | 'class' => 'mb-icon-spacer', |
||
245 | ) |
||
246 | ) ); |
||
247 | |||
248 | } |
||
249 | |||
250 | public function create_menu_item_pair( $primary, $secondary ) { |
||
259 | |||
260 | public function create_menu_item_anchor( $class, $url, $label, $id ) { |
||
263 | |||
264 | public function wpcom_adminbar_add_secondary_groups( $wp_admin_bar ) { |
||
281 | |||
282 | public function add_me_submenu( $wp_admin_bar ) { |
||
283 | $user_id = get_current_user_id(); |
||
284 | if ( empty( $user_id ) ) { |
||
285 | return; |
||
286 | } |
||
287 | |||
288 | $avatar = get_avatar( $this->user_email, 32, 'mm', '', array( 'force_display' => true ) ); |
||
289 | $class = empty( $avatar ) ? '' : 'with-avatar'; |
||
290 | |||
291 | // Add the 'Me' menu |
||
292 | $wp_admin_bar->add_menu( array( |
||
293 | 'id' => 'my-account', |
||
439 | |||
440 | public function add_write_button( $wp_admin_bar ) { |
||
463 | |||
464 | public function add_my_sites_submenu( $wp_admin_bar ) { |
||
855 | } |
||
856 |