Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
50.00% covered (danger)
50.00%
7 / 14
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Monetize_Feature_Flags
63.64% covered (warning)
63.64%
7 / 11
75.00% covered (warning)
75.00%
3 / 4
14.81
0.00% covered (danger)
0.00%
0 / 1
 register
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 filter_payment_plans_url
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 filter_available_modules
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 is_available
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * Monetize dashboard feature flag.
4 *
5 * @package automattic/jetpack
6 */
7
8use Automattic\Jetpack\Monetize\Dashboard;
9use Automattic\Jetpack\Status\Host;
10
11if ( ! defined( 'ABSPATH' ) ) {
12    exit( 0 );
13}
14
15/**
16 * Registers the flag that gates the `monetize` module.
17 *
18 * @since 16.5
19 */
20class Jetpack_Monetize_Feature_Flags {
21
22    const MODULE = 'monetize';
23
24    /**
25     * Register the flag, the module filter, and where Newsletter's plan buttons go.
26     *
27     * @return void
28     */
29    public static function register() {
30        // Another plugin may have loaded the classes without the Monetize package.
31        if ( class_exists( Dashboard::class ) ) {
32            Dashboard::register_feature_flag();
33        }
34
35        add_filter( 'jetpack_get_available_modules', array( __CLASS__, 'filter_available_modules' ) );
36        add_filter( 'jetpack_newsletter_payment_plans_url', array( __CLASS__, 'filter_payment_plans_url' ) );
37    }
38
39    /**
40     * Send Newsletter's plan buttons to the Payments tab even while the module is off, where My Jetpack offers to turn it on.
41     *
42     * @since $$next-version$$
43     *
44     * @param string $url Where the buttons go.
45     * @return string
46     */
47    public static function filter_payment_plans_url( $url ) {
48        if ( ! self::is_available() || ! method_exists( Dashboard::class, 'get_payment_plans_url' ) ) {
49            return $url;
50        }
51        return Dashboard::get_payment_plans_url();
52    }
53
54    /**
55     * Drop the module from the available list while the flag is off, which also keeps it from loading.
56     *
57     * It stays off on Simple, where jetpack-mu-wpcom loads the dashboard itself.
58     *
59     * @param array $modules Available modules, keyed by slug.
60     * @return array
61     */
62    public static function filter_available_modules( $modules ) {
63        if ( ! self::is_available() ) {
64            unset( $modules[ self::MODULE ] );
65        }
66        return $modules;
67    }
68
69    /**
70     * Whether the module is on offer: the flag is on and this isn't Simple.
71     *
72     * @return bool
73     */
74    private static function is_available() {
75        return class_exists( Dashboard::class ) && Dashboard::is_enabled() && ! ( new Host() )->is_wpcom_simple();
76    }
77}
78
79Jetpack_Monetize_Feature_Flags::register();