1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
<?php
// Shared logic between Jetpack admin pages
abstract class Jetpack_Admin_Page {
// Add page specific actions given the page hook
abstract function add_page_actions( $hook );
// Create a menu item for the page and returns the hook
abstract function get_page_hook();
// Enqueue and localize page specific scripts
abstract function page_admin_scripts();
// Render page specific HTML
abstract function page_render();
/**
* Should we block the page rendering because the site is in IDC?
* @var bool
*/
static $block_page_rendering_for_idc;
/**
* Flag to know if we already checked the plan.
*
* @since 4.4.0
*
* @var bool
*/
static $plan_checked = false;
/**
* Function called after admin_styles to load any additional needed styles.
*
* @since 4.3.0
*/
function additional_styles() {}
function __construct() {
$this->jetpack = Jetpack::init();
self::$block_page_rendering_for_idc = (
Jetpack::validate_sync_error_idc_option() && ! Jetpack_Options::get_option( 'safe_mode_confirmed' )
);
}
function add_actions() {
// If user is not an admin and site is in Dev Mode, don't do anything
if ( ! current_user_can( 'manage_options' ) && Jetpack::is_development_mode() ) {
return;
}
// Don't add in the modules page unless modules are available!
if ( $this->dont_show_if_not_active && ! Jetpack::is_active() && ! Jetpack::is_development_mode() ) {
return;
}
// Initialize menu item for the page in the admin
$hook = $this->get_page_hook();
// Attach hooks common to all Jetpack admin pages based on the created
// hook
add_action( "load-$hook", array( $this, 'admin_help' ) );
add_action( "load-$hook", array( $this, 'admin_page_load' ) );
add_action( "admin_head-$hook", array( $this, 'admin_head' ) );
add_action( "admin_print_styles-$hook", array( $this, 'admin_styles' ) );
add_action( "admin_print_scripts-$hook", array( $this, 'admin_scripts' ) );
if ( ! self::$block_page_rendering_for_idc ) {
add_action( "admin_print_styles-$hook", array( $this, 'additional_styles' ) );
}
// Check if the site plan changed and deactivate modules accordingly.
add_action( 'current_screen', array( $this, 'check_plan_deactivate_modules' ) );
// Attach page specific actions in addition to the above
$this->add_page_actions( $hook );
}
function admin_head() {
if ( isset( $_GET['configure'] ) && Jetpack::is_module( $_GET['configure'] ) && current_user_can( 'manage_options' ) ) {
/**
* Fires in the <head> of a particular Jetpack configuation page.
*
* The dynamic portion of the hook name, `$_GET['configure']`,
* refers to the slug of module, such as 'stats', 'sso', etc.
* A complete hook for the latter would be
* 'jetpack_module_configuation_head_sso'.
*
* @since 3.0.0
*/
do_action( 'jetpack_module_configuration_head_' . $_GET['configure'] );
}
}
// Render the page with a common top and bottom part, and page specific content
function render() {
// We're in an IDC: we need a decision made before we show the UI again.
if ( self::$block_page_rendering_for_idc ) {
return;
}
$this->page_render();
}
function admin_help() {
$this->jetpack->admin_help();
}
function admin_page_load() {
// This is big. For the moment, just call the existing one.
$this->jetpack->admin_page_load();
}
function admin_page_top() {
include_once( JETPACK__PLUGIN_DIR . '_inc/header.php' );
}
function admin_page_bottom() {
include_once( JETPACK__PLUGIN_DIR . '_inc/footer.php' );
}
// Add page specific scripts and jetpack stats for all menu pages
function admin_scripts() {
$this->page_admin_scripts(); // Delegate to inheriting class
add_action( 'admin_footer', array( $this->jetpack, 'do_stats' ) );
}
// Enqueue the Jetpack admin stylesheet
function admin_styles() {
$min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
wp_enqueue_style( 'jetpack-admin', plugins_url( "css/jetpack-admin{$min}.css", JETPACK__PLUGIN_FILE ), array( 'genericons' ), JETPACK__VERSION . '-20121016' );
wp_style_add_data( 'jetpack-admin', 'rtl', 'replace' );
wp_style_add_data( 'jetpack-admin', 'suffix', $min );
}
/**
* Checks if WordPress version is too old to have REST API.
*
* @since 4.3
*
* @return bool
*/
function is_wp_version_too_old() {
global $wp_version;
return ( ! function_exists( 'rest_api_init' ) || version_compare( $wp_version, '4.4-z', '<=' ) );
}
/**
* Checks if REST API is enabled.
*
* @since 4.4.2
*
* @return bool
*/
function is_rest_api_enabled() {
return
/** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */
apply_filters( 'rest_enabled', true ) &&
/** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */
apply_filters( 'rest_jsonp_enabled', true ) &&
/** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */
apply_filters( 'rest_authentication_errors', true );
}
/**
* Checks the site plan and deactivates modules that were active but are no longer included in the plan.
*
* @since 4.4.0
*
* @param $page
*
* @return bool|array
*/
function check_plan_deactivate_modules( $page ) {
if (
Jetpack::is_development_mode()
|| ! in_array(
$page->base,
array(
'toplevel_page_jetpack',
'admin_page_jetpack_modules',
'jetpack_page_vaultpress',
'jetpack_page_stats',
'jetpack_page_akismet-key-config'
)
)
|| true === self::$plan_checked
) {
return false;
}
self::$plan_checked = true;
$previous = get_option( 'jetpack_active_plan', '' );
$current = Jetpack_Core_Json_Api_Endpoints::site_data();
if ( ! $current || is_wp_error( $current ) ) {
// If we can't get information about the current plan we don't do anything
self::$plan_checked = true;
return;
}
$to_deactivate = array();
if ( isset( $current->plan->product_slug ) ) {
if (
empty( $previous )
|| ! isset( $previous['product_slug'] )
|| $previous['product_slug'] !== $current->plan->product_slug
) {
$active = Jetpack::get_active_modules();
switch ( $current->plan->product_slug ) {
case 'jetpack_free':
$to_deactivate = array( 'seo-tools', 'videopress', 'google-analytics', 'wordads', 'search' );
break;
case 'jetpack_personal':
case 'jetpack_personal_monthly':
$to_deactivate = array( 'seo-tools', 'videopress', 'google-analytics', 'wordads', 'search' );
break;
case 'jetpack_premium':
case 'jetpack_premium_monthly':
$to_deactivate = array( 'seo-tools', 'google-analytics', 'search' );
break;
}
$to_deactivate = array_intersect( $active, $to_deactivate );
if ( ! empty( $to_deactivate ) ) {
Jetpack::update_active_modules( array_filter( array_diff( $active, $to_deactivate ) ) );
}
}
}
return array(
'previous' => $previous,
'current' => $current,
'deactivate' => $to_deactivate
);
}
}
|