Completed
Push — fix/inline-docs-410 ( f96891...63b75c )
by
unknown
43:24 queued 33:40
created

Jetpack_React_Page::page_render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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