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