Completed
Push — try/dummy-deactivate-dialog ( b7ea92...1d9029 )
by
unknown
24:07 queued 18:02
created

Jetpack_Admin::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

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