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