PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 2.2.0
Kubio AI Page Builder v2.2.0
2.8.3 2.8.2 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
260 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(
60 'action' => 'kubio-remote-notifications-retrieve',
61 '_wpnonce' => wp_create_nonce( 'kubio-remote-notifications-retrieve-nonce' )
62
63 ),
64 admin_url( 'admin-ajax.php' )
65 ); ?>
66 <script>
67 window.fetch("<?php echo esc_url_raw($fetch_url ); ?>")
68 </script>
69 <?php
70 }
71 );
72 }
73
74 /**
75 * Retrieves notifications and saves them in a transient.
76 *
77 * @return void
78 */
79 public static function updateNotificationsData() {
80 check_ajax_referer('kubio-remote-notifications-retrieve-nonce');
81 $url = add_query_arg(
82 array(
83 '_fields' => 'acf,id',
84 'meta_key' => 'license_type',
85 'meta_value' => kubio_is_pro() ? 'pro' : 'free',
86 'kubio_version' => KUBIO_VERSION,
87 'kubio_build' => KUBIO_BUILD_NUMBER,
88 'template' => get_template(),
89 'stylesheet' => get_stylesheet(),
90 'source' => Flags::get( 'start_source', 'other' ),
91 'activated_on' => Flags::get( 'kubio_activation_time', '' ),
92 'pro_activated_on' => Flags::get( 'kubio_pro_activation_time', '' ),
93 ),
94 self::$remote_data_url_base
95 );
96
97 $data = wp_remote_get( $url );
98
99 $code = wp_remote_retrieve_response_code( $data );
100 $body = wp_remote_retrieve_body( $data );
101
102 $posts = json_decode( $body, true );
103
104 if ( $code !== 200 ) {
105 wp_send_json_error( $code );
106 }
107
108 $notifications = array();
109
110 foreach ( $posts as $post ) {
111 $notifications[ $post['id'] ] = $post;
112 }
113
114 $done = set_transient( static::getTransientKey(), $notifications, DAY_IN_SECONDS );
115
116 wp_send_json_success( $done );
117 }
118
119 /**
120 * Adds the stack of notifications for display using `kubio_add_dismissable_notice`.
121 *
122 * @param array $notifications
123 * @return void
124 */
125 private static function displayNotifications( $notifications ) {
126
127 if ( empty( $notifications ) ) {
128 return;
129 }
130
131 foreach ( $notifications as $notification ) {
132 $params = $notification['acf'];
133 $params['id'] = $notification['id'];
134
135 if ( $params['dev'] === true && ! self::isDevMode() ) {
136 continue;
137 }
138
139 if ( ! self::isTimeToDisplay( $params ) ) {
140 continue;
141 }
142
143 $classnames = 'kubio-remote-notification';
144 $allowed_types = array( 'info', 'warning', 'error', 'success' );
145
146 if ( ! empty( $params['type'] ) && in_array( $params['type'], $allowed_types ) ) {
147 $classnames .= ' notice-' . $params['type'] . ' kubio-remote-notification-' . $params['type'];
148 }
149
150 $notice_key = 'kubio-remote-notice-' . $params['id'];
151
152 if ( self::isDevMode() ) {
153 $notice_key .= '-' . time();
154 }
155
156 kubio_add_dismissable_notice(
157 $notice_key,
158 array( NotificationsManager::class, 'displayNotification' ),
159 0,
160 $params,
161 $classnames
162 );
163 }
164 }
165
166 /**
167 * Prints the HTML of a notification for the given params.
168 *
169 * @param $params
170 * @return void
171 */
172 public static function displayNotification( $params ) {
173 $link = $params['primary_link'];
174 $slink = $params['secondary_link'];
175
176 wp_enqueue_script( 'wp-util' ); // make sure to enqueue the admin ajax functions
177 ?>
178 <div class="kubio-remote-notification-wrapper" id="kubio-remote-notification-<?php echo esc_attr( $params['id'] ); ?>">
179 <div class="kubio-remote-notification-icon">
180 <?php echo wp_kses_post( KUBIO_LOGO_SVG ); ?>
181 </div>
182 <?php if ( ! empty( $params['message'] ) ) { ?>
183 <div class="kubio-remote-notification-message"><?php echo wpautop( $params['message'] ); ?></div>
184 <?php } ?>
185 <div class="kubio-remote-notification-buttons">
186 <?php if ( ! empty( $link ) ) { ?>
187 <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>
188 <?php
189 }
190
191 if ( ! empty( $slink ) ) {
192 ?>
193 <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>
194 <?php } ?>
195 </div>
196 </div>
197 <?php
198 }
199
200 /**
201 * Verify if the notification checks the time requirements.
202 *
203 * @param array $params Notification parameters.
204 * @return bool
205 */
206 private static function isTimeToDisplay( array $params ) {
207
208 if ( $params['has_time_boundary'] === true ) {
209 return self::inTimeBoundaries( $params['start_date'], $params['date_end'] );
210 }
211
212 $install_time = Flags::get( 'kubio_activation_time', time() );
213
214 if ( kubio_is_pro() ) {
215 $install_time = Flags::get( 'kubio_pro_activation_time', $install_time );
216 }
217
218 $showAfter = strtotime( '+' . $params['after'] . ' days', $install_time );
219 $time = new DateTime( 'NOW' );
220
221 if ( $showAfter <= $time->getTimeStamp() ) {
222 return true;
223 }
224
225 return false;
226 }
227
228 /**
229 * Checks if the current time is between a given $start and $end date.
230 * If $start or $end are null that generally means there is no restrain for that edge.
231 *
232 * @param $start
233 * @param $end
234 * @return bool
235 */
236 private static function inTimeBoundaries( $start, $end ) {
237 $time = new DateTime( 'today' );
238 $startDate = \DateTime::createFromFormat( 'Ymd', $start );
239
240 if ( $start === null || $startDate && $startDate <= $time ) {
241 $endDate = \DateTime::createFromFormat( 'Ymd', $end );
242
243 if ( $end === null || $endDate && $time <= $endDate ) {
244 return true;
245 }
246 }
247
248 return false;
249 }
250
251 private static function getTransientKey() {
252 $transient = 'kubio_remote_notifications';
253 if ( kubio_is_pro() ) {
254 $transient = 'kubio_pro_remote_notifications';
255 }
256
257 return $transient;
258 }
259 }
260