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 * Promo Slot API: Promo class.
4 *
5 * @package automattic/jetpack-premium-analytics
6 */
7
8namespace Automattic\Jetpack\PremiumAnalytics;
9
10/**
11 * Represents a promotional card another package registers into the dashboard.
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     * Whether the user may dismiss the promo.
75     *
76     * @since $$next-version$$
77     * @var bool
78     */
79    public $dismissible = true;
80
81    /**
82     * Days a dismissal lasts before the promo returns.
83     *
84     * @since $$next-version$$
85     * @var int
86     */
87    public $snooze_days = 90;
88
89    /**
90     * Script module that renders the card; null for the default card.
91     *
92     * @since $$next-version$$
93     * @var string|null
94     */
95    public $render_module = null;
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        // Registration refuses anything else, so reaching here means the callable stopped resolving.
144        return is_callable( $this->is_available )
145            ? (bool) call_user_func( $this->is_available, $this )
146            : false;
147    }
148
149    /**
150     * Returns the representation sent to the browser.
151     *
152     * @since $$next-version$$
153     *
154     * @return array
155     */
156    public function to_array() {
157        return array(
158            'slug'          => $this->slug,
159            'placement'     => $this->placement,
160            'order'         => (int) $this->order,
161            'title'         => $this->title,
162            'description'   => $this->description,
163            'cta_label'     => $this->cta_label,
164            'cta_url'       => $this->cta_url,
165            'dismissible'   => $this->dismissible,
166            'snooze_days'   => $this->snooze_days,
167            'render_module' => $this->render_module,
168        );
169    }
170
171    /**
172     * Hydrates promo properties from the args array.
173     *
174     * @param array $args Promo arguments.
175     * @return void
176     */
177    private function set_props( $args ) {
178        if ( ! is_array( $args ) ) {
179            return;
180        }
181
182        if ( isset( $args['placement'] ) ) {
183            $this->placement = (string) $args['placement'];
184        }
185
186        if ( isset( $args['order'] ) ) {
187            $this->order = (int) $args['order'];
188        }
189
190        // Trimmed: whitespace alone would satisfy the required-field checks and render as an empty card.
191        if ( isset( $args['title'] ) ) {
192            $this->title = trim( (string) $args['title'] );
193        }
194
195        if ( isset( $args['description'] ) ) {
196            $this->description = trim( (string) $args['description'] );
197        }
198
199        if ( isset( $args['cta_label'] ) ) {
200            $this->cta_label = trim( (string) $args['cta_label'] );
201        }
202
203        if ( isset( $args['cta_url'] ) ) {
204            $this->cta_url = trim( (string) $args['cta_url'] );
205        }
206
207        if ( isset( $args['dismissible'] ) ) {
208            $this->dismissible = (bool) $args['dismissible'];
209        }
210
211        if ( isset( $args['snooze_days'] ) ) {
212            $this->snooze_days = (int) $args['snooze_days'];
213        }
214
215        if ( isset( $args['render_module'] ) ) {
216            $this->render_module = (string) $args['render_module'];
217        }
218
219        if ( isset( $args['capability'] ) ) {
220            $this->capability = trim( (string) $args['capability'] );
221        }
222
223        if ( array_key_exists( 'is_available', $args ) ) {
224            $this->is_available = $args['is_available'];
225        }
226    }
227}