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