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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
/**
* List of /site core REST API endpoints used in Jetpack's dashboard.
*
* @package automattic/jetpack
*/
use Automattic\Jetpack\Connection\Client;
/**
* This is the endpoint class for `/site` endpoints.
*/
class Jetpack_Core_API_Site_Endpoint {
/**
* Returns commonly used WP_Error indicating failure to fetch data
*
* @return WP_Error that denotes our inability to fetch the requested data
*/
private static function get_failed_fetch_error() {
return new WP_Error(
'failed_to_fetch_data',
esc_html__( 'Unable to fetch the requested data.', 'jetpack' ),
array( 'status' => 500 )
);
}
/**
* Returns the result of `/sites/%s/features` endpoint call.
*
* @return object $features has 'active' and 'available' properties each of which contain feature slugs.
* 'active' is a simple array of slugs that are active on the current plan.
* 'available' is an object with keys that represent feature slugs and values are arrays
* of plan slugs that enable these features
*/
public static function get_features() {
// Make the API request.
$request = sprintf( '/sites/%d/features', Jetpack_Options::get_option( 'id' ) );
$response = Client::wpcom_json_api_request_as_blog( $request, '1.1' );
// Bail if there was an error or malformed response.
if ( is_wp_error( $response ) || ! is_array( $response ) || ! isset( $response['body'] ) ) {
return self::get_failed_fetch_error();
}
// Decode the results.
$results = json_decode( $response['body'], true );
// Bail if there were no results or plan details returned.
if ( ! is_array( $results ) ) {
return self::get_failed_fetch_error();
}
return rest_ensure_response(
array(
'code' => 'success',
'message' => esc_html__( 'Site features correctly received.', 'jetpack' ),
'data' => wp_remote_retrieve_body( $response ),
)
);
}
/**
* Returns the result of `/sites/%s/purchases` endpoint call.
*
* @return array of site purchases.
*/
public static function get_purchases() {
// Make the API request.
$request = sprintf( '/sites/%d/purchases', Jetpack_Options::get_option( 'id' ) );
$response = Client::wpcom_json_api_request_as_blog( $request, '1.1' );
// Bail if there was an error or malformed response.
if ( is_wp_error( $response ) || ! is_array( $response ) || ! isset( $response['body'] ) ) {
return self::get_failed_fetch_error();
}
if ( 200 !== (int) wp_remote_retrieve_response_code( $response ) ) {
return self::get_failed_fetch_error();
}
// Decode the results.
$results = json_decode( $response['body'], true );
// Bail if there were no results or purchase details returned.
if ( ! is_array( $results ) ) {
return self::get_failed_fetch_error();
}
return rest_ensure_response(
array(
'code' => 'success',
'message' => esc_html__( 'Site purchases correctly received.', 'jetpack' ),
'data' => wp_remote_retrieve_body( $response ),
)
);
}
/**
* Returns the result of `/sites/%d/products` endpoint call.
*
* @return array of site products.
*/
public static function get_products() {
$url = sprintf( '/sites/%d/products?locale=%s&type=jetpack', Jetpack_Options::get_option( 'id' ), get_user_locale() );
$response = Client::wpcom_json_api_request_as_blog( $url, '1.1' );
if ( is_wp_error( $response ) || ! is_array( $response ) || ! isset( $response['body'] ) ) {
return self::get_failed_fetch_error();
}
$results = json_decode( wp_remote_retrieve_body( $response ), true );
if ( ! is_array( $results ) ) {
return self::get_failed_fetch_error();
}
return rest_ensure_response(
array(
'code' => 'success',
'message' => esc_html__( 'Site products correctly received.', 'jetpack' ),
'data' => $results,
)
);
}
/**
* Check that the current user has permissions to request information about this site.
*
* @since 5.1.0
*
* @return bool
*/
public static function can_request() {
return current_user_can( 'jetpack_manage_modules' );
}
/**
* Gets an array of data that show how Jetpack is currently being used to benefit the site.
*
* @since 7.7
*
* @return WP_REST_Response
*/
public static function get_benefits() {
$benefits = array();
/*
* We get different benefits from Stats:
* - this year's visitors
* - Followers (only if subs module is active)
* - Sharing counts (not currently supported in Jetpack -- https://github.com/Automattic/jetpack/issues/844 )
*/
$stats = null;
if ( function_exists( 'stats_get_from_restapi' ) ) {
$stats = stats_get_from_restapi( array( 'fields' => 'stats' ) );
}
$has_stats = null !== $stats && ! is_wp_error( $stats );
// Yearly visitors.
if ( $has_stats && $stats->stats->visitors > 0 ) {
$benefits[] = array(
'name' => 'jetpack-stats',
'title' => esc_html__( 'Site Stats', 'jetpack' ),
'description' => esc_html__( 'Visitors tracked by Jetpack', 'jetpack' ),
'value' => absint( $stats->stats->visitors ),
);
}
// Protect blocked logins.
if ( Jetpack::is_module_active( 'protect' ) ) {
$protect = get_site_option( 'jetpack_protect_blocked_attempts' );
if ( $protect > 0 ) {
$benefits[] = array(
'name' => 'protect',
'title' => esc_html__( 'Brute force protection', 'jetpack' ),
'description' => esc_html__( 'The number of malicious login attempts blocked by Jetpack', 'jetpack' ),
'value' => absint( $protect ),
);
}
}
// Number of followers.
if ( $has_stats && $stats->stats->followers_blog > 0 && Jetpack::is_module_active( 'subscriptions' ) ) {
$benefits[] = array(
'name' => 'subscribers',
'title' => esc_html__( 'Subscribers', 'jetpack' ),
'description' => esc_html__( 'People subscribed to your updates through Jetpack', 'jetpack' ),
'value' => absint( $stats->stats->followers_blog ),
);
}
// VaultPress backups.
if ( Jetpack::is_plugin_active( 'vaultpress/vaultpress.php' ) && class_exists( 'VaultPress' ) ) {
$vaultpress = new VaultPress();
if ( $vaultpress->is_registered() ) {
$data = json_decode( base64_decode( $vaultpress->contact_service( 'plugin_data' ) ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
if ( $data && $data->features->backups && ! empty( $data->backups->stats ) && $data->backups->stats->revisions > 0 ) {
$benefits[] = array(
'name' => 'jetpack-backup',
'title' => esc_html__( 'Jetpack Backup', 'jetpack' ),
'description' => esc_html__( 'The number of times Jetpack has backed up your site and kept it safe', 'jetpack' ),
'value' => absint( $data->backups->stats->revisions ),
);
}
}
}
// Number of forms sent via a Jetpack contact form.
if ( Jetpack::is_module_active( 'contact-form' ) ) {
$contact_form_count = array_sum( get_object_vars( wp_count_posts( 'feedback' ) ) );
if ( $contact_form_count > 0 ) {
$benefits[] = array(
'name' => 'contact-form-feedback',
'title' => esc_html__( 'Contact Form Feedback', 'jetpack' ),
'description' => esc_html__( 'Form submissions stored by Jetpack', 'jetpack' ),
'value' => absint( $contact_form_count ),
);
}
}
// Number of images in the library if Photon is active.
if ( Jetpack::is_module_active( 'photon' ) ) {
$photon_count = array_reduce(
get_object_vars( wp_count_attachments( array( 'image/jpeg', 'image/png', 'image/gif', 'image/bmp', 'image/webp' ) ) ),
function ( $i, $j ) {
return $i + $j;
}
);
if ( $photon_count > 0 ) {
$benefits[] = array(
'name' => 'image-hosting',
'title' => esc_html__( 'Image Hosting', 'jetpack' ),
'description' => esc_html__( 'Super-fast, mobile-ready images served by Jetpack', 'jetpack' ),
'value' => absint( $photon_count ),
);
}
}
// Number of VideoPress videos on the site.
if ( Jetpack::is_module_active( 'videopress' ) ) {
$videopress_attachments = wp_count_attachments( 'video/videopress' );
if (
isset( $videopress_attachments->{'video/videopress'} )
&& $videopress_attachments->{'video/videopress'} > 0
) {
$benefits[] = array(
'name' => 'video-hosting',
'title' => esc_html__( 'Video Hosting', 'jetpack' ),
'description' => esc_html__( 'Ad-free, lightning-fast videos delivered by Jetpack', 'jetpack' ),
'value' => absint( $videopress_attachments->{'video/videopress'} ),
);
}
}
// Number of active Publicize connections.
if ( Jetpack::is_module_active( 'publicize' ) && class_exists( 'Publicize' ) ) {
$publicize = new Publicize();
$connections = $publicize->get_all_connections();
$number_of_connections = 0;
if ( is_array( $connections ) && ! empty( $connections ) ) {
$number_of_connections = count( $connections );
}
if ( $number_of_connections > 0 ) {
$benefits[] = array(
'name' => 'publicize',
'title' => esc_html__( 'Publicize', 'jetpack' ),
'description' => esc_html__( 'Live social media site connections, powered by Jetpack', 'jetpack' ),
'value' => absint( $number_of_connections ),
);
}
}
// Total number of shares.
if ( $has_stats && $stats->stats->shares > 0 ) {
$benefits[] = array(
'name' => 'sharing',
'title' => esc_html__( 'Sharing', 'jetpack' ),
'description' => esc_html__( 'The number of times visitors have shared your posts with the world using Jetpack', 'jetpack' ),
'value' => absint( $stats->stats->shares ),
);
}
if ( Jetpack::is_module_active( 'search' ) && ! class_exists( 'Automattic\\Jetpack\\Search_Plugin\\Jetpack_Search_Plugin' ) ) {
$benefits[] = array(
'name' => 'search',
'title' => esc_html__( 'Search', 'jetpack' ),
'description' => esc_html__( 'Help your visitors find exactly what they are looking for, fast', 'jetpack' ),
);
}
// Finally, return the whole list of benefits.
return rest_ensure_response(
array(
'code' => 'success',
'message' => esc_html__( 'Site benefits correctly received.', 'jetpack' ),
'data' => wp_json_encode( $benefits ),
)
);
}
}
|