Completed
Push — add/tracks ( 83fe2c...85e985 )
by
unknown
102:55 queued 93:40
created

add_legacy_browsers_head_script()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 10
rs 9.4285
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
		// Adding a redirect tag wrapped in browser conditional comments
40
		add_action( 'admin_head', array( $this, 'add_legacy_browsers_head_script' ) );
41
	}
42
43
	/**
44
	 * Add Jetpack Dashboard sub-link and point it to AAG if the user can view stats, manage modules or if Protect is active.
45
	 * Otherwise and only if user is allowed to see the Jetpack Admin, the Dashboard sub-link is added but pointed to Apps tab.
46
	 *
47
	 * Works in Dev Mode or when user is connected.
48
	 *
49
	 * @since 4.3
50
	 */
51
	function jetpack_add_dashboard_sub_nav_item() {
52
		if ( Jetpack::is_development_mode() || Jetpack::is_active() ) {
53
			global $submenu;
54
			if ( current_user_can( 'jetpack_manage_modules' ) || Jetpack::is_module_active( 'protect' ) || current_user_can( 'view_stats' ) ) {
55
				$submenu['jetpack'][] = array( __( 'Dashboard', 'jetpack' ), 'jetpack_admin_page', Jetpack::admin_url( 'page=jetpack#/dashboard' ) );
56
			} elseif ( current_user_can( 'jetpack_admin_page' ) ) {
57
				$submenu['jetpack'][] = array( __( 'Dashboard', 'jetpack' ), 'jetpack_admin_page', Jetpack::admin_url( 'page=jetpack#/apps' ) );
58
			}
59
		}
60
	}
61
62
	/**
63
	 * If user is allowed to see the Jetpack Admin, add Settings sub-link.
64
	 *
65
	 * @since 4.3
66
	 */
67
	function jetpack_add_settings_sub_nav_item() {
68
		if ( ( Jetpack::is_development_mode() || Jetpack::is_active() ) && current_user_can( 'jetpack_admin_page' ) ) {
69
			global $submenu;
70
			$submenu['jetpack'][] = array( __( 'Settings', 'jetpack' ), 'jetpack_admin_page', Jetpack::admin_url( 'page=jetpack#/settings' ) );
71
		}
72
	}
73
74
	function add_fallback_head_meta() {
75
		echo '<meta http-equiv="refresh" content="0; url=?page=jetpack_modules">';
76
	}
77
78
	function add_noscript_head_meta() {
79
		echo '<noscript>';
80
		$this->add_fallback_head_meta();
81
		echo '</noscript>';
82
	}
83
84
	function add_legacy_browsers_head_script() {
85
		echo
86
			"<script type=\"text/javascript\">\n"
87
			. "/*@cc_on\n"
88
			. "if ( @_jscript_version <= 10) {\n"
89
			. "window.location.href = '?page=jetpack_modules';\n"
90
			. "}\n"
91
			. "@*/\n"
92
			. "</script>";
93
	}
94
95 View Code Duplication
	function jetpack_menu_order( $menu_order ) {
96
		$jp_menu_order = array();
97
98
		foreach ( $menu_order as $index => $item ) {
99
			if ( $item != 'jetpack' )
100
				$jp_menu_order[] = $item;
101
102
			if ( $index == 0 )
103
				$jp_menu_order[] = 'jetpack';
104
		}
105
106
		return $jp_menu_order;
107
	}
108
109
	// Render the configuration page for the module if it exists and an error
110
	// screen if the module is not configurable
111
	// @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...
112
	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...
113
		$module_name = preg_replace( '/[^\da-z\-]+/', '', $_GET['configure'] );
114
115
		include_once( JETPACK__PLUGIN_DIR . '_inc/header.php' );
116
		echo '<div class="wrap configure-module">';
117
118
		if ( Jetpack::is_module( $module_name ) && current_user_can( 'jetpack_configure_modules' ) ) {
119
			Jetpack::admin_screen_configure_module( $module_name );
120
		} else {
121
			echo '<h2>' . esc_html__( 'Error, bad module.', 'jetpack' ) . '</h2>';
122
		}
123
124
		echo '</div><!-- /wrap -->';
125
	}
126
127
	function page_render() {
128
		// Handle redirects to configuration pages
129
		if ( ! empty( $_GET['configure'] ) ) {
130
			return $this->render_nojs_configurable( $_GET['configure'] );
131
		}
132
133
		/** This action is already documented in views/admin/admin-page.php */
134
		do_action( 'jetpack_notices' );
135
136
		echo file_get_contents( JETPACK__PLUGIN_DIR . '/_inc/build/static.html' );
137
	}
138
139
	function get_i18n_data() {
140
		$locale_data = @file_get_contents( JETPACK__PLUGIN_DIR . '/languages/json/jetpack-' . get_locale() . '.json' );
141
		if ( $locale_data ) {
142
			return $locale_data;
143
		} else {
144
			return '{}';
145
		}
146
	}
147
148
	/**
149
	 * Gets array of any Jetpack notices that have been dismissed.
150
	 *
151
	 * @since 4.0.1
152
	 * @return mixed|void
153
	 */
154
	function get_dismissed_jetpack_notices() {
155
		$jetpack_dismissed_notices = get_option( 'jetpack_dismissed_notices', array() );
156
		/**
157
		 * Array of notices that have been dismissed.
158
		 *
159
		 * @since 4.0.1
160
		 *
161
		 * @param array $jetpack_dismissed_notices If empty, will not show any Jetpack notices.
162
		 */
163
		$dismissed_notices = apply_filters( 'jetpack_dismissed_notices', $jetpack_dismissed_notices );
164
		return $dismissed_notices;
165
	}
166
167
	function jetpack_get_tracks_user_data() {
168
		if ( ! $user_data = Jetpack::get_connected_user_data() ) {
169
			return false;
170
		}
171
172
		return array(
173
			'userid' => $user_data['ID'],
174
			'username' => $user_data['login'],
175
		);
176
	}
177
178
	function page_admin_scripts() {
179
		if ( $this->is_redirecting ) {
180
			return; // No need for scripts on a fallback page
181
		}
182
183
		$rtl = is_rtl() ? '.rtl' : '';
184
185
		// Enqueue jp.js and localize it
186
		wp_enqueue_script( 'react-plugin', plugins_url( '_inc/build/admin.js', JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION, true );
187
		wp_enqueue_style( 'dops-css', plugins_url( "_inc/build/admin.dops-style$rtl.css", JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION );
188
		wp_enqueue_style( 'components-css', plugins_url( "_inc/build/style.min$rtl.css", JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION );
189
190
		// Required for Analytics
191
		wp_enqueue_script( 'jp-tracks', '//stats.wp.com/w.js?48' );
192
193
		$localeSlug = explode( '_', get_locale() );
194
		$localeSlug = $localeSlug[0];
195
196
		// Collecting roles that can view site stats
197
		$stats_roles = array();
198
		$enabled_roles = function_exists( 'stats_get_option' ) ? stats_get_option( 'roles' ) : array( 'administrator' );
199
		foreach( get_editable_roles() as $slug => $role ) {
200
			$stats_roles[ $slug ] = array(
201
				'name' => translate_user_role( $role['name'] ),
202
				'canView' => in_array( $slug, $enabled_roles, true ),
203
			);
204
		}
205
206
		// Add objects to be passed to the initial state of the app
207
		wp_localize_script( 'react-plugin', 'Initial_State', array(
208
			'WP_API_root' => esc_url_raw( rest_url() ),
209
			'WP_API_nonce' => wp_create_nonce( 'wp_rest' ),
210
			'pluginBaseUrl' => plugins_url( '', JETPACK__PLUGIN_FILE ),
211
			'connectionStatus' => array(
212
				'isActive'  => Jetpack::is_active(),
213
				'isStaging' => Jetpack::is_staging_site(),
214
				'devMode'   => array(
215
					'isActive' => Jetpack::is_development_mode(),
216
					'constant' => defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG,
217
					'url'      => site_url() && false === strpos( site_url(), '.' ),
218
					'filter'   => apply_filters( 'jetpack_development_mode', false ),
219
				),
220
				'isPublic'	=> '1' == get_option( 'blog_public' ),
221
			),
222
			'dismissedNotices' => $this->get_dismissed_jetpack_notices(),
223
			'isDevVersion' => Jetpack::is_development_version(),
224
			'currentVersion' => JETPACK__VERSION,
225
			'happinessGravIds' => jetpack_get_happiness_gravatar_ids(),
226
			'getModules' => Jetpack_Core_Json_Api_Endpoints::get_modules(),
227
			'showJumpstart' => jetpack_show_jumpstart(),
228
			'rawUrl' => Jetpack::build_raw_urls( get_home_url() ),
229
			'adminUrl' => esc_url( admin_url() ),
230
			'stats' => array(
231
				// data is populated asynchronously on page load
232
				'data'  => array(
233
					'general' => false,
234
					'day'     => false,
235
					'week'    => false,
236
					'month'   => false,
237
				),
238
				'roles' => $stats_roles,
239
			),
240
			'settingNames' => array(
241
				'jetpack_holiday_snow_enabled' => function_exists( 'jetpack_holiday_snow_option_name' ) ? jetpack_holiday_snow_option_name() : false,
242
			),
243
			'userData' => array(
244
				'othersLinked' => jetpack_get_other_linked_users(),
245
				'currentUser'  => jetpack_current_user_data(),
246
			),
247
			'locale' => $this->get_i18n_data(),
248
			'localeSlug' => $localeSlug,
249
			'jetpackStateNotices' => array(
250
				'messageCode' => Jetpack::state( 'message' ),
251
				'errorCode' => Jetpack::state( 'error' ),
252
				'errorDescription' => Jetpack::state( 'error_description' ),
253
			),
254
			'tracksUserData' => $this->jetpack_get_tracks_user_data(),
255
		) );
256
	}
257
}
258
259
/*
260
 * List of happiness Gravatar IDs
261
 *
262
 * @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...
263
 * @since 4.1.0
264
 * @return array
265
 */
266
function jetpack_get_happiness_gravatar_ids() {
267
	return array(
268
		'623f42e878dbd146ddb30ebfafa1375b',
269
		'561be467af56cefa58e02782b7ac7510',
270
		'd8ad409290a6ae7b60f128a0b9a0c1c5',
271
		'790618302648bd80fa8a55497dfd8ac8',
272
		'6e238edcb0664c975ccb9e8e80abb307',
273
		'4e6c84eeab0a1338838a9a1e84629c1a',
274
		'9d4b77080c699629e846d3637b3a661c',
275
		'4626de7797aada973c1fb22dfe0e5109',
276
		'190cf13c9cd358521085af13615382d5',
277
		'f7006d10e9f7dd7bea89a001a2a2fd59',
278
		'16acbc88e7aa65104ed289d736cb9698',
279
		'4d5ad4219c6f676ea1e7d40d2e8860e8',
280
		'e301f7d01b09e7578fdfc1b1ec1bc08d',
281
		'42f4c73f5337486e199f6e3b3910f168',
282
		'e7b26de48e76498cff880abca1eed8da',
283
		'764fb02aaae2ff64c0625c763d82b74e',
284
		'4988305772319fb9bc8fce0a7acb3aa1',
285
		'5d8695c4b81592f1255721d2644627ca',
286
		'0e2249a7de3404bc6d5207a45e911187',
287
	);
288
}
289
290
/*
291
 * Only show Jump Start on first activation.
292
 * Any option 'jumpstart' other than 'new connection' will hide it.
293
 *
294
 * The option can be of 4 things, and will be stored as such:
295
 * new_connection      : Brand new connection - Show
296
 * jumpstart_activated : Jump Start has been activated - dismiss
297
 * jetpack_action_taken: Manual activation of a module already happened - dismiss
298
 * jumpstart_dismissed : Manual dismissal of Jump Start - dismiss
299
 *
300
 * @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...
301
 * @since 3.6
302
 * @return bool | show or hide
303
 */
304
function jetpack_show_jumpstart() {
305
	if ( ! Jetpack::is_active() ) {
306
		return false;
307
	}
308
	$jumpstart_option = Jetpack_Options::get_option( 'jumpstart' );
309
310
	$hide_options = array(
311
		'jumpstart_activated',
312
		'jetpack_action_taken',
313
		'jumpstart_dismissed'
314
	);
315
316
	if ( ! $jumpstart_option || in_array( $jumpstart_option, $hide_options ) ) {
317
		return false;
318
	}
319
320
	return true;
321
}
322
323
/*
324
 * Checks to see if there are any other users available to become primary
325
 * Users must both:
326
 * - Be linked to wpcom
327
 * - Be an admin
328
 *
329
 * @return mixed False if no other users are linked, Int if there are.
330
 */
331 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...
332
	// If only one admin
333
	$all_users = count_users();
334
	if ( 2 > $all_users['avail_roles']['administrator'] ) {
335
		return false;
336
	}
337
338
	$users = get_users();
339
	$available = array();
340
	// If no one else is linked to dotcom
341
	foreach ( $users as $user ) {
342
		if ( isset( $user->caps['administrator'] ) && Jetpack::is_user_connected( $user->ID ) ) {
343
			$available[] = $user->ID;
344
		}
345
	}
346
347
	if ( 2 > count( $available ) ) {
348
		return false;
349
	}
350
351
	return count( $available );
352
}
353
354
/*
355
 * Gather data about the master user.
356
 *
357
 * @since 4.1.0
358
 *
359
 * @return array
360
 */
361 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...
362
	$masterID = Jetpack_Options::get_option( 'master_user' );
363
	if ( ! get_user_by( 'id', $masterID ) ) {
364
		return false;
365
	}
366
367
	$jetpack_user = get_userdata( $masterID );
368
	$wpcom_user   = Jetpack::get_connected_user_data( $jetpack_user->ID );
369
	$gravatar     = get_avatar( $jetpack_user->ID, 40 );
370
371
	$master_user_data = array(
372
		'jetpackUser' => $jetpack_user,
373
		'wpcomUser'   => $wpcom_user,
374
		'gravatar'    => $gravatar,
375
	);
376
377
	return $master_user_data;
378
}
379
380
/*
381
 * Gather data about the current user.
382
 *
383
 * @since 4.1.0
384
 *
385
 * @return array
386
 */
387
function jetpack_current_user_data() {
388
	global $current_user;
389
	$is_master_user = $current_user->ID == Jetpack_Options::get_option( 'master_user' );
390
	$dotcom_data    = Jetpack::get_connected_user_data();
391
392
	$current_user_data = array(
393
		'isConnected' => Jetpack::is_user_connected( $current_user->ID ),
394
		'isMaster'    => $is_master_user,
395
		'username'    => $current_user->user_login,
396
		'wpcomUser'   => $dotcom_data,
397
		'gravatar'    => get_avatar( $current_user->ID, 40 ),
398
		'permissions' => array(
399
			'admin_page'         => current_user_can( 'jetpack_admin_page' ),
400
			'connect'            => current_user_can( 'jetpack_connect' ),
401
			'disconnect'         => current_user_can( 'jetpack_disconnect' ),
402
			'manage_modules'     => current_user_can( 'jetpack_manage_modules' ),
403
			'network_admin'      => current_user_can( 'jetpack_network_admin_page' ),
404
			'network_sites_page' => current_user_can( 'jetpack_network_sites_page' ),
405
			'edit_posts'         => current_user_can( 'edit_posts' ),
406
			'manage_options'     => current_user_can( 'manage_options' ),
407
			'view_stats'		 => current_user_can( 'view_stats' ),
408
		),
409
	);
410
411
	return $current_user_data;
412
}
413