Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
81.58% covered (warning)
81.58%
31 / 38
40.00% covered (danger)
40.00%
2 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Podcast_Gate
81.58% covered (warning)
81.58%
31 / 38
40.00% covered (danger)
40.00%
2 / 5
28.91
0.00% covered (danger)
0.00%
0 / 1
 has_product_access
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
4.03
 get_required_plan_slug
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 self_hosted_has_paid_plan
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
7
 get_site_current_purchases
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
 is_grandfathered
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
0.00%
0 / 1
7.05
1<?php
2/**
3 * Podcast product-access gate.
4 *
5 * @package automattic/jetpack-podcast
6 */
7
8namespace Automattic\Jetpack\Podcast;
9
10use Automattic\Jetpack\Current_Plan;
11use Automattic\Jetpack\My_Jetpack\Products as My_Jetpack_Products;
12use Automattic\Jetpack\My_Jetpack\Wpcom_Products;
13use Automattic\Jetpack\Status\Host;
14
15/**
16 * Premium podcast feature gate (dashboard, stats, episode block). Two paths:
17 *
18 * - WordPress.com (Simple/WoA): the `podcasting` plan feature via
19 *   `Current_Plan::supports`, plus the launch-day grandfather rule. Request-
20 *   scoped, so gating another blog needs `switch_to_blog` first.
21 * - Self-hosted Jetpack: a Growth/Complete purchase over the connection
22 *   (PODS-123). Only consulted in admin/editor contexts.
23 */
24class Podcast_Gate {
25
26    const FEATURE_SLUG = 'podcasting';
27
28    /**
29     * Launch-day cutoff for the paying-blog grandfather rule. Paid blogs
30     * registered before this date keep Premium podcast features without
31     * needing the `podcasting` plan feature. WordPress.com only.
32     */
33    const GRANDFATHER_CUTOFF_DATE = '2026-05-18';
34
35    /**
36     * Transient holding the cached `/upgrades` response. Short-lived (30s): dedupes
37     * the lookup across the successive editor/admin loads that consult the gate,
38     * without a synchronous WPCOM request on each one.
39     */
40    const PURCHASES_TRANSIENT = 'jetpack_podcast_site_purchases';
41
42    /**
43     * Whether the current site can use the paid podcast surfaces.
44     *
45     * @return bool
46     */
47    public static function has_product_access(): bool {
48        if ( ! ( new Host() )->is_wpcom_platform() ) {
49            return self::self_hosted_has_paid_plan();
50        }
51
52        $blog_id = get_current_blog_id();
53        if ( $blog_id <= 0 ) {
54            return false;
55        }
56
57        if ( self::is_grandfathered( $blog_id ) ) {
58            return true;
59        }
60
61        return (bool) Current_Plan::supports( self::FEATURE_SLUG );
62    }
63
64    /**
65     * The plan slug offered by the editor's upgrade prompt.
66     *
67     * @return string
68     */
69    public static function get_required_plan_slug(): string {
70        if ( ( new Host() )->is_wpcom_platform() ) {
71            return 'value_bundle';
72        }
73        if ( method_exists( My_Jetpack_Products::class, 'get_bundle_offer' ) ) {
74            return My_Jetpack_Products::get_bundle_offer( 'growth' )['wpcom_product_slug'] ?? '';
75        }
76        return 'jetpack_growth_yearly';
77    }
78
79    /**
80     * Whether a self-hosted site owns a Growth/Complete plan. Matches purchased
81     * product slugs, not the `podcasting` feature (which is true for every Jetpack
82     * site on WordPress.com and can't tell free from paid here).
83     */
84    private static function self_hosted_has_paid_plan(): bool {
85        foreach ( self::get_site_current_purchases() as $purchase ) {
86            $slug = is_array( $purchase ) && isset( $purchase['product_slug'] ) ? $purchase['product_slug'] : '';
87
88            // Prefix match so every Growth/Complete billing term counts.
89            foreach ( array( 'jetpack_growth', 'jetpack_complete', 'jetpack_pro_' ) as $prefix ) {
90                if ( is_string( $slug ) && 0 === strpos( $slug, $prefix ) ) {
91                    return true;
92                }
93            }
94        }
95
96        return false;
97    }
98
99    /**
100     * The site's current purchases from WordPress.com (`/upgrades`). Cached in a
101     * short transient so successive gate checks don't each fire a WPCOM request.
102     * Fails closed to an empty list on any error, without caching it, so the next
103     * request retries rather than serving a stale empty.
104     *
105     * @return array Purchase entries; empty on failure.
106     */
107    private static function get_site_current_purchases(): array {
108        $cached = get_transient( self::PURCHASES_TRANSIENT );
109        if ( is_array( $cached ) ) {
110            return $cached;
111        }
112
113        // My Jetpack reads the same list on the Podcast page, so reuse its request; the round trip turns its objects into arrays.
114        $purchases = Wpcom_Products::get_site_current_purchases();
115        $decoded   = is_array( $purchases ) ? json_decode( wp_json_encode( $purchases, JSON_UNESCAPED_SLASHES ), true ) : null;
116
117        if ( ! is_array( $decoded ) ) {
118            return array();
119        }
120
121        set_transient( self::PURCHASES_TRANSIENT, $decoded, 30 );
122        return $decoded;
123    }
124
125    /**
126     * Whether the blog is grandfathered: registered before the cutoff AND on a paid plan.
127     *
128     * @param int $blog_id Blog ID.
129     */
130    protected static function is_grandfathered( int $blog_id ): bool {
131        if ( ! function_exists( 'get_blog_details' ) ) {
132            return false;
133        }
134        $details = get_blog_details( $blog_id );
135        if ( ! $details || empty( $details->registered ) ) {
136            return false;
137        }
138        $registered_ts = strtotime( $details->registered );
139        if ( false === $registered_ts || $registered_ts >= strtotime( self::GRANDFATHER_CUTOFF_DATE ) ) {
140            return false;
141        }
142
143        $plan = Current_Plan::get();
144        return ! empty( $plan['class'] ) && 'free' !== $plan['class'];
145    }
146}