Completed
Push — add/new-disconnect-dialog ( 733ee6...f4b682 )
by
unknown
30:58 queued 24:22
created

Jetpack_Admin   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 285
Duplicated Lines 9.47 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

Changes 0
Metric Value
dl 27
loc 285
rs 8.8798
c 0
b 0
f 0
wmc 44
lcom 2
cbo 8

11 Methods

Rating   Name   Duplication   Size   Complexity  
B init() 0 16 7
A add_no_store_header() 0 4 1
A __construct() 0 26 1
A sort_requires_connection_last() 11 11 4
C get_modules() 0 113 11
B is_module_available() 0 21 6
B handle_unrecognized_action() 16 37 7
A fix_redirect() 0 8 3
A admin_menu_debugger() 0 13 1
A wrap_debugger_page() 0 7 2
A debugger_page() 0 4 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

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 Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Jetpack_Admin 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, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
4
// Build the Jetpack admin menu as a whole
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( isset( $_GET['page'] ) && $_GET['page'] === 'jetpack' ) {
19
			add_filter( 'nocache_headers', array( 'Jetpack_Admin', 'add_no_store_header' ), 100 );
20
21
			// suppress wpadmin stuffs when rendering iframes
22
			if( isset( $_GET['iframe_request'] ) && $_GET['iframe_request'] && ! defined( 'IFRAME_REQUEST') ) {
23
				define( 'IFRAME_REQUEST', true );
24
			}
25
26
		}
27
28
		if ( is_null( self::$instance ) ) {
29
			self::$instance = new Jetpack_Admin;
30
		}
31
		return self::$instance;
32
	}
33
34
	static function add_no_store_header( $headers ) {
35
		$headers['Cache-Control'] .= ', no-store';
36
		return $headers;
37
	}
38
39
	private function __construct() {
40
		$this->jetpack = Jetpack::init();
41
42
		jetpack_require_lib( 'admin-pages/class.jetpack-react-page' );
43
		$this->jetpack_react = new Jetpack_React_Page;
0 ignored issues
show
Bug introduced by
The property jetpack_react does not seem to exist. Did you mean jetpack?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
44
45
		jetpack_require_lib( 'admin-pages/class.jetpack-settings-page' );
46
		$this->fallback_page = new Jetpack_Settings_Page;
0 ignored issues
show
Bug introduced by
The property fallback_page does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
47
48
		jetpack_require_lib( 'admin-pages/class-jetpack-about-page' );
49
		$this->jetpack_about = new Jetpack_About_Page;
0 ignored issues
show
Bug introduced by
The property jetpack_about does not seem to exist. Did you mean jetpack?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
50
51
		add_action( 'admin_menu',                    array( $this->jetpack_react, 'add_actions' ), 998 );
0 ignored issues
show
Bug introduced by
The property jetpack_react does not seem to exist. Did you mean jetpack?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
52
		add_action( 'jetpack_admin_menu',            array( $this->jetpack_react, 'jetpack_add_dashboard_sub_nav_item' ) );
0 ignored issues
show
Bug introduced by
The property jetpack_react does not seem to exist. Did you mean jetpack?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
53
		add_action( 'jetpack_admin_menu',            array( $this->jetpack_react, 'jetpack_add_settings_sub_nav_item' ) );
0 ignored issues
show
Bug introduced by
The property jetpack_react does not seem to exist. Did you mean jetpack?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
54
		add_action( 'jetpack_admin_menu',            array( $this, 'admin_menu_debugger' ) );
55
		add_action( 'jetpack_admin_menu',            array( $this->fallback_page, 'add_actions' ) );
56
		add_action( 'jetpack_admin_menu',            array( $this->jetpack_about, 'add_actions' ) );
0 ignored issues
show
Bug introduced by
The property jetpack_about does not seem to exist. Did you mean jetpack?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

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

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
247
				wp_safe_redirect( wp_get_referer() );
248
				exit;
249
			default:
250
				return;
251
		}
252
	}
253
254
	function fix_redirect( $module, $redirect = true ) {
255
		if ( ! $redirect ) {
256
			return;
257
		}
258
		if ( wp_get_referer() ) {
259
			add_filter( 'wp_redirect', 'wp_get_referer' );
260
		}
261
	}
262
263
	function admin_menu_debugger() {
264
		jetpack_require_lib( 'debugger' );
265
		Jetpack_Debugger::disconnect_and_redirect();
266
		$debugger_hook = add_submenu_page(
267
			null,
268
			__( 'Debugging Center', 'jetpack' ),
269
			'',
270
			'manage_options',
271
			'jetpack-debugger',
272
			array( $this, 'wrap_debugger_page' )
273
		);
274
		add_action( "admin_head-$debugger_hook", array( 'Jetpack_Debugger', 'jetpack_debug_admin_head' ) );
275
	}
276
277
	function wrap_debugger_page( ) {
278
		nocache_headers();
279
		if ( ! current_user_can( 'manage_options' ) ) {
280
			die( '-1' );
281
		}
282
		Jetpack_Admin_Page::wrap_ui( array( $this, 'debugger_page' ) );
283
	}
284
285
	function debugger_page() {
286
		jetpack_require_lib( 'debugger' );
287
		Jetpack_Debugger::jetpack_debug_display_handler();
288
	}
289
}
290
Jetpack_Admin::init();
291