Completed
Push — fix/gulp-env ( ec4107...cf0b47 )
by
unknown
133:51 queued 124:05
created

Jetpack_React_Page   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 205
Duplicated Lines 13.17 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 10
Bugs 2 Features 5
Metric Value
dl 27
loc 205
rs 10
c 10
b 2
f 5
wmc 30
lcom 1
cbo 3

12 Methods

Rating   Name   Duplication   Size   Complexity  
A get_page_hook() 0 6 1
B add_page_actions() 0 37 5
A jetpack_add_dashboard_sub_nav_item() 0 9 4
A jetpack_add_settings_sub_nav_item() 0 5 1
A add_fallback_head_meta() 0 3 1
A add_noscript_head_meta() 0 5 1
A jetpack_menu_order() 13 13 4
A render_nojs_configurable() 14 15 3
A page_render() 0 12 2
A get_i18n_data() 0 8 2
A get_dismissed_jetpack_notices() 0 12 1
B page_admin_scripts() 0 53 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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