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
|
|
|
|
7
|
|
|
protected $dont_show_if_not_active = false; |
8
|
|
|
|
9
|
|
|
protected $is_redirecting = false; |
10
|
|
|
|
11
|
|
|
function get_page_hook() { |
12
|
|
|
// Add the main admin Jetpack menu |
13
|
|
|
return add_menu_page( 'Jetpack', 'Jetpack', 'jetpack_admin_page', 'jetpack', array( $this, 'render' ), 'div' ); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
function add_page_actions( $hook ) { |
17
|
|
|
/** This action is documented in class.jetpack.php */ |
18
|
|
|
do_action( 'jetpack_admin_menu', $hook ); |
19
|
|
|
|
20
|
|
|
// Place the Jetpack menu item on top and others in the order they appear |
21
|
|
|
add_filter( 'custom_menu_order', '__return_true' ); |
22
|
|
|
add_filter( 'menu_order', array( $this, 'jetpack_menu_order' ) ); |
23
|
|
|
|
24
|
|
|
if ( ! isset( $_GET['page'] ) || 'jetpack' !== $_GET['page'] ) { |
25
|
|
|
return; // No need to handle the fallback redirection if we are not on the Jetpack page |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
// Adding a redirect meta tag if the REST API is disabled |
29
|
|
|
if ( ! $this->is_rest_api_enabled() ) { |
30
|
|
|
$this->is_redirecting = true; |
31
|
|
|
add_action( 'admin_head', array( $this, 'add_fallback_head_meta' ) ); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
// Adding a redirect meta tag wrapped in noscript tags for all browsers in case they have JavaScript disabled |
35
|
|
|
add_action( 'admin_head', array( $this, 'add_noscript_head_meta' ) ); |
36
|
|
|
|
37
|
|
|
// If this is the first time the user is viewing the admin, don't show JITMs. |
38
|
|
|
// This filter is added just in time because this function is called on admin_menu |
39
|
|
|
// and JITMs are initialized on admin_init |
40
|
|
|
if ( Jetpack::is_active() && ! Jetpack_Options::get_option( 'first_admin_view', false ) ) { |
41
|
|
|
Jetpack_Options::update_option( 'first_admin_view', true ); |
42
|
|
|
add_filter( 'jetpack_just_in_time_msgs', '__return_false' ); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Add Jetpack Dashboard sub-link and point it to AAG if the user can view stats, manage modules or if Protect is active. |
48
|
|
|
* |
49
|
|
|
* Works in Dev Mode or when user is connected. |
50
|
|
|
* |
51
|
|
|
* @since 4.3.0 |
52
|
|
|
*/ |
53
|
|
|
function jetpack_add_dashboard_sub_nav_item() { |
54
|
|
View Code Duplication |
if ( Jetpack::is_development_mode() || Jetpack::is_active() ) { |
55
|
|
|
global $submenu; |
56
|
|
|
if ( current_user_can( 'jetpack_admin_page' ) ) { |
57
|
|
|
$submenu['jetpack'][] = array( __( 'Dashboard', 'jetpack' ), 'jetpack_admin_page', 'admin.php?page=jetpack#/dashboard' ); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* If user is allowed to see the Jetpack Admin, add Settings sub-link. |
64
|
|
|
* |
65
|
|
|
* @since 4.3.0 |
66
|
|
|
*/ |
67
|
|
|
function jetpack_add_settings_sub_nav_item() { |
68
|
|
View Code Duplication |
if ( ( Jetpack::is_development_mode() || Jetpack::is_active() ) && current_user_can( 'jetpack_admin_page' ) && current_user_can( 'edit_posts' ) ) { |
69
|
|
|
global $submenu; |
70
|
|
|
$submenu['jetpack'][] = array( __( 'Settings', 'jetpack' ), 'jetpack_admin_page', 'admin.php?page=jetpack#/settings' ); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
function add_fallback_head_meta() { |
75
|
|
|
echo '<meta http-equiv="refresh" content="0; url=?page=jetpack_modules">'; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
function add_noscript_head_meta() { |
79
|
|
|
echo '<noscript>'; |
80
|
|
|
$this->add_fallback_head_meta(); |
81
|
|
|
echo '</noscript>'; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
View Code Duplication |
function jetpack_menu_order( $menu_order ) { |
85
|
|
|
$jp_menu_order = array(); |
86
|
|
|
|
87
|
|
|
foreach ( $menu_order as $index => $item ) { |
88
|
|
|
if ( $item != 'jetpack' ) |
89
|
|
|
$jp_menu_order[] = $item; |
90
|
|
|
|
91
|
|
|
if ( $index == 0 ) |
92
|
|
|
$jp_menu_order[] = 'jetpack'; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $jp_menu_order; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
function page_render() { |
99
|
|
|
/** This action is already documented in views/admin/admin-page.php */ |
100
|
|
|
do_action( 'jetpack_notices' ); |
101
|
|
|
|
102
|
|
|
// Try fetching by patch |
103
|
|
|
$static_html = @file_get_contents( JETPACK__PLUGIN_DIR . '_inc/build/static.html' ); |
104
|
|
|
|
105
|
|
|
if ( false === $static_html ) { |
106
|
|
|
|
107
|
|
|
// If we still have nothing, display an error |
108
|
|
|
echo '<p>'; |
109
|
|
|
esc_html_e( 'Error fetching static.html. Try running: ', 'jetpack' ); |
110
|
|
|
echo '<code>yarn distclean && yarn build</code>'; |
111
|
|
|
echo '</p>'; |
112
|
|
|
} else { |
113
|
|
|
|
114
|
|
|
// We got the static.html so let's display it |
115
|
|
|
echo $static_html; |
116
|
|
|
self::render_footer(); |
117
|
|
|
|
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Gets array of any Jetpack notices that have been dismissed. |
123
|
|
|
* |
124
|
|
|
* @since 4.0.1 |
125
|
|
|
* @return mixed|void |
126
|
|
|
*/ |
127
|
|
|
function get_dismissed_jetpack_notices() { |
128
|
|
|
$jetpack_dismissed_notices = get_option( 'jetpack_dismissed_notices', array() ); |
129
|
|
|
/** |
130
|
|
|
* Array of notices that have been dismissed. |
131
|
|
|
* |
132
|
|
|
* @since 4.0.1 |
133
|
|
|
* |
134
|
|
|
* @param array $jetpack_dismissed_notices If empty, will not show any Jetpack notices. |
135
|
|
|
*/ |
136
|
|
|
$dismissed_notices = apply_filters( 'jetpack_dismissed_notices', $jetpack_dismissed_notices ); |
137
|
|
|
return $dismissed_notices; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
function additional_styles() { |
141
|
|
|
Jetpack_Admin_Page::load_wrapper_styles(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
function page_admin_scripts() { |
145
|
|
|
if ( $this->is_redirecting ) { |
146
|
|
|
return; // No need for scripts on a fallback page |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$script_deps_path = JETPACK__PLUGIN_DIR . '_inc/build/admin.deps.json'; |
150
|
|
|
$script_dependencies = file_exists( $script_deps_path ) |
151
|
|
|
? json_decode( file_get_contents( $script_deps_path ) ) |
152
|
|
|
: array(); |
153
|
|
|
$script_dependencies[] = 'wp-polyfill'; |
154
|
|
|
if ( Jetpack::is_active() || Jetpack::is_development_mode() ) { |
155
|
|
|
wp_enqueue_script( |
156
|
|
|
'react-plugin', |
157
|
|
|
plugins_url( '_inc/build/admin.js', JETPACK__PLUGIN_FILE ), |
158
|
|
|
$script_dependencies, |
159
|
|
|
JETPACK__VERSION, |
160
|
|
|
true |
161
|
|
|
); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
|
165
|
|
View Code Duplication |
if ( ! Jetpack::is_development_mode() && Jetpack::is_active() ) { |
166
|
|
|
// Required for Analytics. |
167
|
|
|
wp_enqueue_script( 'jp-tracks', '//stats.wp.com/w.js', array(), gmdate( 'YW' ), true ); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
// Add objects to be passed to the initial state of the app. |
171
|
|
|
wp_localize_script( 'react-plugin', 'Initial_State', $this->get_initial_state() ); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
function get_initial_state() { |
175
|
|
|
// Load API endpoint base classes and endpoints for getting the module list fed into the JS Admin Page |
176
|
|
|
require_once JETPACK__PLUGIN_DIR . '_inc/lib/core-api/class.jetpack-core-api-xmlrpc-consumer-endpoint.php'; |
177
|
|
|
require_once JETPACK__PLUGIN_DIR . '_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php'; |
178
|
|
|
$moduleListEndpoint = new Jetpack_Core_API_Module_List_Endpoint(); |
179
|
|
|
$modules = $moduleListEndpoint->get_modules(); |
180
|
|
|
|
181
|
|
|
// Preparing translated fields for JSON encoding by transforming all HTML entities to |
182
|
|
|
// respective characters. |
183
|
|
|
foreach( $modules as $slug => $data ) { |
|
|
|
|
184
|
|
|
$modules[ $slug ]['name'] = html_entity_decode( $data['name'] ); |
185
|
|
|
$modules[ $slug ]['description'] = html_entity_decode( $data['description'] ); |
186
|
|
|
$modules[ $slug ]['short_description'] = html_entity_decode( $data['short_description'] ); |
187
|
|
|
$modules[ $slug ]['long_description'] = html_entity_decode( $data['long_description'] ); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
// Collecting roles that can view site stats. |
191
|
|
|
$stats_roles = array(); |
192
|
|
|
$enabled_roles = function_exists( 'stats_get_option' ) ? stats_get_option( 'roles' ) : array( 'administrator' ); |
193
|
|
|
|
194
|
|
|
if ( ! function_exists( 'get_editable_roles' ) ) { |
195
|
|
|
require_once ABSPATH . 'wp-admin/includes/user.php'; |
196
|
|
|
} |
197
|
|
|
foreach ( get_editable_roles() as $slug => $role ) { |
198
|
|
|
$stats_roles[ $slug ] = array( |
199
|
|
|
'name' => translate_user_role( $role['name'] ), |
200
|
|
|
'canView' => is_array( $enabled_roles ) ? in_array( $slug, $enabled_roles, true ) : false, |
201
|
|
|
); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
// Get information about current theme. |
205
|
|
|
$current_theme = wp_get_theme(); |
206
|
|
|
|
207
|
|
|
// Get all themes that Infinite Scroll provides support for natively. |
208
|
|
|
$inf_scr_support_themes = array(); |
209
|
|
|
foreach ( Jetpack::glob_php( JETPACK__PLUGIN_DIR . 'modules/infinite-scroll/themes' ) as $path ) { |
210
|
|
|
if ( is_readable( $path ) ) { |
211
|
|
|
$inf_scr_support_themes[] = basename( $path, '.php' ); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
// Get last post, to build the link to Customizer in the Related Posts module. |
216
|
|
|
$last_post = get_posts( array( 'posts_per_page' => 1 ) ); |
217
|
|
|
$last_post = isset( $last_post[0] ) && $last_post[0] instanceof WP_Post |
|
|
|
|
218
|
|
|
? get_permalink( $last_post[0]->ID ) |
219
|
|
|
: get_home_url(); |
220
|
|
|
|
221
|
|
|
// Ensure that class to get the affiliate code is loaded |
222
|
|
|
if ( ! class_exists( 'Jetpack_Affiliate' ) ) { |
223
|
|
|
require_once JETPACK__PLUGIN_DIR . 'class.jetpack-affiliate.php'; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
$current_user_data = jetpack_current_user_data(); |
227
|
|
|
|
228
|
|
|
return array( |
229
|
|
|
'WP_API_root' => esc_url_raw( rest_url() ), |
230
|
|
|
'WP_API_nonce' => wp_create_nonce( 'wp_rest' ), |
231
|
|
|
'pluginBaseUrl' => plugins_url( '', JETPACK__PLUGIN_FILE ), |
232
|
|
|
'connectionStatus' => array( |
233
|
|
|
'isActive' => Jetpack::is_active(), |
234
|
|
|
'isStaging' => Jetpack::is_staging_site(), |
235
|
|
|
'devMode' => array( |
236
|
|
|
'isActive' => Jetpack::is_development_mode(), |
237
|
|
|
'constant' => defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG, |
238
|
|
|
'url' => site_url() && false === strpos( site_url(), '.' ), |
239
|
|
|
'filter' => apply_filters( 'jetpack_development_mode', false ), |
240
|
|
|
), |
241
|
|
|
'isPublic' => '1' == get_option( 'blog_public' ), |
242
|
|
|
'isInIdentityCrisis' => Jetpack::validate_sync_error_idc_option(), |
243
|
|
|
'sandboxDomain' => JETPACK__SANDBOX_DOMAIN, |
244
|
|
|
), |
245
|
|
|
'connectUrl' => $current_user_data['isConnected'] == false ? Jetpack::init()->build_connect_url( true, false, false ) : '', |
246
|
|
|
'dismissedNotices' => $this->get_dismissed_jetpack_notices(), |
247
|
|
|
'isDevVersion' => Jetpack::is_development_version(), |
248
|
|
|
'currentVersion' => JETPACK__VERSION, |
249
|
|
|
'is_gutenberg_available' => true, |
250
|
|
|
'getModules' => $modules, |
251
|
|
|
'rawUrl' => Jetpack::build_raw_urls( get_home_url() ), |
252
|
|
|
'adminUrl' => esc_url( admin_url() ), |
253
|
|
|
'stats' => array( |
254
|
|
|
// data is populated asynchronously on page load |
255
|
|
|
'data' => array( |
256
|
|
|
'general' => false, |
257
|
|
|
'day' => false, |
258
|
|
|
'week' => false, |
259
|
|
|
'month' => false, |
260
|
|
|
), |
261
|
|
|
'roles' => $stats_roles, |
262
|
|
|
), |
263
|
|
|
'aff' => Jetpack_Affiliate::init()->get_affiliate_code(), |
264
|
|
|
'settings' => $this->get_flattened_settings( $modules ), |
|
|
|
|
265
|
|
|
'userData' => array( |
266
|
|
|
// 'othersLinked' => Jetpack::get_other_linked_admins(), |
267
|
|
|
'currentUser' => $current_user_data, |
268
|
|
|
), |
269
|
|
|
'siteData' => array( |
270
|
|
|
'icon' => has_site_icon() |
271
|
|
|
? apply_filters( 'jetpack_photon_url', get_site_icon_url(), array( 'w' => 64 ) ) |
272
|
|
|
: '', |
273
|
|
|
'siteVisibleToSearchEngines' => '1' == get_option( 'blog_public' ), |
274
|
|
|
/** |
275
|
|
|
* Whether promotions are visible or not. |
276
|
|
|
* |
277
|
|
|
* @since 4.8.0 |
278
|
|
|
* |
279
|
|
|
* @param bool $are_promotions_active Status of promotions visibility. True by default. |
280
|
|
|
*/ |
281
|
|
|
'showPromotions' => apply_filters( 'jetpack_show_promotions', true ), |
282
|
|
|
'isAtomicSite' => jetpack_is_atomic_site(), |
283
|
|
|
'plan' => Jetpack_Plan::get(), |
284
|
|
|
'showBackups' => Jetpack::show_backups_ui(), |
285
|
|
|
), |
286
|
|
|
'themeData' => array( |
287
|
|
|
'name' => $current_theme->get( 'Name' ), |
288
|
|
|
'hasUpdate' => (bool) get_theme_update_available( $current_theme ), |
289
|
|
|
'support' => array( |
290
|
|
|
'infinite-scroll' => current_theme_supports( 'infinite-scroll' ) || in_array( $current_theme->get_stylesheet(), $inf_scr_support_themes ), |
291
|
|
|
), |
292
|
|
|
), |
293
|
|
|
'locale' => Jetpack::get_i18n_data_json(), |
294
|
|
|
'localeSlug' => join( '-', explode( '_', get_user_locale() ) ), |
295
|
|
|
'jetpackStateNotices' => array( |
296
|
|
|
'messageCode' => Jetpack::state( 'message' ), |
297
|
|
|
'errorCode' => Jetpack::state( 'error' ), |
298
|
|
|
'errorDescription' => Jetpack::state( 'error_description' ), |
299
|
|
|
), |
300
|
|
|
'tracksUserData' => Jetpack_Tracks_Client::get_connected_user_tracks_identity(), |
301
|
|
|
'currentIp' => function_exists( 'jetpack_protect_get_ip' ) ? jetpack_protect_get_ip() : false, |
302
|
|
|
'lastPostUrl' => esc_url( $last_post ), |
303
|
|
|
'externalServicesConnectUrls' => $this->get_external_services_connect_urls(), |
304
|
|
|
'calypsoEnv' => Jetpack::get_calypso_env(), |
305
|
|
|
); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
function get_external_services_connect_urls() { |
309
|
|
|
$connect_urls = array(); |
310
|
|
|
jetpack_require_lib( 'class.jetpack-keyring-service-helper' ); |
311
|
|
|
foreach ( Jetpack_Keyring_Service_Helper::$SERVICES as $service_name => $service_info ) { |
312
|
|
|
$connect_urls[ $service_name ] = Jetpack_Keyring_Service_Helper::connect_url( $service_name, $service_info[ 'for' ] ); |
313
|
|
|
} |
314
|
|
|
return $connect_urls; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Returns an array of modules and settings both as first class members of the object. |
319
|
|
|
* |
320
|
|
|
* @param array $modules the result of an API request to get all modules. |
321
|
|
|
* |
322
|
|
|
* @return array flattened settings with modules. |
323
|
|
|
*/ |
324
|
|
|
function get_flattened_settings( $modules ) { |
325
|
|
|
$core_api_endpoint = new Jetpack_Core_API_Data(); |
326
|
|
|
$settings = $core_api_endpoint->get_all_options(); |
327
|
|
|
return $settings->data; |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Gather data about the current user. |
333
|
|
|
* |
334
|
|
|
* @since 4.1.0 |
335
|
|
|
* |
336
|
|
|
* @return array |
337
|
|
|
*/ |
338
|
|
|
function jetpack_current_user_data() { |
339
|
|
|
$current_user = wp_get_current_user(); |
340
|
|
|
$is_master_user = $current_user->ID == Jetpack_Options::get_option( 'master_user' ); |
341
|
|
|
$dotcom_data = Jetpack::get_connected_user_data(); |
342
|
|
|
// Add connected user gravatar to the returned dotcom_data. |
343
|
|
|
$dotcom_data['avatar'] = get_avatar_url( $dotcom_data['email'], array( 'size' => 64, 'default' => 'mysteryman' ) ); |
344
|
|
|
|
345
|
|
|
$current_user_data = array( |
346
|
|
|
'isConnected' => Jetpack::is_user_connected( $current_user->ID ), |
347
|
|
|
'isMaster' => $is_master_user, |
348
|
|
|
'username' => $current_user->user_login, |
349
|
|
|
'id' => $current_user->ID, |
350
|
|
|
'wpcomUser' => $dotcom_data, |
351
|
|
|
'gravatar' => get_avatar( $current_user->ID, 40, 'mm', '', array( 'force_display' => true ) ), |
352
|
|
|
'permissions' => array( |
353
|
|
|
'admin_page' => current_user_can( 'jetpack_admin_page' ), |
354
|
|
|
'connect' => current_user_can( 'jetpack_connect' ), |
355
|
|
|
'disconnect' => current_user_can( 'jetpack_disconnect' ), |
356
|
|
|
'manage_modules' => current_user_can( 'jetpack_manage_modules' ), |
357
|
|
|
'network_admin' => current_user_can( 'jetpack_network_admin_page' ), |
358
|
|
|
'network_sites_page' => current_user_can( 'jetpack_network_sites_page' ), |
359
|
|
|
'edit_posts' => current_user_can( 'edit_posts' ), |
360
|
|
|
'publish_posts' => current_user_can( 'publish_posts' ), |
361
|
|
|
'manage_options' => current_user_can( 'manage_options' ), |
362
|
|
|
'view_stats' => current_user_can( 'view_stats' ), |
363
|
|
|
'manage_plugins' => current_user_can( 'install_plugins' ) |
364
|
|
|
&& current_user_can( 'activate_plugins' ) |
365
|
|
|
&& current_user_can( 'update_plugins' ) |
366
|
|
|
&& current_user_can( 'delete_plugins' ), |
367
|
|
|
), |
368
|
|
|
); |
369
|
|
|
|
370
|
|
|
return $current_user_data; |
371
|
|
|
} |
372
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
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:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.