Completed
Push — branch-4.2 ( 7eec8e...ae5de8 )
by
unknown
36:31 queued 27:00
created

class.jetpack-admin.php (9 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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