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