Completed
Push — fix/3641-connect-site-network-... ( 8abb64...41619d )
by
unknown
478:50 queued 469:13
created

Jetpack_React_Page   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 111
Duplicated Lines 24.32 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 13
Bugs 1 Features 4
Metric Value
wmc 14
c 13
b 1
f 4
lcom 0
cbo 3
dl 27
loc 111
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A get_page_hook() 0 9 1
A add_page_actions() 0 23 1
A jetpack_menu_order() 13 13 4
A render_nojs_configurable() 14 14 3
A page_render() 0 12 2
B page_admin_scripts() 0 28 3

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
	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 page_admin_scripts() {
88
		// Enqueue jp.js and localize it
89
		wp_enqueue_script( 'react-plugin', plugins_url( '_inc/build/admin.js', JETPACK__PLUGIN_FILE ), array(), time(), true );
90
		wp_enqueue_style( 'dops-css', plugins_url( '_inc/build/dops-style.css', JETPACK__PLUGIN_FILE ), array(), time() );
91
		wp_enqueue_style( 'components-css', plugins_url( '_inc/build/style.min.css', JETPACK__PLUGIN_FILE ), array(), time() );
92
		// Add objects to be passed to the initial state of the app
93
		wp_localize_script( 'react-plugin', 'Initial_State', array(
94
			'WP_API_root' => esc_url_raw( rest_url() ),
95
			'WP_API_nonce' => wp_create_nonce( 'wp_rest' ),
96
			'pluginBaseUrl' => plugins_url( '', JETPACK__PLUGIN_FILE ),
97
			'connectionStatus' => Jetpack::is_development_mode() ? 'dev' : (bool) Jetpack::is_active(),
98
			'isDevVersion' => Jetpack::is_development_version(),
99
			'currentVersion' => JETPACK__VERSION,
100
			'happinessGravIds' => jetpack_get_happiness_gravatar_ids(),
101
			'getModules' => Jetpack_Core_Json_Api_Endpoints::get_modules(),
102
			'showJumpstart' => jetpack_show_jumpstart(),
103
			'rawUrl' => Jetpack::build_raw_urls( get_home_url() ),
104
			'adminUrl' => esc_url( admin_url() ),
105
			'statsData' => build_initial_stats_shape(),
106
			'settingNames' => array(
107
				'jetpack_holiday_snow_enabled' => function_exists( 'jetpack_holiday_snow_option_name' ) ? jetpack_holiday_snow_option_name() : false,
108
			),
109
			'userData' => array(
110
				'othersLinked' => jetpack_get_other_linked_users(),
111
				'currentUser'  => jetpack_current_user_data(),
112
			),
113
		) );
114
	}
115
}
116
117
function build_initial_stats_shape() {
118
	if ( ! function_exists( 'stats_get_from_restapi' ) ) {
119
		require_once( JETPACK__PLUGIN_DIR . 'modules/stats.php' );
120
	}
121
122
	return array(
123
		'general' => stats_get_from_restapi(),
124
		'day' => stats_get_from_restapi( array(), 'visits?unit=day&quantity=30' ),
125
		'week' => stats_get_from_restapi( array(), 'visits?unit=week&quantity=14' ),
126
		'month' => stats_get_from_restapi( array(), 'visits?unit=month&quantity=12&' ),
127
	);
128
}
129
130
/*
131
 * List of happiness Gravatar IDs
132
 *
133
 * @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...
134
 * @since 4.1.0
135
 * @return array
136
 */
137
function jetpack_get_happiness_gravatar_ids() {
138
	return array(
139
		'724cd8eaaa1ef46e4c38c4213ee1d8b7',
140
		'623f42e878dbd146ddb30ebfafa1375b',
141
		'561be467af56cefa58e02782b7ac7510',
142
		'd8ad409290a6ae7b60f128a0b9a0c1c5',
143
		'790618302648bd80fa8a55497dfd8ac8',
144
		'6e238edcb0664c975ccb9e8e80abb307',
145
		'4e6c84eeab0a1338838a9a1e84629c1a',
146
		'9d4b77080c699629e846d3637b3a661c',
147
		'4626de7797aada973c1fb22dfe0e5109',
148
		'190cf13c9cd358521085af13615382d5',
149
		'0d6982875acab8158ccc8b77aa67251a',
150
		'f7006d10e9f7dd7bea89a001a2a2fd59',
151
		'16acbc88e7aa65104ed289d736cb9698',
152
		'4d5ad4219c6f676ea1e7d40d2e8860e8',
153
	);
154
}
155
156
/*
157
 * Only show Jump Start on first activation.
158
 * Any option 'jumpstart' other than 'new connection' will hide it.
159
 *
160
 * The option can be of 4 things, and will be stored as such:
161
 * new_connection      : Brand new connection - Show
162
 * jumpstart_activated : Jump Start has been activated - dismiss
163
 * jetpack_action_taken: Manual activation of a module already happened - dismiss
164
 * jumpstart_dismissed : Manual dismissal of Jump Start - dismiss
165
 *
166
 * @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...
167
 * @since 3.6
168
 * @return bool | show or hide
169
 */
170
function jetpack_show_jumpstart() {
171
	if ( ! Jetpack::is_active() ) {
172
		return false;
173
	}
174
	$jumpstart_option = Jetpack_Options::get_option( 'jumpstart' );
175
176
	$hide_options = array(
177
		'jumpstart_activated',
178
		'jetpack_action_taken',
179
		'jumpstart_dismissed'
180
	);
181
182
	if ( ! $jumpstart_option || in_array( $jumpstart_option, $hide_options ) ) {
183
		return false;
184
	}
185
186
	return true;
187
}
188
189
/*
190
 * Checks to see if there are any other users available to become primary
191
 * Users must both:
192
 * - Be linked to wpcom
193
 * - Be an admin
194
 *
195
 * @return mixed False if no other users are linked, Int if there are.
196
 */
197 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...
198
	// If only one admin
199
	$all_users = count_users();
200
	if ( 2 > $all_users['avail_roles']['administrator'] ) {
201
		return false;
202
	}
203
204
	$users = get_users();
205
	$available = array();
206
	// If no one else is linked to dotcom
207
	foreach ( $users as $user ) {
208
		if ( isset( $user->caps['administrator'] ) && Jetpack::is_user_connected( $user->ID ) ) {
209
			$available[] = $user->ID;
210
		}
211
	}
212
213
	if ( 2 > count( $available ) ) {
214
		return false;
215
	}
216
217
	return count( $available );
218
}
219
220
/*
221
 * Gather data about the master user.
222
 *
223
 * @since 4.1.0
224
 *
225
 * @return array
226
 */
227 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...
228
	$masterID = Jetpack_Options::get_option( 'master_user' );
229
	if ( ! get_user_by( 'id', $masterID ) ) {
230
		return false;
231
	}
232
233
	$jetpack_user = get_userdata( $masterID );
234
	$wpcom_user   = Jetpack::get_connected_user_data( $jetpack_user->ID );
235
	$gravatar     = get_avatar( $jetpack_user->ID, 40 );
236
237
	$master_user_data = array(
238
		'jetpackUser' => $jetpack_user,
239
		'wpcomUser'   => $wpcom_user,
240
		'gravatar'    => $gravatar,
241
	);
242
243
	return $master_user_data;
244
}
245
246
/*
247
 * Gather data about the current user.
248
 *
249
 * @since 4.1.0
250
 *
251
 * @return array
252
 */
253
function jetpack_current_user_data() {
254
	global $current_user;
255
	$is_master_user = $current_user->ID == Jetpack_Options::get_option( 'master_user' );
256
	$dotcom_data    = Jetpack::get_connected_user_data();
257
258
	$current_user_data = array(
259
		'isConnected' => Jetpack::is_user_connected( $current_user->ID ),
260
		'isMaster'    => $is_master_user,
261
		'username'    => $current_user->user_login,
262
		'wpcomUser'   => $dotcom_data,
263
		'gravatar'    => get_avatar( $current_user->ID, 40 ),
264
		'permissions' => array(
265
			'admin_page'         => current_user_can( 'jetpack_admin_page' ),
266
			'connect'            => current_user_can( 'jetpack_connect' ),
267
			'disconnect'         => current_user_can( 'jetpack_disconnect' ),
268
			'manage_modules'     => current_user_can( 'jetpack_manage_modules' ),
269
			'network_admin'      => current_user_can( 'jetpack_network_admin_page' ),
270
			'network_sites_page' => current_user_can( 'jetpack_network_sites_page' ),
271
		),
272
	);
273
274
	return $current_user_data;
275
}
276