Completed
Push — add/sync-action ( 9c2804...6a3641 )
by
unknown
192:45 queued 183:25
created

Jetpack_React_Page::render_nojs_configurable()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 14
Ratio 100 %

Importance

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