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