Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
50.00% covered (danger)
50.00%
8 / 16
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Store_Plan
50.00% covered (danger)
50.00%
8 / 16
33.33% covered (danger)
33.33%
1 / 3
13.12
0.00% covered (danger)
0.00%
0 / 1
 is_commerce_plan
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 purchases_include_commerce_plan
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
 get_commerce_plan_slugs
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Commerce plan detection shared across the WordPress.com admin sidebars.
4 *
5 * @package automattic/jetpack-masterbar
6 */
7
8namespace Automattic\Jetpack\Masterbar;
9
10use Automattic\Jetpack\Current_Plan;
11
12/**
13 * Single source of truth for "is the current site on a Commerce plan", used by both admin
14 * sidebars (the nav-unified Atomic_Admin_Menu and the classic wp-admin jetpack-mu-wpcom menu)
15 * so their relabeling stays in sync.
16 */
17class Store_Plan {
18
19    /**
20     * Whether the current site is on a Commerce plan, including the Commerce trial.
21     *
22     * Legacy Woo Express plans (wooexpress-*) are intentionally excluded: those users chose a
23     * Woo-branded product, so they keep the "WooCommerce" label.
24     *
25     * @return bool
26     */
27    public static function is_commerce_plan() {
28        if ( ! function_exists( 'wpcom_get_site_purchases' ) ) {
29            return false;
30        }
31
32        return self::purchases_include_commerce_plan( wpcom_get_site_purchases() );
33    }
34
35    /**
36     * Whether the given purchases include a Commerce plan.
37     *
38     * Split out so callers that fetch purchases their own way (e.g. an injectable seam in
39     * tests) can reuse the same matching logic.
40     *
41     * @param array $purchases Purchase objects, each with an optional product_slug property.
42     * @return bool
43     */
44    public static function purchases_include_commerce_plan( $purchases ) {
45        $commerce_slugs = self::get_commerce_plan_slugs();
46        foreach ( (array) $purchases as $purchase ) {
47            if ( isset( $purchase->product_slug ) && in_array( $purchase->product_slug, $commerce_slugs, true ) ) {
48                return true;
49            }
50        }
51
52        return false;
53    }
54
55    /**
56     * Commerce plan slugs (Commerce and the Commerce trial).
57     *
58     * Derived from the canonical plan list so new ecommerce-* variants are picked up
59     * automatically. The "business" group also holds Woo Express and other business plans,
60     * so it's narrowed to the ecommerce- family (ecommerce-bundle* plus
61     * ecommerce-trial-bundle-monthly); Woo Express (wooexpress-*) is excluded.
62     *
63     * @return string[]
64     */
65    public static function get_commerce_plan_slugs() {
66        return array_values(
67            array_filter(
68                Current_Plan::PLAN_DATA['business']['plans'],
69                static function ( $slug ) {
70                    return str_starts_with( $slug, 'ecommerce-' );
71                }
72            )
73        );
74    }
75}