Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
71.58% |
68 / 95 |
|
52.63% |
10 / 19 |
CRAP | |
0.00% |
0 / 1 |
| Search | |
73.12% |
68 / 93 |
|
52.63% |
10 / 19 |
66.05 | |
0.00% |
0 / 1 |
| get_name | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_title | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 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 | |
90.00% |
18 / 20 |
|
0.00% |
0 / 1 |
3.01 | |||
| get_post_checkout_url | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 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 | |
71.43% |
5 / 7 |
|
0.00% |
0 / 1 |
4.37 | |||
| get_status | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| get_pricing_from_wpcom | |
78.57% |
22 / 28 |
|
0.00% |
0 / 1 |
8.63 | |||
| 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 | |
37.50% |
3 / 8 |
|
0.00% |
0 / 1 |
14.79 | |||
| do_product_specific_activation | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| 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 | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 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\Search\Plan as Search_Plan; |
| 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 | * The product slug |
| 37 | * |
| 38 | * @var string |
| 39 | */ |
| 40 | public static $slug = 'search'; |
| 41 | |
| 42 | /** |
| 43 | * The Jetpack module name |
| 44 | * |
| 45 | * @var string |
| 46 | */ |
| 47 | public static $module_name = 'search'; |
| 48 | |
| 49 | /** |
| 50 | * The slug of the plugin associated with this product. |
| 51 | * |
| 52 | * @var string |
| 53 | */ |
| 54 | public static $plugin_slug = 'jetpack-search'; |
| 55 | |
| 56 | /** |
| 57 | * The category of the product |
| 58 | * |
| 59 | * @var string |
| 60 | */ |
| 61 | public static $category = 'performance'; |
| 62 | |
| 63 | /** |
| 64 | * Search has a standalone plugin |
| 65 | * |
| 66 | * @var bool |
| 67 | */ |
| 68 | public static $has_standalone_plugin = true; |
| 69 | |
| 70 | /** |
| 71 | * Whether this product has a free offering |
| 72 | * |
| 73 | * @var bool |
| 74 | */ |
| 75 | public static $has_free_offering = true; |
| 76 | |
| 77 | /** |
| 78 | * Whether this product requires a plan to work at all |
| 79 | * |
| 80 | * @var bool |
| 81 | */ |
| 82 | public static $requires_plan = true; |
| 83 | |
| 84 | /** |
| 85 | * The filename (id) of the plugin associated with this product. |
| 86 | * |
| 87 | * @var string |
| 88 | */ |
| 89 | public static $plugin_filename = array( |
| 90 | 'jetpack-search/jetpack-search.php', |
| 91 | 'search/jetpack-search.php', |
| 92 | 'jetpack-search-dev/jetpack-search.php', |
| 93 | ); |
| 94 | |
| 95 | /** |
| 96 | * Search only requires site connection |
| 97 | * |
| 98 | * @var boolean |
| 99 | */ |
| 100 | public static $requires_user_connection = true; |
| 101 | |
| 102 | /** |
| 103 | * The feature slug that identifies the paid plan |
| 104 | * |
| 105 | * @var string |
| 106 | */ |
| 107 | public static $feature_identifying_paid_plan = 'search'; |
| 108 | |
| 109 | /** |
| 110 | * Get the product name |
| 111 | * |
| 112 | * @return string |
| 113 | */ |
| 114 | public static function get_name() { |
| 115 | return 'Search'; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Get the product title |
| 120 | * |
| 121 | * @return string |
| 122 | */ |
| 123 | public static function get_title() { |
| 124 | return 'Jetpack Search'; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Get the internationalized product description |
| 129 | * |
| 130 | * @return string |
| 131 | */ |
| 132 | public static function get_description() { |
| 133 | return __( 'Instantly deliver the most relevant results to your visitors.', 'jetpack-my-jetpack' ); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Get the internationalized product long description |
| 138 | * |
| 139 | * @return string |
| 140 | */ |
| 141 | public static function get_long_description() { |
| 142 | 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' ); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Get the internationalized features list |
| 147 | * |
| 148 | * @return array Boost features list |
| 149 | */ |
| 150 | public static function get_features() { |
| 151 | return array( |
| 152 | __( 'Instant search and indexing', 'jetpack-my-jetpack' ), |
| 153 | __( 'Powerful filtering', 'jetpack-my-jetpack' ), |
| 154 | __( 'Supports 38 languages', 'jetpack-my-jetpack' ), |
| 155 | __( 'Spelling correction', 'jetpack-my-jetpack' ), |
| 156 | ); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Get the product princing details |
| 161 | * |
| 162 | * @return array Pricing details |
| 163 | */ |
| 164 | public static function get_pricing_for_ui() { |
| 165 | // Basic pricing info. |
| 166 | $pricing = array_merge( |
| 167 | array( |
| 168 | 'available' => true, |
| 169 | 'trial_available' => static::has_trial_support(), |
| 170 | 'wpcom_product_slug' => static::get_wpcom_product_slug(), |
| 171 | 'wpcom_free_product_slug' => static::get_wpcom_free_product_slug(), |
| 172 | ), |
| 173 | Wpcom_Products::get_product_pricing( static::get_wpcom_product_slug() ) |
| 174 | ); |
| 175 | |
| 176 | $record_count = intval( Search_Stats::estimate_count() ); |
| 177 | $search_pricing = static::get_pricing_from_wpcom( $record_count ); |
| 178 | |
| 179 | if ( is_wp_error( $search_pricing ) ) { |
| 180 | // Default to the current pricing experience when the WPCOM fetch fails so the |
| 181 | // dashboard degrades to the production default, not the legacy single-card view. |
| 182 | $pricing['pricing_version'] = Search_Plan::JETPACK_SEARCH_NEW_PRICING_VERSION; |
| 183 | |
| 184 | // If the generic product pricing was also unavailable, fall back to a USD |
| 185 | // starting price so the pricing grid renders a price instead of "$0". |
| 186 | if ( empty( $pricing['full_price'] ) ) { |
| 187 | $pricing['currency_code'] = 'USD'; |
| 188 | $pricing['full_price'] = self::FALLBACK_STARTING_PRICE_USD; |
| 189 | $pricing['discount_price'] = self::FALLBACK_STARTING_PRICE_USD; |
| 190 | } |
| 191 | |
| 192 | return $pricing; |
| 193 | } |
| 194 | |
| 195 | $pricing['estimated_record_count'] = $record_count; |
| 196 | |
| 197 | return array_merge( $pricing, $search_pricing ); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Get the URL the user is taken after purchasing the product through the checkout |
| 202 | * |
| 203 | * @return ?string |
| 204 | */ |
| 205 | public static function get_post_checkout_url() { |
| 206 | return self::get_manage_url(); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Get the WPCOM product slug used to make the purchase |
| 211 | * |
| 212 | * @return ?string |
| 213 | */ |
| 214 | public static function get_wpcom_product_slug() { |
| 215 | return 'jetpack_search'; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Get the WPCOM free product slug |
| 220 | * |
| 221 | * @return ?string |
| 222 | */ |
| 223 | public static function get_wpcom_free_product_slug() { |
| 224 | return 'jetpack_search_free'; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Returns true if the new_pricing_202208 is set to not empty in URL for testing purpose, or it's active. |
| 229 | */ |
| 230 | public static function is_new_pricing_202208() { |
| 231 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash |
| 232 | if ( isset( $_GET['new_pricing_202208'] ) && $_GET['new_pricing_202208'] ) { |
| 233 | return true; |
| 234 | } |
| 235 | |
| 236 | $record_count = intval( Search_Stats::estimate_count() ); |
| 237 | $search_pricing = static::get_pricing_from_wpcom( $record_count ); |
| 238 | if ( is_wp_error( $search_pricing ) ) { |
| 239 | // Default to the current pricing experience when the WPCOM fetch fails. |
| 240 | return true; |
| 241 | } |
| 242 | |
| 243 | return Search_Plan::JETPACK_SEARCH_NEW_PRICING_VERSION === $search_pricing['pricing_version']; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Override status to `needs_activation` when status is `needs_plan`. |
| 248 | */ |
| 249 | public static function get_status() { |
| 250 | $status = parent::get_status(); |
| 251 | return $status; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Use centralized Search pricing API. |
| 256 | * |
| 257 | * 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. |
| 258 | * |
| 259 | * @param int $record_count Record count to estimate pricing. |
| 260 | * |
| 261 | * @return array|WP_Error |
| 262 | */ |
| 263 | public static function get_pricing_from_wpcom( $record_count ) { |
| 264 | static $pricings = array(); |
| 265 | $connection = new Connection_Manager(); |
| 266 | $blog_id = \Jetpack_Options::get_option( 'id' ); |
| 267 | |
| 268 | if ( isset( $pricings[ $record_count ] ) ) { |
| 269 | return $pricings[ $record_count ]; |
| 270 | } |
| 271 | |
| 272 | // If the site is connected, request pricing with the blog token |
| 273 | if ( $blog_id ) { |
| 274 | $endpoint = sprintf( '/jetpack-search/pricing?record_count=%1$d&locale=%2$s', $record_count, get_user_locale() ); |
| 275 | |
| 276 | // If available in the user data, set the user's currency as one of the params |
| 277 | if ( $connection->is_user_connected() ) { |
| 278 | $user_details = $connection->get_connected_user_data(); |
| 279 | if ( ! empty( $user_details['user_currency'] ) && $user_details['user_currency'] !== 'USD' ) { |
| 280 | $endpoint .= sprintf( '¤cy=%s', $user_details['user_currency'] ); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | $response = Client::wpcom_json_api_request_as_blog( |
| 285 | $endpoint, |
| 286 | '2', |
| 287 | array( 'timeout' => 5 ), |
| 288 | null, |
| 289 | 'wpcom' |
| 290 | ); |
| 291 | } else { |
| 292 | $response = wp_remote_get( |
| 293 | sprintf( Constants::get_constant( 'JETPACK__WPCOM_JSON_API_BASE' ) . '/wpcom/v2/jetpack-search/pricing?record_count=%1$d&locale=%2$s', $record_count, get_user_locale() ), |
| 294 | array( 'timeout' => 5 ) |
| 295 | ); |
| 296 | } |
| 297 | |
| 298 | if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| 299 | // Cache the failure too: get_pricing_for_ui() reaches this twice per request |
| 300 | // (once via has_trial_support(), once directly), and each miss is a 5s timeout. |
| 301 | $pricings[ $record_count ] = new WP_Error( 'search_pricing_fetch_failed' ); |
| 302 | return $pricings[ $record_count ]; |
| 303 | } |
| 304 | |
| 305 | $body = wp_remote_retrieve_body( $response ); |
| 306 | $pricings[ $record_count ] = json_decode( $body, true ); |
| 307 | return $pricings[ $record_count ]; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Checks whether the product supports trial or not |
| 312 | * |
| 313 | * Returns true if it supports. Return false otherwise. |
| 314 | * |
| 315 | * Free products will always return false. |
| 316 | * |
| 317 | * @return boolean |
| 318 | */ |
| 319 | public static function has_trial_support() { |
| 320 | return static::is_new_pricing_202208(); |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Get the product-slugs of the paid plans for this product (not including bundles) |
| 325 | * |
| 326 | * @return array |
| 327 | */ |
| 328 | public static function get_paid_plan_product_slugs() { |
| 329 | return array( |
| 330 | 'jetpack_search', |
| 331 | 'jetpack_search_monthly', |
| 332 | 'jetpack_search_bi_yearly', |
| 333 | ); |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Checks if the site purchases contain a free search plan |
| 338 | * |
| 339 | * @return bool |
| 340 | */ |
| 341 | public static function has_free_plan_for_product() { |
| 342 | $purchases_data = Wpcom_Products::get_site_current_purchases(); |
| 343 | if ( is_wp_error( $purchases_data ) ) { |
| 344 | return false; |
| 345 | } |
| 346 | if ( is_array( $purchases_data ) && ! empty( $purchases_data ) ) { |
| 347 | foreach ( $purchases_data as $purchase ) { |
| 348 | if ( str_contains( $purchase->product_slug, 'jetpack_search_free' ) ) { |
| 349 | return true; |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | return false; |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Activates the product. Try to enable instant search after the Search module was enabled. |
| 358 | * |
| 359 | * @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. |
| 360 | * @return bool|WP_Error |
| 361 | */ |
| 362 | public static function do_product_specific_activation( $product_activation ) { |
| 363 | $product_activation = parent::do_product_specific_activation( $product_activation ); |
| 364 | if ( is_wp_error( $product_activation ) ) { |
| 365 | return $product_activation; |
| 366 | } |
| 367 | |
| 368 | if ( class_exists( 'Automattic\Jetpack\Search\Module_Control' ) ) { |
| 369 | ( new Search_Module_Control() )->enable_instant_search(); |
| 370 | } |
| 371 | |
| 372 | // we don't want to change the success of the activation if we fail to activate instant search. That's not mandatory. |
| 373 | return $product_activation; |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Get the URL the user is taken after activating the product |
| 378 | * |
| 379 | * @return ?string |
| 380 | */ |
| 381 | public static function get_post_activation_url() { |
| 382 | return ''; // stay in My Jetpack page or continue the purchase flow if needed. |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * Get the URL where the user manages the product |
| 387 | * |
| 388 | * @return ?string |
| 389 | */ |
| 390 | public static function get_manage_url() { |
| 391 | return admin_url( 'admin.php?page=jetpack-search' ); |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Return product bundles list |
| 396 | * that supports the product. |
| 397 | * |
| 398 | * @return boolean|array Products bundle list. |
| 399 | */ |
| 400 | public static function is_upgradable_by_bundle() { |
| 401 | return array( 'complete' ); |
| 402 | } |
| 403 | } |