PluginProbe
Tiered Pricing Table for WooCommerce / trunk
Tiered Pricing Table for WooCommerce vtrunk
7.1.7 7.1.5 7.1.4 7.1.1 6.5.0 6.4.0 6.1.0 trunk 1.0 1.1 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 All 90 releases
tier-pricing-table / src / Admin / Notifications / Notifications.php

Notifications.php in Tiered Pricing Table for WooCommerce trunk, at src/Admin/Notifications/Notifications.php

125 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php namespace TierPricingTable\Admin\Notifications;
2
3 use TierPricingTable\Admin\Notifications\Notifications\ActivationNotification;
4 use TierPricingTable\Admin\Notifications\Notifications\BlackFriday2024;
5 use TierPricingTable\Admin\Notifications\Notifications\BlackFriday2025;
6 use TierPricingTable\Admin\Notifications\Notifications\BlackFriday2026;
7 use TierPricingTable\Admin\Notifications\Notifications\BlackFriday2027;
8 use TierPricingTable\Admin\Notifications\Notifications\BlackFriday2028;
9 use TierPricingTable\Admin\Notifications\Notifications\BlackFriday2029;
10 use TierPricingTable\Admin\Notifications\Notifications\BlackFriday2030;
11 use TierPricingTable\Admin\Notifications\Notifications\Notification;
12 use TierPricingTable\Admin\Notifications\Notifications\TwoMonthsUsingDiscount;
13 use TierPricingTable\Admin\Notifications\Notifications\YearUsingDiscount;
14 use TierPricingTable\Core\ServiceContainerTrait;
15
16 /**
17 * Class Notifications
18 *
19 * @package TierPricingTable\Admin\Notifications
20 */
21 class Notifications {
22
23 use ServiceContainerTrait;
24
25 const CLOSE_NOTIFICATION_ACTION = 'tpt_close_notification';
26 const SEEN_NOTIFICATIONS_OPTION_KEY = 'tpt_seen_notifications';
27
28 public function __construct() {
29 add_action( 'init', array( $this, 'init' ) );
30 }
31
32 public function init() {
33 $this->handleClosingNotification();
34 $this->initNotifications();
35 }
36
37 public function handleClosingNotification() {
38 add_action( 'admin_post_' . self::CLOSE_NOTIFICATION_ACTION, function () {
39 $nonce = isset( $_GET['nonce'] ) ? sanitize_text_field( $_GET['nonce'] ) : false;
40
41 if ( ! wp_verify_nonce( $nonce, self::CLOSE_NOTIFICATION_ACTION ) ) {
42 return wp_safe_redirect( wp_get_referer() );
43 }
44
45 $notificationID = isset( $_GET['notification_id'] ) ? sanitize_text_field( $_GET['notification_id'] ) : false;
46
47 if ( ! $notificationID ) {
48 return wp_safe_redirect( wp_get_referer() );
49 }
50
51 $this->markNotificationAsSeen( $notificationID );
52
53 return wp_safe_redirect( wp_get_referer() );
54 } );
55 }
56
57 public function initNotifications() {
58
59 foreach ( $this->getNotifications() as $notification ) {
60 if ( $notification->isActive() ) {
61
62 add_action( 'admin_notices', function () use ( $notification ) {
63 $this->getContainer()->getFileManager()->includeTemplate( $notification->getTemplate(), array(
64 'notification' => $notification,
65 ) );
66 }, 10 );
67
68 return;
69 }
70 }
71 }
72
73 public function getNotificationCloseURL( $id ): string {
74 return add_query_arg( array(
75 'action' => self::CLOSE_NOTIFICATION_ACTION,
76 'nonce' => wp_create_nonce( self::CLOSE_NOTIFICATION_ACTION ),
77 'notification_id' => $id,
78 ), admin_url( 'admin-post.php' ) );
79 }
80
81 public function getSeenNotifications(): array {
82 $seenNotifications = get_option( self::SEEN_NOTIFICATIONS_OPTION_KEY, array() );
83
84 return is_array( $seenNotifications ) ? array_filter( $seenNotifications ) : array();
85 }
86
87 public function updateSeenNotifications( array $seenNotifications ) {
88 update_option( self::SEEN_NOTIFICATIONS_OPTION_KEY, array_filter( $seenNotifications ) );
89 }
90
91 public function markNotificationAsSeen( $id ) {
92 $seenNotifications = $this->getSeenNotifications();
93
94 $seenNotifications[ $id ] = true;
95
96 $this->updateSeenNotifications( $seenNotifications );
97 }
98
99 public function isNotificationSeen( $id ): bool {
100 return array_key_exists( $id, $this->getSeenNotifications() );
101 }
102
103 /**
104 * Get notifications
105 *
106 * @return Notification[]
107 */
108 public function getNotifications(): array {
109 return array(
110 new TwoMonthsUsingDiscount(),
111 new YearUsingDiscount(),
112 // The activation notification was changed to welcome page.
113 // new ActivationNotification(),
114 new BlackFriday2024(),
115 new BlackFriday2025(),
116 new BlackFriday2026(),
117 new BlackFriday2027(),
118 new BlackFriday2028(),
119 new BlackFriday2029(),
120 new BlackFriday2030(),
121 );
122 }
123
124 }
125