Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
93.46% |
200 / 214 |
|
80.00% |
20 / 25 |
CRAP | |
0.00% |
0 / 1 |
| Search | |
93.87% |
199 / 212 |
|
80.00% |
20 / 25 |
77.33 | |
0.00% |
0 / 1 |
| get_name | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_title | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_description | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_long_description | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_features | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| get_pricing_for_ui | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
3 | |||
| get_post_checkout_url | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_wpcom_product_slug | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_wpcom_free_product_slug | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| is_new_pricing_202208 | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
4.05 | |||
| get_status | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| get_pricing_from_wpcom | |
90.70% |
39 / 43 |
|
0.00% |
0 / 1 |
13.14 | |||
| has_trial_support | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_paid_plan_product_slugs | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| has_free_plan_for_product | |
62.50% |
5 / 8 |
|
0.00% |
0 / 1 |
7.90 | |||
| activate_free_product | |
100.00% |
55 / 55 |
|
100.00% |
1 / 1 |
13 | |||
| wpcom_activation_error | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
7 | |||
| error_status | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| activation_error | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
1 | |||
| sync_local_state_after_free_activation | |
84.62% |
11 / 13 |
|
0.00% |
0 / 1 |
6.13 | |||
| set_default_experience_if_unset | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| do_product_specific_activation | |
50.00% |
3 / 6 |
|
0.00% |
0 / 1 |
6.00 | |||
| get_post_activation_url | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_manage_url | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| is_upgradable_by_bundle | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Search product |
| 4 | * |
| 5 | * @package my-jetpack |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\My_Jetpack\Products; |
| 9 | |
| 10 | use Automattic\Jetpack\Connection\Client; |
| 11 | use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
| 12 | use Automattic\Jetpack\Constants; |
| 13 | use Automattic\Jetpack\My_Jetpack\Hybrid_Product; |
| 14 | use Automattic\Jetpack\My_Jetpack\Wpcom_Products; |
| 15 | use Automattic\Jetpack\Search\Module_Control as Search_Module_Control; |
| 16 | use Automattic\Jetpack\Status\Host; |
| 17 | use WP_Error; |
| 18 | |
| 19 | if ( ! defined( 'ABSPATH' ) ) { |
| 20 | exit( 0 ); |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Class responsible for handling the Search product |
| 25 | */ |
| 26 | class Search extends Hybrid_Product { |
| 27 | /** |
| 28 | * Fallback starting price (USD, billed yearly) for the entry record tier, used when |
| 29 | * the WPCOM pricing fetch fails so the dashboard still shows a price, not "$0". |
| 30 | * |
| 31 | * @var float |
| 32 | */ |
| 33 | const FALLBACK_STARTING_PRICE_USD = 100; |
| 34 | |
| 35 | /** |
| 36 | * Search "new pricing" version identifier (introduced 202208). |
| 37 | * |
| 38 | * Intentionally duplicated from Automattic\Jetpack\Search\Plan::JETPACK_SEARCH_NEW_PRICING_VERSION |
| 39 | * rather than referencing that class. My Jetpack is bundled into standalone plugins (e.g. Jetpack |
| 40 | * Boost) that do NOT ship the jetpack-search package, so referencing Search\Plan here fatals with |
| 41 | * "Class not found" the moment this product builds its pricing data (introduced by PR #48892). |
| 42 | * |
| 43 | * @var string |
| 44 | */ |
| 45 | const SEARCH_NEW_PRICING_VERSION = '202208'; |
| 46 | |
| 47 | /** |
| 48 | * Prefix of the transient that keeps a successful WPCOM pricing response, one per site, locale and currency |
| 49 | * |
| 50 | * @var string |
| 51 | */ |
| 52 | const PRICING_TRANSIENT_PREFIX = 'my_jetpack_search_pricing_'; |
| 53 | |
| 54 | /** |
| 55 | * How long, in seconds, a successful WPCOM pricing response is reused |
| 56 | * |
| 57 | * @var int |
| 58 | */ |
| 59 | const PRICING_CACHE_DURATION = DAY_IN_SECONDS; |
| 60 | |
| 61 | /** |
| 62 | * Pricing responses and failures already seen in this request, keyed by record count |
| 63 | * |
| 64 | * @var array |
| 65 | */ |
| 66 | private static $pricings = array(); |
| 67 | |
| 68 | /** |
| 69 | * The product slug |
| 70 | * |
| 71 | * @var string |
| 72 | */ |
| 73 | public static $slug = 'search'; |
| 74 | |
| 75 | /** |
| 76 | * The Jetpack module name |
| 77 | * |
| 78 | * @var string |
| 79 | */ |
| 80 | public static $module_name = 'search'; |
| 81 | |
| 82 | /** |
| 83 | * The slug of the plugin associated with this product. |
| 84 | * |
| 85 | * @var string |
| 86 | */ |
| 87 | public static $plugin_slug = 'jetpack-search'; |
| 88 | |
| 89 | /** |
| 90 | * The category of the product |
| 91 | * |
| 92 | * @var string |
| 93 | */ |
| 94 | public static $category = 'performance'; |
| 95 | |
| 96 | /** |
| 97 | * Search has a standalone plugin |
| 98 | * |
| 99 | * @var bool |
| 100 | */ |
| 101 | public static $has_standalone_plugin = true; |
| 102 | |
| 103 | /** |
| 104 | * Whether this product has a free offering |
| 105 | * |
| 106 | * @var bool |
| 107 | */ |
| 108 | public static $has_free_offering = true; |
| 109 | |
| 110 | /** |
| 111 | * Whether this product requires a plan to work at all |
| 112 | * |
| 113 | * @var bool |
| 114 | */ |
| 115 | public static $requires_plan = true; |
| 116 | |
| 117 | /** |
| 118 | * The filename (id) of the plugin associated with this product. |
| 119 | * |
| 120 | * @var string |
| 121 | */ |
| 122 | public static $plugin_filename = array( |
| 123 | 'jetpack-search/jetpack-search.php', |
| 124 | 'search/jetpack-search.php', |
| 125 | 'jetpack-search-dev/jetpack-search.php', |
| 126 | ); |
| 127 | |
| 128 | /** |
| 129 | * Search only requires site connection |
| 130 | * |
| 131 | * @var boolean |
| 132 | */ |
| 133 | public static $requires_user_connection = true; |
| 134 | |
| 135 | /** |
| 136 | * The feature slug that identifies the paid plan |
| 137 | * |
| 138 | * @var string |
| 139 | */ |
| 140 | public static $feature_identifying_paid_plan = 'search'; |
| 141 | |
| 142 | /** |
| 143 | * Get the product name |
| 144 | * |
| 145 | * @return string |
| 146 | */ |
| 147 | public static function get_name() { |
| 148 | return 'Search'; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Get the product title |
| 153 | * |
| 154 | * @return string |
| 155 | */ |
| 156 | public static function get_title() { |
| 157 | return 'Jetpack Search'; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Get the internationalized product description |
| 162 | * |
| 163 | * @return string |
| 164 | */ |
| 165 | public static function get_description() { |
| 166 | return __( 'Instantly deliver the most relevant results to your visitors.', 'jetpack-my-jetpack' ); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Get the internationalized product long description |
| 171 | * |
| 172 | * @return string |
| 173 | */ |
| 174 | public static function get_long_description() { |
| 175 | return __( 'Help your site visitors find answers instantly so they keep reading and buying. Great for sites with a lot of content.', 'jetpack-my-jetpack' ); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Get the internationalized features list |
| 180 | * |
| 181 | * @return array Boost features list |
| 182 | */ |
| 183 | public static function get_features() { |
| 184 | return array( |
| 185 | __( 'Instant search and indexing', 'jetpack-my-jetpack' ), |
| 186 | __( 'Powerful filtering', 'jetpack-my-jetpack' ), |
| 187 | __( 'Supports 38 languages', 'jetpack-my-jetpack' ), |
| 188 | __( 'Spelling correction', 'jetpack-my-jetpack' ), |
| 189 | ); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Get the product princing details |
| 194 | * |
| 195 | * @return array Pricing details |
| 196 | */ |
| 197 | public static function get_pricing_for_ui() { |
| 198 | // Basic pricing info. |
| 199 | $pricing = array_merge( |
| 200 | array( |
| 201 | 'available' => true, |
| 202 | 'trial_available' => static::has_trial_support(), |
| 203 | 'wpcom_product_slug' => static::get_wpcom_product_slug(), |
| 204 | 'wpcom_free_product_slug' => static::get_wpcom_free_product_slug(), |
| 205 | ), |
| 206 | Wpcom_Products::get_product_pricing( static::get_wpcom_product_slug() ) |
| 207 | ); |
| 208 | |
| 209 | $record_count = intval( Search_Stats::estimate_count() ); |
| 210 | $search_pricing = static::get_pricing_from_wpcom( $record_count ); |
| 211 | |
| 212 | if ( is_wp_error( $search_pricing ) ) { |
| 213 | // Default to the current pricing experience when the WPCOM fetch fails so the |
| 214 | // dashboard degrades to the production default, not the legacy single-card view. |
| 215 | $pricing['pricing_version'] = self::SEARCH_NEW_PRICING_VERSION; |
| 216 | |
| 217 | // If the generic product pricing was also unavailable, fall back to a USD yearly |
| 218 | // starting price so the pricing grid shows a monthly amount instead of "$0". |
| 219 | if ( empty( $pricing['full_price'] ) ) { |
| 220 | $pricing['currency_code'] = 'USD'; |
| 221 | $pricing['full_price'] = self::FALLBACK_STARTING_PRICE_USD; |
| 222 | $pricing['discount_price'] = self::FALLBACK_STARTING_PRICE_USD; |
| 223 | $pricing['product_term'] = 'year'; |
| 224 | } |
| 225 | |
| 226 | return $pricing; |
| 227 | } |
| 228 | |
| 229 | $pricing['estimated_record_count'] = $record_count; |
| 230 | |
| 231 | return array_merge( $pricing, $search_pricing ); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Get the URL the user is taken after purchasing the product through the checkout |
| 236 | * |
| 237 | * @return ?string |
| 238 | */ |
| 239 | public static function get_post_checkout_url() { |
| 240 | return self::get_manage_url(); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Get the WPCOM product slug used to make the purchase |
| 245 | * |
| 246 | * @return ?string |
| 247 | */ |
| 248 | public static function get_wpcom_product_slug() { |
| 249 | return 'jetpack_search'; |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Get the WPCOM free product slug |
| 254 | * |
| 255 | * @return ?string |
| 256 | */ |
| 257 | public static function get_wpcom_free_product_slug() { |
| 258 | return 'jetpack_search_free'; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Returns true if the new_pricing_202208 is set to not empty in URL for testing purpose, or it's active. |
| 263 | */ |
| 264 | public static function is_new_pricing_202208() { |
| 265 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash |
| 266 | if ( isset( $_GET['new_pricing_202208'] ) && $_GET['new_pricing_202208'] ) { |
| 267 | return true; |
| 268 | } |
| 269 | |
| 270 | $record_count = intval( Search_Stats::estimate_count() ); |
| 271 | $search_pricing = static::get_pricing_from_wpcom( $record_count ); |
| 272 | if ( is_wp_error( $search_pricing ) ) { |
| 273 | // Default to the current pricing experience when the WPCOM fetch fails. |
| 274 | return true; |
| 275 | } |
| 276 | |
| 277 | return self::SEARCH_NEW_PRICING_VERSION === $search_pricing['pricing_version']; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Override status to `needs_activation` when status is `needs_plan`. |
| 282 | */ |
| 283 | public static function get_status() { |
| 284 | $status = parent::get_status(); |
| 285 | return $status; |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Use centralized Search pricing API. |
| 290 | * |
| 291 | * The function is also used by the search package, as a result it could be called before site connection - i.e. blog token might not be available. |
| 292 | * |
| 293 | * @param int $record_count Record count to estimate pricing. |
| 294 | * |
| 295 | * @return array|WP_Error |
| 296 | */ |
| 297 | public static function get_pricing_from_wpcom( $record_count ) { |
| 298 | $connection = new Connection_Manager(); |
| 299 | $blog_id = \Jetpack_Options::get_option( 'id' ); |
| 300 | |
| 301 | if ( isset( self::$pricings[ $record_count ] ) ) { |
| 302 | return self::$pricings[ $record_count ]; |
| 303 | } |
| 304 | |
| 305 | $locale = get_user_locale(); |
| 306 | $currency = ''; |
| 307 | if ( $blog_id ) { |
| 308 | $endpoint = sprintf( '/jetpack-search/pricing?record_count=%1$d&locale=%2$s', $record_count, $locale ); |
| 309 | |
| 310 | // If available in the user data, set the user's currency as one of the params |
| 311 | if ( $connection->is_user_connected() ) { |
| 312 | $user_details = $connection->get_connected_user_data(); |
| 313 | if ( ! empty( $user_details['user_currency'] ) && $user_details['user_currency'] !== 'USD' ) { |
| 314 | $currency = $user_details['user_currency']; |
| 315 | $endpoint .= sprintf( '¤cy=%s', $currency ); |
| 316 | } |
| 317 | } |
| 318 | } else { |
| 319 | $endpoint = sprintf( Constants::get_constant( 'JETPACK__WPCOM_JSON_API_BASE' ) . '/wpcom/v2/jetpack-search/pricing?record_count=%1$d&locale=%2$s', $record_count, $locale ); |
| 320 | } |
| 321 | |
| 322 | // One row per site, locale and currency; a row stored for another record count is a miss. |
| 323 | $transient_key = self::PRICING_TRANSIENT_PREFIX . md5( $locale . '|' . $currency . '|' . (int) $blog_id ); |
| 324 | $cached = get_transient( $transient_key ); |
| 325 | if ( is_array( $cached ) && ( $cached['endpoint'] ?? null ) === $endpoint && is_array( $cached['pricing'] ?? null ) ) { |
| 326 | self::$pricings[ $record_count ] = $cached['pricing']; |
| 327 | return self::$pricings[ $record_count ]; |
| 328 | } |
| 329 | |
| 330 | // If the site is connected, request pricing with the blog token |
| 331 | if ( $blog_id ) { |
| 332 | $response = Client::wpcom_json_api_request_as_blog( |
| 333 | $endpoint, |
| 334 | '2', |
| 335 | array( 'timeout' => 5 ), |
| 336 | null, |
| 337 | 'wpcom' |
| 338 | ); |
| 339 | } else { |
| 340 | $response = wp_remote_get( $endpoint, array( 'timeout' => 5 ) ); |
| 341 | } |
| 342 | |
| 343 | if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| 344 | // Cache the failure too: get_pricing_for_ui() reaches this twice per request |
| 345 | // (once via has_trial_support(), once directly), and each miss is a 5s timeout. |
| 346 | self::$pricings[ $record_count ] = new WP_Error( 'search_pricing_fetch_failed' ); |
| 347 | return self::$pricings[ $record_count ]; |
| 348 | } |
| 349 | |
| 350 | $body = wp_remote_retrieve_body( $response ); |
| 351 | self::$pricings[ $record_count ] = json_decode( $body, true ); |
| 352 | if ( is_array( self::$pricings[ $record_count ] ) ) { |
| 353 | set_transient( |
| 354 | $transient_key, |
| 355 | array( |
| 356 | 'endpoint' => $endpoint, |
| 357 | 'pricing' => self::$pricings[ $record_count ], |
| 358 | ), |
| 359 | self::PRICING_CACHE_DURATION |
| 360 | ); |
| 361 | } |
| 362 | return self::$pricings[ $record_count ]; |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Checks whether the product supports trial or not |
| 367 | * |
| 368 | * Returns true if it supports. Return false otherwise. |
| 369 | * |
| 370 | * Free products will always return false. |
| 371 | * |
| 372 | * @return boolean |
| 373 | */ |
| 374 | public static function has_trial_support() { |
| 375 | return static::is_new_pricing_202208(); |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Get the product-slugs of the paid plans for this product (not including bundles) |
| 380 | * |
| 381 | * @return array |
| 382 | */ |
| 383 | public static function get_paid_plan_product_slugs() { |
| 384 | return array( |
| 385 | 'jetpack_search', |
| 386 | 'jetpack_search_monthly', |
| 387 | 'jetpack_search_bi_yearly', |
| 388 | ); |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * Checks if the site purchases contain a free search plan |
| 393 | * |
| 394 | * @return bool |
| 395 | */ |
| 396 | public static function has_free_plan_for_product() { |
| 397 | $purchases_data = Wpcom_Products::get_site_current_purchases(); |
| 398 | if ( is_wp_error( $purchases_data ) ) { |
| 399 | return false; |
| 400 | } |
| 401 | if ( is_array( $purchases_data ) && ! empty( $purchases_data ) ) { |
| 402 | foreach ( $purchases_data as $purchase ) { |
| 403 | if ( str_contains( $purchase->product_slug, 'jetpack_search_free' ) ) { |
| 404 | return true; |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | return false; |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * Grant this site the free Search product without sending the user through a $0 checkout. |
| 413 | * |
| 414 | * Safe to call on every activate click: WordPress.com re-pushes its configuration to this |
| 415 | * site before answering `already_entitled`, which is how a half-finished activation repairs. |
| 416 | * |
| 417 | * @since 6.8.0 |
| 418 | * |
| 419 | * @param string|null $source Where the activation was requested from, for reporting. |
| 420 | * @return array|WP_Error Decoded response body on success; on failure a WP_Error whose data |
| 421 | * carries `checkout_fallback` — true when the $0 checkout could still |
| 422 | * succeed, false when it would refuse the product too. |
| 423 | */ |
| 424 | public static function activate_free_product( $source = null ) { |
| 425 | /* |
| 426 | * Both REST routes reach this, and their permission callbacks disagree on multisite, |
| 427 | * so the bar for creating the subscription is enforced here instead. |
| 428 | */ |
| 429 | if ( ! current_user_can( 'activate_plugins' ) || ( is_multisite() && ! current_user_can( 'manage_network' ) ) ) { |
| 430 | return self::activation_error( |
| 431 | 'rest_cannot_activate', |
| 432 | __( 'You are not allowed to activate Jetpack Search Free on this site.', 'jetpack-my-jetpack' ), |
| 433 | 403, |
| 434 | false |
| 435 | ); |
| 436 | } |
| 437 | |
| 438 | /* |
| 439 | * A Simple site has no Jetpack connection to sign the request with, and buys the |
| 440 | * product from within WordPress.com anyway. |
| 441 | */ |
| 442 | if ( ( new Host() )->is_wpcom_simple() ) { |
| 443 | return self::activation_error( |
| 444 | 'not_supported_on_wpcom_simple', |
| 445 | __( 'Jetpack Search Free is activated through WordPress.com on this site.', 'jetpack-my-jetpack' ), |
| 446 | 400, |
| 447 | true |
| 448 | ); |
| 449 | } |
| 450 | |
| 451 | $blog_id = \Jetpack_Options::get_option( 'id' ); |
| 452 | if ( ! $blog_id ) { |
| 453 | return self::activation_error( |
| 454 | 'site_not_registered', |
| 455 | __( 'Connect your site to WordPress.com to activate Jetpack Search.', 'jetpack-my-jetpack' ), |
| 456 | 403, |
| 457 | true |
| 458 | ); |
| 459 | } |
| 460 | |
| 461 | /* |
| 462 | * Checked before the request because the subscription needs a WordPress.com user to own |
| 463 | * it and a blog token names nobody. Checkout signs the user in itself, so this is a |
| 464 | * fallback rather than a failure. |
| 465 | */ |
| 466 | if ( ! ( new Connection_Manager() )->is_user_connected() ) { |
| 467 | return self::activation_error( |
| 468 | 'no_connected_user', |
| 469 | __( 'Activating Jetpack Search Free requires a connected WordPress.com user.', 'jetpack-my-jetpack' ), |
| 470 | 403, |
| 471 | true |
| 472 | ); |
| 473 | } |
| 474 | |
| 475 | // WordPress.com records an unfamiliar source as `unknown` rather than refusing, so pass |
| 476 | // through whatever the caller sent instead of dropping values this copy predates. |
| 477 | $body = array(); |
| 478 | if ( is_string( $source ) && $source !== '' ) { |
| 479 | $body['source'] = sanitize_key( $source ); |
| 480 | } |
| 481 | |
| 482 | $response = Client::wpcom_json_api_request_as_user( |
| 483 | '/sites/' . $blog_id . '/jetpack-search/activate-free', |
| 484 | '2', |
| 485 | // Well over the 10s default: the grant re-pushes Search configuration to this site |
| 486 | // inside the request, so a timeout here can abandon a subscription already created. |
| 487 | array( |
| 488 | 'method' => 'POST', |
| 489 | 'timeout' => 45, |
| 490 | ), |
| 491 | $body, |
| 492 | 'wpcom' |
| 493 | ); |
| 494 | |
| 495 | // Never reached WordPress.com, so checkout may still work. |
| 496 | if ( is_wp_error( $response ) ) { |
| 497 | return self::activation_error( |
| 498 | 'jetpack_search_free_activation_failed', |
| 499 | $response->get_error_message(), |
| 500 | 500, |
| 501 | true |
| 502 | ); |
| 503 | } |
| 504 | |
| 505 | $status = wp_remote_retrieve_response_code( $response ); |
| 506 | $result = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 507 | |
| 508 | if ( 200 !== $status || ! is_array( $result ) || empty( $result['success'] ) ) { |
| 509 | return self::wpcom_activation_error( $result, $status ); |
| 510 | } |
| 511 | |
| 512 | $result['local_activation'] = self::sync_local_state_after_free_activation(); |
| 513 | |
| 514 | return $result; |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * Rebuild a refusal from WordPress.com as a local WP_Error. |
| 519 | * |
| 520 | * Only a body carrying `checkout_fallback` came from the endpoint. Anything else — a 404 |
| 521 | * before it deploys, a proxy error page — means it was never reached, and checkout still |
| 522 | * works, so those fall back rather than dead-ending the user. |
| 523 | * |
| 524 | * @param array|null $result Decoded response body, when it parsed. |
| 525 | * @param int $status HTTP status code. |
| 526 | * @return WP_Error |
| 527 | */ |
| 528 | private static function wpcom_activation_error( $result, $status ) { |
| 529 | $data = isset( $result['data'] ) && is_array( $result['data'] ) ? $result['data'] : array(); |
| 530 | $code = isset( $result['code'] ) && is_string( $result['code'] ) ? $result['code'] : ''; |
| 531 | |
| 532 | /* |
| 533 | * The flag, not the code, is what marks an answer as the endpoint's own: WordPress |
| 534 | * raises `rest_no_route` with a code too, and that one means checkout still works. |
| 535 | */ |
| 536 | $is_endpoint_refusal = array_key_exists( 'checkout_fallback', $data ); |
| 537 | |
| 538 | if ( ! $is_endpoint_refusal ) { |
| 539 | /* |
| 540 | * Always 502, never the upstream status. This route exists; WordPress.com answering |
| 541 | * 404 before the endpoint deploys is not our 404 — and relaying it would have the |
| 542 | * REST client throw a bare Api404Error with no body, losing the `checkout_fallback` |
| 543 | * that sends the user to checkout in exactly that window. |
| 544 | */ |
| 545 | return self::activation_error( |
| 546 | 'jetpack_search_free_activation_failed', |
| 547 | __( 'Jetpack Search Free could not be activated for this site.', 'jetpack-my-jetpack' ), |
| 548 | 502, |
| 549 | true |
| 550 | ); |
| 551 | } |
| 552 | |
| 553 | return self::activation_error( |
| 554 | $code !== '' ? $code : 'jetpack_search_free_activation_failed', |
| 555 | $result['message'] ?? __( 'Jetpack Search Free could not be activated for this site.', 'jetpack-my-jetpack' ), |
| 556 | self::error_status( $data, $status ), |
| 557 | ! empty( $data['checkout_fallback'] ), |
| 558 | $data |
| 559 | ); |
| 560 | } |
| 561 | |
| 562 | /** |
| 563 | * Pick an error status for an enumerated refusal, never a 2xx. |
| 564 | * |
| 565 | * A success status on a WP_Error reaches the browser as a 200, which both REST clients read |
| 566 | * as a granted product. |
| 567 | * |
| 568 | * @param array $data Error data from WordPress.com. |
| 569 | * @param int $status HTTP status of the response. |
| 570 | * @return int |
| 571 | */ |
| 572 | private static function error_status( $data, $status ) { |
| 573 | foreach ( array( $data['status'] ?? 0, $status ) as $candidate ) { |
| 574 | if ( (int) $candidate >= 400 ) { |
| 575 | return (int) $candidate; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | return 502; |
| 580 | } |
| 581 | |
| 582 | /** |
| 583 | * Build a refusal carrying the `checkout_fallback` flag the UI branches on. |
| 584 | * |
| 585 | * @param string $code Machine readable error code. |
| 586 | * @param string $message Human readable message. |
| 587 | * @param int $status HTTP status. |
| 588 | * @param bool $checkout_fallback Whether the caller should fall back to checkout. |
| 589 | * @param array $extra_data Extra data from WordPress.com to preserve. |
| 590 | * @return WP_Error |
| 591 | */ |
| 592 | private static function activation_error( $code, $message, $status, $checkout_fallback, $extra_data = array() ) { |
| 593 | return new WP_Error( |
| 594 | $code, |
| 595 | $message, |
| 596 | array_merge( |
| 597 | $extra_data, |
| 598 | array( |
| 599 | 'status' => $status, |
| 600 | 'checkout_fallback' => (bool) $checkout_fallback, |
| 601 | ) |
| 602 | ) |
| 603 | ); |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * Bring this site's Search state in line after WordPress.com grants the product. |
| 608 | * |
| 609 | * WordPress.com writes the plan option from a separate request, so the copy cached in this |
| 610 | * process predates it and has to be re-read. A site already entitled through a plan or |
| 611 | * bundle gets no such request at all, and this is the only thing that turns Search on. |
| 612 | * |
| 613 | * @return string `activated`, `unavailable` when this plugin does not ship jetpack-search, |
| 614 | * or an error code the caller can surface — re-running the activation repairs it. |
| 615 | */ |
| 616 | private static function sync_local_state_after_free_activation() { |
| 617 | if ( ! class_exists( 'Automattic\Jetpack\Search\Plan' ) ) { |
| 618 | return 'unavailable'; |
| 619 | } |
| 620 | |
| 621 | $plan_info = ( new \Automattic\Jetpack\Search\Plan() )->get_plan_info_from_wpcom(); |
| 622 | if ( is_wp_error( $plan_info ) ) { |
| 623 | return $plan_info->get_error_code(); |
| 624 | } |
| 625 | |
| 626 | if ( ! class_exists( 'Automattic\Jetpack\Search\Module_Control' ) ) { |
| 627 | return 'unavailable'; |
| 628 | } |
| 629 | |
| 630 | $module_control = new Search_Module_Control(); |
| 631 | $activated = $module_control->activate(); |
| 632 | |
| 633 | if ( is_wp_error( $activated ) ) { |
| 634 | return $activated->get_error_code(); |
| 635 | } |
| 636 | |
| 637 | self::set_default_experience_if_unset( $module_control ); |
| 638 | |
| 639 | return $activated ? 'activated' : 'already_active'; |
| 640 | } |
| 641 | |
| 642 | /** |
| 643 | * Give a site with no experience of its own the same one WordPress.com would have pushed. |
| 644 | * |
| 645 | * Only fires when nothing is stored: deactivating Search deliberately keeps the choice for |
| 646 | * the next re-activation, so a site that picked Embedded must not be flipped to Overlay. |
| 647 | * |
| 648 | * @param Search_Module_Control $module_control Module control for this site. |
| 649 | */ |
| 650 | private static function set_default_experience_if_unset( $module_control ) { |
| 651 | if ( get_option( Search_Module_Control::SEARCH_MODULE_EXPERIENCE_OPTION_KEY ) ) { |
| 652 | return; |
| 653 | } |
| 654 | |
| 655 | // Mirrors Jetpack_Plans_Search::DEFAULT_SEARCH_EXPERIENCE on the WordPress.com side. |
| 656 | $module_control->update_experience( Search_Module_Control::EXPERIENCE_OVERLAY ); |
| 657 | } |
| 658 | |
| 659 | /** |
| 660 | * Activates the product. Try to enable instant search after the Search module was enabled. |
| 661 | * |
| 662 | * @param bool|WP_Error $product_activation Is the result of the top level activation actions. You probably won't do anything if it is an WP_Error. |
| 663 | * @param bool $local Skip connected Search setup. |
| 664 | * @return bool|WP_Error |
| 665 | */ |
| 666 | public static function do_product_specific_activation( $product_activation, $local = false ) { |
| 667 | $product_activation = parent::do_product_specific_activation( $product_activation, $local ); |
| 668 | if ( is_wp_error( $product_activation ) || $local ) { |
| 669 | return $product_activation; |
| 670 | } |
| 671 | |
| 672 | if ( class_exists( 'Automattic\Jetpack\Search\Module_Control' ) ) { |
| 673 | ( new Search_Module_Control() )->enable_instant_search(); |
| 674 | } |
| 675 | |
| 676 | // we don't want to change the success of the activation if we fail to activate instant search. That's not mandatory. |
| 677 | return $product_activation; |
| 678 | } |
| 679 | |
| 680 | /** |
| 681 | * Get the URL the user is taken after activating the product |
| 682 | * |
| 683 | * @return ?string |
| 684 | */ |
| 685 | public static function get_post_activation_url() { |
| 686 | return ''; // stay in My Jetpack page or continue the purchase flow if needed. |
| 687 | } |
| 688 | |
| 689 | /** |
| 690 | * Get the URL where the user manages the product |
| 691 | * |
| 692 | * @return ?string |
| 693 | */ |
| 694 | public static function get_manage_url() { |
| 695 | return admin_url( 'admin.php?page=jetpack-search' ); |
| 696 | } |
| 697 | |
| 698 | /** |
| 699 | * Return product bundles list |
| 700 | * that supports the product. |
| 701 | * |
| 702 | * @return boolean|array Products bundle list. |
| 703 | */ |
| 704 | public static function is_upgradable_by_bundle() { |
| 705 | return array( 'complete' ); |
| 706 | } |
| 707 | } |