Completed
Push — add/tracks ( 1c285b )
by
unknown
08:43
created

Jetpack_React_Page::jetpack_get_tracks_user_data()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 6
c 1
b 0
f 1
nc 2
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
	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 jetpack_get_tracks_user_data() {
116
		if ( ! $user_data = Jetpack::get_connected_user_data() ) {
117
			return false;
118
		}
119
120
		return array(
121
			'userid' => $user_data['ID'],
122
			'username' => $user_data['login'],
123
		);
124
	}
125
126
	function page_admin_scripts() {
127
		// Enqueue jp.js and localize it
128
		wp_enqueue_script( 'react-plugin', plugins_url( '_inc/build/admin.js', JETPACK__PLUGIN_FILE ), array(), time(), true );
129
		wp_enqueue_style( 'dops-css', plugins_url( '_inc/build/dops-style.css', JETPACK__PLUGIN_FILE ), array(), time() );
130
		wp_enqueue_style( 'components-css', plugins_url( '_inc/build/style.min.css', JETPACK__PLUGIN_FILE ), array(), time() );
131
132
		// Required for Analytics
133
		wp_enqueue_script( 'jp-tracks', '//stats.wp.com/w.js?48' );
134
135
		$localeSlug = explode( '_', get_locale() );
136
		$localeSlug = $localeSlug[0];
137
138
		// Add objects to be passed to the initial state of the app
139
		wp_localize_script( 'react-plugin', 'Initial_State', array(
140
			'WP_API_root' => esc_url_raw( rest_url() ),
141
			'WP_API_nonce' => wp_create_nonce( 'wp_rest' ),
142
			'pluginBaseUrl' => plugins_url( '', JETPACK__PLUGIN_FILE ),
143
			'connectionStatus' => array(
144
				'isActive'  => Jetpack::is_active(),
145
				'isStaging' => Jetpack::is_staging_site(),
146
				'devMode'   => array(
147
					'isActive' => Jetpack::is_development_mode(),
148
					'constant' => defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG,
149
					'url'      => site_url() && false === strpos( site_url(), '.' ),
150
					'filter'   => apply_filters( 'jetpack_development_mode', false ),
151
				),
152
			),
153
			'dismissedNotices' => $this->get_dismissed_jetpack_notices(),
154
			'isDevVersion' => Jetpack::is_development_version(),
155
			'currentVersion' => JETPACK__VERSION,
156
			'happinessGravIds' => jetpack_get_happiness_gravatar_ids(),
157
			'getModules' => Jetpack_Core_Json_Api_Endpoints::get_modules(),
158
			'showJumpstart' => jetpack_show_jumpstart(),
159
			'rawUrl' => Jetpack::build_raw_urls( get_home_url() ),
160
			'adminUrl' => esc_url( admin_url() ),
161
			'statsData' => build_initial_stats_shape(),
162
			'settingNames' => array(
163
				'jetpack_holiday_snow_enabled' => function_exists( 'jetpack_holiday_snow_option_name' ) ? jetpack_holiday_snow_option_name() : false,
164
			),
165
			'userData' => array(
166
				'othersLinked' => jetpack_get_other_linked_users(),
167
				'currentUser'  => jetpack_current_user_data(),
168
			),
169
			'locale' => $this->get_i18n_data(),
170
			'localeSlug' => $localeSlug,
171
			'tracksUserData' => $this->jetpack_get_tracks_user_data(),
172
		) );
173
	}
174
}
175
176
function build_initial_stats_shape() {
177
	if ( ! function_exists( 'stats_get_from_restapi' ) ) {
178
		require_once( JETPACK__PLUGIN_DIR . 'modules/stats.php' );
179
	}
180
181
	return array(
182
		'general' => stats_get_from_restapi(),
183
		'day' => stats_get_from_restapi( array(), 'visits?unit=day&quantity=30' ),
184
		'week' => stats_get_from_restapi( array(), 'visits?unit=week&quantity=14' ),
185
		'month' => stats_get_from_restapi( array(), 'visits?unit=month&quantity=12&' ),
186
	);
187
}
188
189
/*
190
 * List of happiness Gravatar IDs
191
 *
192
 * @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...
193
 * @since 4.1.0
194
 * @return array
195
 */
196
function jetpack_get_happiness_gravatar_ids() {
197
	return array(
198
		'724cd8eaaa1ef46e4c38c4213ee1d8b7',
199
		'623f42e878dbd146ddb30ebfafa1375b',
200
		'561be467af56cefa58e02782b7ac7510',
201
		'd8ad409290a6ae7b60f128a0b9a0c1c5',
202
		'790618302648bd80fa8a55497dfd8ac8',
203
		'6e238edcb0664c975ccb9e8e80abb307',
204
		'4e6c84eeab0a1338838a9a1e84629c1a',
205
		'9d4b77080c699629e846d3637b3a661c',
206
		'4626de7797aada973c1fb22dfe0e5109',
207
		'190cf13c9cd358521085af13615382d5',
208
		'0d6982875acab8158ccc8b77aa67251a',
209
		'f7006d10e9f7dd7bea89a001a2a2fd59',
210
		'16acbc88e7aa65104ed289d736cb9698',
211
		'4d5ad4219c6f676ea1e7d40d2e8860e8',
212
	);
213
}
214
215
/*
216
 * Only show Jump Start on first activation.
217
 * Any option 'jumpstart' other than 'new connection' will hide it.
218
 *
219
 * The option can be of 4 things, and will be stored as such:
220
 * new_connection      : Brand new connection - Show
221
 * jumpstart_activated : Jump Start has been activated - dismiss
222
 * jetpack_action_taken: Manual activation of a module already happened - dismiss
223
 * jumpstart_dismissed : Manual dismissal of Jump Start - dismiss
224
 *
225
 * @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...
226
 * @since 3.6
227
 * @return bool | show or hide
228
 */
229
function jetpack_show_jumpstart() {
230
	if ( ! Jetpack::is_active() ) {
231
		return false;
232
	}
233
	$jumpstart_option = Jetpack_Options::get_option( 'jumpstart' );
234
235
	$hide_options = array(
236
		'jumpstart_activated',
237
		'jetpack_action_taken',
238
		'jumpstart_dismissed'
239
	);
240
241
	if ( ! $jumpstart_option || in_array( $jumpstart_option, $hide_options ) ) {
242
		return false;
243
	}
244
245
	return true;
246
}
247
248
/*
249
 * Checks to see if there are any other users available to become primary
250
 * Users must both:
251
 * - Be linked to wpcom
252
 * - Be an admin
253
 *
254
 * @return mixed False if no other users are linked, Int if there are.
255
 */
256 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...
257
	// If only one admin
258
	$all_users = count_users();
259
	if ( 2 > $all_users['avail_roles']['administrator'] ) {
260
		return false;
261
	}
262
263
	$users = get_users();
264
	$available = array();
265
	// If no one else is linked to dotcom
266
	foreach ( $users as $user ) {
267
		if ( isset( $user->caps['administrator'] ) && Jetpack::is_user_connected( $user->ID ) ) {
268
			$available[] = $user->ID;
269
		}
270
	}
271
272
	if ( 2 > count( $available ) ) {
273
		return false;
274
	}
275
276
	return count( $available );
277
}
278
279
/*
280
 * Gather data about the master user.
281
 *
282
 * @since 4.1.0
283
 *
284
 * @return array
285
 */
286 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...
287
	$masterID = Jetpack_Options::get_option( 'master_user' );
288
	if ( ! get_user_by( 'id', $masterID ) ) {
289
		return false;
290
	}
291
292
	$jetpack_user = get_userdata( $masterID );
293
	$wpcom_user   = Jetpack::get_connected_user_data( $jetpack_user->ID );
294
	$gravatar     = get_avatar( $jetpack_user->ID, 40 );
295
296
	$master_user_data = array(
297
		'jetpackUser' => $jetpack_user,
298
		'wpcomUser'   => $wpcom_user,
299
		'gravatar'    => $gravatar,
300
	);
301
302
	return $master_user_data;
303
}
304
305
/*
306
 * Gather data about the current user.
307
 *
308
 * @since 4.1.0
309
 *
310
 * @return array
311
 */
312
function jetpack_current_user_data() {
313
	global $current_user;
314
	$is_master_user = $current_user->ID == Jetpack_Options::get_option( 'master_user' );
315
	$dotcom_data    = Jetpack::get_connected_user_data();
316
317
	$current_user_data = array(
318
		'isConnected' => Jetpack::is_user_connected( $current_user->ID ),
319
		'isMaster'    => $is_master_user,
320
		'username'    => $current_user->user_login,
321
		'wpcomUser'   => $dotcom_data,
322
		'gravatar'    => get_avatar( $current_user->ID, 40 ),
323
		'permissions' => array(
324
			'admin_page'         => current_user_can( 'jetpack_admin_page' ),
325
			'connect'            => current_user_can( 'jetpack_connect' ),
326
			'disconnect'         => current_user_can( 'jetpack_disconnect' ),
327
			'manage_modules'     => current_user_can( 'jetpack_manage_modules' ),
328
			'network_admin'      => current_user_can( 'jetpack_network_admin_page' ),
329
			'network_sites_page' => current_user_can( 'jetpack_network_sites_page' ),
330
			'edit_posts'         => current_user_can( 'edit_posts' ),
331
			'manage_options'     => current_user_can( 'manage_options' ),
332
		),
333
	);
334
335
	return $current_user_data;
336
}
337