Completed
Push — update/try-react-0.15 ( ce045e...4c8711 )
by
unknown
26:02 queued 15:13
created

Jetpack_React_Page::add_page_actions()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 27
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 3
nop 1
dl 0
loc 27
rs 8.439
c 0
b 0
f 0
1
<?php
2
include_once( 'class.jetpack-admin-page.php' );
3
4
// Builds the landing page and its menu
5
class Jetpack_React_Page extends Jetpack_Admin_Page {
6
7
	protected $dont_show_if_not_active = false;
8
9
	protected $is_redirecting = false;
10
11
	function get_page_hook() {
12
		$title = _x( 'Jetpack', 'The menu item label', 'jetpack' );
13
14
		// Add the main admin Jetpack menu
15
		return add_menu_page( 'Jetpack', $title, 'jetpack_admin_page', 'jetpack', array( $this, 'render' ), 'div' );
16
	}
17
18
	function add_page_actions( $hook ) {
19
		/** This action is documented in class.jetpack.php */
20
		do_action( 'jetpack_admin_menu', $hook );
21
22
		// Place the Jetpack menu item on top and others in the order they appear
23
		add_filter( 'custom_menu_order',         '__return_true' );
24
		add_filter( 'menu_order',                array( $this, 'jetpack_menu_order' ) );
25
26
		if ( ! isset( $_GET['page'] ) || 'jetpack' !== $_GET['page'] || ! empty( $_GET['configure'] ) ) {
27
			return; // No need to handle the fallback redirection if we are not on the Jetpack page
28
		}
29
30
		// Adding a redirect meta tag for older WordPress versions
31
		if ( $this->is_wp_version_too_old() ) {
32
			$this->is_redirecting = true;
33
			add_action( 'admin_head', array( $this, 'add_fallback_head_meta' ) );
34
		}
35
36
		// Adding a redirect meta tag wrapped in noscript tags for all browsers in case they have JavaScript disabled
37
		add_action( 'admin_head', array( $this, 'add_noscript_head_meta' ) );
38
39
		// Enqueue admin page styles in head
40
		add_action( 'admin_enqueue_scripts', array( $this, 'page_admin_styles' ) );
41
42
		// Adding a redirect tag wrapped in browser conditional comments
43
		add_action( 'admin_head', array( $this, 'add_legacy_browsers_head_script' ) );
44
	}
45
46
	/**
47
	 * Add Jetpack Dashboard sub-link and point it to AAG if the user can view stats, manage modules or if Protect is active.
48
	 * Otherwise and only if user is allowed to see the Jetpack Admin, the Dashboard sub-link is added but pointed to Apps tab.
49
	 *
50
	 * Works in Dev Mode or when user is connected.
51
	 *
52
	 * @since 4.3.0
53
	 */
54
	function jetpack_add_dashboard_sub_nav_item() {
55
		if ( Jetpack::is_development_mode() || Jetpack::is_active() ) {
56
			global $submenu;
57
			if ( current_user_can( 'jetpack_manage_modules' ) || Jetpack::is_module_active( 'protect' ) || current_user_can( 'view_stats' ) ) {
58
				$submenu['jetpack'][] = array( __( 'Dashboard', 'jetpack' ), 'jetpack_admin_page', Jetpack::admin_url( 'page=jetpack#/dashboard' ) );
59
			} elseif ( current_user_can( 'jetpack_admin_page' ) ) {
60
				$submenu['jetpack'][] = array( __( 'Dashboard', 'jetpack' ), 'jetpack_admin_page', Jetpack::admin_url( 'page=jetpack#/apps' ) );
61
			}
62
		}
63
	}
64
65
	/**
66
	 * If user is allowed to see the Jetpack Admin, add Settings sub-link.
67
	 *
68
	 * @since 4.3.0
69
	 */
70
	function jetpack_add_settings_sub_nav_item() {
71
		if ( ( Jetpack::is_development_mode() || Jetpack::is_active() ) && current_user_can( 'jetpack_admin_page' ) ) {
72
			global $submenu;
73
			$submenu['jetpack'][] = array( __( 'Settings', 'jetpack' ), 'jetpack_admin_page', Jetpack::admin_url( 'page=jetpack#/settings' ) );
74
		}
75
	}
76
77
	function add_fallback_head_meta() {
78
		echo '<meta http-equiv="refresh" content="0; url=?page=jetpack_modules">';
79
	}
80
81
	function add_noscript_head_meta() {
82
		echo '<noscript>';
83
		$this->add_fallback_head_meta();
84
		echo '</noscript>';
85
	}
86
87
	function add_legacy_browsers_head_script() {
88
		echo
89
			"<script type=\"text/javascript\">\n"
90
			. "/*@cc_on\n"
91
			. "if ( @_jscript_version <= 10) {\n"
92
			. "window.location.href = '?page=jetpack_modules';\n"
93
			. "}\n"
94
			. "@*/\n"
95
			. "</script>";
96
	}
97
98 View Code Duplication
	function jetpack_menu_order( $menu_order ) {
99
		$jp_menu_order = array();
100
101
		foreach ( $menu_order as $index => $item ) {
102
			if ( $item != 'jetpack' )
103
				$jp_menu_order[] = $item;
104
105
			if ( $index == 0 )
106
				$jp_menu_order[] = 'jetpack';
107
		}
108
109
		return $jp_menu_order;
110
	}
111
112
	// Render the configuration page for the module if it exists and an error
113
	// screen if the module is not configurable
114
	// @todo remove when real settings are in place
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
115
	function render_nojs_configurable( $module_name ) {
0 ignored issues
show
Unused Code introduced by
The parameter $module_name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
116
		$module_name = preg_replace( '/[^\da-z\-]+/', '', $_GET['configure'] );
117
118
		include_once( JETPACK__PLUGIN_DIR . '_inc/header.php' );
119
		echo '<div class="wrap configure-module">';
120
121
		if ( Jetpack::is_module( $module_name ) && current_user_can( 'jetpack_configure_modules' ) ) {
122
			Jetpack::admin_screen_configure_module( $module_name );
123
		} else {
124
			echo '<h2>' . esc_html__( 'Error, bad module.', 'jetpack' ) . '</h2>';
125
		}
126
127
		echo '</div><!-- /wrap -->';
128
	}
129
130
	function page_render() {
131
		// Handle redirects to configuration pages
132
		if ( ! empty( $_GET['configure'] ) ) {
133
			return $this->render_nojs_configurable( $_GET['configure'] );
134
		}
135
136
		/** This action is already documented in views/admin/admin-page.php */
137
		do_action( 'jetpack_notices' );
138
139
		// Try fetching by patch
140
		$static_html = @file_get_contents( JETPACK__PLUGIN_DIR . '_inc/build/static.html' );
141
142
		if ( false === $static_html ) {
143
144
			// If we still have nothing, display an error
145
			esc_html_e( 'Error fetching static.html.', 'jetpack' );
146
		} else {
147
148
			// We got the static.html so let's display it
149
			echo $static_html;
150
		}
151
	}
152
153
	function get_i18n_data() {
154
155
		// Try fetching by patch
156
		$locale_data = @file_get_contents( JETPACK__PLUGIN_DIR . 'languages/json/jetpack-' . get_locale() . '.json' );
157
158
		if ( false === $locale_data ) {
159
160
			// Return empty if we have nothing to return so it doesn't fail when parsed in JS
161
			return '{}';
162
		} else {
163
164
			// We got the json file so let's return it
165
			return $locale_data;
166
		}
167
	}
168
169
	/**
170
	 * Gets array of any Jetpack notices that have been dismissed.
171
	 *
172
	 * @since 4.0.1
173
	 * @return mixed|void
174
	 */
175
	function get_dismissed_jetpack_notices() {
176
		$jetpack_dismissed_notices = get_option( 'jetpack_dismissed_notices', array() );
177
		/**
178
		 * Array of notices that have been dismissed.
179
		 *
180
		 * @since 4.0.1
181
		 *
182
		 * @param array $jetpack_dismissed_notices If empty, will not show any Jetpack notices.
183
		 */
184
		$dismissed_notices = apply_filters( 'jetpack_dismissed_notices', $jetpack_dismissed_notices );
185
		return $dismissed_notices;
186
	}
187
188
	function jetpack_get_tracks_user_data() {
189
		if ( ! $user_data = Jetpack::get_connected_user_data() ) {
190
			return false;
191
		}
192
193
		return array(
194
			'userid' => $user_data['ID'],
195
			'username' => $user_data['login'],
196
		);
197
	}
198
199
	function page_admin_styles() {
200
		$rtl = is_rtl() ? '.rtl' : '';
201
202
		wp_enqueue_style( 'dops-css', plugins_url( "_inc/build/admin.dops-style$rtl.css", JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION );
203
		wp_enqueue_style( 'components-css', plugins_url( "_inc/build/style.min$rtl.css", JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION );
204
	}
205
206
	function page_admin_scripts() {
207
		if ( $this->is_redirecting ) {
208
			return; // No need for scripts on a fallback page
209
		}
210
211
		$is_dev_mode = Jetpack::is_development_mode();
212
213
		// Enqueue jp.js and localize it
214
		wp_enqueue_script( 'react-plugin', plugins_url( '_inc/build/admin.js', JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION, true );
215
216
		if ( ! $is_dev_mode ) {
217
			// Required for Analytics
218
			wp_enqueue_script( 'jp-tracks', '//stats.wp.com/w.js?48', array(), JETPACK__VERSION, true );
219
		}
220
221
		$localeSlug = explode( '_', get_locale() );
222
		$localeSlug = $localeSlug[0];
223
224
		// Collecting roles that can view site stats
225
		$stats_roles = array();
226
		$enabled_roles = function_exists( 'stats_get_option' ) ? stats_get_option( 'roles' ) : array( 'administrator' );
227
		foreach( get_editable_roles() as $slug => $role ) {
228
			$stats_roles[ $slug ] = array(
229
				'name' => translate_user_role( $role['name'] ),
230
				'canView' => in_array( $slug, $enabled_roles, true ),
231
			);
232
		}
233
234
		$response = rest_do_request( new WP_REST_Request( 'GET', '/jetpack/v4/module/all' ) );
235
		$modules = $response->get_data();
236
237
		// Preparing translated fields for JSON encoding by transforming all HTML entities to
238
		// respective characters.
239
		foreach( $modules as $slug => $data ) {
240
			$modules[ $slug ]['name'] = html_entity_decode( $data['name'] );
241
			$modules[ $slug ]['description'] = html_entity_decode( $data['description'] );
242
			$modules[ $slug ]['short_description'] = html_entity_decode( $data['short_description'] );
243
			$modules[ $slug ]['long_description'] = html_entity_decode( $data['long_description'] );
244
		}
245
246
		// Add objects to be passed to the initial state of the app
247
		wp_localize_script( 'react-plugin', 'Initial_State', array(
248
			'WP_API_root' => esc_url_raw( rest_url() ),
249
			'WP_API_nonce' => wp_create_nonce( 'wp_rest' ),
250
			'pluginBaseUrl' => plugins_url( '', JETPACK__PLUGIN_FILE ),
251
			'connectionStatus' => array(
252
				'isActive'  => Jetpack::is_active(),
253
				'isStaging' => Jetpack::is_staging_site(),
254
				'devMode'   => array(
255
					'isActive' => $is_dev_mode,
256
					'constant' => defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG,
257
					'url'      => site_url() && false === strpos( site_url(), '.' ),
258
					'filter'   => apply_filters( 'jetpack_development_mode', false ),
259
				),
260
				'isPublic'	=> '1' == get_option( 'blog_public' ),
261
			),
262
			'dismissedNotices' => $this->get_dismissed_jetpack_notices(),
263
			'isDevVersion' => Jetpack::is_development_version(),
264
			'currentVersion' => JETPACK__VERSION,
265
			'happinessGravIds' => jetpack_get_happiness_gravatar_ids(),
266
			'getModules' => $modules,
267
			'showJumpstart' => jetpack_show_jumpstart(),
268
			'rawUrl' => Jetpack::build_raw_urls( get_home_url() ),
269
			'adminUrl' => esc_url( admin_url() ),
270
			'stats' => array(
271
				// data is populated asynchronously on page load
272
				'data'  => array(
273
					'general' => false,
274
					'day'     => false,
275
					'week'    => false,
276
					'month'   => false,
277
				),
278
				'roles' => $stats_roles,
279
			),
280
			'settingNames' => array(
281
				'jetpack_holiday_snow_enabled' => function_exists( 'jetpack_holiday_snow_option_name' ) ? jetpack_holiday_snow_option_name() : false,
282
			),
283
			'userData' => array(
284
				'othersLinked' => jetpack_get_other_linked_users(),
285
				'currentUser'  => jetpack_current_user_data(),
286
			),
287
			'locale' => $this->get_i18n_data(),
288
			'localeSlug' => $localeSlug,
289
			'jetpackStateNotices' => array(
290
				'messageCode' => Jetpack::state( 'message' ),
291
				'errorCode' => Jetpack::state( 'error' ),
292
				'errorDescription' => Jetpack::state( 'error_description' ),
293
			),
294
			'tracksUserData' => $this->jetpack_get_tracks_user_data(),
295
			'currentIp' => function_exists( 'jetpack_protect_get_ip' ) ? jetpack_protect_get_ip() : false
296
		) );
297
	}
298
}
299
300
/*
301
 * List of happiness Gravatar IDs
302
 *
303
 * @todo move to functions.global.php when available
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
304
 * @since 4.1.0
305
 * @return array
306
 */
307
function jetpack_get_happiness_gravatar_ids() {
308
	return array(
309
		'623f42e878dbd146ddb30ebfafa1375b',
310
		'561be467af56cefa58e02782b7ac7510',
311
		'd8ad409290a6ae7b60f128a0b9a0c1c5',
312
		'790618302648bd80fa8a55497dfd8ac8',
313
		'6e238edcb0664c975ccb9e8e80abb307',
314
		'4e6c84eeab0a1338838a9a1e84629c1a',
315
		'9d4b77080c699629e846d3637b3a661c',
316
		'4626de7797aada973c1fb22dfe0e5109',
317
		'190cf13c9cd358521085af13615382d5',
318
		'f7006d10e9f7dd7bea89a001a2a2fd59',
319
		'16acbc88e7aa65104ed289d736cb9698',
320
		'4d5ad4219c6f676ea1e7d40d2e8860e8',
321
		'e301f7d01b09e7578fdfc1b1ec1bc08d',
322
		'42f4c73f5337486e199f6e3b3910f168',
323
		'e7b26de48e76498cff880abca1eed8da',
324
		'764fb02aaae2ff64c0625c763d82b74e',
325
		'4988305772319fb9bc8fce0a7acb3aa1',
326
		'5d8695c4b81592f1255721d2644627ca',
327
		'0e2249a7de3404bc6d5207a45e911187',
328
	);
329
}
330
331
/*
332
 * Only show Jump Start on first activation.
333
 * Any option 'jumpstart' other than 'new connection' will hide it.
334
 *
335
 * The option can be of 4 things, and will be stored as such:
336
 * new_connection      : Brand new connection - Show
337
 * jumpstart_activated : Jump Start has been activated - dismiss
338
 * jetpack_action_taken: Manual activation of a module already happened - dismiss
339
 * jumpstart_dismissed : Manual dismissal of Jump Start - dismiss
340
 *
341
 * @todo move to functions.global.php when available
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
342
 * @since 3.6
343
 * @return bool | show or hide
344
 */
345
function jetpack_show_jumpstart() {
346
	if ( ! Jetpack::is_active() ) {
347
		return false;
348
	}
349
	$jumpstart_option = Jetpack_Options::get_option( 'jumpstart' );
350
351
	$hide_options = array(
352
		'jumpstart_activated',
353
		'jetpack_action_taken',
354
		'jumpstart_dismissed'
355
	);
356
357
	if ( ! $jumpstart_option || in_array( $jumpstart_option, $hide_options ) ) {
358
		return false;
359
	}
360
361
	return true;
362
}
363
364
/*
365
 * Checks to see if there are any other users available to become primary
366
 * Users must both:
367
 * - Be linked to wpcom
368
 * - Be an admin
369
 *
370
 * @return mixed False if no other users are linked, Int if there are.
371
 */
372 View Code Duplication
function jetpack_get_other_linked_users() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
373
	// If only one admin
374
	$all_users = count_users();
375
	if ( 2 > $all_users['avail_roles']['administrator'] ) {
376
		return false;
377
	}
378
379
	$users = get_users();
380
	$available = array();
381
	// If no one else is linked to dotcom
382
	foreach ( $users as $user ) {
383
		if ( isset( $user->caps['administrator'] ) && Jetpack::is_user_connected( $user->ID ) ) {
384
			$available[] = $user->ID;
385
		}
386
	}
387
388
	if ( 2 > count( $available ) ) {
389
		return false;
390
	}
391
392
	return count( $available );
393
}
394
395
/*
396
 * Gather data about the master user.
397
 *
398
 * @since 4.1.0
399
 *
400
 * @return array
401
 */
402 View Code Duplication
function jetpack_master_user_data() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
403
	$masterID = Jetpack_Options::get_option( 'master_user' );
404
	if ( ! get_user_by( 'id', $masterID ) ) {
405
		return false;
406
	}
407
408
	$jetpack_user = get_userdata( $masterID );
409
	$wpcom_user   = Jetpack::get_connected_user_data( $jetpack_user->ID );
410
	$gravatar     = get_avatar( $jetpack_user->ID, 40 );
411
412
	$master_user_data = array(
413
		'jetpackUser' => $jetpack_user,
414
		'wpcomUser'   => $wpcom_user,
415
		'gravatar'    => $gravatar,
416
	);
417
418
	return $master_user_data;
419
}
420
421
/**
422
 * Gather data about the current user.
423
 *
424
 * @since 4.1.0
425
 *
426
 * @return array
427
 */
428
function jetpack_current_user_data() {
429
	global $current_user;
430
	$is_master_user = $current_user->ID == Jetpack_Options::get_option( 'master_user' );
431
	$dotcom_data    = Jetpack::get_connected_user_data();
432
	// Add connected user gravatar to the returned dotcom_data
433
	$avatar_data = Jetpack::get_avatar_url( $dotcom_data[ 'email' ] );
434
	$dotcom_data[ 'avatar'] = $avatar_data[ 0 ];
435
436
	$current_user_data = array(
437
		'isConnected' => Jetpack::is_user_connected( $current_user->ID ),
438
		'isMaster'    => $is_master_user,
439
		'username'    => $current_user->user_login,
440
		'wpcomUser'   => $dotcom_data,
441
		'gravatar'    => get_avatar( $current_user->ID, 40 ),
442
		'permissions' => array(
443
			'admin_page'         => current_user_can( 'jetpack_admin_page' ),
444
			'connect'            => current_user_can( 'jetpack_connect' ),
445
			'disconnect'         => current_user_can( 'jetpack_disconnect' ),
446
			'manage_modules'     => current_user_can( 'jetpack_manage_modules' ),
447
			'network_admin'      => current_user_can( 'jetpack_network_admin_page' ),
448
			'network_sites_page' => current_user_can( 'jetpack_network_sites_page' ),
449
			'edit_posts'         => current_user_can( 'edit_posts' ),
450
			'manage_options'     => current_user_can( 'manage_options' ),
451
			'view_stats'		 => current_user_can( 'view_stats' ),
452
			'manage_plugins'	 => current_user_can( 'install_plugins' )
453
									&& current_user_can( 'activate_plugins' )
454
									&& current_user_can( 'update_plugins' )
455
									&& current_user_can( 'delete_plugins' ),
456
		),
457
	);
458
459
	return $current_user_data;
460
}
461