Completed
Push — master-stable ( 20f0b5...af901c )
by
unknown
65:42 queued 56:07
created

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