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 { |
||
1 ignored issue
–
show
|
|||
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() { |
||
25 | $this->jetpack = Jetpack::init(); |
||
26 | |||
27 | jetpack_require_lib( 'admin-pages/class.jetpack-landing-page' ); |
||
28 | $this->landing_page = new Jetpack_Landing_Page; |
||
29 | |||
30 | jetpack_require_lib( 'admin-pages/class.jetpack-settings-page' ); |
||
31 | $this->settings_page = new Jetpack_Settings_Page; |
||
32 | |||
33 | jetpack_require_lib( 'admin-pages/class.jetpack-my-jetpack-page' ); |
||
34 | $this->my_jetpack_page = new Jetpack_My_Jetpack_Page; |
||
35 | |||
36 | if ( isset( $_POST['jetpack-set-master-user'] ) ) { |
||
37 | add_action( 'init', array( $this->my_jetpack_page, 'jetpack_my_jetpack_change_user' ) ); |
||
38 | } |
||
39 | |||
40 | // Add hooks for admin menus |
||
41 | add_action( 'admin_menu', array( $this->landing_page, 'add_actions' ), 998 ); |
||
42 | add_action( 'jetpack_admin_menu', array( $this, 'admin_menu_debugger' ) ); |
||
43 | add_action( 'jetpack_admin_menu', array( $this->settings_page, 'add_actions' ) ); |
||
44 | add_action( 'jetpack_admin_menu', array( $this->my_jetpack_page, 'add_actions' ) ); |
||
45 | |||
46 | |||
47 | // Add redirect to current page for activation/deactivation of modules |
||
48 | add_action( 'jetpack_pre_activate_module', array( $this, 'fix_redirect' ), 10, 2 ); |
||
49 | add_action( 'jetpack_pre_deactivate_module', array( $this, 'fix_redirect' ) ); |
||
50 | |||
51 | // Add module bulk actions handler |
||
52 | add_action( 'jetpack_unrecognized_action', array( $this, 'handle_unrecognized_action' ) ); |
||
53 | } |
||
54 | |||
55 | View Code Duplication | static function sort_requires_connection_last( $module1, $module2 ) { |
|
66 | |||
67 | // Produce JS understandable objects of modules containing information for |
||
68 | // presentation like description, name, configuration url, etc. |
||
69 | function get_modules() { |
||
70 | include_once( JETPACK__PLUGIN_DIR . 'modules/module-info.php' ); |
||
71 | $available_modules = $this->jetpack->get_available_modules(); |
||
72 | $active_modules = $this->jetpack->get_active_modules(); |
||
73 | $modules = array(); |
||
74 | $jetpack_active = Jetpack::is_active() || Jetpack::is_development_mode(); |
||
75 | foreach ( $available_modules as $module ) { |
||
76 | if ( $module_array = $this->jetpack->get_module( $module ) ) { |
||
77 | /** |
||
78 | * Filters each module's short description. |
||
79 | * |
||
80 | * @since 3.0.0 |
||
81 | * |
||
82 | * @param string $module_array['description'] Module description. |
||
83 | * @param string $module Module slug. |
||
84 | */ |
||
85 | $short_desc = apply_filters( 'jetpack_short_module_description', $module_array['description'], $module ); |
||
86 | // Fix: correct multibyte strings truncate with checking for mbstring extension |
||
87 | $short_desc_trunc = ( function_exists( 'mb_strlen' ) ) |
||
88 | ? ( ( mb_strlen( $short_desc ) > 143 ) |
||
89 | ? mb_substr( $short_desc, 0, 140 ) . '...' |
||
90 | : $short_desc ) |
||
91 | : ( ( strlen( $short_desc ) > 143 ) |
||
92 | ? substr( $short_desc, 0, 140 ) . '...' |
||
93 | : $short_desc ); |
||
94 | |||
95 | $module_array['module'] = $module; |
||
96 | $module_array['activated'] = ( $jetpack_active ? in_array( $module, $active_modules ) : false ); |
||
97 | $module_array['deactivate_nonce'] = wp_create_nonce( 'jetpack_deactivate-' . $module ); |
||
98 | $module_array['activate_nonce'] = wp_create_nonce( 'jetpack_activate-' . $module ); |
||
99 | $module_array['available'] = self::is_module_available( $module_array ); |
||
100 | $module_array['short_description'] = $short_desc_trunc; |
||
101 | $module_array['configure_url'] = Jetpack::module_configuration_url( $module ); |
||
102 | |||
103 | ob_start(); |
||
104 | /** |
||
105 | * Allow the display of a "Learn More" button. |
||
106 | * The dynamic part of the action, $module, is the module slug. |
||
107 | * |
||
108 | * @since 3.0.0 |
||
109 | */ |
||
110 | do_action( 'jetpack_learn_more_button_' . $module ); |
||
111 | $module_array['learn_more_button'] = ob_get_clean(); |
||
112 | |||
113 | ob_start(); |
||
114 | View Code Duplication | if ( Jetpack::is_active() && has_action( 'jetpack_module_more_info_connected_' . $module ) ) { |
|
115 | /** |
||
116 | * Allow the display of information text when Jetpack is connected to WordPress.com. |
||
117 | * The dynamic part of the action, $module, is the module slug. |
||
118 | * |
||
119 | * @since 3.0.0 |
||
120 | */ |
||
121 | do_action( 'jetpack_module_more_info_connected_' . $module ); |
||
122 | } else { |
||
123 | /** |
||
124 | * Allow the display of information text when Jetpack is connected to WordPress.com. |
||
125 | * The dynamic part of the action, $module, is the module slug. |
||
126 | * |
||
127 | * @since 3.0.0 |
||
128 | */ |
||
129 | do_action( 'jetpack_module_more_info_' . $module ); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Filter the long description of a module. |
||
134 | * |
||
135 | * @since 3.5.0 |
||
136 | * |
||
137 | * @param string ob_get_clean() The module long description. |
||
138 | * @param string $module The module name. |
||
139 | */ |
||
140 | $module_array['long_description'] = apply_filters( 'jetpack_long_module_description', ob_get_clean(), $module ); |
||
141 | |||
142 | ob_start(); |
||
143 | /** |
||
144 | * Filter the search terms for a module |
||
145 | * |
||
146 | * Search terms are typically added to the module headers, under "Additional Search Queries". |
||
147 | * |
||
148 | * Use syntax: |
||
149 | * function jetpack_$module_search_terms( $terms ) { |
||
150 | * $terms = _x( 'term 1, term 2', 'search terms', 'jetpack' ); |
||
151 | * return $terms; |
||
152 | * } |
||
153 | * add_filter( 'jetpack_search_terms_$module', 'jetpack_$module_search_terms' ); |
||
154 | * |
||
155 | * @since 3.5.0 |
||
156 | * |
||
157 | * @param string The search terms (comma separated). |
||
158 | */ |
||
159 | echo apply_filters( 'jetpack_search_terms_' . $module, $module_array['additional_search_queries'] ); |
||
160 | $module_array['search_terms'] = ob_get_clean(); |
||
161 | |||
162 | $module_array['configurable'] = false; |
||
163 | if ( |
||
164 | current_user_can( 'manage_options' ) && |
||
165 | /** |
||
166 | * Allow the display of a configuration link in the Jetpack Settings screen. |
||
167 | * |
||
168 | * @since 3.0.0 |
||
169 | * |
||
170 | * @param string $module Module name. |
||
171 | * @param bool false Should the Configure module link be displayed? Default to false. |
||
172 | */ |
||
173 | apply_filters( 'jetpack_module_configurable_' . $module, false ) |
||
174 | ) { |
||
175 | $module_array['configurable'] = sprintf( '<a href="%1$s">%2$s</a>', esc_url( Jetpack::module_configuration_url( $module ) ), __( 'Configure', 'jetpack' ) ); |
||
176 | } |
||
177 | |||
178 | $modules[ $module ] = $module_array; |
||
179 | } |
||
180 | } |
||
181 | |||
182 | uasort( $modules, array( $this->jetpack, 'sort_modules' ) ); |
||
183 | |||
184 | if ( ! Jetpack::is_active() ) { |
||
185 | uasort( $modules, array( __CLASS__, 'sort_requires_connection_last' ) ); |
||
186 | } |
||
187 | |||
188 | return $modules; |
||
189 | } |
||
190 | |||
191 | static function is_module_available( $module ) { |
||
192 | if ( ! is_array( $module ) || empty( $module ) ) |
||
193 | return false; |
||
194 | |||
195 | /** |
||
196 | * We never want to show VaultPress as activatable through Jetpack. |
||
197 | */ |
||
198 | if ( 'vaultpress' === $module['module'] ) { |
||
199 | return false; |
||
200 | } |
||
201 | |||
202 | if ( Jetpack::is_development_mode() ) { |
||
203 | return ! ( $module['requires_connection'] ); |
||
204 | } else { |
||
205 | return Jetpack::is_active(); |
||
206 | } |
||
207 | } |
||
208 | |||
209 | function handle_unrecognized_action( $action ) { |
||
210 | switch( $action ) { |
||
211 | case 'bulk-activate' : |
||
212 | if ( ! current_user_can( 'jetpack_activate_modules' ) ) { |
||
213 | break; |
||
214 | } |
||
215 | |||
216 | $modules = (array) $_GET['modules']; |
||
217 | $modules = array_map( 'sanitize_key', $modules ); |
||
218 | check_admin_referer( 'bulk-jetpack_page_jetpack_modules' ); |
||
219 | foreach( $modules as $module ) { |
||
220 | Jetpack::log( 'activate', $module ); |
||
221 | Jetpack::activate_module( $module, false ); |
||
222 | } |
||
223 | // The following two lines will rarely happen, as Jetpack::activate_module normally exits at the end. |
||
224 | wp_safe_redirect( wp_get_referer() ); |
||
225 | exit; |
||
226 | View Code Duplication | case 'bulk-deactivate' : |
|
227 | if ( ! current_user_can( 'jetpack_deactivate_modules' ) ) { |
||
228 | break; |
||
229 | } |
||
230 | |||
231 | $modules = (array) $_GET['modules']; |
||
232 | $modules = array_map( 'sanitize_key', $modules ); |
||
233 | check_admin_referer( 'bulk-jetpack_page_jetpack_modules' ); |
||
234 | foreach ( $modules as $module ) { |
||
235 | Jetpack::log( 'deactivate', $module ); |
||
236 | Jetpack::deactivate_module( $module ); |
||
237 | Jetpack::state( 'message', 'module_deactivated' ); |
||
238 | } |
||
239 | Jetpack::state( 'module', $modules ); |
||
240 | wp_safe_redirect( wp_get_referer() ); |
||
241 | exit; |
||
242 | default: |
||
243 | return; |
||
244 | } |
||
245 | } |
||
246 | |||
247 | function fix_redirect( $module, $redirect = true ) { |
||
248 | if ( ! $redirect ) { |
||
249 | return; |
||
250 | } |
||
251 | if ( wp_get_referer() ) { |
||
252 | add_filter( 'wp_redirect', 'wp_get_referer' ); |
||
253 | } |
||
254 | } |
||
255 | |||
256 | function admin_menu_debugger() { |
||
260 | |||
261 | function debugger_page() { |
||
262 | nocache_headers(); |
||
263 | if ( ! current_user_can( 'manage_options' ) ) { |
||
264 | die( '-1' ); |
||
265 | } |
||
266 | Jetpack_Debugger::jetpack_debug_display_handler(); |
||
268 | } |
||
269 | Jetpack_Admin::init(); |
||
270 |