1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Automattic\Jetpack\Status; |
4
|
|
|
use Automattic\Jetpack\Assets\Logo as Jetpack_Logo; |
5
|
|
|
|
6
|
|
|
// Build the Jetpack admin menu as a whole |
7
|
|
|
class Jetpack_Admin { |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @var Jetpack_Admin |
11
|
|
|
**/ |
12
|
|
|
private static $instance = null; |
13
|
|
|
|
14
|
|
|
static function init() { |
15
|
|
|
if ( isset( $_GET['page'] ) && $_GET['page'] === 'jetpack' ) { |
16
|
|
|
add_filter( 'nocache_headers', array( 'Jetpack_Admin', 'add_no_store_header' ), 100 ); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
if ( is_null( self::$instance ) ) { |
20
|
|
|
self::$instance = new Jetpack_Admin(); |
21
|
|
|
} |
22
|
|
|
return self::$instance; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
static function add_no_store_header( $headers ) { |
26
|
|
|
$headers['Cache-Control'] .= ', no-store'; |
27
|
|
|
return $headers; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
private function __construct() { |
31
|
|
|
jetpack_require_lib( 'admin-pages/class.jetpack-react-page' ); |
32
|
|
|
$this->jetpack_react = new Jetpack_React_Page(); |
|
|
|
|
33
|
|
|
|
34
|
|
|
jetpack_require_lib( 'admin-pages/class.jetpack-settings-page' ); |
35
|
|
|
$this->fallback_page = new Jetpack_Settings_Page(); |
|
|
|
|
36
|
|
|
|
37
|
|
|
jetpack_require_lib( 'admin-pages/class-jetpack-about-page' ); |
38
|
|
|
$this->jetpack_about = new Jetpack_About_Page(); |
|
|
|
|
39
|
|
|
|
40
|
|
|
add_action( 'admin_menu', array( $this->jetpack_react, 'add_actions' ), 998 ); |
41
|
|
|
add_action( 'admin_menu', array( $this->jetpack_react, 'add_actions' ), 998 ); |
42
|
|
|
add_action( 'jetpack_admin_menu', array( $this->jetpack_react, 'jetpack_add_dashboard_sub_nav_item' ) ); |
43
|
|
|
add_action( 'jetpack_admin_menu', array( $this->jetpack_react, 'jetpack_add_set_up_sub_nav_item' ) ); |
44
|
|
|
add_action( 'jetpack_admin_menu', array( $this->jetpack_react, 'jetpack_add_settings_sub_nav_item' ) ); |
45
|
|
|
add_action( 'jetpack_admin_menu', array( $this, 'admin_menu_debugger' ) ); |
46
|
|
|
add_action( 'jetpack_admin_menu', array( $this->fallback_page, 'add_actions' ) ); |
47
|
|
|
add_action( 'jetpack_admin_menu', array( $this->jetpack_about, 'add_actions' ) ); |
48
|
|
|
|
49
|
|
|
// Add redirect to current page for activation/deactivation of modules |
50
|
|
|
add_action( 'jetpack_pre_activate_module', array( $this, 'fix_redirect' ), 10, 2 ); |
51
|
|
|
add_action( 'jetpack_pre_deactivate_module', array( $this, 'fix_redirect' ) ); |
52
|
|
|
|
53
|
|
|
// Add module bulk actions handler |
54
|
|
|
add_action( 'jetpack_unrecognized_action', array( $this, 'handle_unrecognized_action' ) ); |
55
|
|
|
|
56
|
|
|
if ( class_exists( 'Akismet_Admin' ) ) { |
57
|
|
|
// If the site has Jetpack Anti-Spam, change the Akismet menu label accordingly. |
58
|
|
|
$site_products = Jetpack_Plan::get_products(); |
59
|
|
|
$anti_spam_products = array( 'jetpack_anti_spam_monthly', 'jetpack_anti_spam' ); |
60
|
|
|
if ( ! empty( array_intersect( $anti_spam_products, array_column( $site_products, 'product_slug' ) ) ) ) { |
61
|
|
|
// Prevent Akismet from adding a menu item. |
62
|
|
|
add_action( |
63
|
|
|
'admin_menu', |
64
|
|
|
function () { |
65
|
|
|
remove_action( 'admin_menu', array( 'Akismet_Admin', 'admin_menu' ), 5 ); |
66
|
|
|
}, |
67
|
|
|
4 |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
// Add an Anti-spam menu item for Jetpack. |
71
|
|
|
add_action( |
72
|
|
|
'jetpack_admin_menu', |
73
|
|
|
function () { |
74
|
|
|
add_submenu_page( 'jetpack', __( 'Anti-Spam', 'jetpack' ), __( 'Anti-Spam', 'jetpack' ), 'manage_options', 'akismet-key-config', array( 'Akismet_Admin', 'display_page' ) ); |
75
|
|
|
} |
76
|
|
|
); |
77
|
|
|
add_action( 'admin_enqueue_scripts', array( $this, 'akismet_logo_replacement_styles' ) ); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
add_filter( 'jetpack_display_jitms', array( $this, 'should_display_jitms' ), 10, 2 ); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Generate styles to replace Akismet logo for the Jetpack logo. It's a workaround until we create a proper settings page for |
86
|
|
|
* Jetpack Anti-Spam. Without this, we would have to change the logo from Akismet codebase and we want to avoid that. |
87
|
|
|
*/ |
88
|
|
|
public function akismet_logo_replacement_styles() { |
89
|
|
|
$logo = new Jetpack_Logo(); |
90
|
|
|
$logo_base64 = base64_encode( $logo->get_jp_emblem_larger() ); |
91
|
|
|
$logo_base64_url = "data:image/svg+xml;base64,{$logo_base64}"; |
92
|
|
|
$style = ".akismet-masthead__logo-container { background: url({$logo_base64_url}) no-repeat .25rem; height: 1.8125rem; } .akismet-masthead__logo { display: none; }"; |
93
|
|
|
wp_add_inline_style( 'admin-bar', $style ); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
View Code Duplication |
static function sort_requires_connection_last( $module1, $module2 ) { |
97
|
|
|
if ( $module1['requires_connection'] == $module2['requires_connection'] ) { |
98
|
|
|
return 0; |
99
|
|
|
} elseif ( $module1['requires_connection'] ) { |
100
|
|
|
return 1; |
101
|
|
|
} elseif ( $module2['requires_connection'] ) { |
102
|
|
|
return -1; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return 0; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// Produce JS understandable objects of modules containing information for |
109
|
|
|
// presentation like description, name, configuration url, etc. |
110
|
|
|
function get_modules() { |
111
|
|
|
include_once JETPACK__PLUGIN_DIR . 'modules/module-info.php'; |
112
|
|
|
$available_modules = Jetpack::get_available_modules(); |
113
|
|
|
$active_modules = Jetpack::get_active_modules(); |
114
|
|
|
$modules = array(); |
115
|
|
|
$jetpack_active = Jetpack::is_active() || ( new Status() )->is_offline_mode(); |
116
|
|
|
$overrides = Jetpack_Modules_Overrides::instance(); |
117
|
|
|
foreach ( $available_modules as $module ) { |
118
|
|
|
if ( $module_array = Jetpack::get_module( $module ) ) { |
119
|
|
|
/** |
120
|
|
|
* Filters each module's short description. |
121
|
|
|
* |
122
|
|
|
* @since 3.0.0 |
123
|
|
|
* |
124
|
|
|
* @param string $module_array['description'] Module description. |
125
|
|
|
* @param string $module Module slug. |
126
|
|
|
*/ |
127
|
|
|
$short_desc = apply_filters( 'jetpack_short_module_description', $module_array['description'], $module ); |
|
|
|
|
128
|
|
|
// Fix: correct multibyte strings truncate with checking for mbstring extension |
129
|
|
|
$short_desc_trunc = ( function_exists( 'mb_strlen' ) ) |
130
|
|
|
? ( ( mb_strlen( $short_desc ) > 143 ) |
131
|
|
|
? mb_substr( $short_desc, 0, 140 ) . '...' |
132
|
|
|
: $short_desc ) |
133
|
|
|
: ( ( strlen( $short_desc ) > 143 ) |
134
|
|
|
? substr( $short_desc, 0, 140 ) . '...' |
135
|
|
|
: $short_desc ); |
136
|
|
|
|
137
|
|
|
$module_array['module'] = $module; |
138
|
|
|
$module_array['activated'] = ( $jetpack_active ? in_array( $module, $active_modules ) : false ); |
139
|
|
|
$module_array['deactivate_nonce'] = wp_create_nonce( 'jetpack_deactivate-' . $module ); |
140
|
|
|
$module_array['activate_nonce'] = wp_create_nonce( 'jetpack_activate-' . $module ); |
141
|
|
|
$module_array['available'] = self::is_module_available( $module_array ); |
142
|
|
|
$module_array['short_description'] = $short_desc_trunc; |
143
|
|
|
$module_array['configure_url'] = Jetpack::module_configuration_url( $module ); |
144
|
|
|
$module_array['override'] = $overrides->get_module_override( $module ); |
145
|
|
|
|
146
|
|
|
ob_start(); |
147
|
|
|
/** |
148
|
|
|
* Allow the display of a "Learn More" button. |
149
|
|
|
* The dynamic part of the action, $module, is the module slug. |
150
|
|
|
* |
151
|
|
|
* @since 3.0.0 |
152
|
|
|
*/ |
153
|
|
|
do_action( 'jetpack_learn_more_button_' . $module ); |
154
|
|
|
$module_array['learn_more_button'] = ob_get_clean(); |
155
|
|
|
|
156
|
|
|
ob_start(); |
157
|
|
|
/** |
158
|
|
|
* Allow the display of information text when Jetpack is connected to WordPress.com. |
159
|
|
|
* The dynamic part of the action, $module, is the module slug. |
160
|
|
|
* |
161
|
|
|
* @since 3.0.0 |
162
|
|
|
*/ |
163
|
|
|
do_action( 'jetpack_module_more_info_' . $module ); |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Filter the long description of a module. |
167
|
|
|
* |
168
|
|
|
* @since 3.5.0 |
169
|
|
|
* |
170
|
|
|
* @param string ob_get_clean() The module long description. |
171
|
|
|
* @param string $module The module name. |
172
|
|
|
*/ |
173
|
|
|
$module_array['long_description'] = apply_filters( 'jetpack_long_module_description', ob_get_clean(), $module ); |
|
|
|
|
174
|
|
|
|
175
|
|
|
ob_start(); |
176
|
|
|
/** |
177
|
|
|
* Filter the search terms for a module |
178
|
|
|
* |
179
|
|
|
* Search terms are typically added to the module headers, under "Additional Search Queries". |
180
|
|
|
* |
181
|
|
|
* Use syntax: |
182
|
|
|
* function jetpack_$module_search_terms( $terms ) { |
183
|
|
|
* $terms = _x( 'term 1, term 2', 'search terms', 'jetpack' ); |
184
|
|
|
* return $terms; |
185
|
|
|
* } |
186
|
|
|
* add_filter( 'jetpack_search_terms_$module', 'jetpack_$module_search_terms' ); |
187
|
|
|
* |
188
|
|
|
* @since 3.5.0 |
189
|
|
|
* |
190
|
|
|
* @param string The search terms (comma separated). |
191
|
|
|
*/ |
192
|
|
|
echo apply_filters( 'jetpack_search_terms_' . $module, $module_array['additional_search_queries'] ); |
193
|
|
|
$module_array['search_terms'] = ob_get_clean(); |
194
|
|
|
|
195
|
|
|
$module_array['configurable'] = false; |
196
|
|
|
if ( |
197
|
|
|
current_user_can( 'manage_options' ) && |
198
|
|
|
/** |
199
|
|
|
* Allow the display of a configuration link in the Jetpack Settings screen. |
200
|
|
|
* |
201
|
|
|
* @since 3.0.0 |
202
|
|
|
* |
203
|
|
|
* @param string $module Module name. |
204
|
|
|
* @param bool false Should the Configure module link be displayed? Default to false. |
205
|
|
|
*/ |
206
|
|
|
apply_filters( 'jetpack_module_configurable_' . $module, false ) |
207
|
|
|
) { |
208
|
|
|
$module_array['configurable'] = sprintf( '<a href="%1$s">%2$s</a>', esc_url( $module_array['configure_url'] ), __( 'Configure', 'jetpack' ) ); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
$modules[ $module ] = $module_array; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
uasort( $modules, array( 'Jetpack', 'sort_modules' ) ); |
216
|
|
|
|
217
|
|
|
if ( ! Jetpack::is_active() ) { |
218
|
|
|
uasort( $modules, array( __CLASS__, 'sort_requires_connection_last' ) ); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return $modules; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
static function is_module_available( $module ) { |
225
|
|
|
if ( ! is_array( $module ) || empty( $module ) ) { |
226
|
|
|
return false; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* We never want to show VaultPress as activatable through Jetpack. |
231
|
|
|
*/ |
232
|
|
|
if ( 'vaultpress' === $module['module'] ) { |
233
|
|
|
return false; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/* |
237
|
|
|
* WooCommerce Analytics should only be available |
238
|
|
|
* when running WooCommerce 3+ |
239
|
|
|
*/ |
240
|
|
|
if ( |
241
|
|
|
'woocommerce-analytics' === $module['module'] |
242
|
|
|
&& ( |
243
|
|
|
! class_exists( 'WooCommerce' ) |
244
|
|
|
|| version_compare( WC_VERSION, '3.0', '<' ) |
245
|
|
|
) |
246
|
|
|
) { |
247
|
|
|
return false; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
if ( ( new Status() )->is_offline_mode() ) { |
251
|
|
|
return ! ( $module['requires_connection'] ); |
252
|
|
|
} else { |
253
|
|
|
if ( ! Jetpack::is_active() ) { |
254
|
|
|
return false; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
return Jetpack_Plan::supports( $module['module'] ); |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
function handle_unrecognized_action( $action ) { |
262
|
|
|
switch ( $action ) { |
263
|
|
|
case 'bulk-activate': |
264
|
|
|
if ( ! current_user_can( 'jetpack_activate_modules' ) ) { |
265
|
|
|
break; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
$modules = (array) $_GET['modules']; |
269
|
|
|
$modules = array_map( 'sanitize_key', $modules ); |
270
|
|
|
check_admin_referer( 'bulk-jetpack_page_jetpack_modules' ); |
271
|
|
|
foreach ( $modules as $module ) { |
272
|
|
|
Jetpack::log( 'activate', $module ); |
273
|
|
|
Jetpack::activate_module( $module, false ); |
274
|
|
|
} |
275
|
|
|
// The following two lines will rarely happen, as Jetpack::activate_module normally exits at the end. |
276
|
|
|
wp_safe_redirect( wp_get_referer() ); |
277
|
|
|
exit; |
278
|
|
View Code Duplication |
case 'bulk-deactivate': |
279
|
|
|
if ( ! current_user_can( 'jetpack_deactivate_modules' ) ) { |
280
|
|
|
break; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
$modules = (array) $_GET['modules']; |
284
|
|
|
$modules = array_map( 'sanitize_key', $modules ); |
285
|
|
|
check_admin_referer( 'bulk-jetpack_page_jetpack_modules' ); |
286
|
|
|
foreach ( $modules as $module ) { |
287
|
|
|
Jetpack::log( 'deactivate', $module ); |
288
|
|
|
Jetpack::deactivate_module( $module ); |
289
|
|
|
Jetpack::state( 'message', 'module_deactivated' ); |
290
|
|
|
} |
291
|
|
|
Jetpack::state( 'module', $modules ); |
|
|
|
|
292
|
|
|
wp_safe_redirect( wp_get_referer() ); |
293
|
|
|
exit; |
294
|
|
|
default: |
295
|
|
|
return; |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
function fix_redirect( $module, $redirect = true ) { |
300
|
|
|
if ( ! $redirect ) { |
301
|
|
|
return; |
302
|
|
|
} |
303
|
|
|
if ( wp_get_referer() ) { |
304
|
|
|
add_filter( 'wp_redirect', 'wp_get_referer' ); |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
function admin_menu_debugger() { |
309
|
|
|
jetpack_require_lib( 'debugger' ); |
310
|
|
|
Jetpack_Debugger::disconnect_and_redirect(); |
311
|
|
|
$debugger_hook = add_submenu_page( |
312
|
|
|
null, |
313
|
|
|
__( 'Debugging Center', 'jetpack' ), |
314
|
|
|
'', |
315
|
|
|
'manage_options', |
316
|
|
|
'jetpack-debugger', |
317
|
|
|
array( $this, 'wrap_debugger_page' ) |
318
|
|
|
); |
319
|
|
|
add_action( "admin_head-$debugger_hook", array( 'Jetpack_Debugger', 'jetpack_debug_admin_head' ) ); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
function wrap_debugger_page() { |
323
|
|
|
nocache_headers(); |
324
|
|
|
if ( ! current_user_can( 'manage_options' ) ) { |
325
|
|
|
die( '-1' ); |
326
|
|
|
} |
327
|
|
|
Jetpack_Admin_Page::wrap_ui( array( $this, 'debugger_page' ) ); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
function debugger_page() { |
331
|
|
|
jetpack_require_lib( 'debugger' ); |
332
|
|
|
Jetpack_Debugger::jetpack_debug_display_handler(); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
function should_display_jitms( $value, $screen_id ) { |
336
|
|
|
// Disable all JITMs on these pages. |
337
|
|
|
if ( |
338
|
|
|
in_array( |
339
|
|
|
$screen_id, |
340
|
|
|
array( |
341
|
|
|
'jetpack_page_akismet-key-config', |
342
|
|
|
'admin_page_jetpack_modules', |
343
|
|
|
), |
344
|
|
|
true |
345
|
|
|
) ) { |
346
|
|
|
return false; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
// Disable all JITMs on pages where the recommendations banner is displaying. |
350
|
|
|
if ( |
351
|
|
|
in_array( |
352
|
|
|
$screen_id, |
353
|
|
|
array( |
354
|
|
|
'dashboard', |
355
|
|
|
'plugins', |
356
|
|
|
'jetpack_page_stats', |
357
|
|
|
), |
358
|
|
|
true |
359
|
|
|
) |
360
|
|
|
&& \Jetpack_Recommendations_Banner::can_be_displayed() |
361
|
|
|
) { |
362
|
|
|
return false; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
return $value; |
366
|
|
|
} |
367
|
|
|
} |
368
|
|
|
Jetpack_Admin::init(); |
369
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: