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:
| 1 | <?php |
||
| 5 | class Jetpack_Admin { |
||
| 6 | |||
| 7 | /** |
||
| 8 | * @var Jetpack_Admin |
||
| 9 | **/ |
||
| 10 | private static $instance = null; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * @var Jetpack |
||
| 14 | **/ |
||
| 15 | private $jetpack; |
||
| 16 | |||
| 17 | static function init() { |
||
| 18 | if ( is_null( self::$instance ) ) { |
||
| 19 | self::$instance = new Jetpack_Admin; |
||
| 20 | } |
||
| 21 | return self::$instance; |
||
| 22 | } |
||
| 23 | |||
| 24 | private function __construct() { |
||
| 58 | |||
| 59 | View Code Duplication | static function sort_requires_connection_last( $module1, $module2 ) { |
|
| 60 | if ( $module1['requires_connection'] == $module2['requires_connection'] ) { |
||
| 61 | return 0; |
||
| 62 | } elseif ( $module1['requires_connection'] ) { |
||
| 63 | return 1; |
||
| 64 | } elseif ( $module2['requires_connection'] ) { |
||
| 65 | return -1; |
||
| 66 | } |
||
| 67 | |||
| 68 | return 0; |
||
| 69 | } |
||
| 70 | |||
| 71 | // Produce JS understandable objects of modules containing information for |
||
| 72 | // presentation like description, name, configuration url, etc. |
||
| 73 | function get_modules() { |
||
| 74 | include_once( JETPACK__PLUGIN_DIR . 'modules/module-info.php' ); |
||
| 75 | $available_modules = $this->jetpack->get_available_modules(); |
||
| 76 | $active_modules = $this->jetpack->get_active_modules(); |
||
| 77 | $modules = array(); |
||
| 78 | $jetpack_active = Jetpack::is_active() || Jetpack::is_development_mode(); |
||
| 79 | foreach ( $available_modules as $module ) { |
||
| 80 | if ( $module_array = $this->jetpack->get_module( $module ) ) { |
||
| 81 | /** |
||
| 82 | * Filters each module's short description. |
||
| 83 | * |
||
| 84 | * @since 3.0.0 |
||
| 85 | * |
||
| 86 | * @param string $module_array['description'] Module description. |
||
| 87 | * @param string $module Module slug. |
||
| 88 | */ |
||
| 89 | $short_desc = apply_filters( 'jetpack_short_module_description', $module_array['description'], $module ); |
||
| 90 | // Fix: correct multibyte strings truncate with checking for mbstring extension |
||
| 91 | $short_desc_trunc = ( function_exists( 'mb_strlen' ) ) |
||
| 92 | ? ( ( mb_strlen( $short_desc ) > 143 ) |
||
| 93 | ? mb_substr( $short_desc, 0, 140 ) . '...' |
||
| 94 | : $short_desc ) |
||
| 95 | : ( ( strlen( $short_desc ) > 143 ) |
||
| 96 | ? substr( $short_desc, 0, 140 ) . '...' |
||
| 97 | : $short_desc ); |
||
| 98 | |||
| 99 | $module_array['module'] = $module; |
||
| 100 | $module_array['activated'] = ( $jetpack_active ? in_array( $module, $active_modules ) : false ); |
||
| 101 | $module_array['deactivate_nonce'] = wp_create_nonce( 'jetpack_deactivate-' . $module ); |
||
| 102 | $module_array['activate_nonce'] = wp_create_nonce( 'jetpack_activate-' . $module ); |
||
| 103 | $module_array['available'] = self::is_module_available( $module_array ); |
||
| 104 | $module_array['short_description'] = $short_desc_trunc; |
||
| 105 | $module_array['configure_url'] = Jetpack::module_configuration_url( $module ); |
||
| 106 | |||
| 107 | ob_start(); |
||
| 108 | /** |
||
| 109 | * Allow the display of a "Learn More" button. |
||
| 110 | * The dynamic part of the action, $module, is the module slug. |
||
| 111 | * |
||
| 112 | * @since 3.0.0 |
||
| 113 | */ |
||
| 114 | do_action( 'jetpack_learn_more_button_' . $module ); |
||
| 115 | $module_array['learn_more_button'] = ob_get_clean(); |
||
| 116 | |||
| 117 | ob_start(); |
||
| 118 | /** |
||
| 119 | * Allow the display of information text when Jetpack is connected to WordPress.com. |
||
| 120 | * The dynamic part of the action, $module, is the module slug. |
||
| 121 | * |
||
| 122 | * @since 3.0.0 |
||
| 123 | */ |
||
| 124 | do_action( 'jetpack_module_more_info_' . $module ); |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Filter the long description of a module. |
||
| 128 | * |
||
| 129 | * @since 3.5.0 |
||
| 130 | * |
||
| 131 | * @param string ob_get_clean() The module long description. |
||
| 132 | * @param string $module The module name. |
||
| 133 | */ |
||
| 134 | $module_array['long_description'] = apply_filters( 'jetpack_long_module_description', ob_get_clean(), $module ); |
||
| 135 | |||
| 136 | ob_start(); |
||
| 137 | /** |
||
| 138 | * Filter the search terms for a module |
||
| 139 | * |
||
| 140 | * Search terms are typically added to the module headers, under "Additional Search Queries". |
||
| 141 | * |
||
| 142 | * Use syntax: |
||
| 143 | * function jetpack_$module_search_terms( $terms ) { |
||
| 144 | * $terms = _x( 'term 1, term 2', 'search terms', 'jetpack' ); |
||
| 145 | * return $terms; |
||
| 146 | * } |
||
| 147 | * add_filter( 'jetpack_search_terms_$module', 'jetpack_$module_search_terms' ); |
||
| 148 | * |
||
| 149 | * @since 3.5.0 |
||
| 150 | * |
||
| 151 | * @param string The search terms (comma separated). |
||
| 152 | */ |
||
| 153 | echo apply_filters( 'jetpack_search_terms_' . $module, $module_array['additional_search_queries'] ); |
||
| 154 | $module_array['search_terms'] = ob_get_clean(); |
||
| 155 | |||
| 156 | $module_array['configurable'] = false; |
||
| 157 | if ( |
||
| 158 | current_user_can( 'manage_options' ) && |
||
| 159 | /** |
||
| 160 | * Allow the display of a configuration link in the Jetpack Settings screen. |
||
| 161 | * |
||
| 162 | * @since 3.0.0 |
||
| 163 | * |
||
| 164 | * @param string $module Module name. |
||
| 165 | * @param bool false Should the Configure module link be displayed? Default to false. |
||
| 166 | */ |
||
| 167 | apply_filters( 'jetpack_module_configurable_' . $module, false ) |
||
| 168 | ) { |
||
| 169 | $module_array['configurable'] = sprintf( '<a href="%1$s">%2$s</a>', esc_url( Jetpack::module_configuration_url( $module ) ), __( 'Configure', 'jetpack' ) ); |
||
| 170 | } |
||
| 171 | |||
| 172 | $modules[ $module ] = $module_array; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | uasort( $modules, array( $this->jetpack, 'sort_modules' ) ); |
||
| 177 | |||
| 178 | if ( ! Jetpack::is_active() ) { |
||
| 179 | uasort( $modules, array( __CLASS__, 'sort_requires_connection_last' ) ); |
||
| 180 | } |
||
| 181 | |||
| 182 | return $modules; |
||
| 183 | } |
||
| 184 | |||
| 185 | static function is_module_available( $module ) { |
||
| 186 | if ( ! is_array( $module ) || empty( $module ) ) |
||
| 187 | return false; |
||
| 188 | |||
| 189 | /** |
||
| 190 | * We never want to show VaultPress as activatable through Jetpack. |
||
| 191 | */ |
||
| 192 | if ( 'vaultpress' === $module['module'] ) { |
||
| 193 | return false; |
||
| 194 | } |
||
| 195 | |||
| 196 | if ( Jetpack::is_development_mode() ) { |
||
| 197 | return ! ( $module['requires_connection'] ); |
||
| 198 | } else { |
||
| 199 | return Jetpack::is_active(); |
||
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | function handle_unrecognized_action( $action ) { |
||
| 204 | switch( $action ) { |
||
| 205 | case 'bulk-activate' : |
||
| 206 | if ( ! current_user_can( 'jetpack_activate_modules' ) ) { |
||
| 207 | break; |
||
| 208 | } |
||
| 209 | |||
| 210 | $modules = (array) $_GET['modules']; |
||
| 211 | $modules = array_map( 'sanitize_key', $modules ); |
||
| 212 | check_admin_referer( 'bulk-jetpack_page_jetpack_modules' ); |
||
| 213 | foreach( $modules as $module ) { |
||
| 214 | Jetpack::log( 'activate', $module ); |
||
| 215 | Jetpack::activate_module( $module, false ); |
||
| 216 | } |
||
| 217 | // The following two lines will rarely happen, as Jetpack::activate_module normally exits at the end. |
||
| 218 | wp_safe_redirect( wp_get_referer() ); |
||
| 219 | exit; |
||
| 220 | View Code Duplication | case 'bulk-deactivate' : |
|
| 221 | if ( ! current_user_can( 'jetpack_deactivate_modules' ) ) { |
||
| 222 | break; |
||
| 223 | } |
||
| 224 | |||
| 225 | $modules = (array) $_GET['modules']; |
||
| 226 | $modules = array_map( 'sanitize_key', $modules ); |
||
| 227 | check_admin_referer( 'bulk-jetpack_page_jetpack_modules' ); |
||
| 228 | foreach ( $modules as $module ) { |
||
| 229 | Jetpack::log( 'deactivate', $module ); |
||
| 230 | Jetpack::deactivate_module( $module ); |
||
| 231 | Jetpack::state( 'message', 'module_deactivated' ); |
||
| 232 | } |
||
| 233 | Jetpack::state( 'module', $modules ); |
||
| 234 | wp_safe_redirect( wp_get_referer() ); |
||
| 235 | exit; |
||
| 236 | default: |
||
| 237 | return; |
||
| 238 | } |
||
| 239 | } |
||
| 240 | |||
| 241 | function fix_redirect( $module, $redirect = true ) { |
||
| 242 | if ( ! $redirect ) { |
||
| 243 | return; |
||
| 244 | } |
||
| 245 | if ( wp_get_referer() ) { |
||
| 246 | add_filter( 'wp_redirect', 'wp_get_referer' ); |
||
| 247 | } |
||
| 248 | } |
||
| 249 | |||
| 250 | function admin_menu_debugger() { |
||
| 251 | $debugger_hook = add_submenu_page( null, __( 'Jetpack Debugging Center', 'jetpack' ), '', 'manage_options', 'jetpack-debugger', array( $this, 'debugger_page' ) ); |
||
| 252 | add_action( "admin_head-$debugger_hook", array( 'Jetpack_Debugger', 'jetpack_debug_admin_head' ) ); |
||
| 253 | } |
||
| 254 | |||
| 255 | function debugger_page() { |
||
| 262 | } |
||
| 263 | Jetpack_Admin::init(); |
||
| 264 |
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: