Completed
Push — fix/users-not-being-synced-on-... ( 3f7502...fe7bd6 )
by
unknown
51:30 queued 41:55
created

Jetpack_React_Page::page_render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 0
dl 0
loc 12
rs 9.4285
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
		// 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?
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...
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
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...
114 View Code Duplication
	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...
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
		<?php
137
			/** This action is already documented in views/admin/admin-page.php */
138
			do_action( 'jetpack_notices' );
139
		?>
140
		<div id="jp-plugin-container"></div>
141
	<?php }
142
143
	function get_i18n_data() {
144
		$locale_data = @file_get_contents( JETPACK__PLUGIN_DIR . '/languages/json/jetpack-' . get_locale() . '.json' );
145
		if ( $locale_data ) {
146
			return $locale_data;
147
		} else {
148
			return '{}';
149
		}
150
	}
151
152
	/**
153
	 * Gets array of any Jetpack notices that have been dismissed.
154
	 *
155
	 * @since 4.0.1
156
	 * @return mixed|void
157
	 */
158
	function get_dismissed_jetpack_notices() {
159
		$jetpack_dismissed_notices = get_option( 'jetpack_dismissed_notices', array() );
160
		/**
161
		 * Array of notices that have been dismissed.
162
		 *
163
		 * @since 4.0.1
164
		 *
165
		 * @param array $jetpack_dismissed_notices If empty, will not show any Jetpack notices.
166
		 */
167
		$dismissed_notices = apply_filters( 'jetpack_dismissed_notices', $jetpack_dismissed_notices );
168
		return $dismissed_notices;
169
	}
170
171
	function page_admin_scripts() {
172
		if ( $this->is_redirecting ) {
173
			return; // No need for scripts on a fallback page
174
		}
175
176
		// Enqueue jp.js and localize it
177
		wp_enqueue_script( 'react-plugin', plugins_url( '_inc/build/admin.js', JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION, true );
178
		wp_enqueue_style( 'dops-css', plugins_url( '_inc/build/admin.dops-style.css', JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION );
179
		wp_enqueue_style( 'components-css', plugins_url( '_inc/build/style.min.css', JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION );
180
181
		$localeSlug = explode( '_', get_locale() );
182
		$localeSlug = $localeSlug[0];
183
184
		// Collecting roles that can view site stats
185
		$stats_roles = array();
186
		$enabled_roles = function_exists( 'stats_get_option' ) ? stats_get_option( 'roles' ) : array( 'administrator' );
187
		foreach( get_editable_roles() as $slug => $role ) {
188
			$stats_roles[ $slug ] = array(
189
				'name' => translate_user_role( $role['name'] ),
190
				'canView' => in_array( $slug, $enabled_roles, true ),
191
			);
192
		}
193
194
		// Add objects to be passed to the initial state of the app
195
		wp_localize_script( 'react-plugin', 'Initial_State', array(
196
			'WP_API_root' => esc_url_raw( rest_url() ),
197
			'WP_API_nonce' => wp_create_nonce( 'wp_rest' ),
198
			'pluginBaseUrl' => plugins_url( '', JETPACK__PLUGIN_FILE ),
199
			'connectionStatus' => array(
200
				'isActive'  => Jetpack::is_active(),
201
				'isStaging' => Jetpack::is_staging_site(),
202
				'devMode'   => array(
203
					'isActive' => Jetpack::is_development_mode(),
204
					'constant' => defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG,
205
					'url'      => site_url() && false === strpos( site_url(), '.' ),
206
					'filter'   => apply_filters( 'jetpack_development_mode', false ),
207
				),
208
			),
209
			'dismissedNotices' => $this->get_dismissed_jetpack_notices(),
210
			'isDevVersion' => Jetpack::is_development_version(),
211
			'currentVersion' => JETPACK__VERSION,
212
			'happinessGravIds' => jetpack_get_happiness_gravatar_ids(),
213
			'getModules' => Jetpack_Core_Json_Api_Endpoints::get_modules(),
214
			'showJumpstart' => jetpack_show_jumpstart(),
215
			'rawUrl' => Jetpack::build_raw_urls( get_home_url() ),
216
			'adminUrl' => esc_url( admin_url() ),
217
			'stats' => array(
218
				// data is populated asynchronously on page load
219
				'data'  => array(
220
					'general' => false,
221
					'day'     => false,
222
					'week'    => false,
223
					'month'   => false,
224
				),
225
				'roles' => $stats_roles,
226
			),
227
			'settingNames' => array(
228
				'jetpack_holiday_snow_enabled' => function_exists( 'jetpack_holiday_snow_option_name' ) ? jetpack_holiday_snow_option_name() : false,
229
			),
230
			'userData' => array(
231
				'othersLinked' => jetpack_get_other_linked_users(),
232
				'currentUser'  => jetpack_current_user_data(),
233
			),
234
			'locale' => $this->get_i18n_data(),
235
			'localeSlug' => $localeSlug,
236
			'jetpackStateNotices' => array(
237
				'messageCode' => Jetpack::state( 'message' ),
238
				'errorCode' => Jetpack::state( 'error' ),
239
				'errorDescription' => Jetpack::state( 'error_description' ),
240
			),
241
		) );
242
	}
243
}
244
245
/*
246
 * List of happiness Gravatar IDs
247
 *
248
 * @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...
249
 * @since 4.1.0
250
 * @return array
251
 */
252
function jetpack_get_happiness_gravatar_ids() {
253
	return array(
254
		'623f42e878dbd146ddb30ebfafa1375b',
255
		'561be467af56cefa58e02782b7ac7510',
256
		'd8ad409290a6ae7b60f128a0b9a0c1c5',
257
		'790618302648bd80fa8a55497dfd8ac8',
258
		'6e238edcb0664c975ccb9e8e80abb307',
259
		'4e6c84eeab0a1338838a9a1e84629c1a',
260
		'9d4b77080c699629e846d3637b3a661c',
261
		'4626de7797aada973c1fb22dfe0e5109',
262
		'190cf13c9cd358521085af13615382d5',
263
		'f7006d10e9f7dd7bea89a001a2a2fd59',
264
		'16acbc88e7aa65104ed289d736cb9698',
265
		'4d5ad4219c6f676ea1e7d40d2e8860e8',
266
		'e301f7d01b09e7578fdfc1b1ec1bc08d',
267
		'42f4c73f5337486e199f6e3b3910f168',
268
		'e7b26de48e76498cff880abca1eed8da',
269
		'764fb02aaae2ff64c0625c763d82b74e',
270
		'4988305772319fb9bc8fce0a7acb3aa1',
271
		'5d8695c4b81592f1255721d2644627ca',
272
		'0e2249a7de3404bc6d5207a45e911187',
273
	);
274
}
275
276
/*
277
 * Only show Jump Start on first activation.
278
 * Any option 'jumpstart' other than 'new connection' will hide it.
279
 *
280
 * The option can be of 4 things, and will be stored as such:
281
 * new_connection      : Brand new connection - Show
282
 * jumpstart_activated : Jump Start has been activated - dismiss
283
 * jetpack_action_taken: Manual activation of a module already happened - dismiss
284
 * jumpstart_dismissed : Manual dismissal of Jump Start - dismiss
285
 *
286
 * @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...
287
 * @since 3.6
288
 * @return bool | show or hide
289
 */
290
function jetpack_show_jumpstart() {
291
	if ( ! Jetpack::is_active() ) {
292
		return false;
293
	}
294
	$jumpstart_option = Jetpack_Options::get_option( 'jumpstart' );
295
296
	$hide_options = array(
297
		'jumpstart_activated',
298
		'jetpack_action_taken',
299
		'jumpstart_dismissed'
300
	);
301
302
	if ( ! $jumpstart_option || in_array( $jumpstart_option, $hide_options ) ) {
303
		return false;
304
	}
305
306
	return true;
307
}
308
309
/*
310
 * Checks to see if there are any other users available to become primary
311
 * Users must both:
312
 * - Be linked to wpcom
313
 * - Be an admin
314
 *
315
 * @return mixed False if no other users are linked, Int if there are.
316
 */
317 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...
318
	// If only one admin
319
	$all_users = count_users();
320
	if ( 2 > $all_users['avail_roles']['administrator'] ) {
321
		return false;
322
	}
323
324
	$users = get_users();
325
	$available = array();
326
	// If no one else is linked to dotcom
327
	foreach ( $users as $user ) {
328
		if ( isset( $user->caps['administrator'] ) && Jetpack::is_user_connected( $user->ID ) ) {
329
			$available[] = $user->ID;
330
		}
331
	}
332
333
	if ( 2 > count( $available ) ) {
334
		return false;
335
	}
336
337
	return count( $available );
338
}
339
340
/*
341
 * Gather data about the master user.
342
 *
343
 * @since 4.1.0
344
 *
345
 * @return array
346
 */
347 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...
348
	$masterID = Jetpack_Options::get_option( 'master_user' );
349
	if ( ! get_user_by( 'id', $masterID ) ) {
350
		return false;
351
	}
352
353
	$jetpack_user = get_userdata( $masterID );
354
	$wpcom_user   = Jetpack::get_connected_user_data( $jetpack_user->ID );
355
	$gravatar     = get_avatar( $jetpack_user->ID, 40 );
356
357
	$master_user_data = array(
358
		'jetpackUser' => $jetpack_user,
359
		'wpcomUser'   => $wpcom_user,
360
		'gravatar'    => $gravatar,
361
	);
362
363
	return $master_user_data;
364
}
365
366
/*
367
 * Gather data about the current user.
368
 *
369
 * @since 4.1.0
370
 *
371
 * @return array
372
 */
373
function jetpack_current_user_data() {
374
	global $current_user;
375
	$is_master_user = $current_user->ID == Jetpack_Options::get_option( 'master_user' );
376
	$dotcom_data    = Jetpack::get_connected_user_data();
377
378
	$current_user_data = array(
379
		'isConnected' => Jetpack::is_user_connected( $current_user->ID ),
380
		'isMaster'    => $is_master_user,
381
		'username'    => $current_user->user_login,
382
		'wpcomUser'   => $dotcom_data,
383
		'gravatar'    => get_avatar( $current_user->ID, 40 ),
384
		'permissions' => array(
385
			'admin_page'         => current_user_can( 'jetpack_admin_page' ),
386
			'connect'            => current_user_can( 'jetpack_connect' ),
387
			'disconnect'         => current_user_can( 'jetpack_disconnect' ),
388
			'manage_modules'     => current_user_can( 'jetpack_manage_modules' ),
389
			'network_admin'      => current_user_can( 'jetpack_network_admin_page' ),
390
			'network_sites_page' => current_user_can( 'jetpack_network_sites_page' ),
391
			'edit_posts'         => current_user_can( 'edit_posts' ),
392
			'manage_options'     => current_user_can( 'manage_options' ),
393
			'view_stats'		 => current_user_can( 'view_stats' ),
394
		),
395
	);
396
397
	return $current_user_data;
398
}
399