AppNotifications.php
145 lines
| 1 | <?php |
| 2 | |
| 3 | namespace NitroPack\WordPress\Notifications; |
| 4 | use NitroPack\HttpClient\HttpClient; |
| 5 | use \NitroPack\SDK\Filesystem; |
| 6 | |
| 7 | /* |
| 8 | * Class AppNotifications |
| 9 | * |
| 10 | * This class handles the notification.json file, mostly used for app notifications. |
| 11 | * |
| 12 | * @package NitroPack\WordPress\AppNotifications |
| 13 | */ |
| 14 | |
| 15 | class AppNotifications { |
| 16 | private static $instance = NULL; |
| 17 | private $cacheTtl = 3600; |
| 18 | private $getSiteId; |
| 19 | private $notifications; |
| 20 | private $notificationsFile; |
| 21 | |
| 22 | public function __construct() { |
| 23 | $this->getSiteId = get_nitropack()->getSiteId(); |
| 24 | $this->notificationsFile = nitropack_trailingslashit( NITROPACK_DATA_DIR ) . 'notifications.json'; |
| 25 | $this->notifications = NULL; |
| 26 | } |
| 27 | public static function getInstance() { |
| 28 | if ( ! self::$instance ) { |
| 29 | self::$instance = new AppNotifications(); |
| 30 | } |
| 31 | |
| 32 | return self::$instance; |
| 33 | } |
| 34 | public function get( $type = NULL ) { |
| 35 | if ( $this->notifications === NULL ) { |
| 36 | $this->load(); |
| 37 | } |
| 38 | |
| 39 | if ( isset( $this->notifications[ $this->getSiteId ] ) ) { |
| 40 | $result = $this->notifications[ $this->getSiteId ]; |
| 41 | |
| 42 | if ( $type ) { |
| 43 | $notifications = isset( $result['notifications'][ $type ] ) ? $result['notifications'][ $type ] : []; |
| 44 | |
| 45 | } else { |
| 46 | $notifications = $result['notifications']; |
| 47 | } |
| 48 | } else { |
| 49 | $notifications = []; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | return apply_filters( 'get_nitropack_notifications', $notifications, $type ); |
| 54 | } |
| 55 | |
| 56 | private function load() { |
| 57 | $this->notifications = []; |
| 58 | |
| 59 | if ( Filesystem::fileExists( $this->notificationsFile ) ) { |
| 60 | $this->notifications = json_decode( Filesystem::fileGetContents( $this->notificationsFile ), true ); |
| 61 | if ( ! empty( $this->notifications ) && isset( $this->notifications[ $this->getSiteId ] ) ) { |
| 62 | $result = $this->notifications[ $this->getSiteId ]; |
| 63 | if ( $result['last_modified'] + $this->cacheTtl > time() ) { // The cache is still fresh |
| 64 | $this->removeExpiredSystemNotifications(); |
| 65 | return; |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | if ( get_nitropack()->isConnected() ) { |
| 71 | try { |
| 72 | $result = $this->fetch(); |
| 73 | $this->notifications[ $this->getSiteId ] = [ |
| 74 | 'last_modified' => time(), |
| 75 | 'notifications' => $result |
| 76 | ]; |
| 77 | Filesystem::filePutContents( $this->notificationsFile, json_encode( $this->notifications ) ); |
| 78 | } catch (\Exception $e) { |
| 79 | $this->notifications[ $this->getSiteId ] = [ // We need this entry in order to make use of the cache logic |
| 80 | 'last_modified' => time(), |
| 81 | 'error' => $e->getMessage(), |
| 82 | 'notifications' => [] |
| 83 | ]; |
| 84 | Filesystem::filePutContents( $this->notificationsFile, json_encode( $this->notifications ) ); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | public function removeNotificationById( $notificationId ) { |
| 90 | if ( $this->notifications === NULL ) { |
| 91 | $this->load(); |
| 92 | } |
| 93 | |
| 94 | if ( empty( $notificationId ) || ! isset( $this->notifications[ $this->getSiteId ]['notifications'] ) || ! \is_array( $this->notifications[ $this->getSiteId ]['notifications'] ) ) { |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | $removed = false; |
| 99 | foreach ( $this->notifications[ $this->getSiteId ]['notifications'] as $type => $items ) { |
| 100 | if ( ! \is_array( $items ) ) { |
| 101 | continue; |
| 102 | } |
| 103 | |
| 104 | foreach ( $items as $key => $notification ) { |
| 105 | if ( isset( $notification['id'] ) && $notification['id'] === $notificationId ) { |
| 106 | unset( $this->notifications[ $this->getSiteId ]['notifications'][ $type ][ $key ] ); |
| 107 | $this->notifications[ $this->getSiteId ]['notifications'][ $type ] = array_values( $this->notifications[ $this->getSiteId ]['notifications'][ $type ] ); |
| 108 | $removed = true; |
| 109 | break; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | if ( $removed ) { |
| 114 | break; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if ( $removed ) { |
| 119 | $this->notifications[ $this->getSiteId ]['last_modified'] = time(); |
| 120 | Filesystem::filePutContents( $this->notificationsFile, json_encode( $this->notifications ) ); |
| 121 | } |
| 122 | |
| 123 | return $removed; |
| 124 | } |
| 125 | |
| 126 | private function fetch() { |
| 127 | $notificationsUrl = get_nitropack_integration_url( 'notifications_json' ); |
| 128 | $client = new HttpClient( $notificationsUrl ); |
| 129 | $client->setHeader( "x-nitro-platform", "wordpress" ); |
| 130 | $client->fetch(); |
| 131 | $resp = $client->getStatusCode() == 200 ? json_decode( $client->getBody(), true ) : false; |
| 132 | return $resp ? $resp['notifications'] : []; |
| 133 | } |
| 134 | |
| 135 | private function removeExpiredSystemNotifications() { |
| 136 | if ( isset( $this->notifications[ $this->getSiteId ]['notifications']['system'] ) ) { |
| 137 | date_default_timezone_set( 'UTC' ); |
| 138 | foreach ( $this->notifications[ $this->getSiteId ]['notifications']['system'] as $key => $notification ) { |
| 139 | if ( strtotime( $notification['end_date'] ) < time() ) { |
| 140 | unset( $this->notifications[ $this->getSiteId ]['notifications']['system'][ $key ] ); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |