PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / trunk
Kubio AI Page Builder vtrunk
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 1 year ago Core 1 month ago DemoSites 1 month ago AssetsDependencyInjector.php 1 year ago Config.php 1 year ago FileLog.php 1 year ago Flags.php 1 year ago GoogleFontsLocalLoader.php 1 year ago GutenbergControls.php 1 year ago Migrations.php 1 year ago NotificationsManager.php 1 month ago PluginsManager.php 2 years ago
NotificationsManager.php
302 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 . '::onSchedule' ) ) {
14 wp_schedule_event( time(), 'twicedaily', NotificationsManager::class . '::onSchedule' );
15 }
16 add_action(NotificationsManager::class . '::onSchedule', NotificationsManager::class . '::onSchedule' );
17 }
18
19 /**
20 * Checks if this WordPress instances is declared as a development environment.
21 * Relies on the `KUBIO_NOTIFICATIONS_DEV_MODE` constant.
22 *
23 * @return bool
24 */
25 private static function isDevMode() {
26 return ( defined( 'KUBIO_NOTIFICATIONS_DEV_MODE' ) && KUBIO_NOTIFICATIONS_DEV_MODE );
27 }
28
29 /**
30 * Verifies the data and displays remote notifications accordingly.
31 *
32 * @return void
33 */
34 public static function init() {
35
36 // check if we have cached data in transient
37 $notifications = get_transient( static::getTransientKey() );
38
39 if ( $notifications === false || self::isDevMode() ) {
40 // No notifications, try to get them from remote and cache them.
41 static::prepareRetrieveRemoteNotifications();
42 }
43
44 static::displayNotifications( $notifications );
45
46 add_action( 'wp_ajax_kubio-remote-notifications-retrieve', array( NotificationsManager::class, 'updateNotificationsData' ) );
47 }
48
49 public static function onSchedule() {
50 // check if we have cached data in transient
51 $notifications = get_transient( static::getTransientKey() );
52
53 if ( $notifications === false || self::isDevMode() ) {
54 // No notifications, try to get them from remote and cache them.
55 static::callNotificationsEndpoint();
56 }
57 }
58
59 /**
60 * Adds a JavaScript code which fetches notifications asynchronously.
61 *
62 * @return void
63 */
64 public static function prepareRetrieveRemoteNotifications() {
65
66 add_action(
67 'admin_footer',
68 function () {
69 $fetch_url = add_query_arg(
70 array(
71 'action' => 'kubio-remote-notifications-retrieve',
72 '_wpnonce' => wp_create_nonce( 'kubio-remote-notifications-retrieve-nonce' ),
73
74 ),
75 admin_url( 'admin-ajax.php' )
76 ); ?>
77 <script>
78 window.fetch("<?php echo esc_url_raw( $fetch_url ); ?>")
79 </script>
80 <?php
81 }
82 );
83 }
84
85 /**
86 * Retrieves notifications and saves them in a transient.
87 *
88 * @return void
89 */
90 public static function updateNotificationsData() {
91 check_ajax_referer( 'kubio-remote-notifications-retrieve-nonce' );
92 $done = static::callNotificationsEndpoint();
93 wp_send_json_success( $done );
94
95 }
96
97 public static function callNotificationsEndpoint() {
98
99 $url = add_query_arg(
100 array(
101 '_fields' => 'acf,id',
102 // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
103 'meta_key' => 'license_type',
104 // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
105 'meta_value' => apply_filters( 'kubio/notifications/license_type', 'free' ),
106 'kubio_version' => KUBIO_VERSION,
107 'kubio_build' => KUBIO_BUILD_NUMBER,
108 'kubio_theme_version' => wp_get_theme()->get( 'Version' ),
109 'template' => get_template(),
110 'stylesheet' => get_stylesheet(),
111 'source' => Flags::get( 'start_source', 'other' ),
112 'f' => Flags::get( 'kubio_f' ),
113 'activated_on' => Flags::get( 'kubio_activation_time', '' ),
114 'pro_activated_on' => Flags::get( 'kubio_pro_activation_time', '' ),
115 ),
116 self::$remote_data_url_base
117 );
118
119 $data = wp_remote_get( $url );
120
121 $code = wp_remote_retrieve_response_code( $data );
122 $body = wp_remote_retrieve_body( $data );
123
124 $posts = json_decode( $body, true );
125
126 if ( $code !== 200 ) {
127 wp_send_json_error( $code );
128 }
129
130 $notifications = array();
131
132 foreach ( $posts as $post ) {
133 $notifications[ $post['id'] ] = $post;
134 }
135
136 $done = set_transient( static::getTransientKey(), $notifications, DAY_IN_SECONDS );
137
138 return $done;
139 }
140
141 /**
142 * Adds the stack of notifications for display using `kubio_add_dismissable_notice`.
143 *
144 * @param array $notifications
145 * @return void
146 */
147 private static function displayNotifications( $notifications ) {
148
149 if ( empty( $notifications ) ) {
150 return;
151 }
152
153 foreach ( $notifications as $notification ) {
154 $params = $notification['acf'];
155 $params['id'] = $notification['id'];
156
157 if ( $params['dev'] === true && ! self::isDevMode() ) {
158 continue;
159 }
160
161 if ( ! self::isTimeToDisplay( $params ) ) {
162 continue;
163 }
164
165 $classnames = 'kubio-remote-notification';
166 $allowed_types = array( 'info', 'warning', 'error', 'success' );
167
168 if ( ! empty( $params['type'] ) && in_array( $params['type'], $allowed_types ) ) {
169 $classnames .= ' notice-' . $params['type'] . ' kubio-remote-notification-' . $params['type'];
170 }
171
172 $notice_key = 'kubio-remote-notice-' . $params['id'];
173
174 if ( self::isDevMode() ) {
175 $notice_key .= '-' . time();
176 }
177
178 kubio_add_dismissable_notice(
179 $notice_key,
180 array( NotificationsManager::class, 'displayNotification' ),
181 0,
182 $params,
183 $classnames
184 );
185 }
186 }
187
188 /**
189 * Prints the HTML of a notification for the given params.
190 *
191 * @param $params
192 * @return void
193 */
194 public static function displayNotification( $params ) {
195 $link = $params['primary_link'];
196 $slink = $params['secondary_link'];
197
198 $args = array(
199 'utm_theme' => get_template(),
200 'utm_childtheme' => get_stylesheet(),
201 'utm_install_source' => Flags::get( 'start_source', 'other' ),
202 'utm_activated_on' => Flags::get( 'kubio_activation_time', '' ),
203 'utm_pro_activated_on' => Flags::get( 'kubio_pro_activation_time', '' ),
204 'utm_campaign' => 'wp-notice',
205 'utm_medium' => 'wp',
206 );
207
208 if ( ! empty( $link ) ) {
209 $link['url'] = add_query_arg( $args, $link['url'] );
210 }
211
212 if ( ! empty( $slink ) ) {
213 $slink['url'] = add_query_arg( $args, $slink['url'] );
214 }
215
216 wp_enqueue_script( 'wp-util' ); // make sure to enqueue the admin ajax functions
217 ?>
218 <div class="kubio-remote-notification-wrapper" id="kubio-remote-notification-<?php echo esc_attr( $params['id'] ); ?>">
219 <div class="kubio-remote-notification-icon">
220 <?php
221 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
222 echo wp_kses_post( KUBIO_LOGO_SVG );
223 ?>
224 </div>
225 <?php if ( ! empty( $params['message'] ) ) { ?>
226 <div class="kubio-remote-notification-message">
227 <?php
228 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
229 echo wpautop( $params['message'] );
230 ?>
231 </div>
232 <?php } ?>
233 <div class="kubio-remote-notification-buttons">
234 <?php if ( ! empty( $link ) ) { ?>
235 <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>
236 <?php
237 }
238
239 if ( ! empty( $slink ) ) {
240 ?>
241 <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>
242 <?php } ?>
243 </div>
244 </div>
245 <?php
246 }
247
248 /**
249 * Verify if the notification checks the time requirements.
250 *
251 * @param array $params Notification parameters.
252 * @return bool
253 */
254 private static function isTimeToDisplay( array $params ) {
255
256 if ( $params['has_time_boundary'] === true ) {
257 return self::inTimeBoundaries( $params['start_date'], $params['date_end'] );
258 }
259
260 $install_time = Flags::get( 'kubio_activation_time', time() );
261
262 $install_time = apply_filters( 'kubio/notifications/install_time', $install_time );
263
264 $show_after = strtotime( '+' . $params['after'] . ' days', $install_time );
265 $time = new DateTime( 'NOW' );
266
267 if ( $show_after <= $time->getTimeStamp() ) {
268 return true;
269 }
270
271 return false;
272 }
273
274 /**
275 * Checks if the current time is between a given $start and $end date.
276 * If $start or $end are null that generally means there is no restrain for that edge.
277 *
278 * @param $start
279 * @param $end
280 * @return bool
281 */
282 private static function inTimeBoundaries( $start, $end ) {
283 $time = new DateTime( 'today' );
284 $start_date = \DateTime::createFromFormat( 'Ymd', $start );
285
286 if ( $start === null || $start_date && $start_date <= $time ) {
287 $end_date = \DateTime::createFromFormat( 'Ymd', $end );
288
289 if ( $end === null || $end_date && $time <= $end_date ) {
290 return true;
291 }
292 }
293
294 return false;
295 }
296
297 private static function getTransientKey() {
298 $transient = apply_filters( 'kubio/notifications/transient_key', 'kubio_remote_notifications' );
299 return $transient;
300 }
301 }
302