Completed
Push — update/wp-requirements-wp57 ( 343d2f...374f74 )
by Jeremy
64:01 queued 53:47
created

Jetpack_Admin::should_display_jitms_on_screen()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 2
dl 0
loc 32
rs 9.408
c 0
b 0
f 0
1
<?php
2
3
use Automattic\Jetpack\Status;
4
use Automattic\Jetpack\Assets\Logo as Jetpack_Logo;
5
6
// Build the Jetpack admin menu as a whole
7
class Jetpack_Admin {
8
9
	/**
10
	 * @var Jetpack_Admin
11
	 **/
12
	private static $instance = null;
13
14
	static function init() {
15
		if ( isset( $_GET['page'] ) && $_GET['page'] === 'jetpack' ) {
16
			add_filter( 'nocache_headers', array( 'Jetpack_Admin', 'add_no_store_header' ), 100 );
17
		}
18
19
		if ( is_null( self::$instance ) ) {
20
			self::$instance = new Jetpack_Admin();
21
		}
22
		return self::$instance;
23
	}
24
25
	static function add_no_store_header( $headers ) {
26
		$headers['Cache-Control'] .= ', no-store';
27
		return $headers;
28
	}
29
30
	private function __construct() {
31
		jetpack_require_lib( 'admin-pages/class.jetpack-react-page' );
32
		$this->jetpack_react = new Jetpack_React_Page();
0 ignored issues
show
Bug introduced by
The property jetpack_react 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...
33
34
		jetpack_require_lib( 'admin-pages/class.jetpack-settings-page' );
35
		$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...
36
37
		jetpack_require_lib( 'admin-pages/class-jetpack-about-page' );
38
		$this->jetpack_about = new Jetpack_About_Page();
0 ignored issues
show
Bug introduced by
The property jetpack_about 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...
39
40
		add_action( 'admin_menu', array( $this->jetpack_react, 'add_actions' ), 998 );
41
		add_action( 'admin_menu', array( $this->jetpack_react, 'add_actions' ), 998 );
42
		add_action( 'jetpack_admin_menu', array( $this->jetpack_react, 'jetpack_add_dashboard_sub_nav_item' ) );
43
		add_action( 'jetpack_admin_menu', array( $this->jetpack_react, 'jetpack_add_settings_sub_nav_item' ) );
44
		add_action( 'jetpack_admin_menu', array( $this, 'admin_menu_debugger' ) );
45
		add_action( 'jetpack_admin_menu', array( $this->fallback_page, 'add_actions' ) );
46
		add_action( 'jetpack_admin_menu', array( $this->jetpack_about, 'add_actions' ) );
47
48
		// Add redirect to current page for activation/deactivation of modules
49
		add_action( 'jetpack_pre_activate_module', array( $this, 'fix_redirect' ), 10, 2 );
50
		add_action( 'jetpack_pre_deactivate_module', array( $this, 'fix_redirect' ) );
51
52
		// Add module bulk actions handler
53
		add_action( 'jetpack_unrecognized_action', array( $this, 'handle_unrecognized_action' ) );
54
55
		if ( class_exists( 'Akismet_Admin' ) ) {
56
			// If the site has Jetpack Anti-Spam, change the Akismet menu label accordingly.
57
			$site_products      = Jetpack_Plan::get_products();
58
			$anti_spam_products = array( 'jetpack_anti_spam_monthly', 'jetpack_anti_spam' );
59
			if ( ! empty( array_intersect( $anti_spam_products, array_column( $site_products, 'product_slug' ) ) ) ) {
60
				// Prevent Akismet from adding a menu item.
61
				add_action(
62
					'admin_menu',
63
					function () {
64
						remove_action( 'admin_menu', array( 'Akismet_Admin', 'admin_menu' ), 5 );
65
					},
66
					4
67
				);
68
69
				// Add an Anti-spam menu item for Jetpack.
70
				add_action(
71
					'jetpack_admin_menu',
72
					function () {
73
						add_submenu_page( 'jetpack', __( 'Anti-Spam', 'jetpack' ), __( 'Anti-Spam', 'jetpack' ), 'manage_options', 'akismet-key-config', array( 'Akismet_Admin', 'display_page' ) );
74
					}
75
				);
76
				add_action( 'admin_enqueue_scripts', array( $this, 'akismet_logo_replacement_styles' ) );
77
			}
78
		}
79
80
		add_filter( 'jetpack_display_jitms_on_screen', array( $this, 'should_display_jitms_on_screen' ), 10, 2 );
81
	}
82
83
	/**
84
	 * Generate styles to replace Akismet logo for the Jetpack logo. It's a workaround until we create a proper settings page for
85
	 * Jetpack Anti-Spam. Without this, we would have to change the logo from Akismet codebase and we want to avoid that.
86
	 */
87
	public function akismet_logo_replacement_styles() {
88
		$logo            = new Jetpack_Logo();
89
		$logo_base64     = base64_encode( $logo->get_jp_emblem_larger() );
90
		$logo_base64_url = "data:image/svg+xml;base64,{$logo_base64}";
91
		$style           = ".akismet-masthead__logo-container { background: url({$logo_base64_url}) no-repeat .25rem; height: 1.8125rem; } .akismet-masthead__logo { display: none; }";
92
		wp_add_inline_style( 'admin-bar', $style );
93
	}
94
95 View Code Duplication
	static function sort_requires_connection_last( $module1, $module2 ) {
96
		if ( $module1['requires_connection'] == $module2['requires_connection'] ) {
97
			return 0;
98
		} elseif ( $module1['requires_connection'] ) {
99
			return 1;
100
		} elseif ( $module2['requires_connection'] ) {
101
			return -1;
102
		}
103
104
		return 0;
105
	}
106
107
	// Produce JS understandable objects of modules containing information for
108
	// presentation like description, name, configuration url, etc.
109
	function get_modules() {
110
		include_once JETPACK__PLUGIN_DIR . 'modules/module-info.php';
111
		$available_modules = Jetpack::get_available_modules();
112
		$active_modules    = Jetpack::get_active_modules();
113
		$modules           = array();
114
		$jetpack_active    = Jetpack::is_active() || ( new Status() )->is_offline_mode();
115
		$overrides         = Jetpack_Modules_Overrides::instance();
116
		foreach ( $available_modules as $module ) {
117
			if ( $module_array = Jetpack::get_module( $module ) ) {
118
				/**
119
				 * Filters each module's short description.
120
				 *
121
				 * @since 3.0.0
122
				 *
123
				 * @param string $module_array['description'] Module description.
124
				 * @param string $module Module slug.
125
				 */
126
				$short_desc = apply_filters( 'jetpack_short_module_description', $module_array['description'], $module );
0 ignored issues
show
Unused Code introduced by
The call to apply_filters() has too many arguments starting with $module.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
127
				// Fix: correct multibyte strings truncate with checking for mbstring extension
128
				$short_desc_trunc = ( function_exists( 'mb_strlen' ) )
129
							? ( ( mb_strlen( $short_desc ) > 143 )
130
								? mb_substr( $short_desc, 0, 140 ) . '...'
131
								: $short_desc )
132
							: ( ( strlen( $short_desc ) > 143 )
133
								? substr( $short_desc, 0, 140 ) . '...'
134
								: $short_desc );
135
136
				$module_array['module']            = $module;
137
				$module_array['activated']         = ( $jetpack_active ? in_array( $module, $active_modules ) : false );
138
				$module_array['deactivate_nonce']  = wp_create_nonce( 'jetpack_deactivate-' . $module );
139
				$module_array['activate_nonce']    = wp_create_nonce( 'jetpack_activate-' . $module );
140
				$module_array['available']         = self::is_module_available( $module_array );
141
				$module_array['short_description'] = $short_desc_trunc;
142
				$module_array['configure_url']     = Jetpack::module_configuration_url( $module );
143
				$module_array['override']          = $overrides->get_module_override( $module );
144
145
				ob_start();
146
				/**
147
				 * Allow the display of a "Learn More" button.
148
				 * The dynamic part of the action, $module, is the module slug.
149
				 *
150
				 * @since 3.0.0
151
				 */
152
				do_action( 'jetpack_learn_more_button_' . $module );
153
				$module_array['learn_more_button'] = ob_get_clean();
154
155
				ob_start();
156
				/**
157
				 * Allow the display of information text when Jetpack is connected to WordPress.com.
158
				 * The dynamic part of the action, $module, is the module slug.
159
				 *
160
				 * @since 3.0.0
161
				 */
162
				do_action( 'jetpack_module_more_info_' . $module );
163
164
				/**
165
				* Filter the long description of a module.
166
				*
167
				* @since 3.5.0
168
				*
169
				* @param string ob_get_clean() The module long description.
170
				* @param string $module The module name.
171
				*/
172
				$module_array['long_description'] = apply_filters( 'jetpack_long_module_description', ob_get_clean(), $module );
0 ignored issues
show
Unused Code introduced by
The call to apply_filters() has too many arguments starting with $module.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
173
174
				ob_start();
175
				/**
176
				 * Filter the search terms for a module
177
				 *
178
				 * Search terms are typically added to the module headers, under "Additional Search Queries".
179
				 *
180
				 * Use syntax:
181
				 * function jetpack_$module_search_terms( $terms ) {
182
				 *  $terms = _x( 'term 1, term 2', 'search terms', 'jetpack' );
183
				 *  return $terms;
184
				 * }
185
				 * add_filter( 'jetpack_search_terms_$module', 'jetpack_$module_search_terms' );
186
				 *
187
				 * @since 3.5.0
188
				 *
189
				 * @param string The search terms (comma separated).
190
				 */
191
				echo apply_filters( 'jetpack_search_terms_' . $module, $module_array['additional_search_queries'] );
192
				$module_array['search_terms'] = ob_get_clean();
193
194
				$module_array['configurable'] = false;
195
				if (
196
					current_user_can( 'manage_options' ) &&
197
					/**
198
					 * Allow the display of a configuration link in the Jetpack Settings screen.
199
					 *
200
					 * @since 3.0.0
201
					 *
202
					 * @param string $module Module name.
203
					 * @param bool false Should the Configure module link be displayed? Default to false.
204
					 */
205
					apply_filters( 'jetpack_module_configurable_' . $module, false )
206
				) {
207
					$module_array['configurable'] = sprintf( '<a href="%1$s">%2$s</a>', esc_url( $module_array['configure_url'] ), __( 'Configure', 'jetpack' ) );
208
				}
209
210
				$modules[ $module ] = $module_array;
211
			}
212
		}
213
214
		uasort( $modules, array( 'Jetpack', 'sort_modules' ) );
215
216
		if ( ! Jetpack::is_active() ) {
217
			uasort( $modules, array( __CLASS__, 'sort_requires_connection_last' ) );
218
		}
219
220
		return $modules;
221
	}
222
223
	static function is_module_available( $module ) {
224
		if ( ! is_array( $module ) || empty( $module ) ) {
225
			return false;
226
		}
227
228
		/**
229
		 * We never want to show VaultPress as activatable through Jetpack.
230
		 */
231
		if ( 'vaultpress' === $module['module'] ) {
232
			return false;
233
		}
234
235
		/*
236
		 * WooCommerce Analytics should only be available
237
		 * when running WooCommerce 3+
238
		 */
239
		if (
240
			'woocommerce-analytics' === $module['module']
241
			&& (
242
				! class_exists( 'WooCommerce' )
243
				|| version_compare( WC_VERSION, '3.0', '<' )
244
			)
245
		) {
246
			return false;
247
		}
248
249
		if ( ( new Status() )->is_offline_mode() ) {
250
			return ! ( $module['requires_connection'] );
251
		} else {
252
			if ( ! Jetpack::is_active() ) {
253
				return false;
254
			}
255
256
			return Jetpack_Plan::supports( $module['module'] );
257
		}
258
	}
259
260
	function handle_unrecognized_action( $action ) {
261
		switch ( $action ) {
262
			case 'bulk-activate':
263
				if ( ! current_user_can( 'jetpack_activate_modules' ) ) {
264
					break;
265
				}
266
267
				$modules = (array) $_GET['modules'];
268
				$modules = array_map( 'sanitize_key', $modules );
269
				check_admin_referer( 'bulk-jetpack_page_jetpack_modules' );
270
				foreach ( $modules as $module ) {
271
					Jetpack::log( 'activate', $module );
272
					Jetpack::activate_module( $module, false );
273
				}
274
				// The following two lines will rarely happen, as Jetpack::activate_module normally exits at the end.
275
				wp_safe_redirect( wp_get_referer() );
276
				exit;
277 View Code Duplication
			case 'bulk-deactivate':
278
				if ( ! current_user_can( 'jetpack_deactivate_modules' ) ) {
279
					break;
280
				}
281
282
				$modules = (array) $_GET['modules'];
283
				$modules = array_map( 'sanitize_key', $modules );
284
				check_admin_referer( 'bulk-jetpack_page_jetpack_modules' );
285
				foreach ( $modules as $module ) {
286
					Jetpack::log( 'deactivate', $module );
287
					Jetpack::deactivate_module( $module );
288
					Jetpack::state( 'message', 'module_deactivated' );
289
				}
290
				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...
291
				wp_safe_redirect( wp_get_referer() );
292
				exit;
293
			default:
294
				return;
295
		}
296
	}
297
298
	function fix_redirect( $module, $redirect = true ) {
299
		if ( ! $redirect ) {
300
			return;
301
		}
302
		if ( wp_get_referer() ) {
303
			add_filter( 'wp_redirect', 'wp_get_referer' );
304
		}
305
	}
306
307
	function admin_menu_debugger() {
308
		jetpack_require_lib( 'debugger' );
309
		Jetpack_Debugger::disconnect_and_redirect();
310
		$debugger_hook = add_submenu_page(
311
			null,
312
			__( 'Debugging Center', 'jetpack' ),
313
			'',
314
			'manage_options',
315
			'jetpack-debugger',
316
			array( $this, 'wrap_debugger_page' )
317
		);
318
		add_action( "admin_head-$debugger_hook", array( 'Jetpack_Debugger', 'jetpack_debug_admin_head' ) );
319
	}
320
321
	function wrap_debugger_page() {
322
		nocache_headers();
323
		if ( ! current_user_can( 'manage_options' ) ) {
324
			die( '-1' );
325
		}
326
		Jetpack_Admin_Page::wrap_ui( array( $this, 'debugger_page' ) );
327
	}
328
329
	function debugger_page() {
330
		jetpack_require_lib( 'debugger' );
331
		Jetpack_Debugger::jetpack_debug_display_handler();
332
	}
333
334
	/**
335
	 * Determines if JITMs should display on a particular screen.
336
	 *
337
	 * @param bool   $value The default value of the filter.
338
	 * @param string $screen_id The ID of the screen being tested for JITM display.
339
	 *
340
	 * @return bool True if JITMs should display, false otherwise.
341
	 */
342
	public function should_display_jitms_on_screen( $value, $screen_id ) {
343
		// Disable all JITMs on these pages.
344
		if (
345
		in_array(
346
			$screen_id,
347
			array(
348
				'jetpack_page_akismet-key-config',
349
				'admin_page_jetpack_modules',
350
			),
351
			true
352
		) ) {
353
			return false;
354
		}
355
356
		// Disable all JITMs on pages where the recommendations banner is displaying.
357
		if (
358
			in_array(
359
				$screen_id,
360
				array(
361
					'dashboard',
362
					'plugins',
363
					'jetpack_page_stats',
364
				),
365
				true
366
			)
367
			&& \Jetpack_Recommendations_Banner::can_be_displayed()
368
		) {
369
			return false;
370
		}
371
372
		return $value;
373
	}
374
}
375
Jetpack_Admin::init();
376