Completed
Push — fix/space-twitter-4455 ( 859b70...d25e05 )
by Jeremy
20:31 queued 11:02
created

Jetpack_React_Page::page_admin_scripts()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 72
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 7
eloc 52
c 3
b 0
f 0
nc 5
nop 0
dl 0
loc 72
rs 6.7427

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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'] );
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(), JETPACK__VERSION, true );
163
		wp_enqueue_style( 'dops-css', plugins_url( '_inc/build/admin.dops-style.css', JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION );
164
		wp_enqueue_style( 'components-css', plugins_url( '_inc/build/style.min.css', JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION );
165
166
		$localeSlug = explode( '_', get_locale() );
167
		$localeSlug = $localeSlug[0];
168
169
		// Collecting roles that can view site stats
170
		$stats_roles = array();
171
		$enabled_roles = function_exists( 'stats_get_option' ) ? stats_get_option( 'roles' ) : array( 'administrator' );
172
		foreach( get_editable_roles() as $slug => $role ) {
173
			$stats_roles[ $slug ] = array(
174
				'name' => translate_user_role( $role['name'] ),
175
				'canView' => in_array( $slug, $enabled_roles, true ),
176
			);
177
		}
178
179
		// Add objects to be passed to the initial state of the app
180
		wp_localize_script( 'react-plugin', 'Initial_State', array(
181
			'WP_API_root' => esc_url_raw( rest_url() ),
182
			'WP_API_nonce' => wp_create_nonce( 'wp_rest' ),
183
			'pluginBaseUrl' => plugins_url( '', JETPACK__PLUGIN_FILE ),
184
			'connectionStatus' => array(
185
				'isActive'  => Jetpack::is_active(),
186
				'isStaging' => Jetpack::is_staging_site(),
187
				'devMode'   => array(
188
					'isActive' => Jetpack::is_development_mode(),
189
					'constant' => defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG,
190
					'url'      => site_url() && false === strpos( site_url(), '.' ),
191
					'filter'   => apply_filters( 'jetpack_development_mode', false ),
192
				),
193
			),
194
			'dismissedNotices' => $this->get_dismissed_jetpack_notices(),
195
			'isDevVersion' => Jetpack::is_development_version(),
196
			'currentVersion' => JETPACK__VERSION,
197
			'happinessGravIds' => jetpack_get_happiness_gravatar_ids(),
198
			'getModules' => Jetpack_Core_Json_Api_Endpoints::get_modules(),
199
			'showJumpstart' => jetpack_show_jumpstart(),
200
			'rawUrl' => Jetpack::build_raw_urls( get_home_url() ),
201
			'adminUrl' => esc_url( admin_url() ),
202
			'stats' => array(
203
				// data is populated asynchronously on page load
204
				'data'  => array(
205
					'general' => false,
206
					'day'     => false,
207
					'week'    => false,
208
					'month'   => false,
209
				),
210
				'roles' => $stats_roles,
211
			),
212
			'settingNames' => array(
213
				'jetpack_holiday_snow_enabled' => function_exists( 'jetpack_holiday_snow_option_name' ) ? jetpack_holiday_snow_option_name() : false,
214
			),
215
			'userData' => array(
216
				'othersLinked' => jetpack_get_other_linked_users(),
217
				'currentUser'  => jetpack_current_user_data(),
218
			),
219
			'locale' => $this->get_i18n_data(),
220
			'localeSlug' => $localeSlug,
221
			'jetpackStateNotices' => array(
222
				'messageCode' => Jetpack::state( 'message' ),
223
				'errorCode' => Jetpack::state( 'error' ),
224
				'errorDescription' => Jetpack::state( 'error_description' ),
225
			),
226
		) );
227
	}
228
}
229
230
/*
231
 * List of happiness Gravatar IDs
232
 *
233
 * @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...
234
 * @since 4.1.0
235
 * @return array
236
 */
237
function jetpack_get_happiness_gravatar_ids() {
238
	return array(
239
		'623f42e878dbd146ddb30ebfafa1375b',
240
		'561be467af56cefa58e02782b7ac7510',
241
		'd8ad409290a6ae7b60f128a0b9a0c1c5',
242
		'790618302648bd80fa8a55497dfd8ac8',
243
		'6e238edcb0664c975ccb9e8e80abb307',
244
		'4e6c84eeab0a1338838a9a1e84629c1a',
245
		'9d4b77080c699629e846d3637b3a661c',
246
		'4626de7797aada973c1fb22dfe0e5109',
247
		'190cf13c9cd358521085af13615382d5',
248
		'f7006d10e9f7dd7bea89a001a2a2fd59',
249
		'16acbc88e7aa65104ed289d736cb9698',
250
		'4d5ad4219c6f676ea1e7d40d2e8860e8',
251
		'e301f7d01b09e7578fdfc1b1ec1bc08d',
252
		'42f4c73f5337486e199f6e3b3910f168',
253
		'e7b26de48e76498cff880abca1eed8da',
254
		'764fb02aaae2ff64c0625c763d82b74e',
255
		'4988305772319fb9bc8fce0a7acb3aa1',
256
		'5d8695c4b81592f1255721d2644627ca',
257
		'0e2249a7de3404bc6d5207a45e911187',
258
	);
259
}
260
261
/*
262
 * Only show Jump Start on first activation.
263
 * Any option 'jumpstart' other than 'new connection' will hide it.
264
 *
265
 * The option can be of 4 things, and will be stored as such:
266
 * new_connection      : Brand new connection - Show
267
 * jumpstart_activated : Jump Start has been activated - dismiss
268
 * jetpack_action_taken: Manual activation of a module already happened - dismiss
269
 * jumpstart_dismissed : Manual dismissal of Jump Start - dismiss
270
 *
271
 * @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...
272
 * @since 3.6
273
 * @return bool | show or hide
274
 */
275
function jetpack_show_jumpstart() {
276
	if ( ! Jetpack::is_active() ) {
277
		return false;
278
	}
279
	$jumpstart_option = Jetpack_Options::get_option( 'jumpstart' );
280
281
	$hide_options = array(
282
		'jumpstart_activated',
283
		'jetpack_action_taken',
284
		'jumpstart_dismissed'
285
	);
286
287
	if ( ! $jumpstart_option || in_array( $jumpstart_option, $hide_options ) ) {
288
		return false;
289
	}
290
291
	return true;
292
}
293
294
/*
295
 * Checks to see if there are any other users available to become primary
296
 * Users must both:
297
 * - Be linked to wpcom
298
 * - Be an admin
299
 *
300
 * @return mixed False if no other users are linked, Int if there are.
301
 */
302 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...
303
	// If only one admin
304
	$all_users = count_users();
305
	if ( 2 > $all_users['avail_roles']['administrator'] ) {
306
		return false;
307
	}
308
309
	$users = get_users();
310
	$available = array();
311
	// If no one else is linked to dotcom
312
	foreach ( $users as $user ) {
313
		if ( isset( $user->caps['administrator'] ) && Jetpack::is_user_connected( $user->ID ) ) {
314
			$available[] = $user->ID;
315
		}
316
	}
317
318
	if ( 2 > count( $available ) ) {
319
		return false;
320
	}
321
322
	return count( $available );
323
}
324
325
/*
326
 * Gather data about the master user.
327
 *
328
 * @since 4.1.0
329
 *
330
 * @return array
331
 */
332 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...
333
	$masterID = Jetpack_Options::get_option( 'master_user' );
334
	if ( ! get_user_by( 'id', $masterID ) ) {
335
		return false;
336
	}
337
338
	$jetpack_user = get_userdata( $masterID );
339
	$wpcom_user   = Jetpack::get_connected_user_data( $jetpack_user->ID );
340
	$gravatar     = get_avatar( $jetpack_user->ID, 40 );
341
342
	$master_user_data = array(
343
		'jetpackUser' => $jetpack_user,
344
		'wpcomUser'   => $wpcom_user,
345
		'gravatar'    => $gravatar,
346
	);
347
348
	return $master_user_data;
349
}
350
351
/*
352
 * Gather data about the current user.
353
 *
354
 * @since 4.1.0
355
 *
356
 * @return array
357
 */
358
function jetpack_current_user_data() {
359
	global $current_user;
360
	$is_master_user = $current_user->ID == Jetpack_Options::get_option( 'master_user' );
361
	$dotcom_data    = Jetpack::get_connected_user_data();
362
363
	$current_user_data = array(
364
		'isConnected' => Jetpack::is_user_connected( $current_user->ID ),
365
		'isMaster'    => $is_master_user,
366
		'username'    => $current_user->user_login,
367
		'wpcomUser'   => $dotcom_data,
368
		'gravatar'    => get_avatar( $current_user->ID, 40 ),
369
		'permissions' => array(
370
			'admin_page'         => current_user_can( 'jetpack_admin_page' ),
371
			'connect'            => current_user_can( 'jetpack_connect' ),
372
			'disconnect'         => current_user_can( 'jetpack_disconnect' ),
373
			'manage_modules'     => current_user_can( 'jetpack_manage_modules' ),
374
			'network_admin'      => current_user_can( 'jetpack_network_admin_page' ),
375
			'network_sites_page' => current_user_can( 'jetpack_network_sites_page' ),
376
			'edit_posts'         => current_user_can( 'edit_posts' ),
377
			'manage_options'     => current_user_can( 'manage_options' ),
378
			'view_stats'		 => current_user_can( 'view_stats' ),
379
		),
380
	);
381
382
	return $current_user_data;
383
}
384