Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
10.00% covered (danger)
10.00%
15 / 150
33.33% covered (danger)
33.33%
2 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Core_API_Site_Endpoint
10.00% covered (danger)
10.00%
15 / 150
33.33% covered (danger)
33.33%
2 / 6
1588.56
0.00% covered (danger)
0.00%
0 / 1
 get_failed_fetch_error
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 get_features
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
30
 get_purchases
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
 get_products
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
30
 can_request
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_benefits
0.00% covered (danger)
0.00%
0 / 106
0.00% covered (danger)
0.00%
0 / 1
1056
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2/**
3 * List of /site core REST API endpoints used in Jetpack's dashboard.
4 *
5 * @package automattic/jetpack
6 */
7
8use Automattic\Jetpack\Connection\Client;
9use Automattic\Jetpack\My_Jetpack\Wpcom_Products;
10use Automattic\Jetpack\Publicize\Publicize;
11use Automattic\Jetpack\Stats\WPCOM_Stats;
12
13/**
14 * This is the endpoint class for `/site` endpoints.
15 */
16class Jetpack_Core_API_Site_Endpoint {
17    /**
18     * Returns commonly used WP_Error indicating failure to fetch data
19     *
20     * @return WP_Error that denotes our inability to fetch the requested data
21     */
22    private static function get_failed_fetch_error() {
23        return new WP_Error(
24            'failed_to_fetch_data',
25            esc_html__( 'Unable to fetch the requested data.', 'jetpack' ),
26            array( 'status' => 500 )
27        );
28    }
29
30    /**
31     * Returns the result of `/sites/%s/features` endpoint call.
32     *
33     * @return object $features has 'active' and 'available' properties each of which contain feature slugs.
34     *                  'active' is a simple array of slugs that are active on the current plan.
35     *                  'available' is an object with keys that represent feature slugs and values are arrays
36     *                     of plan slugs that enable these features
37     */
38    public static function get_features() {
39        // Make the API request.
40        $request  = sprintf( '/sites/%d/features', Jetpack_Options::get_option( 'id' ) );
41        $response = Client::wpcom_json_api_request_as_blog( $request, '1.1' );
42
43        // Bail if there was an error or malformed response.
44        if ( is_wp_error( $response ) || ! is_array( $response ) || ! isset( $response['body'] ) ) {
45            return self::get_failed_fetch_error();
46        }
47
48        // Decode the results.
49        $results = json_decode( $response['body'], true );
50
51        // Bail if there were no results or plan details returned.
52        if ( ! is_array( $results ) ) {
53            return self::get_failed_fetch_error();
54        }
55
56        return rest_ensure_response(
57            array(
58                'code'    => 'success',
59                'message' => esc_html__( 'Site features correctly received.', 'jetpack' ),
60                'data'    => wp_remote_retrieve_body( $response ),
61            )
62        );
63    }
64
65    /**
66     * Returns the result of `/upgrades` endpoint call.
67     *
68     * @return WP_REST_Response|WP_Error The site purchases, or an error if they could not be fetched.
69     */
70    public static function get_purchases() {
71        // Share My Jetpack's cached lookup, which other Jetpack pages also read, instead of asking WordPress.com again.
72        $purchases = Wpcom_Products::get_site_current_purchases();
73        if ( ! is_array( $purchases ) ) {
74            return self::get_failed_fetch_error();
75        }
76
77        return rest_ensure_response(
78            array(
79                'code'    => 'success',
80                'message' => esc_html__( 'Site purchases correctly received.', 'jetpack' ),
81                'data'    => wp_json_encode( $purchases, JSON_UNESCAPED_SLASHES ),
82            )
83        );
84    }
85
86    /**
87     * Returns the result of `/sites/%d/products` endpoint call.
88     *
89     * @return array of site products.
90     */
91    public static function get_products() {
92        $url      = sprintf( '/sites/%d/products?locale=%s&type=jetpack', Jetpack_Options::get_option( 'id' ), get_user_locale() );
93        $response = Client::wpcom_json_api_request_as_blog( $url, '1.1' );
94
95        if ( is_wp_error( $response ) || ! is_array( $response ) || ! isset( $response['body'] ) ) {
96            return self::get_failed_fetch_error();
97        }
98
99        $results = json_decode( wp_remote_retrieve_body( $response ), true );
100        if ( ! is_array( $results ) ) {
101            return self::get_failed_fetch_error();
102        }
103
104        return rest_ensure_response(
105            array(
106                'code'    => 'success',
107                'message' => esc_html__( 'Site products correctly received.', 'jetpack' ),
108                'data'    => $results,
109            )
110        );
111    }
112
113    /**
114     * Check that the current user has permissions to request information about this site.
115     *
116     * @since 5.1.0
117     *
118     * @return bool
119     */
120    public static function can_request() {
121        return current_user_can( 'jetpack_manage_modules' );
122    }
123
124    /**
125     * Gets an array of data that show how Jetpack is currently being used to benefit the site.
126     *
127     * @since 7.7
128     *
129     * @return WP_REST_Response
130     */
131    public static function get_benefits() {
132        $benefits = array();
133
134        /*
135         * We get different benefits from Stats:
136         * - this year's visitors
137         * - Followers (only if subs module is active)
138         * - Sharing counts (not currently supported in Jetpack -- https://github.com/Automattic/jetpack/issues/844 )
139         */
140        $wpcom_stats = new WPCOM_Stats();
141        $stats       = $wpcom_stats->convert_stats_array_to_object(
142            $wpcom_stats->get_stats( array( 'fields' => 'stats' ) )
143        );
144        $has_stats   = null !== $stats && ! is_wp_error( $stats );
145
146        // Yearly visitors.
147        if ( $has_stats && $stats->stats->visitors > 0 ) {
148            $benefits[] = array(
149                'name'        => 'jetpack-stats',
150                'title'       => esc_html__( 'Jetpack Stats', 'jetpack' ),
151                'description' => esc_html__( 'Visitors tracked by Jetpack', 'jetpack' ),
152                'value'       => absint( $stats->stats->visitors ),
153            );
154        }
155
156        // Protect blocked logins.
157        if ( Jetpack::is_module_active( 'protect' ) ) {
158            $protect = get_site_option( 'jetpack_protect_blocked_attempts' );
159            if ( $protect > 0 ) {
160                $benefits[] = array(
161                    'name'        => 'protect',
162                    'title'       => esc_html__( 'Login protection', 'jetpack' ),
163                    'description' => esc_html__( 'The number of malicious login attempts blocked by Jetpack', 'jetpack' ),
164                    'value'       => absint( $protect ),
165                );
166            }
167        }
168
169        // Number of followers.
170        if ( $has_stats && $stats->stats->followers_blog > 0 && Jetpack::is_module_active( 'subscriptions' ) ) {
171            $benefits[] = array(
172                'name'        => 'subscribers',
173                'title'       => esc_html__( 'Subscribers', 'jetpack' ),
174                'description' => esc_html__( 'People subscribed to your updates through Jetpack', 'jetpack' ),
175                'value'       => absint( $stats->stats->followers_blog ),
176            );
177        }
178
179        // VaultPress backups.
180        if ( Jetpack::is_plugin_active( 'vaultpress/vaultpress.php' ) && class_exists( 'VaultPress' ) ) {
181            $vaultpress = new VaultPress();
182            if ( $vaultpress->is_registered() ) {
183                $data = json_decode( base64_decode( $vaultpress->contact_service( 'plugin_data' ) ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
184                if ( $data && $data->features->backups && ! empty( $data->backups->stats ) && $data->backups->stats->revisions > 0 ) {
185                    $benefits[] = array(
186                        'name'        => 'jetpack-backup',
187                        'title'       => esc_html__( 'Jetpack Backup', 'jetpack' ),
188                        'description' => esc_html__( 'The number of times Jetpack has backed up your site and kept it safe', 'jetpack' ),
189                        'value'       => absint( $data->backups->stats->revisions ),
190                    );
191                }
192            }
193        }
194
195        // Number of forms sent via a Jetpack contact form.
196        if ( Jetpack::is_module_active( 'contact-form' ) ) {
197            $contact_form_count = array_sum( get_object_vars( wp_count_posts( 'feedback' ) ) );
198            if ( $contact_form_count > 0 ) {
199                $benefits[] = array(
200                    'name'        => 'contact-form-feedback',
201                    'title'       => esc_html__( 'Contact Form Feedback', 'jetpack' ),
202                    'description' => esc_html__( 'Form submissions stored by Jetpack', 'jetpack' ),
203                    'value'       => absint( $contact_form_count ),
204                );
205            }
206        }
207
208        // Number of images in the library if Photon is active.
209        if ( Jetpack::is_module_active( 'photon' ) ) {
210            $photon_count = array_reduce(
211                get_object_vars( wp_count_attachments( array( 'image/jpeg', 'image/png', 'image/gif', 'image/bmp', 'image/webp' ) ) ),
212                function ( $i, $j ) {
213                    return $i + $j;
214                }
215            );
216            if ( $photon_count > 0 ) {
217                $benefits[] = array(
218                    'name'        => 'image-hosting',
219                    'title'       => esc_html__( 'Image Hosting', 'jetpack' ),
220                    'description' => esc_html__( 'Super-fast, mobile-ready images served by Jetpack', 'jetpack' ),
221                    'value'       => absint( $photon_count ),
222                );
223            }
224        }
225
226        // Number of VideoPress videos on the site.
227        if ( Jetpack::is_module_active( 'videopress' ) ) {
228            $videopress_attachments = wp_count_attachments( 'video/videopress' );
229            if (
230                isset( $videopress_attachments->{'video/videopress'} )
231                && $videopress_attachments->{'video/videopress'} > 0
232            ) {
233                $benefits[] = array(
234                    'name'        => 'video-hosting',
235                    'title'       => esc_html__( 'Video Hosting', 'jetpack' ),
236                    'description' => esc_html__( 'Ad-free, lightning-fast videos delivered by Jetpack', 'jetpack' ),
237                    'value'       => absint( $videopress_attachments->{'video/videopress'} ),
238                );
239            }
240        }
241
242        // Number of active Publicize connections.
243        if ( Jetpack::is_module_active( 'publicize' ) && class_exists( Publicize::class ) ) {
244            $publicize   = new Publicize();
245            $connections = $publicize->get_all_connections();
246
247            $number_of_connections = 0;
248            if ( is_array( $connections ) && ! empty( $connections ) ) {
249                $number_of_connections = count( $connections );
250            }
251
252            if ( $number_of_connections > 0 ) {
253                $benefits[] = array(
254                    'name'        => 'publicize',
255                    'title'       => esc_html__( 'Jetpack Social', 'jetpack' ),
256                    'description' => esc_html__( 'Live social media site connections, powered by Jetpack', 'jetpack' ),
257                    'value'       => absint( $number_of_connections ),
258                );
259            }
260        }
261
262        // Total number of shares.
263        if ( $has_stats && $stats->stats->shares > 0 ) {
264            $benefits[] = array(
265                'name'        => 'sharing',
266                'title'       => esc_html__( 'Sharing', 'jetpack' ),
267                'description' => esc_html__( 'The number of times visitors have shared your posts with the world using Jetpack', 'jetpack' ),
268                'value'       => absint( $stats->stats->shares ),
269            );
270        }
271
272        if ( Jetpack::is_module_active( 'search' ) && ! class_exists( 'Automattic\\Jetpack\\Search_Plugin\\Jetpack_Search_Plugin' ) ) {
273            $benefits[] = array(
274                'name'        => 'search',
275                'title'       => esc_html__( 'Search', 'jetpack' ),
276                'description' => esc_html__( 'Help your visitors find exactly what they are looking for, fast', 'jetpack' ),
277            );
278        }
279
280        // Finally, return the whole list of benefits.
281        return rest_ensure_response(
282            array(
283                'code'    => 'success',
284                'message' => esc_html__( 'Site benefits correctly received.', 'jetpack' ),
285                'data'    => wp_json_encode( $benefits, JSON_UNESCAPED_SLASHES ),
286            )
287        );
288    }
289}