Upgrade.php
9.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<?php
namespace WP_Rocket\Engine\License;
use WP_Rocket\Abstract_Render;
use WP_Rocket\Engine\License\API\Pricing;
use WP_Rocket\Engine\License\API\User;
class Upgrade extends Abstract_Render {
/**
* Pricing instance
*
* @var Pricing
*/
private $pricing;
/**
* User instance
*
* @var User
*/
private $user;
/**
* Instantiate the class
*
* @param Pricing $pricing Pricing instance.
* @param User $user User instance.
* @param string $template_path Path to the views.
*/
public function __construct( Pricing $pricing, User $user, $template_path ) {
parent::__construct( $template_path );
$this->pricing = $pricing;
$this->user = $user;
}
/**
* Displays the upgrade section in the license block on the dashboard
*
* @return void
*/
public function display_upgrade_section() {
if ( ! $this->can_upgrade() ) {
return;
}
echo $this->generate( 'upgrade-section' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Displays the upgrade pop on the dashboard
*
* @return void
*/
public function display_upgrade_popin() {
if ( rocket_get_constant( 'WP_ROCKET_WHITE_LABEL_ACCOUNT' ) ) {
return;
}
if ( ! $this->can_upgrade() ) {
return;
}
$data = [
'is_promo_active' => $this->pricing->is_promo_active(),
'upgrades' => $this->get_upgrade_choices(),
];
echo $this->generate( 'upgrade-popin', $data ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Adds the notification bubble to WP Rocket menu item when a promo is active
*
* @param string $menu_title Menu title.
* @return string
*/
public function add_notification_bubble( $menu_title ) {
if ( ! $this->can_use_promo() ) {
return $menu_title;
}
if ( false !== get_transient( 'rocket_promo_seen_' . get_current_user_id() ) ) {
return $menu_title;
}
return $menu_title . ' <span class="rocket-promo-bubble">!</span>';
}
/**
* Prevents the notification bubble from showing once the user accessed the dashboard once
*
* @return void
*/
public function dismiss_notification_bubble() {
if ( ! $this->can_use_promo() ) {
return;
}
$user_id = get_current_user_id();
if ( false !== get_transient( "rocket_promo_seen_{$user_id}" ) ) {
return;
}
set_transient( "rocket_promo_seen_{$user_id}", 1, 2 * WEEK_IN_SECONDS );
}
/**
* Displays the promotion banner
*
* @return void
*/
public function display_promo_banner() {
if ( ! $this->can_use_promo() ) {
return;
}
if ( false !== get_transient( 'rocket_promo_banner_' . get_current_user_id() ) ) {
return;
}
$promo = $this->pricing->get_promo_data();
$promo_name = isset( $promo->name ) ? $promo->name : '';
$promo_discount = isset( $promo->discount_percent ) ? $promo->discount_percent : 0;
$data = [
'name' => $promo_name,
'discount_percent' => $promo_discount,
'countdown' => $this->get_countdown_data(),
'message' => $this->get_promo_message( $promo_name, $promo_discount ),
];
echo $this->generate( 'promo-banner', $data ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* AJAX callback to dismiss the promotion banner
*
* @return void
*/
public function dismiss_promo_banner() {
check_ajax_referer( 'rocket-ajax', 'nonce', true );
if ( ! current_user_can( 'rocket_manage_options' ) ) {
return;
}
$user = get_current_user_id();
if ( false !== get_transient( "rocket_promo_banner_{$user}" ) ) {
return;
}
set_transient( "rocket_promo_banner_{$user}", 1, 2 * WEEK_IN_SECONDS );
wp_send_json_success();
}
/**
* Adds the promotion end time to WP Rocket localize script data
*
* @since 3.7.4
*
* @param array $data Localize script data.
* @return array
*/
public function add_localize_script_data( $data ) {
if ( ! is_array( $data ) ) {
$data = (array) $data;
}
if ( ! $this->can_use_promo() ) {
return $data;
}
$data['promo_end'] = $this->pricing->get_promo_end();
return $data;
}
/**
* Returns an array containing the remaining days, hours, minutes & seconds for the promotion
*
* @since 3.7.4
*
* @return array
*/
private function get_countdown_data() {
$data = [
'days' => 0,
'hours' => 0,
'minutes' => 0,
'seconds' => 0,
];
if ( rocket_get_constant( 'WP_ROCKET_IS_TESTING', false ) ) {
return $data;
}
$promo_end = $this->pricing->get_promo_end();
if ( 0 === $promo_end ) {
return $data;
}
$now = date_create();
$end = date_timestamp_set( date_create(), $promo_end );
if ( $now > $end ) {
return $data;
}
$remaining = date_diff( $now, $end );
$format = explode( ' ', $remaining->format( '%d %H %i %s' ) );
$data['days'] = $format[0];
$data['hours'] = $format[1];
$data['minutes'] = $format[2];
$data['seconds'] = $format[3];
return $data;
}
/**
* Returns the promotion message to display in the banner
*
* @param string $promo_name Name of the promotion.
* @param int $promo_discount Discount percentage.
*
* @return string
*/
private function get_promo_message( $promo_name = '', $promo_discount = 0 ) {
$choices = 0;
$license = $this->user->get_license_type();
$plus_websites = $this->pricing->get_plus_websites_count();
if ( $license === $plus_websites ) {
$choices = 2;
} elseif (
$license >= $this->pricing->get_single_websites_count()
&&
$license < $plus_websites
) {
$choices = 1;
}
return sprintf(
// translators: %1$s = promotion name, %2$s = <br>, %3$s = <strong>, %4$s = promotion discount percentage, %5$s = </strong>.
_n(
'Take advantage of %1$s to speed up more websites:%2$s get a %3$s%4$s off%5$s for %3$supgrading your license to Plus or Infinite!%5$s',
'Take advantage of %1$s to speed up more websites:%2$s get a %3$s%4$s off%5$s for %3$supgrading your license to Infinite!%5$s',
$choices,
'rocket'
),
$promo_name,
'<br>',
'<strong>',
$promo_discount . '%',
'</strong>'
);
}
/**
* Checks if current user can use the promotion
*
* @since 3.7.4
*
* @return boolean
*/
private function can_use_promo() {
if ( rocket_get_constant( 'WP_ROCKET_WHITE_LABEL_ACCOUNT' ) ) {
return false;
}
if ( ! $this->can_upgrade() ) {
return false;
}
if ( $this->is_expired_soon() ) {
return false;
}
if ( $this->is_new_user() ) {
return false;
}
return $this->pricing->is_promo_active();
}
/**
* Checks if the license expires in less than 30 days
*
* @return boolean
*/
private function is_expired_soon() {
$expiration_delay = $this->user->get_license_expiration() - time();
return 30 * DAY_IN_SECONDS > $expiration_delay;
}
/**
* Checks if the User license bought less than 14 days
*
* @return boolean
*/
private function is_new_user() {
return ( 14 * DAY_IN_SECONDS ) > time() - $this->user->get_creation_date();
}
/**
* Checks if the current license can upgrade
*
* @return boolean
*/
private function can_upgrade() {
return (
-1 !== $this->user->get_license_type()
&&
! $this->user->is_license_expired()
);
}
/**
* Gets the upgrade choices depending on the current license level
*
* @return array
*/
private function get_upgrade_choices() {
$choices = [];
$license = $this->user->get_license_type();
$plus_websites = $this->pricing->get_plus_websites_count();
if ( $license === $plus_websites ) {
$choices['infinite'] = $this->get_upgrade_from_plus_to_infinite_data();
} elseif (
$license >= $this->pricing->get_single_websites_count()
&&
$license < $plus_websites
) {
$choices['plus'] = $this->get_upgrade_from_single_to_plus_data();
$choices['infinite'] = $this->get_upgrade_from_single_to_infinite_data();
}
return $choices;
}
/**
* Gets the data to upgrade from single to plus
*
* @return array
*/
private function get_upgrade_from_single_to_plus_data() {
$price = $this->pricing->get_single_to_plus_price();
$data = [
'name' => 'Plus',
'price' => $price,
'websites' => $this->pricing->get_plus_websites_count(),
'upgrade_url' => $this->user->get_upgrade_plus_url(),
];
if ( $this->pricing->is_promo_active() ) {
$regular_price = $this->pricing->get_regular_single_to_plus_price();
$data['saving'] = $regular_price - $price;
$data['regular_price'] = $regular_price;
}
return $data;
}
/**
* Gets the data to upgrade from single to infinite
*
* @return array
*/
private function get_upgrade_from_single_to_infinite_data() {
$price = $this->pricing->get_single_to_infinite_price();
$data = [
'name' => 'Infinite',
'price' => $price,
'websites' => __( 'Unlimited', 'rocket' ),
'upgrade_url' => $this->user->get_upgrade_infinite_url(),
];
if ( $this->pricing->is_promo_active() ) {
$regular_price = $this->pricing->get_regular_single_to_infinite_price();
$data['saving'] = $regular_price - $price;
$data['regular_price'] = $regular_price;
}
return $data;
}
/**
* Gets the data to upgrade from plus to infinite
*
* @return array
*/
private function get_upgrade_from_plus_to_infinite_data() {
$price = $this->pricing->get_plus_to_infinite_price();
$data = [
'name' => 'Infinite',
'price' => $price,
'websites' => __( 'Unlimited', 'rocket' ),
'upgrade_url' => $this->user->get_upgrade_infinite_url(),
];
if ( $this->pricing->is_promo_active() ) {
$regular_price = $this->pricing->get_regular_plus_to_infinite_price();
$data['saving'] = $regular_price - $price;
$data['regular_price'] = $regular_price;
}
return $data;
}
}