Completed
Push — v2/videopress ( 946a33...00803f )
by George
35:29
created

Jetpack_Admin::get_modules()   D

Complexity

Conditions 11
Paths 136

Size

Total Lines 111
Code Lines 42

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 111
rs 4.9629
cc 11
eloc 42
nc 136
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 ( 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;
0 ignored issues
show
Bug introduced by
The property landing_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...
29
30
		jetpack_require_lib( 'admin-pages/class.jetpack-settings-page' );
31
		$this->settings_page = new Jetpack_Settings_Page;
0 ignored issues
show
Bug introduced by
The property settings_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...
32
33
		jetpack_require_lib( 'admin-pages/class.jetpack-my-jetpack-page' );
34
		$this->my_jetpack_page = new Jetpack_My_Jetpack_Page;
0 ignored issues
show
Bug introduced by
The property my_jetpack_page 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...
35
36
		if ( isset( $_POST['jetpack-set-master-user'] ) ) {
37
			add_action( 'init', array( $this->my_jetpack_page, 'jetpack_my_jetpack_change_user' ) );
0 ignored issues
show
Bug introduced by
The property my_jetpack_page 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...
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' ) );
0 ignored issues
show
Bug introduced by
The property my_jetpack_page 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...
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 ) {
56
		if ( $module1['requires_connection'] == $module2['requires_connection'] ) {
57
			return 0;
58
		} elseif ( $module1['requires_connection'] ) {
59
			return 1;
60
		} elseif ( $module2['requires_connection'] ) {
61
			return -1;
62
		}
63
64
		return 0;
65
	}
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
				/**
115
				 * Allow the display of information text when Jetpack is connected to WordPress.com.
116
				 * The dynamic part of the action, $module, is the module slug.
117
				 *
118
				 * @since 3.0.0
119
				 */
120
				do_action( 'jetpack_module_more_info_' . $module );
121
122
				/**
123
				* Filter the long description of a module.
124
	 			*
125
	 			* @since 3.5.0
126
	 			*
127
	 			* @param string ob_get_clean() The module long description.
128
				* @param string $module The module name.
129
	 			*/
130
				$module_array['long_description'] = apply_filters( 'jetpack_long_module_description', ob_get_clean(), $module );
131
132
				ob_start();
133
				/**
134
				 * Filter the search terms for a module
135
				 *
136
				 * Search terms are typically added to the module headers, under "Additional Search Queries".
137
				 *
138
				 * Use syntax:
139
				 * function jetpack_$module_search_terms( $terms ) {
140
				 *  $terms = _x( 'term 1, term 2', 'search terms', 'jetpack' );
141
				 *  return $terms;
142
				 * }
143
				 * add_filter( 'jetpack_search_terms_$module', 'jetpack_$module_search_terms' );
144
				 *
145
				 * @since 3.5.0
146
				 *
147
				 * @param string The search terms (comma separated).
148
				 */
149
				echo apply_filters( 'jetpack_search_terms_' . $module, $module_array['additional_search_queries'] );
150
				$module_array['search_terms'] = ob_get_clean();
151
152
				$module_array['configurable'] = false;
153
				if (
154
					current_user_can( 'manage_options' ) &&
155
					/**
156
					 * Allow the display of a configuration link in the Jetpack Settings screen.
157
					 *
158
					 * @since 3.0.0
159
					 *
160
					 * @param string $module Module name.
161
					 * @param bool false Should the Configure module link be displayed? Default to false.
162
					 */
163
					apply_filters( 'jetpack_module_configurable_' . $module, false )
164
				) {
165
					$module_array['configurable'] = sprintf( '<a href="%1$s">%2$s</a>', esc_url( Jetpack::module_configuration_url( $module ) ), __( 'Configure', 'jetpack' ) );
166
				}
167
168
				$modules[ $module ] = $module_array;
169
			}
170
		}
171
172
		uasort( $modules, array( $this->jetpack, 'sort_modules' ) );
173
174
		if ( ! Jetpack::is_active() ) {
175
			uasort( $modules, array( __CLASS__, 'sort_requires_connection_last' ) );
176
		}
177
178
		return $modules;
179
	}
180
181
	static function is_module_available( $module ) {
182
		if ( ! is_array( $module ) || empty( $module ) )
183
			return false;
184
185
		/**
186
		 * We never want to show VaultPress as activatable through Jetpack.
187
		 */
188
		if ( 'vaultpress' === $module['module'] ) {
189
			return false;
190
		}
191
192
		if ( Jetpack::is_development_mode() ) {
193
			return ! ( $module['requires_connection'] );
194
		} else {
195
			return Jetpack::is_active();
196
		}
197
	}
198
199
	function handle_unrecognized_action( $action ) {
200
		switch( $action ) {
201
			case 'bulk-activate' :
202
				if ( ! current_user_can( 'jetpack_activate_modules' ) ) {
203
					break;
204
				}
205
206
				$modules = (array) $_GET['modules'];
207
				$modules = array_map( 'sanitize_key', $modules );
208
				check_admin_referer( 'bulk-jetpack_page_jetpack_modules' );
209
				foreach( $modules as $module ) {
210
					Jetpack::log( 'activate', $module );
211
					Jetpack::activate_module( $module, false );
212
				}
213
				// The following two lines will rarely happen, as Jetpack::activate_module normally exits at the end.
214
				wp_safe_redirect( wp_get_referer() );
215
				exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method handle_unrecognized_action() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
216 View Code Duplication
			case 'bulk-deactivate' :
217
				if ( ! current_user_can( 'jetpack_deactivate_modules' ) ) {
218
					break;
219
				}
220
221
				$modules = (array) $_GET['modules'];
222
				$modules = array_map( 'sanitize_key', $modules );
223
				check_admin_referer( 'bulk-jetpack_page_jetpack_modules' );
224
				foreach ( $modules as $module ) {
225
					Jetpack::log( 'deactivate', $module );
226
					Jetpack::deactivate_module( $module );
227
					Jetpack::state( 'message', 'module_deactivated' );
228
				}
229
				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...
230
				wp_safe_redirect( wp_get_referer() );
231
				exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method handle_unrecognized_action() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
232
			default:
233
				return;
234
		}
235
	}
236
237
	function fix_redirect( $module, $redirect = true ) {
238
		if ( ! $redirect ) {
239
			return;
240
		}
241
		if ( wp_get_referer() ) {
242
			add_filter( 'wp_redirect', 'wp_get_referer' );
243
		}
244
	}
245
246
	function admin_menu_debugger() {
247
		$debugger_hook = add_submenu_page( null, __( 'Jetpack Debugging Center', 'jetpack' ), '', 'manage_options', 'jetpack-debugger', array( $this, 'debugger_page' ) );
248
		add_action( "admin_head-$debugger_hook", array( 'Jetpack_Debugger', 'jetpack_debug_admin_head' ) );
249
	}
250
251
	function debugger_page() {
252
		nocache_headers();
253
		if ( ! current_user_can( 'manage_options' ) ) {
254
			die( '-1' );
0 ignored issues
show
Coding Style Compatibility introduced by
The method debugger_page() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
255
		}
256
		Jetpack_Debugger::jetpack_debug_display_handler();
257
	}
258
}
259
Jetpack_Admin::init();
260