Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
Wpcom_PayPal_Buttons_Rollout
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
6 / 6
10
100.00% covered (success)
100.00%
1 / 1
 init
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 rollout_default
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 apply_kill_switch
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 is_killed
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 percent
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 blog_bucket
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * PayPal API-managed buttons rollout on WordPress.com: a percent of Simple sites, Atomic once PERCENT is 100, behind a kill switch.
4 *
5 * @package automattic/jetpack-mu-wpcom
6 */
7
8namespace Automattic\Jetpack\Jetpack_Mu_Wpcom;
9
10use Automattic\Jetpack\Constants;
11
12/**
13 * Turns the `paypal-payments-api-managed-buttons` flag on for a percent of sites, and lets WordPress.com force it off.
14 *
15 * Precedence, highest first: kill switch, the site's Tools > Feature Flags override, the rollout default.
16 * Temporary: remove once PERCENT is 100, the flag defaults on everywhere, and the kill switch is retired.
17 * The PayPal onboarding endpoint calls is_killed(), so remove that call with this class.
18 */
19class Wpcom_PayPal_Buttons_Rollout {
20
21    /**
22     * The flag PayPal_Payment_Buttons registers in the paypal-payments package, which this package does not require.
23     */
24    const FLAG = 'paypal-payments-api-managed-buttons';
25
26    /**
27     * Network option that, when truthy, turns the flag off on every Simple site and refuses live PayPal for every caller.
28     * Atomic reads its own options, so use KILLED there.
29     */
30    const KILL_SWITCH_OPTION = 'wpcom_paypal_buttons_kill_switch';
31
32    /**
33     * Percent of Simple sites with the flag on, 0-100. Atomic turns on at 100. Only ever raise it.
34     */
35    const PERCENT = 0;
36
37    /**
38     * True turns the flag off on Simple and Atomic, and refuses live PayPal for every caller.
39     */
40    const KILLED = false;
41
42    /**
43     * Register the hooks.
44     *
45     * @return void
46     */
47    public static function init() {
48        // Before Wpcom_Feature_Flags::filter_enabled() at 10, so a per-site override beats this default.
49        add_filter( 'jetpack_feature_flag_enabled', array( self::class, 'rollout_default' ), 5, 2 );
50        add_filter( 'jetpack_feature_flag_enabled_' . self::FLAG, array( self::class, 'apply_kill_switch' ), PHP_INT_MAX );
51    }
52
53    /**
54     * Turn the flag on for sites in the rollout and off for the rest.
55     *
56     * WordPress.com stays on the rollout whatever default the package registers.
57     *
58     * @param bool   $enabled Whether the flag is enabled.
59     * @param string $name    Flag name.
60     * @return bool Whether the flag is enabled.
61     */
62    public static function rollout_default( $enabled, $name ) {
63        if ( self::FLAG !== $name ) {
64            return $enabled;
65        }
66
67        if ( Constants::is_true( 'IS_ATOMIC' ) ) {
68            return self::percent() >= 100;
69        }
70
71        return self::blog_bucket( get_current_blog_id() ) < self::percent();
72    }
73
74    /**
75     * Force the flag off while the kill switch is on.
76     *
77     * @param bool $enabled Whether the flag is enabled.
78     * @return bool Whether the flag is enabled.
79     */
80    public static function apply_kill_switch( $enabled ) {
81        return self::is_killed() ? false : (bool) $enabled;
82    }
83
84    /**
85     * Whether the kill switch is on.
86     *
87     * @return bool
88     */
89    public static function is_killed() {
90        /**
91         * Filters whether the PayPal API-managed buttons kill switch is on. When on, it forces the flag off and refuses live PayPal for every caller.
92         *
93         * @since $$next-version$$
94         *
95         * @param bool $killed Whether the kill switch is on. Defaults to KILLED or the `wpcom_paypal_buttons_kill_switch` network option.
96         */
97        return (bool) apply_filters( 'wpcom_paypal_buttons_kill_switch', static::KILLED || wp_validate_boolean( get_site_option( self::KILL_SWITCH_OPTION, false ) ) );
98    }
99
100    /**
101     * The rollout percent.
102     *
103     * @return int
104     */
105    private static function percent() {
106        /**
107         * Filters the PayPal API-managed buttons rollout percent, for tests. Ramp by raising PERCENT.
108         *
109         * @since $$next-version$$
110         *
111         * @param int $percent 0-100. Defaults to PERCENT.
112         */
113        return (int) apply_filters( 'wpcom_paypal_buttons_rollout_percent', self::PERCENT );
114    }
115
116    /**
117     * The site's bucket, 0-99. Salted with the flag, so each rollout turns on its own set of sites.
118     *
119     * `abs()` after the modulo: 32-bit PHP returns a signed `crc32()`, and `abs( PHP_INT_MIN )` overflows.
120     *
121     * @param int $blog_id Blog ID.
122     * @return int
123     */
124    private static function blog_bucket( $blog_id ) {
125        return abs( crc32( self::FLAG . ':' . (int) $blog_id ) % 100 );
126    }
127}