Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
33.33% covered (danger)
33.33%
2 / 6
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCom_Themes_Tiers
33.33% covered (danger)
33.33%
2 / 6
66.67% covered (warning)
66.67%
2 / 3
12.41
0.00% covered (danger)
0.00%
0 / 1
 tier_of
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 feature_for
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 site_can_use
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * Class WPCom_Themes_Tiers.
4 * What a site's plan includes, by theme tier.
5 *
6 * @package automattic/jetpack-mu-wpcom
7 */
8
9/**
10 * Answers which theme tiers a site's plan includes.
11 *
12 * Entitlement only. Whether a surface shows a tier it cannot install is that surface's own
13 * decision: core's Add Themes tabs hide partner themes because they need their own subscription,
14 * while the Marketplace tab lists them to sell them.
15 */
16class WPCom_Themes_Tiers {
17
18    /**
19     * The tier every site can use.
20     */
21    const FREE = 'free';
22
23    /**
24     * The tier of a theme as WordPress.com returns it.
25     *
26     * @param stdClass $theme A theme from the WordPress.com themes API.
27     * @return string Tier slug, premium for a theme that reports none.
28     */
29    public static function tier_of( $theme ): string {
30        return $theme->theme_tier->slug ?? 'premium';
31    }
32
33    /**
34     * The capability name gating a tier.
35     *
36     * @param string $tier Tier slug.
37     * @return string
38     */
39    public static function feature_for( string $tier ): string {
40        return $tier . '-themes';
41    }
42
43    /**
44     * Whether this site's plan includes a tier.
45     *
46     * Necessary but not sufficient for the partner tier, which every paid plan includes while each
47     * partner theme still needs its own subscription that no plan feature reports.
48     *
49     * @param string $tier Tier slug.
50     * @return bool True for the free tier, which needs no plan. For the rest, false when the
51     *              platform cannot answer, so an unknown host offers nothing.
52     */
53    public static function site_can_use( string $tier ): bool {
54        // There is no free-themes capability, so asking the platform would deny every free theme.
55        if ( self::FREE === $tier ) {
56            return true;
57        }
58
59        return function_exists( 'wpcom_site_has_feature' )
60            && (bool) wpcom_site_has_feature( self::feature_for( $tier ) );
61    }
62}