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 |
||
| 8 | class Jetpack_React_Page extends Jetpack_Admin_Page { |
||
| 9 | |||
| 10 | protected $dont_show_if_not_active = false; |
||
| 11 | |||
| 12 | protected $is_redirecting = false; |
||
| 13 | |||
| 14 | function get_page_hook() { |
||
| 15 | // Add the main admin Jetpack menu |
||
| 16 | return add_menu_page( 'Jetpack', 'Jetpack', 'jetpack_admin_page', 'jetpack', array( $this, 'render' ), 'div' ); |
||
| 17 | } |
||
| 18 | |||
| 19 | function add_page_actions( $hook ) { |
||
| 20 | /** This action is documented in class.jetpack.php */ |
||
| 21 | do_action( 'jetpack_admin_menu', $hook ); |
||
| 22 | |||
| 23 | // Place the Jetpack menu item on top and others in the order they appear |
||
| 24 | add_filter( 'custom_menu_order', '__return_true' ); |
||
| 25 | add_filter( 'menu_order', array( $this, 'jetpack_menu_order' ) ); |
||
| 26 | |||
| 27 | if ( ! isset( $_GET['page'] ) || 'jetpack' !== $_GET['page'] ) { |
||
| 28 | return; // No need to handle the fallback redirection if we are not on the Jetpack page |
||
| 29 | } |
||
| 30 | |||
| 31 | // Adding a redirect meta tag if the REST API is disabled |
||
| 32 | if ( ! $this->is_rest_api_enabled() ) { |
||
| 33 | $this->is_redirecting = true; |
||
| 34 | add_action( 'admin_head', array( $this, 'add_fallback_head_meta' ) ); |
||
| 35 | } |
||
| 36 | |||
| 37 | // Adding a redirect meta tag wrapped in noscript tags for all browsers in case they have JavaScript disabled |
||
| 38 | add_action( 'admin_head', array( $this, 'add_noscript_head_meta' ) ); |
||
| 39 | |||
| 40 | // If this is the first time the user is viewing the admin, don't show JITMs. |
||
| 41 | // This filter is added just in time because this function is called on admin_menu |
||
| 42 | // and JITMs are initialized on admin_init |
||
| 43 | if ( Jetpack::is_active() && ! Jetpack_Options::get_option( 'first_admin_view', false ) ) { |
||
| 44 | Jetpack_Options::update_option( 'first_admin_view', true ); |
||
| 45 | add_filter( 'jetpack_just_in_time_msgs', '__return_false' ); |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Add Jetpack Dashboard sub-link and point it to AAG if the user can view stats, manage modules or if Protect is active. |
||
| 51 | * |
||
| 52 | * Works in Dev Mode or when user is connected. |
||
| 53 | * |
||
| 54 | * @since 4.3.0 |
||
| 55 | */ |
||
| 56 | function jetpack_add_dashboard_sub_nav_item() { |
||
| 57 | View Code Duplication | if ( ( new Status() )->is_development_mode() || Jetpack::is_active() ) { |
|
| 58 | global $submenu; |
||
| 59 | if ( current_user_can( 'jetpack_admin_page' ) ) { |
||
| 60 | $submenu['jetpack'][] = array( __( 'Dashboard', 'jetpack' ), 'jetpack_admin_page', 'admin.php?page=jetpack#/dashboard' ); |
||
| 61 | } |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * If user is allowed to see the Jetpack Admin, add Settings sub-link. |
||
| 67 | * |
||
| 68 | * @since 4.3.0 |
||
| 69 | */ |
||
| 70 | function jetpack_add_settings_sub_nav_item() { |
||
| 71 | View Code Duplication | if ( ( ( new Status() )->is_development_mode() || Jetpack::is_active() ) && current_user_can( 'jetpack_admin_page' ) && current_user_can( 'edit_posts' ) ) { |
|
| 72 | global $submenu; |
||
| 73 | $submenu['jetpack'][] = array( __( 'Settings', 'jetpack' ), 'jetpack_admin_page', 'admin.php?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 | View Code Duplication | function jetpack_menu_order( $menu_order ) { |
|
| 88 | $jp_menu_order = array(); |
||
| 89 | |||
| 90 | foreach ( $menu_order as $index => $item ) { |
||
| 91 | if ( $item != 'jetpack' ) |
||
| 92 | $jp_menu_order[] = $item; |
||
| 93 | |||
| 94 | if ( $index == 0 ) |
||
| 95 | $jp_menu_order[] = 'jetpack'; |
||
| 96 | } |
||
| 97 | |||
| 98 | return $jp_menu_order; |
||
| 99 | } |
||
| 100 | |||
| 101 | function page_render() { |
||
| 102 | /** This action is already documented in views/admin/admin-page.php */ |
||
| 103 | do_action( 'jetpack_notices' ); |
||
| 104 | |||
| 105 | // Try fetching by patch |
||
| 106 | $static_html = @file_get_contents( JETPACK__PLUGIN_DIR . '_inc/build/static.html' ); |
||
| 107 | |||
| 108 | if ( false === $static_html ) { |
||
| 109 | |||
| 110 | // If we still have nothing, display an error |
||
| 111 | echo '<p>'; |
||
| 112 | esc_html_e( 'Error fetching static.html. Try running: ', 'jetpack' ); |
||
| 113 | echo '<code>yarn distclean && yarn build</code>'; |
||
| 114 | echo '</p>'; |
||
| 115 | } else { |
||
| 116 | |||
| 117 | // We got the static.html so let's display it |
||
| 118 | echo $static_html; |
||
| 119 | self::render_footer(); |
||
| 120 | |||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Gets array of any Jetpack notices that have been dismissed. |
||
| 126 | * |
||
| 127 | * @since 4.0.1 |
||
| 128 | * @return mixed|void |
||
| 129 | */ |
||
| 130 | function get_dismissed_jetpack_notices() { |
||
| 131 | $jetpack_dismissed_notices = get_option( 'jetpack_dismissed_notices', array() ); |
||
| 132 | /** |
||
| 133 | * Array of notices that have been dismissed. |
||
| 134 | * |
||
| 135 | * @since 4.0.1 |
||
| 136 | * |
||
| 137 | * @param array $jetpack_dismissed_notices If empty, will not show any Jetpack notices. |
||
| 138 | */ |
||
| 139 | $dismissed_notices = apply_filters( 'jetpack_dismissed_notices', $jetpack_dismissed_notices ); |
||
| 140 | return $dismissed_notices; |
||
| 141 | } |
||
| 142 | |||
| 143 | function additional_styles() { |
||
| 144 | Jetpack_Admin_Page::load_wrapper_styles(); |
||
| 145 | } |
||
| 146 | |||
| 147 | function page_admin_scripts() { |
||
| 148 | if ( $this->is_redirecting ) { |
||
| 149 | return; // No need for scripts on a fallback page |
||
| 150 | } |
||
| 151 | |||
| 152 | $is_development_mode = ( new Status() )->is_development_mode(); |
||
| 153 | $script_deps_path = JETPACK__PLUGIN_DIR . '_inc/build/admin.asset.php'; |
||
| 154 | $script_dependencies = array( 'wp-polyfill' ); |
||
| 155 | if ( file_exists( $script_deps_path ) ) { |
||
| 156 | $asset_manifest = include $script_deps_path; |
||
| 157 | $script_dependencies = $asset_manifest['dependencies']; |
||
| 158 | } |
||
| 159 | |||
| 160 | if ( Jetpack::is_active() || $is_development_mode ) { |
||
| 161 | wp_enqueue_script( |
||
| 162 | 'react-plugin', |
||
| 163 | plugins_url( '_inc/build/admin.js', JETPACK__PLUGIN_FILE ), |
||
| 164 | $script_dependencies, |
||
| 165 | JETPACK__VERSION, |
||
| 166 | true |
||
| 167 | ); |
||
| 168 | } |
||
| 169 | |||
| 170 | |||
| 171 | if ( ! $is_development_mode && Jetpack::is_active() ) { |
||
| 172 | // Required for Analytics. |
||
| 173 | wp_enqueue_script( 'jp-tracks', '//stats.wp.com/w.js', array(), gmdate( 'YW' ), true ); |
||
| 174 | } |
||
| 175 | |||
| 176 | // Add objects to be passed to the initial state of the app. |
||
| 177 | wp_localize_script( 'react-plugin', 'Initial_State', $this->get_initial_state() ); |
||
| 178 | } |
||
| 179 | |||
| 180 | function get_initial_state() { |
||
| 181 | // Load API endpoint base classes and endpoints for getting the module list fed into the JS Admin Page |
||
| 182 | require_once JETPACK__PLUGIN_DIR . '_inc/lib/core-api/class.jetpack-core-api-xmlrpc-consumer-endpoint.php'; |
||
| 183 | require_once JETPACK__PLUGIN_DIR . '_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php'; |
||
| 184 | $moduleListEndpoint = new Jetpack_Core_API_Module_List_Endpoint(); |
||
| 185 | $modules = $moduleListEndpoint->get_modules(); |
||
| 186 | |||
| 187 | // Preparing translated fields for JSON encoding by transforming all HTML entities to |
||
| 188 | // respective characters. |
||
| 189 | foreach( $modules as $slug => $data ) { |
||
|
|
|||
| 190 | $modules[ $slug ]['name'] = html_entity_decode( $data['name'] ); |
||
| 191 | $modules[ $slug ]['description'] = html_entity_decode( $data['description'] ); |
||
| 192 | $modules[ $slug ]['short_description'] = html_entity_decode( $data['short_description'] ); |
||
| 193 | $modules[ $slug ]['long_description'] = html_entity_decode( $data['long_description'] ); |
||
| 194 | } |
||
| 195 | |||
| 196 | // Collecting roles that can view site stats. |
||
| 197 | $stats_roles = array(); |
||
| 198 | $enabled_roles = function_exists( 'stats_get_option' ) ? stats_get_option( 'roles' ) : array( 'administrator' ); |
||
| 199 | |||
| 200 | if ( ! function_exists( 'get_editable_roles' ) ) { |
||
| 201 | require_once ABSPATH . 'wp-admin/includes/user.php'; |
||
| 202 | } |
||
| 203 | foreach ( get_editable_roles() as $slug => $role ) { |
||
| 204 | $stats_roles[ $slug ] = array( |
||
| 205 | 'name' => translate_user_role( $role['name'] ), |
||
| 206 | 'canView' => is_array( $enabled_roles ) ? in_array( $slug, $enabled_roles, true ) : false, |
||
| 207 | ); |
||
| 208 | } |
||
| 209 | |||
| 210 | // Get information about current theme. |
||
| 211 | $current_theme = wp_get_theme(); |
||
| 212 | |||
| 213 | // Get all themes that Infinite Scroll provides support for natively. |
||
| 214 | $inf_scr_support_themes = array(); |
||
| 215 | foreach ( Jetpack::glob_php( JETPACK__PLUGIN_DIR . 'modules/infinite-scroll/themes' ) as $path ) { |
||
| 216 | if ( is_readable( $path ) ) { |
||
| 217 | $inf_scr_support_themes[] = basename( $path, '.php' ); |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | // Get last post, to build the link to Customizer in the Related Posts module. |
||
| 222 | $last_post = get_posts( array( 'posts_per_page' => 1 ) ); |
||
| 223 | $last_post = isset( $last_post[0] ) && $last_post[0] instanceof WP_Post |
||
| 224 | ? get_permalink( $last_post[0]->ID ) |
||
| 225 | : get_home_url(); |
||
| 226 | |||
| 227 | $current_user_data = jetpack_current_user_data(); |
||
| 228 | |||
| 229 | $status = new Status(); |
||
| 230 | |||
| 231 | return array( |
||
| 232 | 'WP_API_root' => esc_url_raw( rest_url() ), |
||
| 233 | 'WP_API_nonce' => wp_create_nonce( 'wp_rest' ), |
||
| 234 | 'pluginBaseUrl' => plugins_url( '', JETPACK__PLUGIN_FILE ), |
||
| 235 | 'connectionStatus' => array( |
||
| 236 | 'isActive' => Jetpack::is_active(), |
||
| 237 | 'isStaging' => $status->is_staging_site(), |
||
| 238 | 'devMode' => array( |
||
| 239 | 'isActive' => $status->is_development_mode(), |
||
| 240 | 'constant' => defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG, |
||
| 241 | 'url' => site_url() && false === strpos( site_url(), '.' ), |
||
| 242 | 'filter' => apply_filters( 'jetpack_development_mode', false ), |
||
| 243 | ), |
||
| 244 | 'isPublic' => '1' == get_option( 'blog_public' ), // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison |
||
| 245 | 'isInIdentityCrisis' => Jetpack::validate_sync_error_idc_option(), |
||
| 246 | 'sandboxDomain' => JETPACK__SANDBOX_DOMAIN, |
||
| 247 | ), |
||
| 248 | 'connectUrl' => false == $current_user_data['isConnected'] // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison |
||
| 249 | ? Jetpack::init()->build_connect_url( true, false, false ) |
||
| 250 | : '', |
||
| 251 | 'dismissedNotices' => $this->get_dismissed_jetpack_notices(), |
||
| 252 | 'isDevVersion' => Jetpack::is_development_version(), |
||
| 253 | 'currentVersion' => JETPACK__VERSION, |
||
| 254 | 'is_gutenberg_available' => true, |
||
| 255 | 'getModules' => $modules, |
||
| 256 | 'rawUrl' => Jetpack::build_raw_urls( get_home_url() ), |
||
| 257 | 'adminUrl' => esc_url( admin_url() ), |
||
| 258 | 'stats' => array( |
||
| 259 | // data is populated asynchronously on page load. |
||
| 260 | 'data' => array( |
||
| 261 | 'general' => false, |
||
| 262 | 'day' => false, |
||
| 263 | 'week' => false, |
||
| 264 | 'month' => false, |
||
| 265 | ), |
||
| 266 | 'roles' => $stats_roles, |
||
| 267 | ), |
||
| 268 | 'aff' => Partner::init()->get_partner_code( Partner::AFFILIATE_CODE ), |
||
| 269 | 'partnerSubsidiaryId' => Partner::init()->get_partner_code( Partner::SUBSIDIARY_CODE ), |
||
| 270 | 'settings' => $this->get_flattened_settings( $modules ), |
||
| 271 | 'userData' => array( |
||
| 272 | 'currentUser' => $current_user_data, |
||
| 273 | ), |
||
| 274 | 'siteData' => array( |
||
| 275 | 'icon' => has_site_icon() |
||
| 276 | ? apply_filters( 'jetpack_photon_url', get_site_icon_url(), array( 'w' => 64 ) ) |
||
| 277 | : '', |
||
| 278 | 'siteVisibleToSearchEngines' => '1' == get_option( 'blog_public' ), // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison |
||
| 279 | /** |
||
| 280 | * Whether promotions are visible or not. |
||
| 281 | * |
||
| 282 | * @since 4.8.0 |
||
| 283 | * |
||
| 284 | * @param bool $are_promotions_active Status of promotions visibility. True by default. |
||
| 285 | */ |
||
| 286 | 'showPromotions' => apply_filters( 'jetpack_show_promotions', true ), |
||
| 287 | 'isAtomicSite' => jetpack_is_atomic_site(), |
||
| 288 | 'plan' => Jetpack_Plan::get(), |
||
| 289 | 'showBackups' => Jetpack::show_backups_ui(), |
||
| 290 | 'isMultisite' => is_multisite(), |
||
| 291 | ), |
||
| 292 | 'themeData' => array( |
||
| 293 | 'name' => $current_theme->get( 'Name' ), |
||
| 294 | 'hasUpdate' => (bool) get_theme_update_available( $current_theme ), |
||
| 295 | 'support' => array( |
||
| 296 | 'infinite-scroll' => current_theme_supports( 'infinite-scroll' ) || in_array( $current_theme->get_stylesheet(), $inf_scr_support_themes, true ), |
||
| 297 | ), |
||
| 298 | ), |
||
| 299 | 'locale' => Jetpack::get_i18n_data_json(), |
||
| 300 | 'localeSlug' => join( '-', explode( '_', get_user_locale() ) ), |
||
| 301 | 'jetpackStateNotices' => array( |
||
| 302 | 'messageCode' => Jetpack::state( 'message' ), |
||
| 303 | 'errorCode' => Jetpack::state( 'error' ), |
||
| 304 | 'errorDescription' => Jetpack::state( 'error_description' ), |
||
| 305 | ), |
||
| 306 | 'tracksUserData' => Jetpack_Tracks_Client::get_connected_user_tracks_identity(), |
||
| 307 | 'currentIp' => function_exists( 'jetpack_protect_get_ip' ) ? jetpack_protect_get_ip() : false, |
||
| 308 | 'lastPostUrl' => esc_url( $last_post ), |
||
| 309 | 'externalServicesConnectUrls' => $this->get_external_services_connect_urls(), |
||
| 310 | 'calypsoEnv' => Jetpack::get_calypso_env(), |
||
| 311 | ); |
||
| 312 | } |
||
| 313 | |||
| 314 | function get_external_services_connect_urls() { |
||
| 315 | $connect_urls = array(); |
||
| 316 | jetpack_require_lib( 'class.jetpack-keyring-service-helper' ); |
||
| 317 | foreach ( Jetpack_Keyring_Service_Helper::$SERVICES as $service_name => $service_info ) { |
||
| 318 | $connect_urls[ $service_name ] = Jetpack_Keyring_Service_Helper::connect_url( $service_name, $service_info[ 'for' ] ); |
||
| 319 | } |
||
| 320 | return $connect_urls; |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Returns an array of modules and settings both as first class members of the object. |
||
| 325 | * |
||
| 326 | * @param array $modules the result of an API request to get all modules. |
||
| 327 | * |
||
| 328 | * @return array flattened settings with modules. |
||
| 329 | */ |
||
| 330 | function get_flattened_settings( $modules ) { |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Gather data about the current user. |
||
| 339 | * |
||
| 340 | * @since 4.1.0 |
||
| 341 | * |
||
| 342 | * @return array |
||
| 343 | */ |
||
| 344 | function jetpack_current_user_data() { |
||
| 345 | $current_user = wp_get_current_user(); |
||
| 346 | $is_master_user = $current_user->ID == Jetpack_Options::get_option( 'master_user' ); |
||
| 347 | $dotcom_data = Jetpack::get_connected_user_data(); |
||
| 348 | |||
| 387 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.