Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/wp-rocket/inc/Engine/License/Renewal.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 + declare(strict_types=1);
3 +
4 + namespace WP_Rocket\Engine\License;
5 +
6 + use WP_Rocket\Abstract_Render;
7 + use WP_Rocket\Admin\Options_Data;
8 + use WP_Rocket\Engine\License\API\Pricing;
9 + use WP_Rocket\Engine\License\API\User;
10 +
11 + class Renewal extends Abstract_Render {
12 + /**
13 + * Pricing instance
14 + *
15 + * @var Pricing
16 + */
17 + private $pricing;
18 +
19 + /**
20 + * User instance
21 + *
22 + * @var User
23 + */
24 + private $user;
25 +
26 + /**
27 + * Options_Data instance
28 + *
29 + * @var Options_Data
30 + */
31 + private $options;
32 +
33 + /**
34 + * Instantiate the class
35 + *
36 + * @param Pricing $pricing Pricing instance.
37 + * @param User $user User instance.
38 + * @param Options_Data $options Options_Data instance.
39 + * @param string $template_path Path to the views.
40 + */
41 + public function __construct( Pricing $pricing, User $user, Options_Data $options, $template_path ) {
42 + parent::__construct( $template_path );
43 +
44 + $this->pricing = $pricing;
45 + $this->user = $user;
46 + $this->options = $options;
47 + }
48 +
49 + /**
50 + * Displays the renewal banner for users expiring in less than 30 days
51 + *
52 + * @since 3.7.5
53 + *
54 + * @return void
55 + */
56 + public function display_renewal_soon_banner() {
57 + if ( rocket_get_constant( 'WP_ROCKET_WHITE_LABEL_ACCOUNT' ) ) {
58 + return;
59 + }
60 +
61 + if ( $this->user->is_license_expired() ) {
62 + return;
63 + }
64 +
65 + if ( ! $this->is_expired_soon() ) {
66 + return;
67 + }
68 +
69 + $data = $this->get_banner_data();
70 + $data['countdown'] = $this->get_countdown_data();
71 + $data['more_info'] = true;
72 +
73 + if ( -1 === $this->user->get_license_type() ) {
74 + $data['message'] = sprintf(
75 + // translators: %1$s WP Rocket plugin name.
76 + esc_html__( 'Your %1$s%2$s license is about to expire%3$s: you will soon lose access to product updates and support.', 'rocket' ),
77 + '<strong>',
78 + WP_ROCKET_PLUGIN_NAME,
79 + '</strong>'
80 + );
81 + $data['more_info'] = false;
82 + }
83 +
84 + echo $this->generate( 'renewal-soon-banner', $data ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
85 + }
86 +
87 + /**
88 + * Displays the renewal banner for expired users
89 + *
90 + * @since 3.7.5
91 + *
92 + * @return void
93 + */
94 + public function display_renewal_expired_banner() {
95 + if ( rocket_get_constant( 'WP_ROCKET_WHITE_LABEL_ACCOUNT' ) ) {
96 + return;
97 + }
98 +
99 + if ( 0 === $this->user->get_license_expiration() ) {
100 + return;
101 + }
102 +
103 + if ( ! $this->user->is_license_expired() ) {
104 + return;
105 + }
106 +
107 + if ( false !== get_transient( 'rocket_renewal_banner_' . get_current_user_id() ) ) {
108 + return;
109 + }
110 +
111 + $expiration = $this->user->get_license_expiration();
112 + $expired_since = ( time() - $expiration ) / DAY_IN_SECONDS;
113 +
114 + if (
115 + $this->user->is_auto_renew()
116 + &&
117 + 4 > $expired_since
118 + ) {
119 + return;
120 + }
121 +
122 + $ocd_enabled = $this->options->get( 'optimize_css_delivery', 0 );
123 + $renewal_url = $this->user->get_renewal_url();
124 +
125 + $message = null;
126 +
127 + if ( $ocd_enabled ) {
128 + if ( 15 > $expired_since ) {
129 + // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
130 + echo $this->generate(
131 + 'renewal-expired-banner-ocd',
132 + [
133 + 'renewal_url' => $renewal_url,
134 + 'message' => $message,
135 + 'disabled_date' => date_i18n( get_option( 'date_format' ), $expiration + 15 * DAY_IN_SECONDS ),
136 + ]
137 + );
138 + // phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped
139 + } elseif ( 90 > $expired_since ) {
140 + // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
141 + echo $this->generate(
142 + 'renewal-expired-banner-ocd-disabled',
143 + [
144 + 'renewal_url' => $renewal_url,
145 + 'message' => $message,
146 + ]
147 + );
148 + // phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped
149 + } elseif ( 90 < $expired_since ) {
150 + // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
151 + echo $this->generate(
152 + 'renewal-expired-banner',
153 + [
154 + 'renewal_url' => $renewal_url,
155 + 'message' => $message,
156 + ]
157 + );
158 + // phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped
159 + }
160 + } elseif ( ! $ocd_enabled ) {
161 + // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
162 + echo $this->generate(
163 + 'renewal-expired-banner',
164 + [
165 + 'renewal_url' => $renewal_url,
166 + 'message' => $message,
167 + ]
168 + );
169 + // phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped
170 + }
171 + }
172 +
173 + /**
174 + * Get base data to display in the banners
175 + *
176 + * @since 3.7.5
177 + *
178 + * @return array
179 + */
180 + private function get_banner_data() {
181 + $price = esc_html( '$' . number_format_i18n( $this->get_price(), 2 ) );
182 +
183 + $message = sprintf(
184 + // translators: %1$s = <strong>, %2$s = </strong>, %3$s = discount price.
185 + esc_html__( 'Renew before it is too late, you will pay %1$s%3$s%2$s.', 'rocket' ),
186 + '<strong>',
187 + '</strong>',
188 + $price
189 + );
190 +
191 + if ( $this->is_grandfather() ) {
192 + $message = sprintf(
193 + // translators: %1$s = <strong>, %2$s = discount percentage, %3$s = </strong>, %4$s = discount price.
194 + esc_html__( 'Renew with a %1$s%2$s discount%3$s before it is too late, you will only pay %1$s%4$s%3$s!', 'rocket' ),
195 + '<strong>',
196 + esc_html( $this->get_discount_percent() . '%' ),
197 + '</strong>',
198 + $price
199 + );
200 + }
201 +
202 + return [
203 + 'message' => $message,
204 + 'renewal_url' => $this->user->get_renewal_url(),
205 + ];
206 + }
207 +
208 + /**
209 + * AJAX callback to dismiss the renewal banner for expired users
210 + *
211 + * @since 3.7.5
212 + *
213 + * @return void
214 + */
215 + public function dismiss_renewal_expired_banner() {
216 + check_ajax_referer( 'rocket-ajax', 'nonce', true );
217 +
218 + if ( ! current_user_can( 'rocket_manage_options' ) ) {
219 + return;
220 + }
221 +
222 + $transient = 'rocket_renewal_banner_' . get_current_user_id();
223 +
224 + if ( false !== get_transient( $transient ) ) {
225 + return;
226 + }
227 +
228 + set_transient( $transient, 1, MONTH_IN_SECONDS );
229 +
230 + wp_send_json_success();
231 + }
232 +
233 + /**
234 + * Adds the license expiration time to WP Rocket localize script data
235 + *
236 + * @since 3.7.5
237 + *
238 + * @param array $data Localize script data.
239 + * @return array
240 + */
241 + public function add_localize_script_data( array $data ) {
242 + if ( $this->user->is_license_expired() ) {
243 + return $data;
244 + }
245 +
246 + if ( ! $this->is_expired_soon() ) {
247 + return $data;
248 + }
249 +
250 + $data['license_expiration'] = $this->user->get_license_expiration();
251 +
252 + return $data;
253 + }
254 +
255 + /**
256 + * Checks if the license expires in less than 30 days
257 + *
258 + * @since 3.7.5
259 + *
260 + * @return boolean
261 + */
262 + private function is_expired_soon() {
263 + if ( $this->user->is_auto_renew() ) {
264 + return false;
265 + }
266 +
267 + $expiration_delay = $this->user->get_license_expiration() - time();
268 +
269 + return 30 * DAY_IN_SECONDS > $expiration_delay;
270 + }
271 +
272 + /**
273 + * Gets the discount corresponding to the current user status
274 + *
275 + * @since 3.7.5
276 + *
277 + * @return int
278 + * @phpstan-ignore-next-line
279 + */
280 + private function get_discount_percent() {
281 + $prices = $this->get_license_pricing_data();
282 +
283 + $renewals = $this->get_user_renewal_status();
284 +
285 + if ( ! isset( $prices->prices, $prices->prices->renewal ) ) {
286 + return 0;
287 + }
288 +
289 + $prices = $prices->prices;
290 +
291 + if ( $renewals['is_grandfather'] ) {
292 + return $renewals['discount_percent']->is_grandfather;
293 + }
294 +
295 + return 0;
296 + }
297 +
298 + /**
299 + * Is user grandfathered
300 + *
301 + * @return bool
302 + */
303 + private function is_grandfather(): bool {
304 + $renewals = $this->get_user_renewal_status();
305 +
306 + return key_exists( 'is_grandfather', $renewals ) && $renewals['is_grandfather'];
307 + }
308 +
309 + /**
310 + * Is user grandmothered
311 + *
312 + * @return bool
313 + * @phpstan-ignore-next-line
314 + */
315 + private function has_grandmother(): bool {
316 + $renewals = $this->get_user_renewal_status();
317 +
318 + return key_exists( 'is_grandmother', $renewals ) && $renewals['is_grandmother'];
319 + }
320 +
321 + /**
322 + * Gets the price corresponding to the current user status
323 + *
324 + * @since 3.7.5
325 + *
326 + * @return int
327 + */
328 + private function get_price() {
329 + $renewals = $this->get_user_renewal_status();
330 +
331 + $license = $this->get_license_pricing_data();
332 +
333 + if (
334 + $renewals['is_grandfather']
335 + &&
336 + ! $renewals['is_expired']
337 + ) {
338 + return isset( $license->prices->renewal->is_grandfather ) ? $license->prices->renewal->is_grandfather : 0;
339 + }
340 +
341 + if ( $renewals['is_grandmother'] &&
342 + ! $renewals['is_expired'] ) {
343 + return isset( $license->prices->renewal->is_grandmother ) ? $license->prices->renewal->is_grandmother : 0;
344 + }
345 +
346 + return isset( $license->prices->renewal->not_grandfather ) ? $license->prices->renewal->not_grandfather : 0;
347 + }
348 +
349 + /**
350 + * Gets the user renewal status
351 + *
352 + * @since 3.7.5
353 + *
354 + * @return array
355 + */
356 + private function get_user_renewal_status(): array {
357 + $renewals = $this->pricing->get_renewals_data();
358 +
359 + if ( ! isset( $renewals->extra_days, $renewals->grandfather_date, $renewals->discount_percent, $renewals->grandmother_date ) ) {
360 + return [];
361 + }
362 +
363 + return [
364 + 'discount_percent' => $renewals->discount_percent,
365 + 'is_expired' => time() > ( $this->user->get_license_expiration() + ( $renewals->extra_days * DAY_IN_SECONDS ) ),
366 + 'is_grandfather' => $renewals->grandfather_date > $this->user->get_creation_date(),
367 + 'is_grandmother' => $renewals->grandmother_date > $this->user->get_creation_date(),
368 + ];
369 + }
370 +
371 + /**
372 + * Gets the license pricing data corresponding to the user license
373 + *
374 + * @since 3.7.5
375 + *
376 + * @return object|null
377 + */
378 + private function get_license_pricing_data() {
379 + $license = $this->user->get_license_type();
380 + $plus_websites = $this->pricing->get_plus_websites_count();
381 +
382 + if ( $license === $plus_websites ) {
383 + return $this->pricing->get_plus_pricing();
384 + } elseif (
385 + $license >= $this->pricing->get_single_websites_count()
386 + &&
387 + $license < $plus_websites
388 + ) {
389 + return $this->pricing->get_single_pricing();
390 + }
391 +
392 + return $this->pricing->get_infinite_pricing();
393 + }
394 +
395 + /**
396 + * Gets the countdown data to display for the renewal soon banner
397 + *
398 + * @since 3.7.5
399 + *
400 + * @return array
401 + */
402 + private function get_countdown_data() {
403 + $data = [
404 + 'days' => 0,
405 + 'hours' => 0,
406 + 'minutes' => 0,
407 + 'seconds' => 0,
408 + ];
409 +
410 + if ( rocket_get_constant( 'WP_ROCKET_IS_TESTING', false ) ) {
411 + return $data;
412 + }
413 +
414 + $expiration = $this->user->get_license_expiration();
415 +
416 + if ( 0 === $expiration ) {
417 + return $data;
418 + }
419 +
420 + $now = date_create();
421 + $end = date_timestamp_set( date_create(), $expiration );
422 +
423 + if ( $now > $end ) {
424 + return $data;
425 + }
426 +
427 + $remaining = date_diff( $now, $end );
428 + $format = explode( ' ', $remaining->format( '%d %H %i %s' ) );
429 +
430 + $data['days'] = $format[0];
431 + $data['hours'] = $format[1];
432 + $data['minutes'] = $format[2];
433 + $data['seconds'] = $format[3];
434 +
435 + return $data;
436 + }
437 +
438 + /**
439 + * Add license expiring warning to OCD label
440 + *
441 + * @param array $args Setting field arguments.
442 + *
443 + * @return array
444 + */
445 + public function add_license_expire_warning( $args ): array {
446 + if ( 'optimize_css_delivery' !== $args['id'] ) {
447 + return $args;
448 + }
449 +
450 + if ( ! $this->user->is_license_expired() ) {
451 + return $args;
452 + }
453 +
454 + $ocd = $this->options->get( 'optimize_css_delivery', 0 );
455 + $whitelabel = rocket_get_constant( 'WP_ROCKET_WHITE_LABEL_ACCOUNT', false );
456 + $expired_since = ( time() - $this->user->get_license_expiration() ) / DAY_IN_SECONDS;
457 + $message = ' <span class="wpr-icon-important wpr-checkbox-warning">';
458 +
459 + if (
460 + (
461 + $whitelabel
462 + &&
463 + 15 > $expired_since
464 + &&
465 + $ocd
466 + )
467 + ||
468 + (
469 + ! $whitelabel
470 + &&
471 + $this->user->is_auto_renew()
472 + &&
473 + 4 > $expired_since
474 + )
475 + ||
476 + (
477 + $whitelabel
478 + &&
479 + $this->user->is_auto_renew()
480 + &&
481 + 4 > $expired_since
482 + &&
483 + ! $ocd
484 + )
485 + ) {
486 + return $args;
487 + } elseif (
488 + ! $whitelabel
489 + &&
490 + 15 > $expired_since
491 + &&
492 + $ocd
493 + ) {
494 + $message .= sprintf(
495 + // translators: %1$s = <a>, %2$s = </a>.
496 + __( 'You need a valid license to continue using this feature. %1$sRenew now%2$s before losing access.', 'rocket' ),
497 + '<a href="' . esc_url( $this->user->get_renewal_url() ) . '" target="_blank">',
498 + '</a>'
499 + );
500 + } elseif (
501 + (
502 + ! $whitelabel
503 + &&
504 + 15 < $expired_since
505 + )
506 + ||
507 + (
508 + ! $whitelabel
509 + &&
510 + 15 > $expired_since
511 + &&
512 + ! $ocd
513 + )
514 + ) {
515 + $message .= sprintf(
516 + // translators: %1$s = <a>, %2$s = </a>.
517 + __( 'You need an active license to enable this option. %1$sRenew now%2$s.', 'rocket' ),
518 + '<a href="' . esc_url( $this->user->get_renewal_url() ) . '" target="_blank">',
519 + '</a>'
520 + );
521 + } elseif (
522 + (
523 + $whitelabel
524 + &&
525 + 15 < $expired_since
526 + )
527 + ||
528 + (
529 + $whitelabel
530 + &&
531 + 15 > $expired_since
532 + &&
533 + ! $ocd
534 + )
535 + ) {
536 + $doc = 'https://docs.wp-rocket.me/article/1711-what-happens-if-my-license-expires';
537 + $locale = current( array_slice( explode( '_', get_user_locale() ), 0, 1 ) );
538 +
539 + if ( 'fr' === $locale ) {
540 + $doc = 'https://fr.docs.wp-rocket.me/article/1712-que-se-passe-t-il-si-ma-licence-expire';
541 + }
542 +
543 + $message .= sprintf(
544 + // translators: %1$s = <a>, %2$s = </a>.
545 + __( 'You need an active license to enable this option. %1$sMore info%2$s.', 'rocket' ),
546 + '<a href="' . $doc . '?utm_source=wp_plugin&utm_medium=wp_rocket" target="_blank">',
547 + '</a>'
548 + );
549 + }
550 +
551 + $message .= '</span>';
552 +
553 + $args['label'] = $args['label'] . $message;
554 +
555 + return $args;
556 + }
557 +
558 + /**
559 + * Adds the notification bubble to WP Rocket menu item when expired
560 + *
561 + * @param string $menu_title Menu title.
562 + *
563 + * @return string
564 + */
565 + public function add_expired_bubble( $menu_title ): string {
566 + if ( rocket_get_constant( 'WP_ROCKET_WHITE_LABEL_ACCOUNT', false ) ) {
567 + return $menu_title;
568 + }
569 +
570 + if ( ! $this->user->is_license_expired() ) {
571 + return $menu_title;
572 + }
573 +
574 + if ( false !== get_transient( 'wpr_dashboard_seen_' . get_current_user_id() ) ) {
575 + return $menu_title;
576 + }
577 +
578 + $expired_since = ( time() - $this->user->get_license_expiration() ) / DAY_IN_SECONDS;
579 + $auto_renew = $this->user->is_auto_renew();
580 + $ocd_enabled = $this->options->get( 'optimize_css_delivery', 0 );
581 +
582 + if (
583 + $ocd_enabled
584 + &&
585 + $auto_renew
586 + &&
587 + 4 > $expired_since
588 + ) {
589 + return $menu_title;
590 + }
591 +
592 + if (
593 + ! $auto_renew
594 + &&
595 + ! $ocd_enabled
596 + &&
597 + 4 < $expired_since
598 +
599 + ) {
600 + return $menu_title;
601 + }
602 +
603 + if (
604 + $auto_renew
605 + &&
606 + ! $ocd_enabled
607 + &&
608 + (
609 + 4 > $expired_since
610 + ||
611 + 15 < $expired_since
612 + )
613 + ) {
614 + return $menu_title;
615 + }
616 +
617 + return $menu_title . ' <span class="awaiting-mod">!</span>';
618 + }
619 +
620 + /**
621 + * Sets the dashboard seen transient to hide the expired bubble
622 + *
623 + * @return void
624 + */
625 + public function set_dashboard_seen_transient() {
626 + if ( ! $this->user->is_license_expired() ) {
627 + return;
628 + }
629 +
630 + if ( ! $this->options->get( 'optimize_css_delivery', 0 ) ) {
631 + return;
632 + }
633 +
634 + $current_user = get_current_user_id();
635 +
636 + if ( false !== get_transient( "wpr_dashboard_seen_{$current_user}" ) ) {
637 + return;
638 + }
639 +
640 + $expired_since = ( time() - $this->user->get_license_expiration() ) / DAY_IN_SECONDS;
641 +
642 + if ( 15 > $expired_since ) {
643 + set_transient( "wpr_dashboard_seen_{$current_user}", 1, 15 * DAY_IN_SECONDS );
644 + } elseif ( 15 < $expired_since ) {
645 + set_transient( "wpr_dashboard_seen_{$current_user}", 1, YEAR_IN_SECONDS );
646 + }
647 + }
648 +
649 + /**
650 + * Disable optimize CSS delivery setting
651 + *
652 + * @param array $args Array of setting field arguments.
653 + *
654 + * @return array
655 + */
656 + public function maybe_disable_ocd( $args ) {
657 + if ( 'optimize_css_delivery' !== $args['id'] ) {
658 + return $args;
659 + }
660 +
661 + if ( ! $this->user->is_license_expired() ) {
662 + return $args;
663 + }
664 +
665 + $expired_since = ( time() - $this->user->get_license_expiration() ) / DAY_IN_SECONDS;
666 +
667 + if (
668 + 15 > $expired_since
669 + ||
670 + (
671 + $this->user->is_auto_renew()
672 + &&
673 + 4 > $expired_since
674 + )
675 + ) {
676 + return $args;
677 + }
678 +
679 + $args['value'] = 0;
680 +
681 + if (
682 + isset( $args['container_class'] )
683 + &&
684 + ! in_array( 'wpr-isDisabled', $args['container_class'], true )
685 + ) {
686 + $args['container_class'][] = 'wpr-isDisabled';
687 + }
688 +
689 + $args['input_attr']['disabled'] = 1;
690 +
691 + return $args;
692 + }
693 +
694 + /**
695 + * Disables the RUCSS & Async CSS options if license is expired since more than 15 days
696 + *
697 + * @param mixed $value Current option value.
698 + *
699 + * @return mixed
700 + */
701 + public function maybe_disable_option( $value ) {
702 + $expired_since = ( time() - $this->user->get_license_expiration() ) / DAY_IN_SECONDS;
703 +
704 + if ( 15 > $expired_since ) {
705 + return $value;
706 + }
707 +
708 + return 0;
709 + }
710 + }
711 +