Completed
Push — add/new-disconnect-dialog ( 58c0f4...b0d5ef )
by
unknown
06:59
created

class.jetpack-admin.php ➔ hide_admin_notices()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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