Completed
Push — add/sync-action ( 45ef1e...ba7a9e )
by
unknown
65:55 queued 56:50
created

Jetpack_React_Page::page_admin_scripts()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 34
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

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