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_Admin_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_Admin_Page, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | abstract class Jetpack_Admin_Page { |
||
| 8 | // Add page specific actions given the page hook |
||
| 9 | abstract function add_page_actions( $hook ); |
||
| 10 | |||
| 11 | // Create a menu item for the page and returns the hook |
||
| 12 | abstract function get_page_hook(); |
||
| 13 | |||
| 14 | // Enqueue and localize page specific scripts |
||
| 15 | abstract function page_admin_scripts(); |
||
| 16 | |||
| 17 | // Render page specific HTML |
||
| 18 | abstract function page_render(); |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Should we block the page rendering because the site is in IDC? |
||
| 22 | * |
||
| 23 | * @var bool |
||
| 24 | */ |
||
| 25 | static $block_page_rendering_for_idc; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Function called after admin_styles to load any additional needed styles. |
||
| 29 | * |
||
| 30 | * @since 4.3.0 |
||
| 31 | */ |
||
| 32 | function additional_styles() {} |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The constructor. |
||
| 36 | */ |
||
| 37 | public function __construct() { |
||
| 38 | add_action( 'jetpack_loaded', array( $this, 'on_jetpack_loaded' ) ); |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Runs on Jetpack being ready to load its packages. |
||
| 43 | * |
||
| 44 | * @param Jetpack $jetpack object. |
||
| 45 | */ |
||
| 46 | public function on_jetpack_loaded( $jetpack ) { |
||
| 47 | $this->jetpack = $jetpack; |
||
|
|
|||
| 48 | |||
| 49 | self::$block_page_rendering_for_idc = ( |
||
| 50 | Jetpack::validate_sync_error_idc_option() && ! Jetpack_Options::get_option( 'safe_mode_confirmed' ) |
||
| 51 | ); |
||
| 52 | } |
||
| 53 | |||
| 54 | function add_actions() { |
||
| 55 | global $pagenow; |
||
| 56 | |||
| 57 | $is_development_mode = ( new Status() )->is_development_mode(); |
||
| 58 | // If user is not an admin and site is in Dev Mode or not connected yet then don't do anything. |
||
| 59 | if ( ! current_user_can( 'manage_options' ) && ( $is_development_mode || ! Jetpack::is_active() ) ) { |
||
| 60 | return; |
||
| 61 | } |
||
| 62 | |||
| 63 | // Don't add in the modules page unless modules are available! |
||
| 64 | if ( $this->dont_show_if_not_active && ! Jetpack::is_active() && ! $is_development_mode ) { |
||
| 65 | return; |
||
| 66 | } |
||
| 67 | |||
| 68 | // Initialize menu item for the page in the admin |
||
| 69 | $hook = $this->get_page_hook(); |
||
| 70 | |||
| 71 | // Attach hooks common to all Jetpack admin pages based on the created |
||
| 72 | // hook |
||
| 73 | add_action( "load-$hook", array( $this, 'admin_help' ) ); |
||
| 74 | add_action( "load-$hook", array( $this, 'admin_page_load' ) ); |
||
| 75 | add_action( "admin_print_styles-$hook", array( $this, 'admin_styles' ) ); |
||
| 76 | add_action( "admin_print_scripts-$hook", array( $this, 'admin_scripts' ) ); |
||
| 77 | |||
| 78 | if ( ! self::$block_page_rendering_for_idc ) { |
||
| 79 | add_action( "admin_print_styles-$hook", array( $this, 'additional_styles' ) ); |
||
| 80 | } |
||
| 81 | // If someone just activated Jetpack, let's show them a fullscreen connection banner. |
||
| 82 | if ( |
||
| 83 | ( 'admin.php' === $pagenow && isset( $_GET['page'] ) && 'jetpack' === $_GET['page'] ) |
||
| 84 | && ! Jetpack::is_active() |
||
| 85 | && current_user_can( 'jetpack_connect' ) |
||
| 86 | && ! $is_development_mode |
||
| 87 | ) { |
||
| 88 | add_action( 'admin_enqueue_scripts', array( 'Jetpack_Connection_Banner', 'enqueue_banner_scripts' ) ); |
||
| 89 | add_action( 'admin_enqueue_scripts', array( 'Jetpack_Connection_Banner', 'enqueue_connect_button_scripts' ) ); |
||
| 90 | add_action( 'admin_print_styles', array( Jetpack::init(), 'admin_banner_styles' ) ); |
||
| 91 | add_action( 'admin_notices', array( 'Jetpack_Connection_Banner', 'render_connect_prompt_full_screen' ) ); |
||
| 92 | delete_transient( 'activated_jetpack' ); |
||
| 93 | } |
||
| 94 | |||
| 95 | // If Jetpack not yet connected, but user is viewing one of the pages with a Jetpack connection banner. |
||
| 96 | if ( |
||
| 97 | ( 'index.php' === $pagenow || 'plugins.php' === $pagenow ) |
||
| 98 | && ! Jetpack::is_active() |
||
| 99 | && current_user_can( 'jetpack_connect' ) |
||
| 100 | && ! $is_development_mode |
||
| 101 | ) { |
||
| 102 | add_action( 'admin_enqueue_scripts', array( 'Jetpack_Connection_Banner', 'enqueue_connect_button_scripts' ) ); |
||
| 103 | } |
||
| 104 | |||
| 105 | // Check if the site plan changed and deactivate modules accordingly. |
||
| 106 | add_action( 'current_screen', array( $this, 'check_plan_deactivate_modules' ) ); |
||
| 107 | |||
| 108 | // Attach page specific actions in addition to the above |
||
| 109 | $this->add_page_actions( $hook ); |
||
| 110 | } |
||
| 111 | |||
| 112 | // Render the page with a common top and bottom part, and page specific content |
||
| 113 | function render() { |
||
| 114 | // We're in an IDC: we need a decision made before we show the UI again. |
||
| 115 | if ( self::$block_page_rendering_for_idc ) { |
||
| 116 | return; |
||
| 117 | } |
||
| 118 | |||
| 119 | // Check if we are looking at the main dashboard |
||
| 120 | if ( isset( $_GET['page'] ) && 'jetpack' === $_GET['page'] ) { |
||
| 121 | $this->page_render(); |
||
| 122 | return; |
||
| 123 | } |
||
| 124 | self::wrap_ui( array( $this, 'page_render' ) ); |
||
| 125 | } |
||
| 126 | |||
| 127 | function admin_help() { |
||
| 128 | $this->jetpack->admin_help(); |
||
| 129 | } |
||
| 130 | |||
| 131 | function admin_page_load() { |
||
| 132 | // This is big. For the moment, just call the existing one. |
||
| 133 | $this->jetpack->admin_page_load(); |
||
| 134 | } |
||
| 135 | |||
| 136 | // Add page specific scripts and jetpack stats for all menu pages |
||
| 137 | function admin_scripts() { |
||
| 138 | $this->page_admin_scripts(); // Delegate to inheriting class |
||
| 139 | add_action( 'admin_footer', array( $this->jetpack, 'do_stats' ) ); |
||
| 140 | } |
||
| 141 | |||
| 142 | // Enqueue the Jetpack admin stylesheet |
||
| 143 | function admin_styles() { |
||
| 144 | $min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; |
||
| 145 | |||
| 146 | wp_enqueue_style( 'jetpack-admin', plugins_url( "css/jetpack-admin{$min}.css", JETPACK__PLUGIN_FILE ), array( 'genericons' ), JETPACK__VERSION . '-20121016' ); |
||
| 147 | wp_style_add_data( 'jetpack-admin', 'rtl', 'replace' ); |
||
| 148 | wp_style_add_data( 'jetpack-admin', 'suffix', $min ); |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Checks if REST API is enabled. |
||
| 153 | * |
||
| 154 | * @since 4.4.2 |
||
| 155 | * |
||
| 156 | * @return bool |
||
| 157 | */ |
||
| 158 | function is_rest_api_enabled() { |
||
| 159 | return /** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */ |
||
| 160 | apply_filters( 'rest_enabled', true ) && |
||
| 161 | /** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */ |
||
| 162 | apply_filters( 'rest_jsonp_enabled', true ) && |
||
| 163 | /** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */ |
||
| 164 | apply_filters( 'rest_authentication_errors', true ); |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Checks the site plan and deactivates modules that were active but are no longer included in the plan. |
||
| 169 | * |
||
| 170 | * @since 4.4.0 |
||
| 171 | * |
||
| 172 | * @param $page |
||
| 173 | * |
||
| 174 | * @return array |
||
| 175 | */ |
||
| 176 | function check_plan_deactivate_modules( $page ) { |
||
| 177 | if ( |
||
| 178 | ( new Status() )->is_development_mode() |
||
| 179 | || ! in_array( |
||
| 180 | $page->base, |
||
| 181 | array( |
||
| 182 | 'toplevel_page_jetpack', |
||
| 183 | 'admin_page_jetpack_modules', |
||
| 184 | 'jetpack_page_vaultpress', |
||
| 185 | 'jetpack_page_stats', |
||
| 186 | 'jetpack_page_akismet-key-config', |
||
| 187 | ) |
||
| 188 | ) |
||
| 189 | ) { |
||
| 190 | return false; |
||
| 191 | } |
||
| 192 | |||
| 193 | $current = Jetpack_Plan::get(); |
||
| 194 | |||
| 195 | $to_deactivate = array(); |
||
| 196 | if ( isset( $current['product_slug'] ) ) { |
||
| 197 | $active = Jetpack::get_active_modules(); |
||
| 198 | switch ( $current['product_slug'] ) { |
||
| 199 | View Code Duplication | case 'jetpack_free': |
|
| 200 | $to_deactivate = array( 'seo-tools', 'videopress', 'google-analytics', 'wordads', 'search' ); |
||
| 201 | break; |
||
| 202 | case 'jetpack_personal': |
||
| 203 | View Code Duplication | case 'jetpack_personal_monthly': |
|
| 204 | $to_deactivate = array( 'seo-tools', 'videopress', 'google-analytics', 'wordads', 'search' ); |
||
| 205 | break; |
||
| 206 | case 'jetpack_premium': |
||
| 207 | case 'jetpack_premium_monthly': |
||
| 208 | $to_deactivate = array( 'seo-tools', 'google-analytics', 'search' ); |
||
| 209 | break; |
||
| 210 | } |
||
| 211 | $to_deactivate = array_intersect( $active, $to_deactivate ); |
||
| 212 | |||
| 213 | $to_leave_enabled = array(); |
||
| 214 | foreach ( $to_deactivate as $feature ) { |
||
| 215 | if ( Jetpack_Plan::supports( $feature ) ) { |
||
| 216 | $to_leave_enabled [] = $feature; |
||
| 217 | } |
||
| 218 | } |
||
| 219 | $to_deactivate = array_diff( $to_deactivate, $to_leave_enabled ); |
||
| 220 | |||
| 221 | if ( ! empty( $to_deactivate ) ) { |
||
| 222 | Jetpack::update_active_modules( array_filter( array_diff( $active, $to_deactivate ) ) ); |
||
| 223 | } |
||
| 224 | } |
||
| 225 | return array( |
||
| 226 | 'current' => $current, |
||
| 227 | 'deactivate' => $to_deactivate, |
||
| 228 | ); |
||
| 229 | } |
||
| 230 | |||
| 231 | static function load_wrapper_styles() { |
||
| 271 | |||
| 272 | public static function wrap_ui( $callback, $args = array() ) { |
||
| 396 | } |
||
| 397 |
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: