Completed
Push — enhance/style-connection-notic... ( 4b05d7...9039cc )
by
unknown
39:04 queued 30:07
created

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