Completed
Push — add/sso-analytics ( 683a91...e67d7d )
by
unknown
162:06 queued 152:34
created

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

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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