Core
4 years ago
DemoSites
4 years ago
Config.php
4 years ago
Flags.php
4 years ago
GoogleFontsLocalLoader.php
4 years ago
Migrations.php
4 years ago
NotificationsManager.php
4 years ago
PluginsManager.php
4 years ago
NotificationsManager.php
245 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 | } |
| 14 | |
| 15 | /** |
| 16 | * Checks if this WordPress instances is declared as a development environment. |
| 17 | * Relies on the `KUBIO_NOTIFICATIONS_DEV_MODE` constant. |
| 18 | * |
| 19 | * @return bool |
| 20 | */ |
| 21 | private static function isDevMode() { |
| 22 | return ( defined( 'KUBIO_NOTIFICATIONS_DEV_MODE' ) && KUBIO_NOTIFICATIONS_DEV_MODE ); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Verifies the data and displays remote notifications accordingly. |
| 27 | * |
| 28 | * @return void |
| 29 | */ |
| 30 | public static function init() { |
| 31 | |
| 32 | // check if we have cached data in transient |
| 33 | $notifications = get_transient( static::getTransientKey() ); |
| 34 | |
| 35 | if ( $notifications === false || self::isDevMode() ) { |
| 36 | // No notifications, try to get them from remote and cache them. |
| 37 | static::prepareRetrieveRemoteNotifications(); |
| 38 | } |
| 39 | |
| 40 | static::displayNotifications( $notifications ); |
| 41 | |
| 42 | add_action( 'wp_ajax_kubio-remote-notifications-retrieve', array( NotificationsManager::class, 'updateNotificationsData' ) ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Adds a JavaScript code which fetches notifications asynchronously. |
| 47 | * |
| 48 | * @return void |
| 49 | */ |
| 50 | public static function prepareRetrieveRemoteNotifications() { |
| 51 | |
| 52 | add_action( |
| 53 | 'admin_footer', |
| 54 | function () { |
| 55 | $fetch_url = add_query_arg( |
| 56 | array( 'action' => 'kubio-remote-notifications-retrieve' ), |
| 57 | admin_url( 'admin-ajax.php' ) |
| 58 | ); ?> |
| 59 | <script> |
| 60 | window.fetch("<?php echo esc_js( $fetch_url ); ?>") |
| 61 | </script> |
| 62 | <?php |
| 63 | } |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Retrieves notifications and saves them in a transient. |
| 69 | * |
| 70 | * @return void |
| 71 | */ |
| 72 | public static function updateNotificationsData() { |
| 73 | $data = wp_remote_get( |
| 74 | add_query_arg( |
| 75 | array( |
| 76 | '_fields' => 'acf,id', |
| 77 | 'meta_key' => 'license_type', |
| 78 | 'meta_value' => kubio_is_pro() ? 'pro' : 'free', |
| 79 | ), |
| 80 | self::$remote_data_url_base |
| 81 | ) |
| 82 | ); |
| 83 | |
| 84 | $code = wp_remote_retrieve_response_code( $data ); |
| 85 | $body = wp_remote_retrieve_body( $data ); |
| 86 | |
| 87 | $posts = json_decode( $body, true ); |
| 88 | |
| 89 | if ( $code !== 200 ) { |
| 90 | wp_send_json_error( $code ); |
| 91 | } |
| 92 | |
| 93 | $notifications = array(); |
| 94 | |
| 95 | foreach ( $posts as $post ) { |
| 96 | $notifications[ $post['id'] ] = $post; |
| 97 | } |
| 98 | |
| 99 | $done = set_transient( static::getTransientKey(), $notifications, DAY_IN_SECONDS ); |
| 100 | |
| 101 | wp_send_json_success( $done ); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Adds the stack of notifications for display using `kubio_add_dismissable_notice`. |
| 106 | * |
| 107 | * @param array $notifications |
| 108 | * @return void |
| 109 | */ |
| 110 | private static function displayNotifications( $notifications ) { |
| 111 | |
| 112 | if ( empty( $notifications ) ) { |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | foreach ( $notifications as $notification ) { |
| 117 | $params = $notification['acf']; |
| 118 | $params['id'] = $notification['id']; |
| 119 | |
| 120 | if ( $params['dev'] === true && ! self::isDevMode() ) { |
| 121 | continue; |
| 122 | } |
| 123 | |
| 124 | if ( ! self::isTimeToDisplay( $params ) ) { |
| 125 | continue; |
| 126 | } |
| 127 | |
| 128 | $classnames = 'kubio-remote-notification'; |
| 129 | $allowed_types = array( 'info', 'warning', 'error', 'success' ); |
| 130 | |
| 131 | if ( ! empty( $params['type'] ) && in_array( $params['type'], $allowed_types ) ) { |
| 132 | $classnames .= ' notice-' . $params['type'] . ' kubio-remote-notification-' . $params['type']; |
| 133 | } |
| 134 | |
| 135 | $notice_key = 'kubio-remote-notice-' . $params['id']; |
| 136 | |
| 137 | if ( self::isDevMode() ) { |
| 138 | $notice_key .= '-' . time(); |
| 139 | } |
| 140 | |
| 141 | kubio_add_dismissable_notice( |
| 142 | $notice_key, |
| 143 | array( NotificationsManager::class, 'displayNotification' ), |
| 144 | 0, |
| 145 | $params, |
| 146 | $classnames |
| 147 | ); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Prints the HTML of a notification for the given params. |
| 153 | * |
| 154 | * @param $params |
| 155 | * @return void |
| 156 | */ |
| 157 | public static function displayNotification( $params ) { |
| 158 | $link = $params['primary_link']; |
| 159 | $slink = $params['secondary_link']; |
| 160 | |
| 161 | wp_enqueue_script( 'wp-util' ); // make sure to enqueue the admin ajax functions |
| 162 | ?> |
| 163 | <div class="kubio-remote-notification-wrapper" id="kubio-remote-notification-<?php echo $params['id']; ?>"> |
| 164 | <div class="kubio-remote-notification-icon"> |
| 165 | <?php echo wp_kses_post( KUBIO_LOGO_SVG ); ?> |
| 166 | </div> |
| 167 | <?php if ( ! empty( $params['message'] ) ) { ?> |
| 168 | <div class="kubio-remote-notification-message"><?php echo wpautop( $params['message'] ); ?></div> |
| 169 | <?php } ?> |
| 170 | <div class="kubio-remote-notification-buttons"> |
| 171 | <?php if ( ! empty( $link ) ) { ?> |
| 172 | <a target="_blank" href="<?php echo $link['url']; ?>" class="button button-large kubio-remote-notification-primary"><?php echo $link['title']; ?></a> |
| 173 | <?php |
| 174 | } |
| 175 | |
| 176 | if ( ! empty( $slink ) ) { |
| 177 | ?> |
| 178 | <a target="_blank" href="<?php echo $slink['url']; ?>" class="button button-link kubio-remote-notification-secondary"><?php echo $slink['title']; ?></a> |
| 179 | <?php } ?> |
| 180 | </div> |
| 181 | </div> |
| 182 | <?php |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Verify if the notification checks the time requirements. |
| 187 | * |
| 188 | * @param array $params Notification parameters. |
| 189 | * @return bool |
| 190 | */ |
| 191 | private static function isTimeToDisplay( array $params ) { |
| 192 | |
| 193 | if ( $params['has_time_boundary'] === true ) { |
| 194 | return self::inTimeBoundaries( $params['start_date'], $params['date_end'] ); |
| 195 | } |
| 196 | |
| 197 | $install_time = Flags::get( 'kubio_activation_time', time() ); |
| 198 | |
| 199 | if ( kubio_is_pro() ) { |
| 200 | $install_time = Flags::get( 'kubio_pro_activation_time', $install_time ); |
| 201 | } |
| 202 | |
| 203 | $showAfter = strtotime( '+' . $params['after'] . ' days', $install_time ); |
| 204 | $time = new DateTime( 'NOW' ); |
| 205 | |
| 206 | if ( $showAfter <= $time->getTimeStamp() ) { |
| 207 | return true; |
| 208 | } |
| 209 | |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Checks if the current time is between a given $start and $end date. |
| 215 | * If $start or $end are null that generally means there is no restrain for that edge. |
| 216 | * |
| 217 | * @param $start |
| 218 | * @param $end |
| 219 | * @return bool |
| 220 | */ |
| 221 | private static function inTimeBoundaries( $start, $end ) { |
| 222 | $time = new DateTime( 'today' ); |
| 223 | $startDate = \DateTime::createFromFormat( 'Ymd', $start ); |
| 224 | |
| 225 | if ( $start === null || $startDate && $startDate <= $time ) { |
| 226 | $endDate = \DateTime::createFromFormat( 'Ymd', $end ); |
| 227 | |
| 228 | if ( $end === null || $endDate && $time <= $endDate ) { |
| 229 | return true; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | return false; |
| 234 | } |
| 235 | |
| 236 | private static function getTransientKey() { |
| 237 | $transient = 'kubio_remote_notifications'; |
| 238 | if ( kubio_is_pro() ) { |
| 239 | $transient = 'kubio_pro_remote_notifications'; |
| 240 | } |
| 241 | |
| 242 | return $transient; |
| 243 | } |
| 244 | } |
| 245 |