Completed
Push — feature/videopress-uploader ( 46da43...bb1f5c )
by
unknown
22:02 queued 10:48
created

_inc/lib/admin-pages/class.jetpack-react-page.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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?
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
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
		'724cd8eaaa1ef46e4c38c4213ee1d8b7',
189
		'623f42e878dbd146ddb30ebfafa1375b',
190
		'561be467af56cefa58e02782b7ac7510',
191
		'd8ad409290a6ae7b60f128a0b9a0c1c5',
192
		'790618302648bd80fa8a55497dfd8ac8',
193
		'6e238edcb0664c975ccb9e8e80abb307',
194
		'4e6c84eeab0a1338838a9a1e84629c1a',
195
		'9d4b77080c699629e846d3637b3a661c',
196
		'4626de7797aada973c1fb22dfe0e5109',
197
		'190cf13c9cd358521085af13615382d5',
198
		'0d6982875acab8158ccc8b77aa67251a',
199
		'f7006d10e9f7dd7bea89a001a2a2fd59',
200
		'16acbc88e7aa65104ed289d736cb9698',
201
		'4d5ad4219c6f676ea1e7d40d2e8860e8',
202
	);
203
}
204
205
/*
206
 * Only show Jump Start on first activation.
207
 * Any option 'jumpstart' other than 'new connection' will hide it.
208
 *
209
 * The option can be of 4 things, and will be stored as such:
210
 * new_connection      : Brand new connection - Show
211
 * jumpstart_activated : Jump Start has been activated - dismiss
212
 * jetpack_action_taken: Manual activation of a module already happened - dismiss
213
 * jumpstart_dismissed : Manual dismissal of Jump Start - dismiss
214
 *
215
 * @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...
216
 * @since 3.6
217
 * @return bool | show or hide
218
 */
219
function jetpack_show_jumpstart() {
220
	if ( ! Jetpack::is_active() ) {
221
		return false;
222
	}
223
	$jumpstart_option = Jetpack_Options::get_option( 'jumpstart' );
224
225
	$hide_options = array(
226
		'jumpstart_activated',
227
		'jetpack_action_taken',
228
		'jumpstart_dismissed'
229
	);
230
231
	if ( ! $jumpstart_option || in_array( $jumpstart_option, $hide_options ) ) {
232
		return false;
233
	}
234
235
	return true;
236
}
237
238
/*
239
 * Checks to see if there are any other users available to become primary
240
 * Users must both:
241
 * - Be linked to wpcom
242
 * - Be an admin
243
 *
244
 * @return mixed False if no other users are linked, Int if there are.
245
 */
246 View Code Duplication
function jetpack_get_other_linked_users() {
247
	// If only one admin
248
	$all_users = count_users();
249
	if ( 2 > $all_users['avail_roles']['administrator'] ) {
250
		return false;
251
	}
252
253
	$users = get_users();
254
	$available = array();
255
	// If no one else is linked to dotcom
256
	foreach ( $users as $user ) {
257
		if ( isset( $user->caps['administrator'] ) && Jetpack::is_user_connected( $user->ID ) ) {
258
			$available[] = $user->ID;
259
		}
260
	}
261
262
	if ( 2 > count( $available ) ) {
263
		return false;
264
	}
265
266
	return count( $available );
267
}
268
269
/*
270
 * Gather data about the master user.
271
 *
272
 * @since 4.1.0
273
 *
274
 * @return array
275
 */
276 View Code Duplication
function jetpack_master_user_data() {
277
	$masterID = Jetpack_Options::get_option( 'master_user' );
278
	if ( ! get_user_by( 'id', $masterID ) ) {
279
		return false;
280
	}
281
282
	$jetpack_user = get_userdata( $masterID );
283
	$wpcom_user   = Jetpack::get_connected_user_data( $jetpack_user->ID );
284
	$gravatar     = get_avatar( $jetpack_user->ID, 40 );
285
286
	$master_user_data = array(
287
		'jetpackUser' => $jetpack_user,
288
		'wpcomUser'   => $wpcom_user,
289
		'gravatar'    => $gravatar,
290
	);
291
292
	return $master_user_data;
293
}
294
295
/*
296
 * Gather data about the current user.
297
 *
298
 * @since 4.1.0
299
 *
300
 * @return array
301
 */
302
function jetpack_current_user_data() {
303
	global $current_user;
304
	$is_master_user = $current_user->ID == Jetpack_Options::get_option( 'master_user' );
305
	$dotcom_data    = Jetpack::get_connected_user_data();
306
307
	$current_user_data = array(
308
		'isConnected' => Jetpack::is_user_connected( $current_user->ID ),
309
		'isMaster'    => $is_master_user,
310
		'username'    => $current_user->user_login,
311
		'wpcomUser'   => $dotcom_data,
312
		'gravatar'    => get_avatar( $current_user->ID, 40 ),
313
		'permissions' => array(
314
			'admin_page'         => current_user_can( 'jetpack_admin_page' ),
315
			'connect'            => current_user_can( 'jetpack_connect' ),
316
			'disconnect'         => current_user_can( 'jetpack_disconnect' ),
317
			'manage_modules'     => current_user_can( 'jetpack_manage_modules' ),
318
			'network_admin'      => current_user_can( 'jetpack_network_admin_page' ),
319
			'network_sites_page' => current_user_can( 'jetpack_network_sites_page' ),
320
			'edit_posts'         => current_user_can( 'edit_posts' ),
321
			'manage_options'     => current_user_can( 'manage_options' ),
322
		),
323
	);
324
325
	return $current_user_data;
326
}
327