Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
24.62% covered (danger)
24.62%
49 / 199
0.00% covered (danger)
0.00%
0 / 11
CRAP
n/a
0 / 0
_wpcom_get_current_blog_id
80.00% covered (warning)
80.00%
8 / 10
0.00% covered (danger)
0.00%
0 / 1
7.39
wpcom_site_has_feature
65.22% covered (warning)
65.22%
15 / 23
0.00% covered (danger)
0.00%
0 / 1
42.37
wpcom_site_can_upload_videos
60.00% covered (warning)
60.00%
6 / 10
0.00% covered (danger)
0.00%
0 / 1
8.30
wpcom_get_site_purchases
78.26% covered (warning)
78.26%
18 / 23
0.00% covered (danger)
0.00%
0 / 1
7.50
_wpcom_features_get_simple_site_purchases
0.00% covered (danger)
0.00%
0 / 75
0.00% covered (danger)
0.00%
0 / 1
240
wpcom_datetime_to_iso8601
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
wpcom_product_has_feature
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
20
wpcom_purchase_has_feature
20.00% covered (danger)
20.00%
2 / 10
0.00% covered (danger)
0.00%
0 / 1
4.05
wpcom_get_product_features
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
110
_convert_product_to_purchase
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
72
wpcom_feature_exists
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * THIS FILE EXISTS VERBATIM IN WPCOM AND WPCOMSH.
4 *
5 * DANGER DANGER DANGER!!!
6 * If you make any changes to this file you must MANUALLY update this file in both WPCOM and WPCOMSH.
7 *
8 * This file provides WPCOM_Features class wrapper functions that make checking for a specific feature easy and uniform
9 * across WPCOM and WPCOMSH.
10 *
11 * @package WPCOM_Features
12 */
13
14/**
15 * Load `WPCOM_Features` class.
16 */
17require_once __DIR__ . '/class-wpcom-features.php';
18require_once __DIR__ . '/class-wpcom-site-purchase.php';
19
20/**
21 * Internal function to retrieve the current WP.com blog ID depending on the environment.
22 *
23 * @return int The current blog ID.
24 */
25function _wpcom_get_current_blog_id() {
26    if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
27        return get_current_blog_id();
28    }
29
30    /*
31     * Atomic sites have the WP.com blog ID stored as a Jetpack option. `Jetpack_Options` reads it
32     * from the platform's external storage first, then from the `jetpack_options` row. The row
33     * alone is not enough: after a site migration, the row comes from the source site's database
34     * and has no `id`. get_wpcom_blog_id() in jetpack-mu-wpcom returns this value too.
35     */
36    if ( class_exists( 'Jetpack_Options' ) ) {
37        $blog_id = (int) Jetpack_Options::get_option( 'id' );
38        if ( $blog_id > 0 ) {
39            return $blog_id;
40        }
41    }
42
43    // Read the row directly, so that this works when Jetpack is not loaded.
44    $jetpack_options = get_option( 'jetpack_options' );
45    if ( is_array( $jetpack_options ) && isset( $jetpack_options['id'] ) ) {
46        return (int) $jetpack_options['id'];
47    }
48
49    return get_current_blog_id();
50}
51
52/**
53 * Whether a given feature is available to the current (or specified) site.
54 *
55 * This function pulls the purchases for a given site and uses WPCOM_Features to check if any of those purchases
56 * include the requested $feature.
57 *
58 * @param string $feature A singular feature.
59 * @param int    $blog_id Optional. Blog ID. Defaults to current blog.
60 *
61 * @return bool Does the site have the feature?
62 */
63function wpcom_site_has_feature( $feature, $blog_id = 0 ) {
64    if ( ! $blog_id ) {
65        $blog_id = _wpcom_get_current_blog_id();
66    }
67
68    $blog = null;
69    if ( defined( 'IS_ATOMIC' ) && IS_ATOMIC ) {
70        $site_type = 'wpcom';
71    } else {
72        $blog      = get_blog_details( $blog_id, false );
73        $site_type = is_blog_wpcom( $blog ) || is_blog_atomic( $blog ) ? 'wpcom' : 'jetpack';
74    }
75
76    // A8C override for certain sites.
77    if ( $feature === WPCOM_Features::ADVANCED_SEO && in_array( $blog_id, WPCOM_FEATURES::A8C_SITES_WITH_ADDITIONAL_SEO_FEATURES, true ) ) {
78        return true;
79    }
80
81    /*
82     * A8C override for internal P2s
83     */
84    if ( $feature === WPCOM_Features::AI_ASSISTANT && ( function_exists( 'wpcom_is_automattic_p2_site' ) && wpcom_is_automattic_p2_site( $blog_id ) ) ) {
85        return true;
86    }
87
88    /*
89     * A8C override for wp.org sites to enable JP search
90     */
91    if ( $feature === WPCOM_Features::CLASSIC_SEARCH && ( function_exists( 'wpcom_is_wporg_jp_index' ) && wpcom_is_wporg_jp_index( $blog_id ) ) ) {
92        return true;
93    }
94
95    /*
96     * Sites carrying the 'wordads-free-access' sticker have WordAds regardless of plan.
97     * The wpcom features list honours that sticker separately, but Jetpack gates module
98     * activation through its plan check, which defers here, so without this the sticker
99     * has no effect on Atomic.
100     */
101    if ( in_array( $feature, array( WPCOM_Features::WORDADS, WPCOM_Features::WORDADS_JETPACK ), true ) ) {
102        if ( defined( 'IS_ATOMIC' ) && IS_ATOMIC && function_exists( 'wpcomsh_is_site_sticker_active' ) ) {
103            if ( wpcomsh_is_site_sticker_active( 'wordads-free-access' ) ) {
104                return true;
105            }
106        } elseif ( function_exists( 'has_blog_sticker' ) && has_blog_sticker( 'wordads-free-access', $blog_id ) ) {
107            return true;
108        }
109    }
110
111    $purchases = wpcom_get_site_purchases( $blog_id );
112
113    if ( isset( $blog->registered ) ) {
114        WPCOM_Features::add_free_plan_purchase( $purchases, $site_type, $blog->registered );
115    }
116
117    return WPCOM_Features::has_feature( $feature, $purchases, $site_type, $blog_id );
118}
119
120/**
121 * Find out if the site can upload video files.
122 *
123 * This checks if the site has either VideoPress or the general video upload capability.
124 * Sites with the UPLOAD_VIDEO_FILES feature can upload videos even without VideoPress
125 * (e.g. Premium plans with the gating-business-q1 sticker).
126 *
127 * @param int $blog_id Blog ID. Defaults to the current blog ID if none is passed.
128 * @return bool Whether the site can upload video files.
129 */
130function wpcom_site_can_upload_videos( $blog_id = 0 ) {
131    if ( ! $blog_id ) {
132        $blog_id = _wpcom_get_current_blog_id();
133    }
134
135    // VideoPress includes video upload capability.
136    // On WPCOM, use wpcom_site_has_videopress() to respect the filter.
137    // On WPCOMSH/Atomic, that function doesn't exist so use direct feature check.
138    if ( function_exists( 'wpcom_site_has_videopress' ) ) {
139        if ( wpcom_site_has_videopress( $blog_id ) ) {
140            return true;
141        }
142    } elseif ( wpcom_site_has_feature( WPCOM_Features::VIDEOPRESS, $blog_id ) ) {
143        return true;
144    }
145
146    // Check for the general video upload feature (Premium+ plans with gating-business-q1 sticker).
147    if ( wpcom_site_has_feature( WPCOM_Features::UPLOAD_VIDEO_FILES, $blog_id ) ) {
148        return true;
149    }
150
151    /**
152     * Filters whether the site can upload video files.
153     *
154     * @param bool $can_upload_videos Whether the site can upload video files.
155     * @param int  $blog_id Blog ID.
156     */
157    return apply_filters( 'wpcom_site_can_upload_videos', false, $blog_id );
158}
159
160/**
161 * Returns a list of purchased products.
162 *
163 * This function checks if we're on an Atomic (WPCOMSH) or Simple (WPCOM) site, and pulls the purchases for that current
164 * site.
165 *
166 * @throws Error If $blog_id !== current_blog_id on Atomic sites.
167 *
168 * @param int $blog_id Optional. Blog ID. Defaults to current blog.
169 *
170 * @return array An array of WPCOM_Site_Purchase objects.
171 */
172function wpcom_get_site_purchases( $blog_id = 0 ) {
173    if ( ! $blog_id ) {
174        $blog_id = _wpcom_get_current_blog_id();
175    }
176
177    if ( defined( 'IS_ATOMIC' ) && IS_ATOMIC ) {
178        /*
179         * The local blog ID can only mean this site. An older jetpack-mu-wpcom reads only the
180         * `jetpack_options` row, which can lack the ID after a site migration, and then passes it.
181         */
182        if ( get_current_blog_id() === (int) $blog_id ) {
183            $blog_id = _wpcom_get_current_blog_id();
184        }
185
186        if ( _wpcom_get_current_blog_id() !== $blog_id ) {
187            throw new Error(
188                'Atomic sites do not support looking up features for sites other than the current site.'
189            );
190        }
191
192        // Atomic site (WPCOMSH) purchases are stored in Atomic Persistent Data as a JSON encoded string.
193        $persistent_data = wpcomsh_get_atomic_persistent_data();
194
195        if ( ! $persistent_data->WPCOM_PURCHASES ) { // phpcs:ignore WordPress.NamingConventions
196            return array();
197        }
198
199        // An entry that is not an object cannot be a purchase. Skipping keeps a payload of the
200        // wrong shape a degraded answer rather than a fatal on a near-universal code path.
201        $entries = array_filter( (array) json_decode( $persistent_data->WPCOM_PURCHASES ), 'is_object' ); // phpcs:ignore WordPress.NamingConventions
202
203        $purchases = array_map(
204            static fn ( $entry ) => WPCOM_Site_Purchase::from_synced_payload( $entry, $blog_id ),
205            array_values( $entries )
206        );
207
208    } else {
209        // Allow overriding the blog ID for feature checks.
210        $blog_id = apply_filters( 'wpcom_site_has_feature_blog_id', $blog_id );
211
212        $purchases = array_map(
213            static fn ( $row ) => WPCOM_Site_Purchase::from_store_row( $row, $blog_id ),
214            _wpcom_features_get_simple_site_purchases( $blog_id )
215        );
216    }
217
218    return $purchases;
219}
220
221/**
222 * INTERNAL function to fetch purchases for a WPCOM Simple site.
223 * The function will return an empty array if we're running in an Atomic context.
224 *
225 * @param int $blog_id The blog ID to fetch purchases for.
226 * @return array The currently active purchases on the site.
227 */
228function _wpcom_features_get_simple_site_purchases( $blog_id ) {
229    global $wpdb;
230
231    if ( defined( 'IS_ATOMIC' ) && IS_ATOMIC ) {
232        // Make _super_ sure the function is available.
233        if ( function_exists( '_doing_it_wrong' ) ) {
234            _doing_it_wrong(
235                __FUNCTION__,
236                'Support for this function is only in available in contexts where the WordPress.com Store databases are available.',
237                false // No version.
238            );
239        }
240
241        return array();
242    }
243
244    if ( ! $blog_id ) {
245        $blog_id = _wpcom_get_current_blog_id();
246    }
247
248    // Optional skip, used for suspended/spam/deleted sites.
249    $skip_purchase_lookup = apply_filters( 'wpcom_simple_skip_purchase_lookup', false, $blog_id );
250    if ( $skip_purchase_lookup ) {
251        return array();
252    }
253
254    // 'site_purchases' belong to $global_groups in ./wp-content/object-cache.php
255    $wp_cache_group = 'site_purchases';
256    $wp_cache_found = false;
257
258    // The DB table is included in $wp_cache_key to avoid cache pollution between the production and test store.
259    $wp_cache_key = "$blog_id-{$wpdb->store_subscriptions}";
260
261    // Check wp_cache_get() for $purchases. If none exist $wp_cache_found will be false.
262    $purchases = wp_cache_get( $wp_cache_key, $wp_cache_group, false, $wp_cache_found );
263
264    if ( false !== $wp_cache_found ) {
265        return (array) $purchases;
266    }
267
268    $lock_key   = "{$wp_cache_key}_building";
269    $lock_found = false;
270    // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
271    $lock = wp_cache_get( $lock_key, $wp_cache_group, false, $lock_found );
272    if ( $lock_found ) {
273        // Another request is already rebuilding the purchases cache.
274        // Let's try 2 short sleeps of 200ms to see if that finishes, and if not,
275        // return an empty set of purchases. The DB must be backed up and we don't
276        // want to contribute to the problem.
277        $attempts = 2;
278        while ( $attempts > 0 ) {
279            // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
280            $lock = wp_cache_get( $lock_key, $wp_cache_group, true /* force remote recheck */, $lock_found );
281            if ( ! $lock_found ) {
282                // The lock is no longer in cache, so we can break out of the loop.
283                break;
284            }
285            // Sleep 200ms
286            usleep( 200000 );
287            --$attempts;
288        }
289
290        // Has the first request has populated the cache?
291        $purchases = wp_cache_get( $wp_cache_key, $wp_cache_group, true /* force remote recheck */, $wp_cache_found );
292        if ( false !== $wp_cache_found ) {
293            return (array) $purchases;
294        }
295
296        // It's still not there, return [] as a fallback;
297        // Also let future checks for this blog for the life of this request fail
298        $target_blog_id = $blog_id;
299        add_filter(
300            'wpcom_simple_skip_purchase_lookup',
301            function ( $skip, $checked_blog_id ) use ( $target_blog_id ) {
302                if ( $checked_blog_id === $target_blog_id ) {
303                    return true; // Skip purchases lookup
304                }
305                return $skip;
306            },
307            10,
308            2
309        );
310        return array();
311    }
312
313    // Let other requests know that we are rebuilding purchases, to stop multiples of the same
314    // request from stacking up.
315    wp_cache_set( $lock_key, true, $wp_cache_group, 30 ); // (30 second TTL)
316
317    // Get $purchases with a direct SQL query.
318    // We are intentionally NOT using the Purchases API as this code needs to be runnable
319    // in some contexts where the billing code-base is not available.
320    // See pdqkMK-18D-p2 for more discussion and context.
321
322    // phpcs:ignore WordPress.DB.DirectDatabaseQuery
323    $purchases = $wpdb->get_results(
324        $wpdb->prepare(
325            "
326                    SELECT
327                        product.product_slug,
328                        product.product_id,
329                        product.billing_product_id,
330                        product.product_type,
331                        subscription.subscribed_date,
332                        subscription.expiry AS 'expiry_date',
333                        subscription.id AS subscription_id,
334                        subscription.auto_renew AS user_allows_auto_renew
335                    FROM `$wpdb->store_subscriptions` AS subscription
336                    LEFT JOIN `$wpdb->store_products` AS product ON subscription.product_id = product.product_id
337                    WHERE
338                        subscription.blog_id = %d
339                        AND subscription.active = 1
340                    ORDER BY subscription.id DESC
341                    ",
342            $blog_id
343        )
344    );
345
346    static $billing_product_data = array();
347
348    $billing_product_ids          = array_unique( wp_list_pluck( $purchases, 'billing_product_id' ) );
349    $billing_product_ids_to_query = array_diff( $billing_product_ids, array_keys( $billing_product_data ) );
350    if ( ! empty( $billing_product_ids_to_query ) ) {
351        // We need to query the billing_products table via a separate query.
352
353        // phpcs:ignore WordPress.DB.DirectDatabaseQuery
354        $billing_products = $wpdb->get_results(
355            "
356                    SELECT
357                        product_id,
358                        product_slug
359                    FROM
360                        `$wpdb->billing_products`
361                    WHERE
362                        " . $wpdb->build_IN_condition( 'product_id', $billing_product_ids_to_query, '%d' ) // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
363        );
364
365        foreach ( $billing_products as $billing_product ) {
366            $billing_product_data[ $billing_product->product_id ] = $billing_product->product_slug;
367        }
368    }
369
370    // Format the dates to match WPCOMSH data.
371    foreach ( $purchases as $purchase ) {
372        $purchase->billing_product_slug   = $billing_product_data[ $purchase->billing_product_id ] ?? '';
373        $purchase->subscribed_date        = wpcom_datetime_to_iso8601( $purchase->subscribed_date );
374        $purchase->expiry_date            = wpcom_datetime_to_iso8601( $purchase->expiry_date );
375        $purchase->user_allows_auto_renew = ! empty( $purchase->user_allows_auto_renew );
376        // Ensure we remove billing_product_id from the purchase data.
377        unset( $purchase->billing_product_id );
378    }
379
380    /*
381    * Cache the $purchases for 6-8 hours. Otherwise, the cache is invalidated when a purchase is made, using:
382    * add_action( 'subscription_changed', 'clear_wp_cache_site_purchases', 10, 1 );
383    * Found in ./wp-content/mu-plugins/wpcom-features.php
384    */
385    // phpcs:ignore WordPress.WP.AlternativeFunctions.rand_mt_rand
386    wp_cache_set( $wp_cache_key, $purchases, $wp_cache_group, ( 6 * HOUR_IN_SECONDS ) + mt_rand( 1, 2 * 60 * MINUTE_IN_SECONDS ) );
387    wp_cache_delete( $lock_key, $wp_cache_group ); // Release the lock telling other requests we are building purchases.
388
389    return $purchases;
390}
391
392/**
393 * Parse and format a date string to ISO8601, but fall back to $default if the string is bad or '0000-00-00'.
394 *
395 * @param string $date A string representing a datetime that we wish to format to ISO8601.
396 * @param string $default Use this datetime if $date errors. Useful for predictable unit testing. Defaults to 'now'.
397 *
398 * @return string A date string in ISO8601 format.
399 */
400function wpcom_datetime_to_iso8601( $date, $default = 'now' ) {
401    /*
402     * Datetimes containing '0000-00-00' convert to '-001-11-30T00:00:00+00:00' which is not useful, so set it or
403     * empty $date to $default.
404     */
405    if ( empty( $date ) || false !== strpos( $date, '0000-00-00' ) ) {
406        $date = $default;
407    }
408
409    try {
410        return ( new DateTime( $date ) )->format( 'c' );
411    } catch ( Exception $e ) {
412        return ( new DateTime( $default ) )->format( 'c' );
413    }
414}
415
416/**
417 * Checks whether the given product contains the passed feature.
418 *
419 * This function converts atomic supported plan slugs and other product aliases to product objects. It then uses
420 * WPCOM_Features to check if product include the requested $feature.
421 *
422 * Do not pass a Store_Subscription to this function. For that case, use wpcom_purchase_has_feature().
423 *
424 * @param string|int|Store_Product $product A Store_Product object, a product slug, or ID.
425 * @param string                   $feature The name of the feature to check.
426 *
427 * @return bool
428 */
429function wpcom_product_has_feature( $product, $feature ) {
430    if ( defined( 'IS_ATOMIC' ) && IS_ATOMIC ) {
431        _doing_it_wrong(
432            __FUNCTION__,
433            'Support for this function is only in available in contexts where the store products database is available.',
434            '' // No version.
435        );
436        return false;
437    }
438
439    $purchase = _convert_product_to_purchase( $product );
440    if ( ! $purchase ) {
441        return false;
442    }
443
444    return wpcom_purchase_has_feature( $purchase, $feature );
445}
446
447/**
448 * Checks whether the given purchase (Store_Subscription) contains the passed feature.
449 *
450 * This function is similar to `wpcom_product_has_feature` with the difference that this function can check for legacy
451 * features because purchases contain a `subscribed_date` field whereas products do not.
452 *
453 * Do not pass a Store_Product to this function. For that case, use wpcom_product_has_feature().
454 *
455 * @param Store_Subscription|object $purchase A Store_Subscription object or purchase serialized object.
456 * @param string                    $feature The name of the feature to check.
457 *
458 * @return bool
459 */
460function wpcom_purchase_has_feature( $purchase, $feature ) {
461    if ( $purchase instanceof Store_Subscription ) {
462        /**
463         * We retrieve the product_slug and product_type directly from the Store_Product_List
464         * cache instead of relying on the internals of Store_Subscription to retrieve it.
465         *
466         * The issue is that simply "->product_slug" or "->product_type" can call a custom __get(),
467         * which can issue SQL queries that take > 10ms for information is not needed. This assignment
468         * grabs the value directly from the cached store_products data avoiding any unnecessary queries.
469         */
470        $product = Store_Product_List::get_from_cache()[ $purchase->product_id ];
471
472        $purchase = (object) array(
473            'product_slug'    => $product['product_slug'],
474            'product_id'      => (string) $purchase->product_id,
475            'product_type'    => $product['product_type'],
476            'subscribed_date' => wpcom_datetime_to_iso8601( $purchase->subscribed_date ),
477            'expiry_date'     => wpcom_datetime_to_iso8601( $purchase->expiry ),
478        );
479    }
480
481    return WPCOM_Features::has_feature( $feature, array( $purchase ) );
482}
483
484/**
485 * Returns a list of features that are associated with the passed product.
486 *
487 * @param string|int|Store_Product $product A Store_Product object, a product slug, or ID.
488 *
489 * @return string[]
490 */
491function wpcom_get_product_features( $product ) {
492    if ( defined( 'IS_ATOMIC' ) && IS_ATOMIC ) {
493        _doing_it_wrong(
494            __FUNCTION__,
495            'Support for this function is only in available in contexts where the store products database is available.',
496            '' // No version.
497        );
498        return array();
499    }
500    // @codeCoverageIgnoreStart
501    $purchase = _convert_product_to_purchase( $product );
502    if ( ! $purchase ) {
503        return array();
504    }
505
506    $cache_group = 'site_purchases';
507    $cache_found = false;
508
509    /*
510    Adjust the cache key to include site legacy/gating_2026 status. Applying it to all plans
511     * and not just Personal/Premium to make it future-proof and avoid issue like
512     * WPCOM_Store::get_wpcom_personal_plans() not returning the Personal trial plan.
513     *
514     * The cohort is not the only per-site input: `required_sticker` branches read stickers too, and
515     * `site_purchases` is a global cache group. WPCOM_Features builds the whole suffix because it
516     * owns the map that decides what varies.
517     *
518     * method_exists() covers the deploy window, when this file can land before the class does. The
519     * class that lacks the method still carries the pre-2026 map, which varies only on
520     * `gating-business-q1`, so the fallback keys on that rather than dropping the cohort from the
521     * key entirely -- an empty suffix would let two Personal/Premium sites in different cohorts
522     * share one entry. Drop the guard once the class change is fully deployed.
523     */
524    $blog_id_for_gating = _wpcom_get_current_blog_id();
525    if ( method_exists( 'WPCOM_Features', 'get_site_gating_cache_suffix' ) ) {
526        $gating_cache_suffix = WPCOM_Features::get_site_gating_cache_suffix( $blog_id_for_gating );
527    } elseif ( function_exists( 'has_blog_sticker' ) && has_blog_sticker( 'gating-business-q1', $blog_id_for_gating ) ) {
528        $gating_cache_suffix = '_gatingbq1';
529    } else {
530        $gating_cache_suffix = '';
531    }
532
533    $cache_key = $purchase->product_slug
534        . $gating_cache_suffix
535        . filemtime( __DIR__ . '/class-wpcom-features.php' );
536    $features  = wp_cache_get( $cache_key, $cache_group, false, $cache_found );
537
538    if ( false === $cache_found ) {
539        $features = array();
540
541        foreach ( WPCOM_Features::get_feature_slugs() as $feature ) {
542            if ( wpcom_purchase_has_feature( $purchase, $feature ) ) {
543                $features[] = $feature;
544            }
545        }
546
547        wp_cache_set( $cache_key, $features, $cache_group, DAY_IN_SECONDS );
548    }
549
550    return $features;
551    // @codeCoverageIgnoreEnd
552}
553
554/**
555 * Converts a store product to a purchase object compatible with `WPCOM_Features::has_feature`.
556 *
557 * @param string|int|Store_Product $product A Store_Product object, a product slug, or ID.
558 *
559 * @return null|object
560 */
561function _convert_product_to_purchase( $product ) {
562    if ( ! is_numeric( $product ) && ! is_string( $product ) && ! ( $product instanceof Store_Product ) ) {
563        _doing_it_wrong(
564            __FUNCTION__,
565            'The $purchase parameter should be of type string|int|Store_Product.',
566            false // No version.
567        );
568        return null;
569    }
570
571    if ( is_string( $product ) && ! is_numeric( $product ) ) {
572        require_once WP_CONTENT_DIR . '/admin-plugins/wpcom-billing/class.wpcom-billingdaddy.php';
573        $product = WPCOM_Billingdaddy::store_product_slug_to_product_id( $product );
574    }
575
576    if ( is_numeric( $product ) ) {
577        $product_cache = Store_Product_List::get_from_cache();
578        if ( ! array_key_exists( $product, $product_cache ) ) {
579            return null;
580        }
581        $product = (object) $product_cache[ $product ];
582    }
583
584    return (object) array(
585        'product_slug'    => $product->product_slug,
586        'product_id'      => (string) $product->product_id,
587        'product_type'    => $product->product_type,
588        'subscribed_date' => null,
589        'expiry_date'     => null,
590    );
591}
592
593/**
594 * Checks whether the given feature exists in WordPress.com.
595 *
596 * @param string $feature The name of the feature to check.
597 *
598 * @return bool Whether the feature exists.
599 */
600function wpcom_feature_exists( $feature ) {
601    return WPCOM_Features::feature_exists( $feature );
602}