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 Jetpack_Calypsoify 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 Jetpack_Calypsoify, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
6 | class Jetpack_Calypsoify { |
||
7 | |||
8 | /** |
||
9 | * Singleton instance of `Jetpack_Calypsoify`. |
||
10 | * |
||
11 | * @var object |
||
12 | */ |
||
13 | public static $instance = false; |
||
14 | |||
15 | /** |
||
16 | * Is Calypsoify enabled, based on any value of `calypsoify` user meta. |
||
17 | * |
||
18 | * @var bool |
||
19 | */ |
||
20 | public $is_calypsoify_enabled = false; |
||
21 | |||
22 | private function __construct() { |
||
25 | |||
26 | public static function getInstance() { |
||
33 | |||
34 | public function setup() { |
||
35 | $this->is_calypsoify_enabled = 1 == (int) get_user_meta( get_current_user_id(), 'calypsoify', true ); |
||
36 | add_action( 'admin_init', array( $this, 'check_param' ), 4 ); |
||
37 | |||
38 | if ( $this->is_calypsoify_enabled ) { |
||
39 | add_action( 'admin_init', array( $this, 'setup_admin' ), 6 ); |
||
40 | add_action( 'admin_menu', array( $this, 'remove_core_menus' ), 100 ); |
||
41 | add_action( 'admin_menu', array( $this, 'add_custom_menus' ), 101 ); |
||
42 | } |
||
43 | |||
44 | // Make this always available -- in case calypsoify gets toggled off. |
||
45 | add_action( 'wp_ajax_jetpack_toggle_autoupdate', array( $this, 'jetpack_toggle_autoupdate' ) ); |
||
46 | add_filter( 'handle_bulk_actions-plugins', array( $this, 'handle_bulk_actions_plugins' ), 10, 3 ); |
||
47 | } |
||
48 | |||
49 | public function setup_admin() { |
||
50 | // Masterbar is currently required for this to work properly. Mock the instance of it |
||
51 | if ( ! Jetpack::is_module_active( 'masterbar' ) ) { |
||
52 | $this->mock_masterbar_activation(); |
||
53 | } |
||
54 | |||
55 | if ( $this->is_page_gutenberg() ) { |
||
56 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_for_gutenberg' ), 100 ); |
||
57 | return; |
||
58 | } |
||
59 | |||
60 | add_action( 'admin_init', array( $this, 'check_page' ) ); |
||
61 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ), 100 ); |
||
62 | add_action( 'in_admin_header', array( $this, 'insert_sidebar_html' ) ); |
||
63 | add_action( 'wp_before_admin_bar_render', array( $this, 'modify_masterbar' ), 100000 ); |
||
64 | |||
65 | add_filter( 'get_user_option_admin_color', array( $this, 'admin_color_override' ) ); |
||
66 | |||
67 | add_action( 'manage_plugins_columns', array( $this, 'manage_plugins_columns_header' ) ); |
||
68 | add_action( 'manage_plugins_custom_column', array( $this, 'manage_plugins_custom_column' ), 10, 2 ); |
||
69 | add_filter( 'bulk_actions-plugins', array( $this, 'bulk_actions_plugins' ) ); |
||
70 | |||
71 | if ( 'plugins.php' === basename( $_SERVER['PHP_SELF'] ) ) { |
||
72 | add_action( 'admin_notices', array( $this, 'plugins_admin_notices' ) ); |
||
73 | } |
||
74 | } |
||
75 | |||
76 | public function manage_plugins_columns_header( $columns ) { |
||
82 | |||
83 | public function manage_plugins_custom_column( $column_name, $slug ) { |
||
123 | |||
124 | public static function get_dotorg_repo_plugins() { |
||
128 | |||
129 | public function bulk_actions_plugins( $bulk_actions ) { |
||
134 | |||
135 | public function handle_bulk_actions_plugins( $redirect_to, $action, $slugs ) { |
||
152 | |||
153 | public function plugins_admin_notices() { |
||
162 | |||
163 | public function jetpack_toggle_autoupdate() { |
||
194 | |||
195 | public function admin_color_override( $color ) { |
||
198 | |||
199 | public function mock_masterbar_activation() { |
||
203 | |||
204 | public function remove_core_menus() { |
||
205 | remove_menu_page( 'edit.php?post_type=feedback' ); |
||
206 | remove_menu_page( 'options-general.php' ); |
||
207 | remove_menu_page( 'index.php' ); |
||
208 | remove_menu_page( 'jetpack' ); |
||
209 | remove_menu_page( 'edit.php' ); |
||
210 | remove_menu_page( 'upload.php' ); |
||
211 | remove_menu_page( 'edit.php?post_type=page' ); |
||
212 | remove_menu_page( 'edit-comments.php' ); |
||
213 | remove_menu_page( 'themes.php' ); |
||
214 | remove_menu_page( 'plugins.php' ); |
||
215 | remove_menu_page( 'users.php' ); |
||
216 | remove_menu_page( 'tools.php' ); |
||
217 | remove_menu_page( 'link-manager.php' ); |
||
218 | |||
219 | // Core settings pages |
||
220 | remove_submenu_page( 'options-general.php', 'options-general.php' ); |
||
221 | remove_submenu_page( 'options-general.php', 'options-writing.php' ); |
||
222 | remove_submenu_page( 'options-general.php', 'options-reading.php' ); |
||
223 | remove_submenu_page( 'options-general.php', 'options-discussion.php' ); |
||
224 | remove_submenu_page( 'options-general.php', 'options-media.php' ); |
||
225 | remove_submenu_page( 'options-general.php', 'options-permalink.php' ); |
||
226 | remove_submenu_page( 'options-general.php', 'privacy.php' ); |
||
227 | remove_submenu_page( 'options-general.php', 'sharing' ); |
||
228 | } |
||
229 | |||
230 | public function add_custom_menus() { |
||
231 | global $menu, $submenu; |
||
232 | |||
233 | if ( $_GET[ 'post_type' ] == 'feedback' ) { |
||
234 | // there is currently no gridicon for feeback, so using dashicon |
||
235 | add_menu_page( __( 'Feedback', 'jetpack' ), __( 'Feedback', 'jetpack' ), 'edit_pages', 'edit.php?post_type=feedback', '', 'dashicons-feedback', 1 ); |
||
236 | remove_submenu_page( 'edit.php?post_type=feedback', 'feedback-export'); |
||
237 | } else { |
||
238 | add_menu_page( __( 'Manage Plugins', 'jetpack' ), __( 'Manage Plugins', 'jetpack' ), 'activate_plugins', 'plugins.php', '', $this->installed_plugins_icon(), 1 ); |
||
239 | // Count the settings page submenus, if it's zero then don't show this. |
||
240 | if ( empty( $submenu['options-general.php'] ) ) { |
||
241 | remove_menu_page( 'options-general.php' ); |
||
242 | } else { |
||
243 | // Rename and make sure the plugin settings menu is always last. |
||
244 | // Sneaky plugins seem to override this otherwise. |
||
245 | // Settings is always key 80. |
||
246 | $menu[80][0] = __( 'Plugin Settings', 'jetpack' ); |
||
247 | $menu[ max( array_keys( $menu ) ) + 1 ] = $menu[80]; |
||
248 | unset( $menu[80] ); |
||
249 | } |
||
250 | } |
||
251 | } |
||
252 | |||
253 | public function enqueue() { |
||
254 | wp_enqueue_style( 'calypsoify_wpadminmods_css', plugin_dir_url( __FILE__ ) . 'style.min.css', false, JETPACK__VERSION ); |
||
255 | wp_style_add_data( 'calypsoify_wpadminmods_css', 'rtl', 'replace' ); |
||
256 | wp_style_add_data( 'calypsoify_wpadminmods_css', 'suffix', '.min' ); |
||
257 | |||
258 | wp_enqueue_script( 'calypsoify_wpadminmods_js', plugin_dir_url( __FILE__ ) . 'mods.js', false, JETPACK__VERSION ); |
||
259 | wp_localize_script( 'calypsoify_wpadminmods_js', 'CalypsoifyOpts', array( |
||
260 | 'nonces' => array( |
||
261 | 'autoupdate_plugins' => wp_create_nonce( 'jetpack_toggle_autoupdate-plugins' ), |
||
262 | 'autoupdate_plugins_translations' => wp_create_nonce( 'jetpack_toggle_autoupdate-plugins_translations' ), |
||
263 | ) |
||
264 | ) ); |
||
265 | } |
||
266 | |||
267 | public function enqueue_for_gutenberg() { |
||
268 | wp_enqueue_style( 'calypsoify_wpadminmods_css', plugin_dir_url( __FILE__ ) . 'style-gutenberg.min.css', false, JETPACK__VERSION ); |
||
269 | wp_style_add_data( 'calypsoify_wpadminmods_css', 'rtl', 'replace' ); |
||
270 | wp_style_add_data( 'calypsoify_wpadminmods_css', 'suffix', '.min' ); |
||
271 | |||
272 | wp_enqueue_script( 'calypsoify_wpadminmods_js', plugin_dir_url( __FILE__ ) . 'mods-gutenberg.js', false, JETPACK__VERSION ); |
||
273 | wp_localize_script( |
||
274 | 'calypsoify_wpadminmods_js', |
||
275 | 'calypsoifyGutenberg', |
||
276 | array( |
||
277 | 'closeUrl' => $this->get_close_gutenberg_url(), |
||
278 | 'manageReusableBlocksUrl' => $this->get_calypso_origin() . '/types/wp_block' . $this->get_site_suffix(), |
||
279 | ) |
||
280 | ); |
||
281 | } |
||
282 | |||
283 | public function insert_sidebar_html() { |
||
284 | $heading = ( $_GET[ 'post_type' ] == 'feedback' ) ? 'Feedback' : 'Plugins'; |
||
285 | ?> |
||
286 | <a href="<?php echo esc_url( 'https://wordpress.com/stats/day/' . Jetpack::build_raw_urls( home_url() ) ); ?>" id="calypso-sidebar-header"> |
||
287 | <svg class="gridicon gridicons-chevron-left" height="24" width="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><g><path d="M14 20l-8-8 8-8 1.414 1.414L8.828 12l6.586 6.586"></path></g></svg> |
||
288 | |||
289 | <ul> |
||
290 | <li id="calypso-sitename"><?php bloginfo( 'name' ); ?></li> |
||
291 | <li id="calypso-plugins"><?php esc_html_e( $heading ); ?></li> |
||
292 | </ul> |
||
293 | </a> |
||
294 | <?php |
||
295 | } |
||
296 | |||
297 | public function modify_masterbar() { |
||
298 | global $wp_admin_bar; |
||
299 | |||
300 | // Add proper links to masterbar top sections. |
||
301 | $my_sites_node = (object) $wp_admin_bar->get_node( 'blog' ); |
||
302 | $my_sites_node->href = 'https://wordpress.com/stats/day/' . Jetpack::build_raw_urls( home_url() ); |
||
303 | $wp_admin_bar->add_node( $my_sites_node ); |
||
304 | |||
305 | $reader_node = (object) $wp_admin_bar->get_node( 'newdash' ); |
||
306 | $reader_node->href = 'https://wordpress.com'; |
||
307 | $wp_admin_bar->add_node( $reader_node ); |
||
308 | |||
309 | $me_node = (object) $wp_admin_bar->get_node( 'my-account' ); |
||
310 | $me_node->href = 'https://wordpress.com/me'; |
||
311 | $wp_admin_bar->add_node( $me_node ); |
||
312 | } |
||
313 | |||
314 | private function installed_plugins_icon() { |
||
315 | $svg = '<svg class="gridicon gridicons-plugins" height="24" width="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 24"><g><path d="M16 8V3c0-.552-.448-1-1-1s-1 .448-1 1v5h-4V3c0-.552-.448-1-1-1s-1 .448-1 1v5H5v4c0 2.79 1.637 5.193 4 6.317V22h6v-3.683c2.363-1.124 4-3.527 4-6.317V8h-3z" fill="black"></path></g></svg>'; |
||
316 | |||
317 | return 'data:image/svg+xml;base64,' . base64_encode( $svg ); |
||
318 | } |
||
319 | |||
320 | /** |
||
321 | * Returns the Calypso domain that originated the current request. |
||
322 | * |
||
323 | * @return string |
||
324 | */ |
||
325 | private function get_calypso_origin() { |
||
326 | $origin = ! empty( $_GET['origin'] ) ? $_GET['origin'] : 'https://wordpress.com'; |
||
327 | $whitelist = array( |
||
328 | 'http://calypso.localhost:3000', |
||
329 | 'http://127.0.0.1:41050', // Desktop App |
||
330 | 'https://wpcalypso.wordpress.com', |
||
331 | 'https://horizon.wordpress.com', |
||
332 | 'https://wordpress.com', |
||
333 | ); |
||
334 | return in_array( $origin, $whitelist ) ? $origin : 'https://wordpress.com'; |
||
335 | |||
336 | View Code Duplication | function get_site_suffix() { |
|
348 | } |
||
349 | |||
350 | /** |
||
351 | * Returns the site slug suffix to be used as part of the Calypso URLs. It already |
||
352 | * includes the slash separator at the beginning. |
||
353 | * |
||
354 | * @example "https://wordpress.com/block-editor" . $this->get_site_suffix() |
||
355 | * |
||
356 | * @return string |
||
357 | */ |
||
358 | View Code Duplication | private function get_site_suffix() { |
|
370 | |||
371 | /** |
||
372 | * Returns the Calypso URL that displays either the current post type list (if no args |
||
373 | * are supplied) or the classic editor for the current post (if a post ID is supplied). |
||
374 | * |
||
375 | * @param int|null $post_id |
||
376 | * @return string |
||
377 | */ |
||
378 | public function get_calypso_url( $post_id = null ) { |
||
396 | |||
397 | /** |
||
398 | * Returns the URL to be used on the block editor close button for going back to the |
||
399 | * Calypso post list. |
||
400 | * |
||
401 | * @return string |
||
402 | */ |
||
403 | public function get_close_gutenberg_url() { |
||
406 | |||
407 | /** |
||
408 | * Returns the URL for switching the user's editor to the Calypso (WordPress.com Classic) editor. |
||
409 | * |
||
410 | * @return string |
||
411 | */ |
||
412 | public function get_switch_to_classic_editor_url() { |
||
419 | |||
420 | public function check_param() { |
||
433 | |||
434 | public function check_page() { |
||
444 | |||
445 | /** |
||
446 | * Return whether a post type should display the Gutenberg/block editor. |
||
447 | * |
||
448 | * @since 6.7.0 |
||
449 | */ |
||
450 | public function is_post_type_gutenberg( $post_type ) { |
||
453 | |||
454 | public function is_page_gutenberg() { |
||
482 | } |
||
483 | |||
484 | $Jetpack_Calypsoify = Jetpack_Calypsoify::getInstance(); |
||
485 |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.