Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
39.39% |
13 / 33 |
|
60.00% |
3 / 5 |
CRAP | n/a |
0 / 0 |
|
| Automattic\Jetpack\PremiumAnalytics\register_promo | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| Automattic\Jetpack\PremiumAnalytics\get_available_promos | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| Automattic\Jetpack\PremiumAnalytics\compute_available_promos | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
12 | |||
| Automattic\Jetpack\PremiumAnalytics\configure_promos | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| Automattic\Jetpack\PremiumAnalytics\inject_promos_script_data | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
4.01 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Promos: the registry helpers and the gates a registered promo passes before it shows. |
| 4 | * |
| 5 | * Internal to this package. The dashboard renders what is registered here, so there is no |
| 6 | * versioned contract to hold. |
| 7 | * |
| 8 | * @package automattic/jetpack-premium-analytics |
| 9 | */ |
| 10 | |
| 11 | namespace Automattic\Jetpack\PremiumAnalytics; |
| 12 | |
| 13 | require_once __DIR__ . '/class-promo.php'; |
| 14 | require_once __DIR__ . '/class-promo-registry.php'; |
| 15 | |
| 16 | /** |
| 17 | * Registers a promo. |
| 18 | * |
| 19 | * @internal |
| 20 | * |
| 21 | * @since $$next-version$$ |
| 22 | * |
| 23 | * @param string $slug Promo identifier, namespaced like `acme/offer`. |
| 24 | * @param array $args Optional. Promo arguments. |
| 25 | * @return Promo|false The registered promo on success, or false on failure. |
| 26 | */ |
| 27 | function register_promo( $slug, $args = array() ) { |
| 28 | return Promo_Registry::get_instance()->register( $slug, $args ); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Retrieves the promos to show, ordered by `order` and then `slug`. |
| 33 | * |
| 34 | * @since $$next-version$$ |
| 35 | * |
| 36 | * @return Promo[] Ordered list of available promos. |
| 37 | */ |
| 38 | function get_available_promos() { |
| 39 | return Promo_Registry::get_instance()->remember_available( __NAMESPACE__ . '\\compute_available_promos' ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Applies the gates to the registered promos. Call `get_available_promos()` instead, which keeps |
| 44 | * the result for the rest of the request so no promo's rule runs twice. |
| 45 | * |
| 46 | * @since $$next-version$$ |
| 47 | * |
| 48 | * @return Promo[] Ordered list of available promos. |
| 49 | */ |
| 50 | function compute_available_promos() { |
| 51 | /** This filter is documented in projects/plugins/jetpack/_inc/lib/admin-pages/class-jetpack-redux-state-helper.php */ |
| 52 | if ( ! apply_filters( 'jetpack_show_promotions', true ) ) { |
| 53 | return array(); |
| 54 | } |
| 55 | |
| 56 | $promos = array_values( |
| 57 | array_filter( |
| 58 | Promo_Registry::get_instance()->get_all_registered(), |
| 59 | static function ( Promo $promo ) { |
| 60 | // The Stats roles setting can let an editor or an author open the dashboard, and an |
| 61 | // upgrade is not theirs to buy. |
| 62 | return current_user_can( $promo->capability ) && $promo->is_available(); |
| 63 | } |
| 64 | ) |
| 65 | ); |
| 66 | |
| 67 | usort( |
| 68 | $promos, |
| 69 | static function ( Promo $a, Promo $b ) { |
| 70 | return array( $a->order, $a->slug ) <=> array( $b->order, $b->slug ); |
| 71 | } |
| 72 | ); |
| 73 | |
| 74 | return $promos; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Configures the promos. |
| 79 | * |
| 80 | * @since $$next-version$$ |
| 81 | * |
| 82 | * @return void |
| 83 | */ |
| 84 | function configure_promos() { |
| 85 | add_filter( 'jetpack_admin_js_script_data', __NAMESPACE__ . '\\inject_promos_script_data', 20 ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Publishes the available promos as `premium_analytics.promos` in the script data. |
| 90 | * |
| 91 | * @since $$next-version$$ |
| 92 | * |
| 93 | * @param array $data The script data passed by the assets package. |
| 94 | * @return array |
| 95 | */ |
| 96 | function inject_promos_script_data( array $data ): array { |
| 97 | if ( ! Analytics::renders_dashboard() ) { |
| 98 | return $data; |
| 99 | } |
| 100 | |
| 101 | if ( ! isset( $data['premium_analytics'] ) || ! is_array( $data['premium_analytics'] ) ) { |
| 102 | $data['premium_analytics'] = array(); |
| 103 | } |
| 104 | |
| 105 | $data['premium_analytics']['promos'] = array_map( |
| 106 | static function ( Promo $promo ) { |
| 107 | return $promo->to_array(); |
| 108 | }, |
| 109 | get_available_promos() |
| 110 | ); |
| 111 | |
| 112 | return $data; |
| 113 | } |