Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.56% covered (success)
95.56%
43 / 45
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Promo
95.56% covered (success)
95.56%
43 / 45
75.00% covered (warning)
75.00%
3 / 4
19
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 is_available
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 to_array
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
 set_props
91.67% covered (success)
91.67%
22 / 24
0.00% covered (danger)
0.00%
0 / 1
13.10
1<?php
2/**
3 * Promos: Promo class.
4 *
5 * @package automattic/jetpack-premium-analytics
6 */
7
8namespace Automattic\Jetpack\PremiumAnalytics;
9
10/**
11 * Represents a promotional card the dashboard offers.
12 *
13 * @since $$next-version$$
14 */
15final class Promo {
16
17    /**
18     * Promo identifier, namespaced like `acme/offer`.
19     *
20     * @since $$next-version$$
21     * @var string
22     */
23    public $slug;
24
25    /**
26     * Where the promo renders.
27     *
28     * @since $$next-version$$
29     * @var string
30     */
31    public $placement = '';
32
33    /**
34     * Sort order.
35     *
36     * @since $$next-version$$
37     * @var int
38     */
39    public $order = 10;
40
41    /**
42     * Card title.
43     *
44     * @since $$next-version$$
45     * @var string
46     */
47    public $title = '';
48
49    /**
50     * Card description.
51     *
52     * @since $$next-version$$
53     * @var string
54     */
55    public $description = '';
56
57    /**
58     * Call-to-action label.
59     *
60     * @since $$next-version$$
61     * @var string
62     */
63    public $cta_label = '';
64
65    /**
66     * Call-to-action URL.
67     *
68     * @since $$next-version$$
69     * @var string
70     */
71    public $cta_url = '';
72
73    /**
74     * Call-to-action style: `primary` is filled, `secondary` is outlined.
75     *
76     * @since $$next-version$$
77     * @var string
78     */
79    public $cta_variant = 'primary';
80
81    /**
82     * Whether the user may dismiss the promo.
83     *
84     * @since $$next-version$$
85     * @var bool
86     */
87    public $dismissible = true;
88
89    /**
90     * Days a dismissal lasts before the promo returns.
91     *
92     * @since $$next-version$$
93     * @var int
94     */
95    public $snooze_days = 90;
96
97    /**
98     * Capability a user needs to be offered the promo. The Stats roles setting can let an editor or
99     * an author open the dashboard, and an upgrade is not theirs to buy.
100     *
101     * @since $$next-version$$
102     * @var string
103     */
104    public $capability = 'manage_options';
105
106    /**
107     * Availability flag or callback; null when the registration declared none.
108     *
109     * @var bool|callable|null
110     */
111    private $is_available = null;
112
113    /**
114     * Constructor.
115     *
116     * @since $$next-version$$
117     *
118     * @param string $slug Promo identifier.
119     * @param array  $args Optional. Promo arguments.
120     */
121    public function __construct( $slug, $args = array() ) {
122        $this->slug = $slug;
123
124        $this->set_props( $args );
125    }
126
127    /**
128     * Returns whether this promo should be shown. A promo with no rule is not.
129     *
130     * @since $$next-version$$
131     *
132     * @return bool
133     */
134    public function is_available() {
135        if ( null === $this->is_available ) {
136            return false;
137        }
138
139        if ( is_bool( $this->is_available ) ) {
140            return $this->is_available;
141        }
142
143        return is_callable( $this->is_available )
144            ? (bool) call_user_func( $this->is_available, $this )
145            : false;
146    }
147
148    /**
149     * Returns the representation sent to the browser.
150     *
151     * @since $$next-version$$
152     *
153     * @return array
154     */
155    public function to_array() {
156        return array(
157            'slug'        => $this->slug,
158            'placement'   => $this->placement,
159            'order'       => (int) $this->order,
160            'title'       => $this->title,
161            'description' => $this->description,
162            'cta_label'   => $this->cta_label,
163            'cta_url'     => $this->cta_url,
164            'cta_variant' => $this->cta_variant,
165            'dismissible' => $this->dismissible,
166            'snooze_days' => $this->snooze_days,
167        );
168    }
169
170    /**
171     * Hydrates promo properties from the args array.
172     *
173     * @param array $args Promo arguments.
174     * @return void
175     */
176    private function set_props( $args ) {
177        if ( ! is_array( $args ) ) {
178            return;
179        }
180
181        if ( isset( $args['placement'] ) ) {
182            $this->placement = (string) $args['placement'];
183        }
184
185        if ( isset( $args['order'] ) ) {
186            $this->order = (int) $args['order'];
187        }
188
189        // Trimmed: whitespace alone would satisfy the required-field checks and render as an empty card.
190        if ( isset( $args['title'] ) ) {
191            $this->title = trim( (string) $args['title'] );
192        }
193
194        if ( isset( $args['description'] ) ) {
195            $this->description = trim( (string) $args['description'] );
196        }
197
198        if ( isset( $args['cta_label'] ) ) {
199            $this->cta_label = trim( (string) $args['cta_label'] );
200        }
201
202        if ( isset( $args['cta_url'] ) ) {
203            $this->cta_url = trim( (string) $args['cta_url'] );
204        }
205
206        if ( isset( $args['cta_variant'] ) ) {
207            $this->cta_variant = trim( (string) $args['cta_variant'] );
208        }
209
210        if ( isset( $args['dismissible'] ) ) {
211            $this->dismissible = (bool) $args['dismissible'];
212        }
213
214        if ( isset( $args['snooze_days'] ) ) {
215            $this->snooze_days = (int) $args['snooze_days'];
216        }
217
218        if ( isset( $args['capability'] ) ) {
219            $this->capability = trim( (string) $args['capability'] );
220        }
221
222        if ( array_key_exists( 'is_available', $args ) ) {
223            $this->is_available = $args['is_available'];
224        }
225    }
226}