Completed
Push — add/test-query-string-deprecat... ( f99640 )
by
unknown
06:26
created

class.jetpack-react-page.php ➔ jetpack_current_user_data()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 8
nop 0
dl 0
loc 43
rs 8.9208
c 0
b 0
f 0
1
<?php
2
use Automattic\Jetpack\Status;
3
4
include_once( 'class.jetpack-admin-page.php' );
5
6
// Builds the landing page and its menu
7
class Jetpack_React_Page extends Jetpack_Admin_Page {
8
9
	protected $dont_show_if_not_active = false;
10
11
	protected $is_redirecting = false;
12
13
	function get_page_hook() {
14
		// Add the main admin Jetpack menu
15
		return add_menu_page( 'Jetpack', 'Jetpack', 'jetpack_admin_page', 'jetpack', array( $this, 'render' ), 'div' );
16
	}
17
18
	function add_page_actions( $hook ) {
19
		/** This action is documented in class.jetpack.php */
20
		do_action( 'jetpack_admin_menu', $hook );
21
22
		// Place the Jetpack menu item on top and others in the order they appear
23
		add_filter( 'custom_menu_order',         '__return_true' );
24
		add_filter( 'menu_order',                array( $this, 'jetpack_menu_order' ) );
25
26
		if ( ! isset( $_GET['page'] ) || 'jetpack' !== $_GET['page'] ) {
27
			return; // No need to handle the fallback redirection if we are not on the Jetpack page
28
		}
29
30
		// Adding a redirect meta tag if the REST API is disabled
31
		if ( ! $this->is_rest_api_enabled() ) {
32
			$this->is_redirecting = true;
33
			add_action( 'admin_head', array( $this, 'add_fallback_head_meta' ) );
34
		}
35
36
		// Adding a redirect meta tag wrapped in noscript tags for all browsers in case they have JavaScript disabled
37
		add_action( 'admin_head', array( $this, 'add_noscript_head_meta' ) );
38
39
		// If this is the first time the user is viewing the admin, don't show JITMs.
40
		// This filter is added just in time because this function is called on admin_menu
41
		// and JITMs are initialized on admin_init
42
		if ( Jetpack::is_active() && ! Jetpack_Options::get_option( 'first_admin_view', false ) ) {
43
			Jetpack_Options::update_option( 'first_admin_view', true );
44
			add_filter( 'jetpack_just_in_time_msgs', '__return_false' );
45
		}
46
	}
47
48
	/**
49
	 * Add Jetpack Dashboard sub-link and point it to AAG if the user can view stats, manage modules or if Protect is active.
50
	 *
51
	 * Works in Dev Mode or when user is connected.
52
	 *
53
	 * @since 4.3.0
54
	 */
55
	function jetpack_add_dashboard_sub_nav_item() {
56 View Code Duplication
		if ( ( new Status() )->is_development_mode() || Jetpack::is_active() ) {
57
			global $submenu;
58
			if ( current_user_can( 'jetpack_admin_page' ) ) {
59
				$submenu['jetpack'][] = array( __( 'Dashboard', 'jetpack' ), 'jetpack_admin_page', 'admin.php?page=jetpack#/dashboard' );
60
			}
61
		}
62
	}
63
64
	/**
65
	 * If user is allowed to see the Jetpack Admin, add Settings sub-link.
66
	 *
67
	 * @since 4.3.0
68
	 */
69
	function jetpack_add_settings_sub_nav_item() {
70 View Code Duplication
		if ( ( ( new Status() )->is_development_mode() || Jetpack::is_active() ) && current_user_can( 'jetpack_admin_page' ) && current_user_can( 'edit_posts' ) ) {
71
			global $submenu;
72
			$submenu['jetpack'][] = array( __( 'Settings', 'jetpack' ), 'jetpack_admin_page', 'admin.php?page=jetpack#/settings' );
73
		}
74
	}
75
76
	function add_fallback_head_meta() {
77
		echo '<meta http-equiv="refresh" content="0; url=?page=jetpack_modules">';
78
	}
79
80
	function add_noscript_head_meta() {
81
		echo '<noscript>';
82
		$this->add_fallback_head_meta();
83
		echo '</noscript>';
84
	}
85
86 View Code Duplication
	function jetpack_menu_order( $menu_order ) {
87
		$jp_menu_order = array();
88
89
		foreach ( $menu_order as $index => $item ) {
90
			if ( $item != 'jetpack' )
91
				$jp_menu_order[] = $item;
92
93
			if ( $index == 0 )
94
				$jp_menu_order[] = 'jetpack';
95
		}
96
97
		return $jp_menu_order;
98
	}
99
100
	function page_render() {
101
		/** This action is already documented in views/admin/admin-page.php */
102
		do_action( 'jetpack_notices' );
103
104
		// Try fetching by patch
105
		$static_html = @file_get_contents( JETPACK__PLUGIN_DIR . '_inc/build/static.html' );
106
107
		if ( false === $static_html ) {
108
109
			// If we still have nothing, display an error
110
			echo '<p>';
111
			esc_html_e( 'Error fetching static.html. Try running: ', 'jetpack' );
112
			echo '<code>yarn distclean && yarn build</code>';
113
			echo '</p>';
114
		} else {
115
116
			// We got the static.html so let's display it
117
			echo $static_html;
118
			self::render_footer();
119
120
		}
121
	}
122
123
	/**
124
	 * Gets array of any Jetpack notices that have been dismissed.
125
	 *
126
	 * @since 4.0.1
127
	 * @return mixed|void
128
	 */
129
	function get_dismissed_jetpack_notices() {
130
		$jetpack_dismissed_notices = get_option( 'jetpack_dismissed_notices', array() );
131
		/**
132
		 * Array of notices that have been dismissed.
133
		 *
134
		 * @since 4.0.1
135
		 *
136
		 * @param array $jetpack_dismissed_notices If empty, will not show any Jetpack notices.
137
		 */
138
		$dismissed_notices = apply_filters( 'jetpack_dismissed_notices', $jetpack_dismissed_notices );
139
		return $dismissed_notices;
140
	}
141
142
	function additional_styles() {
143
		Jetpack_Admin_Page::load_wrapper_styles();
144
	}
145
146
	function page_admin_scripts() {
147
		if ( $this->is_redirecting ) {
148
			return; // No need for scripts on a fallback page
149
		}
150
151
		$is_development_mode = ( new Status() )->is_development_mode();
152
		$script_deps_path    = JETPACK__PLUGIN_DIR . '_inc/build/admin.asset.php';
153
		$script_dependencies = array( 'wp-polyfill' );
154
		if ( file_exists( $script_deps_path ) ) {
155
			$asset_manifest      = include $script_deps_path;
156
			$script_dependencies = $asset_manifest['dependencies'];
157
		}
158
159
		if ( Jetpack::is_active() || $is_development_mode ) {
160
			wp_enqueue_script(
161
				'react-plugin',
162
				plugins_url( '_inc/build/admin.js', JETPACK__PLUGIN_FILE ),
163
				$script_dependencies,
164
				JETPACK__VERSION,
165
				true
166
			);
167
		}
168
169
170
		if ( ! $is_development_mode && Jetpack::is_active() ) {
171
			// Required for Analytics.
172
			wp_enqueue_script( 'jp-tracks', '//stats.wp.com/w.js', array(), gmdate( 'YW' ), true );
173
		}
174
175
		// Add objects to be passed to the initial state of the app.
176
		wp_localize_script( 'react-plugin', 'Initial_State', $this->get_initial_state() );
177
	}
178
179
	function get_initial_state() {
180
		// Load API endpoint base classes and endpoints for getting the module list fed into the JS Admin Page
181
		require_once JETPACK__PLUGIN_DIR . '_inc/lib/core-api/class.jetpack-core-api-xmlrpc-consumer-endpoint.php';
182
		require_once JETPACK__PLUGIN_DIR . '_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php';
183
		$moduleListEndpoint = new Jetpack_Core_API_Module_List_Endpoint();
184
		$modules = $moduleListEndpoint->get_modules();
185
186
		// Preparing translated fields for JSON encoding by transforming all HTML entities to
187
		// respective characters.
188
		foreach( $modules as $slug => $data ) {
0 ignored issues
show
Bug introduced by
The expression $modules of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
189
			$modules[ $slug ]['name'] = html_entity_decode( $data['name'] );
190
			$modules[ $slug ]['description'] = html_entity_decode( $data['description'] );
191
			$modules[ $slug ]['short_description'] = html_entity_decode( $data['short_description'] );
192
			$modules[ $slug ]['long_description'] = html_entity_decode( $data['long_description'] );
193
		}
194
195
		// Collecting roles that can view site stats.
196
		$stats_roles = array();
197
		$enabled_roles = function_exists( 'stats_get_option' ) ? stats_get_option( 'roles' ) : array( 'administrator' );
198
199
		if ( ! function_exists( 'get_editable_roles' ) ) {
200
			require_once ABSPATH . 'wp-admin/includes/user.php';
201
		}
202
		foreach ( get_editable_roles() as $slug => $role ) {
203
			$stats_roles[ $slug ] = array(
204
				'name' => translate_user_role( $role['name'] ),
205
				'canView' => is_array( $enabled_roles ) ? in_array( $slug, $enabled_roles, true ) : false,
206
			);
207
		}
208
209
		// Get information about current theme.
210
		$current_theme = wp_get_theme();
211
212
		// Get all themes that Infinite Scroll provides support for natively.
213
		$inf_scr_support_themes = array();
214
		foreach ( Jetpack::glob_php( JETPACK__PLUGIN_DIR . 'modules/infinite-scroll/themes' ) as $path ) {
215
			if ( is_readable( $path ) ) {
216
				$inf_scr_support_themes[] = basename( $path, '.php' );
217
			}
218
		}
219
220
		// Get last post, to build the link to Customizer in the Related Posts module.
221
		$last_post = get_posts( array( 'posts_per_page' => 1 ) );
222
		$last_post = isset( $last_post[0] ) && $last_post[0] instanceof WP_Post
0 ignored issues
show
Bug introduced by
The class WP_Post does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
223
			? get_permalink( $last_post[0]->ID )
224
			: get_home_url();
225
226
		// Ensure that class to get the affiliate code is loaded
227
		if ( ! class_exists( 'Jetpack_Affiliate' ) ) {
228
			require_once JETPACK__PLUGIN_DIR . 'class.jetpack-affiliate.php';
229
		}
230
231
		$current_user_data = jetpack_current_user_data();
232
233
		return array(
234
			'WP_API_root' => esc_url_raw( rest_url() ),
235
			'WP_API_nonce' => wp_create_nonce( 'wp_rest' ),
236
			'pluginBaseUrl' => plugins_url( '', JETPACK__PLUGIN_FILE ),
237
			'connectionStatus' => array(
238
				'isActive'  => Jetpack::is_active(),
239
				'isStaging' => Jetpack::is_staging_site(),
240
				'devMode'   => array(
241
					'isActive' => ( new Status() )->is_development_mode(),
242
					'constant' => defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG,
243
					'url'      => site_url() && false === strpos( site_url(), '.' ),
244
					'filter'   => apply_filters( 'jetpack_development_mode', false ),
245
				),
246
				'isPublic'	=> '1' == get_option( 'blog_public' ),
247
				'isInIdentityCrisis' => Jetpack::validate_sync_error_idc_option(),
248
				'sandboxDomain' => JETPACK__SANDBOX_DOMAIN,
249
			),
250
			'connectUrl' => $current_user_data['isConnected'] == false ? Jetpack::init()->build_connect_url( true, false, false ) : '',
251
			'dismissedNotices' => $this->get_dismissed_jetpack_notices(),
252
			'isDevVersion' => Jetpack::is_development_version(),
253
			'currentVersion' => JETPACK__VERSION,
254
			'is_gutenberg_available' => true,
255
			'getModules' => $modules,
256
			'rawUrl' => Jetpack::build_raw_urls( get_home_url() ),
257
			'adminUrl' => esc_url( admin_url() ),
258
			'stats' => array(
259
				// data is populated asynchronously on page load
260
				'data'  => array(
261
					'general' => false,
262
					'day'     => false,
263
					'week'    => false,
264
					'month'   => false,
265
				),
266
				'roles' => $stats_roles,
267
			),
268
			'aff' => Jetpack_Affiliate::init()->get_affiliate_code(),
269
			'settings' => $this->get_flattened_settings( $modules ),
0 ignored issues
show
Bug introduced by
It seems like $modules defined by $moduleListEndpoint->get_modules() on line 184 can also be of type string; however, Jetpack_React_Page::get_flattened_settings() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
270
			'userData' => array(
271
//				'othersLinked' => Jetpack::get_other_linked_admins(),
272
				'currentUser'  => $current_user_data,
273
			),
274
			'siteData' => array(
275
				'icon'                       => has_site_icon()
276
					? apply_filters( 'jetpack_photon_url', get_site_icon_url(), array( 'w' => 64 ) )
277
					: '',
278
				'siteVisibleToSearchEngines' => '1' == get_option( 'blog_public' ), // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
279
				/**
280
				 * Whether promotions are visible or not.
281
				 *
282
				 * @since 4.8.0
283
				 *
284
				 * @param bool $are_promotions_active Status of promotions visibility. True by default.
285
				 */
286
				'showPromotions'             => apply_filters( 'jetpack_show_promotions', true ),
287
				'isAtomicSite'               => jetpack_is_atomic_site(),
288
				'plan'                       => Jetpack_Plan::get(),
289
				'showBackups'                => Jetpack::show_backups_ui(),
290
				'isMultisite'                => is_multisite(),
291
			),
292
			'themeData' => array(
293
				'name'      => $current_theme->get( 'Name' ),
294
				'hasUpdate' => (bool) get_theme_update_available( $current_theme ),
295
				'support'   => array(
296
					'infinite-scroll' => current_theme_supports( 'infinite-scroll' ) || in_array( $current_theme->get_stylesheet(), $inf_scr_support_themes ),
297
				),
298
			),
299
			'locale' => Jetpack::get_i18n_data_json(),
300
			'localeSlug' => join( '-', explode( '_', get_user_locale() ) ),
301
			'jetpackStateNotices' => array(
302
				'messageCode' => Jetpack::state( 'message' ),
303
				'errorCode' => Jetpack::state( 'error' ),
304
				'errorDescription' => Jetpack::state( 'error_description' ),
305
			),
306
			'tracksUserData' => Jetpack_Tracks_Client::get_connected_user_tracks_identity(),
307
			'currentIp' => function_exists( 'jetpack_protect_get_ip' ) ? jetpack_protect_get_ip() : false,
308
			'lastPostUrl' => esc_url( $last_post ),
309
			'externalServicesConnectUrls' => $this->get_external_services_connect_urls(),
310
			'calypsoEnv' => Jetpack::get_calypso_env(),
311
		);
312
	}
313
314
	function get_external_services_connect_urls() {
315
		$connect_urls = array();
316
		jetpack_require_lib( 'class.jetpack-keyring-service-helper' );
317
		foreach ( Jetpack_Keyring_Service_Helper::$SERVICES as $service_name => $service_info ) {
318
			$connect_urls[ $service_name ] = Jetpack_Keyring_Service_Helper::connect_url( $service_name, $service_info[ 'for' ] );
319
		}
320
		return $connect_urls;
321
	}
322
323
	/**
324
	 * Returns an array of modules and settings both as first class members of the object.
325
	 *
326
	 * @param array $modules the result of an API request to get all modules.
327
	 *
328
	 * @return array flattened settings with modules.
329
	 */
330
	function get_flattened_settings( $modules ) {
331
		$core_api_endpoint = new Jetpack_Core_API_Data();
332
		$settings = $core_api_endpoint->get_all_options();
333
		return $settings->data;
334
	}
335
}
336
337
/**
338
 * Gather data about the current user.
339
 *
340
 * @since 4.1.0
341
 *
342
 * @return array
343
 */
344
function jetpack_current_user_data() {
345
	$current_user   = wp_get_current_user();
346
	$is_master_user = $current_user->ID == Jetpack_Options::get_option( 'master_user' );
347
	$dotcom_data    = Jetpack::get_connected_user_data();
348
349
	// Add connected user gravatar to the returned dotcom_data.
350
	$dotcom_data['avatar'] = ( ! empty( $dotcom_data['email'] ) ?
351
		get_avatar_url(
352
			$dotcom_data['email'],
353
			array(
354
				'size'    => 64,
355
				'default' => 'mysteryman',
356
			)
357
		)
358
		: false );
359
360
	$current_user_data = array(
361
		'isConnected' => Jetpack::is_user_connected( $current_user->ID ),
362
		'isMaster'    => $is_master_user,
363
		'username'    => $current_user->user_login,
364
		'id'          => $current_user->ID,
365
		'wpcomUser'   => $dotcom_data,
366
		'gravatar'    => get_avatar( $current_user->ID, 40, 'mm', '', array( 'force_display' => true ) ),
367
		'permissions' => array(
368
			'admin_page'         => current_user_can( 'jetpack_admin_page' ),
369
			'connect'            => current_user_can( 'jetpack_connect' ),
370
			'disconnect'         => current_user_can( 'jetpack_disconnect' ),
371
			'manage_modules'     => current_user_can( 'jetpack_manage_modules' ),
372
			'network_admin'      => current_user_can( 'jetpack_network_admin_page' ),
373
			'network_sites_page' => current_user_can( 'jetpack_network_sites_page' ),
374
			'edit_posts'         => current_user_can( 'edit_posts' ),
375
			'publish_posts'      => current_user_can( 'publish_posts' ),
376
			'manage_options'     => current_user_can( 'manage_options' ),
377
			'view_stats'		 => current_user_can( 'view_stats' ),
378
			'manage_plugins'	 => current_user_can( 'install_plugins' )
379
									&& current_user_can( 'activate_plugins' )
380
									&& current_user_can( 'update_plugins' )
381
									&& current_user_can( 'delete_plugins' ),
382
		),
383
	);
384
385
	return $current_user_data;
386
}
387