PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 2.1.3
Kubio AI Page Builder v2.1.3
2.8.1 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.9.0 2.0.0 2.1.1 2.1.2 2.1.3 2.2.0 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.5 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.6.2 2.6.3 2.6.5 2.6.6 2.6.7 2.7.0 2.7.1 2.7.2 2.7.3 2.8.0
kubio / lib / src / NotificationsManager.php
kubio / lib / src Last commit date
CLI 2 years ago Core 2 years ago DemoSites 2 years ago AssetsDependencyInjector.php 3 years ago Config.php 3 years ago FileLog.php 2 years ago Flags.php 2 years ago GoogleFontsLocalLoader.php 2 years ago Migrations.php 4 years ago NotificationsManager.php 2 years ago PluginsManager.php 2 years ago
NotificationsManager.php
254 lines
1 <?php
2
3 namespace Kubio;
4
5 use DateTime;
6
7 class NotificationsManager {
8
9 private static $remote_data_url_base = 'https://kubiobuilder.com/wp-json/wp/v2/notification';
10
11 public static function load() {
12 add_action( 'admin_init', array( NotificationsManager::class, 'init' ) );
13 if ( ! wp_next_scheduled( NotificationsManager::class . '::init' ) ) {
14 wp_schedule_event( time(), 'twicedaily', NotificationsManager::class . '::init' );
15 }
16 }
17
18 /**
19 * Checks if this WordPress instances is declared as a development environment.
20 * Relies on the `KUBIO_NOTIFICATIONS_DEV_MODE` constant.
21 *
22 * @return bool
23 */
24 private static function isDevMode() {
25 return ( defined( 'KUBIO_NOTIFICATIONS_DEV_MODE' ) && KUBIO_NOTIFICATIONS_DEV_MODE );
26 }
27
28 /**
29 * Verifies the data and displays remote notifications accordingly.
30 *
31 * @return void
32 */
33 public static function init() {
34
35 // check if we have cached data in transient
36 $notifications = get_transient( static::getTransientKey() );
37
38 if ( $notifications === false || self::isDevMode() ) {
39 // No notifications, try to get them from remote and cache them.
40 static::prepareRetrieveRemoteNotifications();
41 }
42
43 static::displayNotifications( $notifications );
44
45 add_action( 'wp_ajax_kubio-remote-notifications-retrieve', array( NotificationsManager::class, 'updateNotificationsData' ) );
46 }
47
48 /**
49 * Adds a JavaScript code which fetches notifications asynchronously.
50 *
51 * @return void
52 */
53 public static function prepareRetrieveRemoteNotifications() {
54
55 add_action(
56 'admin_footer',
57 function () {
58 $fetch_url = add_query_arg(
59 array( 'action' => 'kubio-remote-notifications-retrieve' ),
60 admin_url( 'admin-ajax.php' )
61 ); ?>
62 <script>
63 window.fetch("<?php echo esc_js( $fetch_url ); ?>")
64 </script>
65 <?php
66 }
67 );
68 }
69
70 /**
71 * Retrieves notifications and saves them in a transient.
72 *
73 * @return void
74 */
75 public static function updateNotificationsData() {
76
77 $url = add_query_arg(
78 array(
79 '_fields' => 'acf,id',
80 'meta_key' => 'license_type',
81 'meta_value' => kubio_is_pro() ? 'pro' : 'free',
82 'kubio_version' => KUBIO_VERSION,
83 'kubio_build' => KUBIO_BUILD_NUMBER,
84 'template' => get_template(),
85 'stylesheet' => get_stylesheet(),
86 'source' => Flags::get( 'start_source', 'other' ),
87 ),
88 self::$remote_data_url_base
89 );
90
91 $data = wp_remote_get( $url );
92
93 $code = wp_remote_retrieve_response_code( $data );
94 $body = wp_remote_retrieve_body( $data );
95
96 $posts = json_decode( $body, true );
97
98 if ( $code !== 200 ) {
99 wp_send_json_error( $code );
100 }
101
102 $notifications = array();
103
104 foreach ( $posts as $post ) {
105 $notifications[ $post['id'] ] = $post;
106 }
107
108 $done = set_transient( static::getTransientKey(), $notifications, DAY_IN_SECONDS );
109
110 wp_send_json_success( $done );
111 }
112
113 /**
114 * Adds the stack of notifications for display using `kubio_add_dismissable_notice`.
115 *
116 * @param array $notifications
117 * @return void
118 */
119 private static function displayNotifications( $notifications ) {
120
121 if ( empty( $notifications ) ) {
122 return;
123 }
124
125 foreach ( $notifications as $notification ) {
126 $params = $notification['acf'];
127 $params['id'] = $notification['id'];
128
129 if ( $params['dev'] === true && ! self::isDevMode() ) {
130 continue;
131 }
132
133 if ( ! self::isTimeToDisplay( $params ) ) {
134 continue;
135 }
136
137 $classnames = 'kubio-remote-notification';
138 $allowed_types = array( 'info', 'warning', 'error', 'success' );
139
140 if ( ! empty( $params['type'] ) && in_array( $params['type'], $allowed_types ) ) {
141 $classnames .= ' notice-' . $params['type'] . ' kubio-remote-notification-' . $params['type'];
142 }
143
144 $notice_key = 'kubio-remote-notice-' . $params['id'];
145
146 if ( self::isDevMode() ) {
147 $notice_key .= '-' . time();
148 }
149
150 kubio_add_dismissable_notice(
151 $notice_key,
152 array( NotificationsManager::class, 'displayNotification' ),
153 0,
154 $params,
155 $classnames
156 );
157 }
158 }
159
160 /**
161 * Prints the HTML of a notification for the given params.
162 *
163 * @param $params
164 * @return void
165 */
166 public static function displayNotification( $params ) {
167 $link = $params['primary_link'];
168 $slink = $params['secondary_link'];
169
170 wp_enqueue_script( 'wp-util' ); // make sure to enqueue the admin ajax functions
171 ?>
172 <div class="kubio-remote-notification-wrapper" id="kubio-remote-notification-<?php echo esc_attr( $params['id'] ); ?>">
173 <div class="kubio-remote-notification-icon">
174 <?php echo wp_kses_post( KUBIO_LOGO_SVG ); ?>
175 </div>
176 <?php if ( ! empty( $params['message'] ) ) { ?>
177 <div class="kubio-remote-notification-message"><?php echo wpautop( $params['message'] ); ?></div>
178 <?php } ?>
179 <div class="kubio-remote-notification-buttons">
180 <?php if ( ! empty( $link ) ) { ?>
181 <a target="_blank" href="<?php echo esc_url( $link['url'] ); ?>" class="button button-large kubio-remote-notification-primary"><?php echo esc_html( $link['title'] ); ?></a>
182 <?php
183 }
184
185 if ( ! empty( $slink ) ) {
186 ?>
187 <a target="_blank" href="<?php echo esc_url( $slink['url'] ); ?>" class="button button-link kubio-remote-notification-secondary"><?php echo esc_html( $slink['title'] ); ?></a>
188 <?php } ?>
189 </div>
190 </div>
191 <?php
192 }
193
194 /**
195 * Verify if the notification checks the time requirements.
196 *
197 * @param array $params Notification parameters.
198 * @return bool
199 */
200 private static function isTimeToDisplay( array $params ) {
201
202 if ( $params['has_time_boundary'] === true ) {
203 return self::inTimeBoundaries( $params['start_date'], $params['date_end'] );
204 }
205
206 $install_time = Flags::get( 'kubio_activation_time', time() );
207
208 if ( kubio_is_pro() ) {
209 $install_time = Flags::get( 'kubio_pro_activation_time', $install_time );
210 }
211
212 $showAfter = strtotime( '+' . $params['after'] . ' days', $install_time );
213 $time = new DateTime( 'NOW' );
214
215 if ( $showAfter <= $time->getTimeStamp() ) {
216 return true;
217 }
218
219 return false;
220 }
221
222 /**
223 * Checks if the current time is between a given $start and $end date.
224 * If $start or $end are null that generally means there is no restrain for that edge.
225 *
226 * @param $start
227 * @param $end
228 * @return bool
229 */
230 private static function inTimeBoundaries( $start, $end ) {
231 $time = new DateTime( 'today' );
232 $startDate = \DateTime::createFromFormat( 'Ymd', $start );
233
234 if ( $start === null || $startDate && $startDate <= $time ) {
235 $endDate = \DateTime::createFromFormat( 'Ymd', $end );
236
237 if ( $end === null || $endDate && $time <= $endDate ) {
238 return true;
239 }
240 }
241
242 return false;
243 }
244
245 private static function getTransientKey() {
246 $transient = 'kubio_remote_notifications';
247 if ( kubio_is_pro() ) {
248 $transient = 'kubio_pro_remote_notifications';
249 }
250
251 return $transient;
252 }
253 }
254