1
|
|
|
<?php |
2
|
|
|
use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
3
|
|
|
use Automattic\Jetpack\Status; |
4
|
|
|
|
5
|
|
|
include_once( 'class.jetpack-admin-page.php' ); |
6
|
|
|
require_once __DIR__ . '/class-jetpack-redux-state-helper.php'; |
7
|
|
|
|
8
|
|
|
// Builds the landing page and its menu |
9
|
|
|
class Jetpack_React_Page extends Jetpack_Admin_Page { |
10
|
|
|
|
11
|
|
|
protected $dont_show_if_not_active = false; |
12
|
|
|
|
13
|
|
|
protected $is_redirecting = false; |
14
|
|
|
|
15
|
|
|
function get_page_hook() { |
16
|
|
|
// Add the main admin Jetpack menu |
17
|
|
|
return add_menu_page( 'Jetpack', 'Jetpack', 'jetpack_admin_page', 'jetpack', array( $this, 'render' ), 'div', 3 ); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
function add_page_actions( $hook ) { |
21
|
|
|
/** This action is documented in class.jetpack.php */ |
22
|
|
|
do_action( 'jetpack_admin_menu', $hook ); |
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_connection_ready() && ! 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
|
|
|
if ( ( new Status() )->is_offline_mode() || Jetpack::is_connection_ready() ) { |
55
|
|
|
add_submenu_page( 'jetpack', __( 'Dashboard', 'jetpack' ), __( 'Dashboard', 'jetpack' ), 'jetpack_admin_page', 'jetpack#/dashboard', '__return_null' ); |
56
|
|
|
remove_submenu_page( 'jetpack', 'jetpack' ); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Determine whether a user can access the Jetpack Settings page. |
62
|
|
|
* |
63
|
|
|
* Rules are: |
64
|
|
|
* - user is allowed to see the Jetpack Admin |
65
|
|
|
* - site is connected or in offline mode |
66
|
|
|
* - non-admins only need access to the settings when there are modules they can manage. |
67
|
|
|
* |
68
|
|
|
* @return bool $can_access_settings Can the user access settings. |
69
|
|
|
*/ |
70
|
|
|
private function can_access_settings() { |
71
|
|
|
$connection = new Connection_Manager( 'jetpack' ); |
72
|
|
|
$status = new Status(); |
73
|
|
|
|
74
|
|
|
// User must have the necessary permissions to see the Jetpack settings pages. |
75
|
|
|
if ( ! current_user_can( 'edit_posts' ) ) { |
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// In offline mode, allow access to admins. |
80
|
|
|
if ( $status->is_offline_mode() && current_user_can( 'manage_options' ) ) { |
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// If not in offline mode but site is not connected, bail. |
85
|
|
|
if ( ! Jetpack::is_connection_ready() ) { |
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/* |
90
|
|
|
* Additional checks for non-admins. |
91
|
|
|
*/ |
92
|
|
|
if ( ! current_user_can( 'manage_options' ) ) { |
93
|
|
|
// If the site isn't connected at all, bail. |
94
|
|
|
if ( ! $connection->has_connected_owner() ) { |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/* |
99
|
|
|
* If they haven't connected their own account yet, |
100
|
|
|
* they have no use for the settings page. |
101
|
|
|
* They will not be able to manage any settings. |
102
|
|
|
*/ |
103
|
|
|
if ( ! $connection->is_user_connected() ) { |
104
|
|
|
return false; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/* |
108
|
|
|
* Non-admins only have access to settings |
109
|
|
|
* for the following modules: |
110
|
|
|
* - Publicize |
111
|
|
|
* - Post By Email |
112
|
|
|
* If those modules are not available, bail. |
113
|
|
|
*/ |
114
|
|
|
if ( |
115
|
|
|
! Jetpack::is_module_active( 'post-by-email' ) |
116
|
|
|
&& ! Jetpack::is_module_active( 'publicize' ) |
117
|
|
|
) { |
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// fallback. |
123
|
|
|
return true; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Jetpack Settings sub-link. |
128
|
|
|
* |
129
|
|
|
* @since 4.3.0 |
130
|
|
|
* @since 9.7.0 If Connection does not have an owner, restrict it to admins |
131
|
|
|
*/ |
132
|
|
|
function jetpack_add_settings_sub_nav_item() { |
133
|
|
|
if ( $this->can_access_settings() ) { |
134
|
|
|
add_submenu_page( 'jetpack', __( 'Settings', 'jetpack' ), __( 'Settings', 'jetpack' ), 'jetpack_admin_page', 'jetpack#/settings', '__return_null' ); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
function add_fallback_head_meta() { |
139
|
|
|
echo '<meta http-equiv="refresh" content="0; url=?page=jetpack_modules">'; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
function add_noscript_head_meta() { |
143
|
|
|
echo '<noscript>'; |
144
|
|
|
$this->add_fallback_head_meta(); |
145
|
|
|
echo '</noscript>'; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Custom menu order. |
150
|
|
|
* |
151
|
|
|
* @deprecated since 9.2.0 |
152
|
|
|
* @param array $menu_order Menu order. |
153
|
|
|
* @return array |
154
|
|
|
*/ |
155
|
|
|
function jetpack_menu_order( $menu_order ) { |
156
|
|
|
_deprecated_function( __METHOD__, 'jetpack-9.2' ); |
157
|
|
|
|
158
|
|
|
return $menu_order; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
function page_render() { |
162
|
|
|
/** This action is already documented in views/admin/admin-page.php */ |
163
|
|
|
do_action( 'jetpack_notices' ); |
164
|
|
|
|
165
|
|
|
// Try fetching by patch |
166
|
|
|
$static_html = @file_get_contents( JETPACK__PLUGIN_DIR . '_inc/build/static.html' ); |
167
|
|
|
|
168
|
|
|
if ( false === $static_html ) { |
169
|
|
|
|
170
|
|
|
// If we still have nothing, display an error |
171
|
|
|
echo '<p>'; |
172
|
|
|
esc_html_e( 'Error fetching static.html. Try running: ', 'jetpack' ); |
173
|
|
|
echo '<code>pnpm run distclean && pnpx jetpack build plugins/jetpack</code>'; |
174
|
|
|
echo '</p>'; |
175
|
|
|
} else { |
176
|
|
|
|
177
|
|
|
// We got the static.html so let's display it |
178
|
|
|
echo $static_html; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
function additional_styles() { |
183
|
|
|
Jetpack_Admin_Page::load_wrapper_styles(); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
function page_admin_scripts() { |
187
|
|
|
if ( $this->is_redirecting ) { |
188
|
|
|
return; // No need for scripts on a fallback page |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$status = new Status(); |
192
|
|
|
$is_offline_mode = $status->is_offline_mode(); |
193
|
|
|
$site_suffix = $status->get_site_suffix(); |
194
|
|
|
$script_deps_path = JETPACK__PLUGIN_DIR . '_inc/build/admin.asset.php'; |
195
|
|
|
$script_dependencies = array( 'wp-polyfill' ); |
196
|
|
|
if ( file_exists( $script_deps_path ) ) { |
197
|
|
|
$asset_manifest = include $script_deps_path; |
198
|
|
|
$script_dependencies = $asset_manifest['dependencies']; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
wp_enqueue_script( |
202
|
|
|
'react-plugin', |
203
|
|
|
plugins_url( '_inc/build/admin.js', JETPACK__PLUGIN_FILE ), |
204
|
|
|
$script_dependencies, |
205
|
|
|
JETPACK__VERSION, |
206
|
|
|
true |
207
|
|
|
); |
208
|
|
|
|
209
|
|
|
if ( ! $is_offline_mode && Jetpack::is_connection_ready() ) { |
210
|
|
|
// Required for Analytics. |
211
|
|
|
wp_enqueue_script( 'jp-tracks', '//stats.wp.com/w.js', array(), gmdate( 'YW' ), true ); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
wp_set_script_translations( 'react-plugin', 'jetpack' ); |
215
|
|
|
|
216
|
|
|
// Add objects to be passed to the initial state of the app. |
217
|
|
|
// Use wp_add_inline_script instead of wp_localize_script, see https://core.trac.wordpress.org/ticket/25280. |
218
|
|
|
wp_add_inline_script( 'react-plugin', 'var Initial_State=JSON.parse(decodeURIComponent("' . rawurlencode( wp_json_encode( Jetpack_Redux_State_Helper::get_initial_state() ) ) . '"));', 'before' ); |
219
|
|
|
|
220
|
|
|
// This will set the default URL of the jp_redirects lib. |
221
|
|
|
wp_add_inline_script( 'react-plugin', 'var jetpack_redirects = { currentSiteRawUrl: "' . $site_suffix . '" };', 'before' ); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|