Completed
Push — add/fallback-views ( 111d6f...097491 )
by
unknown
21:40 queued 11:40
created

class.jetpack-react-page.php ➔ jetpack_current_user_data()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 20
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 25
rs 8.8571
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
	protected $dont_show_if_not_active = false;
7
8
	function get_page_hook() {
9
		$title = _x( 'Jetpack', 'The menu item label', 'jetpack' );
10
11
		// Add the main admin Jetpack menu
12
		add_menu_page( 'Jetpack', $title, 'jetpack_admin_page', 'jetpack', array( $this, 'render' ), 'div' );
13
14
		// also create the submenu
15
		return add_submenu_page( 'jetpack', $title, __( 'Dashboard', 'jetpack' ), 'jetpack_admin_page', 'jetpack' );
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
42 View Code Duplication
	function jetpack_menu_order( $menu_order ) {
43
		$jp_menu_order = array();
44
45
		foreach ( $menu_order as $index => $item ) {
46
			if ( $item != 'jetpack' )
47
				$jp_menu_order[] = $item;
48
49
			if ( $index == 0 )
50
				$jp_menu_order[] = 'jetpack';
51
		}
52
53
		return $jp_menu_order;
54
	}
55
56
	function render_old_admin(  ) {
57
		include_once( JETPACK__PLUGIN_DIR . '_inc/header.php' );
58
		include_once( 'jetpack-admin-unsupported.php' );
59
		include_once( JETPACK__PLUGIN_DIR . '_inc/footer.php' );
60
	}
61
62
	function maybe_render_nojs() { ?>
63
		<div id="show-if-no-js" style="display: block;">
64
			<div id="hide-for-now" style="display: none;" >
65
				<?php $this->render_old_admin(); ?>
66
			</div>
67
		</div>
68
		<script>document.getElementById( 'show-if-no-js' ).style.display='none';</script>
69
	<?php }
70
71
	function page_render() {
72
		if ( wp_version_too_old() || isset( $_GET[ 'configure' ] ) ) {
73
			$this->render_old_admin();
74
			return;
75
		} ?>
76
77
		<div id="jp-plugin-container" class="hide-if-no-js"></div>
78
79
		<?php $this->maybe_render_nojs();
80
81
		/** This action is already documented in views/admin/admin-page.php */
82
		do_action( 'jetpack_notices' );
83
	}
84
85
	function get_i18n_data() {
86
		$locale_data = @file_get_contents( JETPACK__PLUGIN_DIR . '/languages/json/jetpack-' . get_locale() . '.json' );
87
		if ( $locale_data ) {
88
			return $locale_data;
89
		} else {
90
			return '{}';
91
		}
92
	}
93
94
	/**
95
	 * Gets array of any Jetpack notices that have been dismissed.
96
	 *
97
	 * @since 4.0.1
98
	 * @return mixed|void
99
	 */
100
	function get_dismissed_jetpack_notices() {
101
		$jetpack_dismissed_notices = get_option( 'jetpack_dismissed_notices', array() );
102
		/**
103
		 * Array of notices that have been dismissed.
104
		 *
105
		 * @since 4.0.1
106
		 *
107
		 * @param array $jetpack_dismissed_notices If empty, will not show any Jetpack notices.
108
		 */
109
		$dismissed_notices = apply_filters( 'jetpack_dismissed_notices', $jetpack_dismissed_notices );
110
		return $dismissed_notices;
111
	}
112
113
	function page_admin_scripts() {
114
		wp_enqueue_style( 'dops-css', plugins_url( '_inc/build/dops-style.css', JETPACK__PLUGIN_FILE ), array(), time() );
115
		wp_enqueue_style( 'components-css', plugins_url( '_inc/build/style.min.css', JETPACK__PLUGIN_FILE ), array(), time() );
116
117
		if ( wp_version_too_old() ) {
118
			return;
119
		}
120
		// Enqueue jp.js and localize it
121
		wp_enqueue_script( 'react-plugin', plugins_url( '_inc/build/admin.js', JETPACK__PLUGIN_FILE ), array(), time(), true );
122
		wp_enqueue_style( 'dops-css', plugins_url( '_inc/build/dops-style.css', JETPACK__PLUGIN_FILE ), array(), time() );
123
		wp_enqueue_style( 'components-css', plugins_url( '_inc/build/style.min.css', JETPACK__PLUGIN_FILE ), array(), time() );
124
125
		$localeSlug = explode( '_', get_locale() );
126
		$localeSlug = $localeSlug[0];
127
128
		// Add objects to be passed to the initial state of the app
129
		wp_localize_script( 'react-plugin', 'Initial_State', array(
130
			'WP_API_root' => esc_url_raw( rest_url() ),
131
			'WP_API_nonce' => wp_create_nonce( 'wp_rest' ),
132
			'pluginBaseUrl' => plugins_url( '', JETPACK__PLUGIN_FILE ),
133
			'connectionStatus' => array(
134
				'isActive'  => Jetpack::is_active(),
135
				'isStaging' => Jetpack::is_staging_site(),
136
				'devMode'   => array(
137
					'isActive' => Jetpack::is_development_mode(),
138
					'constant' => defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG,
139
					'url'      => site_url() && false === strpos( site_url(), '.' ),
140
					'filter'   => apply_filters( 'jetpack_development_mode', false ),
141
				),
142
			),
143
			'dismissedNotices' => $this->get_dismissed_jetpack_notices(),
144
			'isDevVersion' => Jetpack::is_development_version(),
145
			'currentVersion' => JETPACK__VERSION,
146
			'happinessGravIds' => jetpack_get_happiness_gravatar_ids(),
147
			'getModules' => Jetpack_Core_Json_Api_Endpoints::get_modules(),
148
			'showJumpstart' => jetpack_show_jumpstart(),
149
			'rawUrl' => Jetpack::build_raw_urls( get_home_url() ),
150
			'adminUrl' => esc_url( admin_url() ),
151
			'statsData' => build_initial_stats_shape(),
152
			'settingNames' => array(
153
				'jetpack_holiday_snow_enabled' => function_exists( 'jetpack_holiday_snow_option_name' ) ? jetpack_holiday_snow_option_name() : false,
154
			),
155
			'userData' => array(
156
				'othersLinked' => jetpack_get_other_linked_users(),
157
				'currentUser'  => jetpack_current_user_data(),
158
			),
159
			'locale' => $this->get_i18n_data(),
160
			'localeSlug' => $localeSlug,
161
		) );
162
	}
163
}
164
165
/*
166
 * We will render a "basic", non-js page if any of the following:
167
 *
168
 * - WP version <= 4.4
169
 * - JavaScript disabled
170
 *
171
 * @return bool
172
 */
173
function wp_version_too_old() {
174
	global $wp_version;
175
	return version_compare( $wp_version, '4.4-z', '<=' );
176
}
177
178
function build_initial_stats_shape() {
179
	if ( ! function_exists( 'stats_get_from_restapi' ) ) {
180
		require_once( JETPACK__PLUGIN_DIR . 'modules/stats.php' );
181
	}
182
183
	return array(
184
		'general' => stats_get_from_restapi(),
185
		'day' => stats_get_from_restapi( array(), 'visits?unit=day&quantity=30' ),
186
		'week' => stats_get_from_restapi( array(), 'visits?unit=week&quantity=14' ),
187
		'month' => stats_get_from_restapi( array(), 'visits?unit=month&quantity=12&' ),
188
	);
189
}
190
191
/*
192
 * List of happiness Gravatar IDs
193
 *
194
 * @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...
195
 * @since 4.1.0
196
 * @return array
197
 */
198
function jetpack_get_happiness_gravatar_ids() {
199
	return array(
200
		'724cd8eaaa1ef46e4c38c4213ee1d8b7',
201
		'623f42e878dbd146ddb30ebfafa1375b',
202
		'561be467af56cefa58e02782b7ac7510',
203
		'd8ad409290a6ae7b60f128a0b9a0c1c5',
204
		'790618302648bd80fa8a55497dfd8ac8',
205
		'6e238edcb0664c975ccb9e8e80abb307',
206
		'4e6c84eeab0a1338838a9a1e84629c1a',
207
		'9d4b77080c699629e846d3637b3a661c',
208
		'4626de7797aada973c1fb22dfe0e5109',
209
		'190cf13c9cd358521085af13615382d5',
210
		'0d6982875acab8158ccc8b77aa67251a',
211
		'f7006d10e9f7dd7bea89a001a2a2fd59',
212
		'16acbc88e7aa65104ed289d736cb9698',
213
		'4d5ad4219c6f676ea1e7d40d2e8860e8',
214
	);
215
}
216
217
/*
218
 * Only show Jump Start on first activation.
219
 * Any option 'jumpstart' other than 'new connection' will hide it.
220
 *
221
 * The option can be of 4 things, and will be stored as such:
222
 * new_connection      : Brand new connection - Show
223
 * jumpstart_activated : Jump Start has been activated - dismiss
224
 * jetpack_action_taken: Manual activation of a module already happened - dismiss
225
 * jumpstart_dismissed : Manual dismissal of Jump Start - dismiss
226
 *
227
 * @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...
228
 * @since 3.6
229
 * @return bool | show or hide
230
 */
231
function jetpack_show_jumpstart() {
232
	if ( ! Jetpack::is_active() ) {
233
		return false;
234
	}
235
	$jumpstart_option = Jetpack_Options::get_option( 'jumpstart' );
236
237
	$hide_options = array(
238
		'jumpstart_activated',
239
		'jetpack_action_taken',
240
		'jumpstart_dismissed'
241
	);
242
243
	if ( ! $jumpstart_option || in_array( $jumpstart_option, $hide_options ) ) {
244
		return false;
245
	}
246
247
	return true;
248
}
249
250
/*
251
 * Checks to see if there are any other users available to become primary
252
 * Users must both:
253
 * - Be linked to wpcom
254
 * - Be an admin
255
 *
256
 * @return mixed False if no other users are linked, Int if there are.
257
 */
258 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...
259
	// If only one admin
260
	$all_users = count_users();
261
	if ( 2 > $all_users['avail_roles']['administrator'] ) {
262
		return false;
263
	}
264
265
	$users = get_users();
266
	$available = array();
267
	// If no one else is linked to dotcom
268
	foreach ( $users as $user ) {
269
		if ( isset( $user->caps['administrator'] ) && Jetpack::is_user_connected( $user->ID ) ) {
270
			$available[] = $user->ID;
271
		}
272
	}
273
274
	if ( 2 > count( $available ) ) {
275
		return false;
276
	}
277
278
	return count( $available );
279
}
280
281
/*
282
 * Gather data about the master user.
283
 *
284
 * @since 4.1.0
285
 *
286
 * @return array
287
 */
288 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...
289
	$masterID = Jetpack_Options::get_option( 'master_user' );
290
	if ( ! get_user_by( 'id', $masterID ) ) {
291
		return false;
292
	}
293
294
	$jetpack_user = get_userdata( $masterID );
295
	$wpcom_user   = Jetpack::get_connected_user_data( $jetpack_user->ID );
296
	$gravatar     = get_avatar( $jetpack_user->ID, 40 );
297
298
	$master_user_data = array(
299
		'jetpackUser' => $jetpack_user,
300
		'wpcomUser'   => $wpcom_user,
301
		'gravatar'    => $gravatar,
302
	);
303
304
	return $master_user_data;
305
}
306
307
/*
308
 * Gather data about the current user.
309
 *
310
 * @since 4.1.0
311
 *
312
 * @return array
313
 */
314
function jetpack_current_user_data() {
315
	global $current_user;
316
	$is_master_user = $current_user->ID == Jetpack_Options::get_option( 'master_user' );
317
	$dotcom_data    = Jetpack::get_connected_user_data();
318
319
	$current_user_data = array(
320
		'isConnected' => Jetpack::is_user_connected( $current_user->ID ),
321
		'isMaster'    => $is_master_user,
322
		'username'    => $current_user->user_login,
323
		'wpcomUser'   => $dotcom_data,
324
		'gravatar'    => get_avatar( $current_user->ID, 40 ),
325
		'permissions' => array(
326
			'admin_page'         => current_user_can( 'jetpack_admin_page' ),
327
			'connect'            => current_user_can( 'jetpack_connect' ),
328
			'disconnect'         => current_user_can( 'jetpack_disconnect' ),
329
			'manage_modules'     => current_user_can( 'jetpack_manage_modules' ),
330
			'network_admin'      => current_user_can( 'jetpack_network_admin_page' ),
331
			'network_sites_page' => current_user_can( 'jetpack_network_sites_page' ),
332
			'edit_posts'         => current_user_can( 'edit_posts' ),
333
			'manage_options'     => current_user_can( 'manage_options' ),
334
		),
335
	);
336
337
	return $current_user_data;
338
}
339