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_React_Page 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_React_Page, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class Jetpack_React_Page extends Jetpack_Admin_Page { |
||
| 6 | |||
| 7 | protected $dont_show_if_not_active = false; |
||
| 8 | |||
| 9 | protected $is_redirecting = false; |
||
| 10 | |||
| 11 | function get_page_hook() { |
||
| 17 | |||
| 18 | function add_page_actions( $hook ) { |
||
| 19 | /** This action is documented in class.jetpack.php */ |
||
| 20 | do_action( 'jetpack_admin_menu', $hook ); |
||
| 21 | |||
| 22 | // Place the Jetpack menu item on top and others in the order they appear |
||
| 23 | add_filter( 'custom_menu_order', '__return_true' ); |
||
| 24 | add_filter( 'menu_order', array( $this, 'jetpack_menu_order' ) ); |
||
| 25 | |||
| 26 | if ( ! isset( $_GET['page'] ) || 'jetpack' !== $_GET['page'] || ! empty( $_GET['configure'] ) ) { |
||
| 27 | return; // No need to handle the fallback redirection if we are not on the Jetpack page |
||
| 28 | } |
||
| 29 | |||
| 30 | // Adding a redirect meta tag for older WordPress versions |
||
| 31 | if ( $this->is_wp_version_too_old() ) { |
||
| 32 | $this->is_redirecting = true; |
||
| 33 | add_action( 'admin_head', array( $this, 'add_fallback_head_meta' ) ); |
||
| 34 | } |
||
| 35 | |||
| 36 | // Adding a redirect meta tag wrapped in noscript tags for all browsers in case they have JavaScript disabled |
||
| 37 | add_action( 'admin_head', array( $this, 'add_noscript_head_meta' ) ); |
||
| 38 | |||
| 39 | // Enqueue admin page styles in head |
||
| 40 | add_action( 'admin_head', array( $this, 'page_admin_styles' ) ); |
||
| 41 | |||
| 42 | // Adding a redirect tag wrapped in browser conditional comments |
||
| 43 | add_action( 'admin_head', array( $this, 'add_legacy_browsers_head_script' ) ); |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Add Jetpack Dashboard sub-link and point it to AAG if the user can view stats, manage modules or if Protect is active. |
||
| 48 | * Otherwise and only if user is allowed to see the Jetpack Admin, the Dashboard sub-link is added but pointed to Apps tab. |
||
| 49 | * |
||
| 50 | * Works in Dev Mode or when user is connected. |
||
| 51 | * |
||
| 52 | * @since 4.3 |
||
| 53 | */ |
||
| 54 | function jetpack_add_dashboard_sub_nav_item() { |
||
| 55 | if ( Jetpack::is_development_mode() || Jetpack::is_active() ) { |
||
| 56 | global $submenu; |
||
| 57 | if ( current_user_can( 'jetpack_manage_modules' ) || Jetpack::is_module_active( 'protect' ) || current_user_can( 'view_stats' ) ) { |
||
| 58 | $submenu['jetpack'][] = array( __( 'Dashboard', 'jetpack' ), 'jetpack_admin_page', Jetpack::admin_url( 'page=jetpack#/dashboard' ) ); |
||
| 59 | } elseif ( current_user_can( 'jetpack_admin_page' ) ) { |
||
| 60 | $submenu['jetpack'][] = array( __( 'Dashboard', 'jetpack' ), 'jetpack_admin_page', Jetpack::admin_url( 'page=jetpack#/apps' ) ); |
||
| 61 | } |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * If user is allowed to see the Jetpack Admin, add Settings sub-link. |
||
| 67 | * |
||
| 68 | * @since 4.3 |
||
| 69 | */ |
||
| 70 | function jetpack_add_settings_sub_nav_item() { |
||
| 71 | if ( ( Jetpack::is_development_mode() || Jetpack::is_active() ) && current_user_can( 'jetpack_admin_page' ) ) { |
||
| 72 | global $submenu; |
||
| 73 | $submenu['jetpack'][] = array( __( 'Settings', 'jetpack' ), 'jetpack_admin_page', Jetpack::admin_url( 'page=jetpack#/settings' ) ); |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | function add_fallback_head_meta() { |
||
| 78 | echo '<meta http-equiv="refresh" content="0; url=?page=jetpack_modules">'; |
||
| 79 | } |
||
| 80 | |||
| 81 | function add_noscript_head_meta() { |
||
| 82 | echo '<noscript>'; |
||
| 83 | $this->add_fallback_head_meta(); |
||
| 84 | echo '</noscript>'; |
||
| 85 | } |
||
| 86 | |||
| 87 | function add_legacy_browsers_head_script() { |
||
| 88 | echo |
||
| 89 | "<script type=\"text/javascript\">\n" |
||
| 90 | . "/*@cc_on\n" |
||
| 91 | . "if ( @_jscript_version <= 10) {\n" |
||
| 92 | . "window.location.href = '?page=jetpack_modules';\n" |
||
| 93 | . "}\n" |
||
| 94 | . "@*/\n" |
||
| 95 | . "</script>"; |
||
| 96 | } |
||
| 97 | |||
| 98 | View Code Duplication | function jetpack_menu_order( $menu_order ) { |
|
| 99 | $jp_menu_order = array(); |
||
| 100 | |||
| 101 | foreach ( $menu_order as $index => $item ) { |
||
| 102 | if ( $item != 'jetpack' ) |
||
| 103 | $jp_menu_order[] = $item; |
||
| 104 | |||
| 105 | if ( $index == 0 ) |
||
| 106 | $jp_menu_order[] = 'jetpack'; |
||
| 107 | } |
||
| 108 | |||
| 109 | return $jp_menu_order; |
||
| 110 | } |
||
| 111 | |||
| 112 | // Render the configuration page for the module if it exists and an error |
||
| 113 | // screen if the module is not configurable |
||
| 114 | // @todo remove when real settings are in place |
||
|
|
|||
| 115 | function render_nojs_configurable( $module_name ) { |
||
| 116 | $module_name = preg_replace( '/[^\da-z\-]+/', '', $_GET['configure'] ); |
||
| 117 | |||
| 118 | include_once( JETPACK__PLUGIN_DIR . '_inc/header.php' ); |
||
| 119 | echo '<div class="wrap configure-module">'; |
||
| 120 | |||
| 121 | if ( Jetpack::is_module( $module_name ) && current_user_can( 'jetpack_configure_modules' ) ) { |
||
| 122 | Jetpack::admin_screen_configure_module( $module_name ); |
||
| 123 | } else { |
||
| 124 | echo '<h2>' . esc_html__( 'Error, bad module.', 'jetpack' ) . '</h2>'; |
||
| 125 | } |
||
| 126 | |||
| 127 | echo '</div><!-- /wrap -->'; |
||
| 128 | } |
||
| 129 | |||
| 130 | function page_render() { |
||
| 131 | // Handle redirects to configuration pages |
||
| 132 | if ( ! empty( $_GET['configure'] ) ) { |
||
| 133 | return $this->render_nojs_configurable( $_GET['configure'] ); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** This action is already documented in views/admin/admin-page.php */ |
||
| 137 | do_action( 'jetpack_notices' ); |
||
| 138 | |||
| 139 | echo file_get_contents( JETPACK__PLUGIN_DIR . '_inc/build/static.html' ); |
||
| 140 | } |
||
| 141 | |||
| 142 | function get_i18n_data() { |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Gets array of any Jetpack notices that have been dismissed. |
||
| 153 | * |
||
| 154 | * @since 4.0.1 |
||
| 155 | * @return mixed|void |
||
| 156 | */ |
||
| 157 | function get_dismissed_jetpack_notices() { |
||
| 169 | |||
| 170 | function jetpack_get_tracks_user_data() { |
||
| 171 | if ( ! $user_data = Jetpack::get_connected_user_data() ) { |
||
| 172 | return false; |
||
| 173 | } |
||
| 174 | |||
| 175 | return array( |
||
| 176 | 'userid' => $user_data['ID'], |
||
| 177 | 'username' => $user_data['login'], |
||
| 178 | ); |
||
| 180 | |||
| 181 | function page_admin_styles() { |
||
| 187 | |||
| 188 | function page_admin_scripts() { |
||
| 270 | } |
||
| 271 | |||
| 429 |