Skip to content

Commit

Permalink
fix: handle notification version and created_at and updated_at dates
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubmikita committed Jun 2, 2024
1 parent cd214cb commit c73cc27
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -745,11 +745,6 @@ parameters:
count: 1
path: src/Admin/ImportExport.php

-
message: "#^Parameter \\#1 \\$args of function get_posts expects array\\{numberposts\\?\\: int, category\\?\\: int\\|string, include\\?\\: array\\<int\\>, exclude\\?\\: array\\<int\\>, suppress_filters\\?\\: bool, attachment_id\\?\\: int, author\\?\\: int\\|string, author_name\\?\\: string, \\.\\.\\.\\}\\|null, array\\{post_type\\: 'notification', post_status\\: array\\{'publish', 'draft'\\}, posts_per_page\\: \\-1, post__in\\: non\\-empty\\-array\\<int, string\\>\\} given\\.$#"
count: 1
path: src/Admin/ImportExport.php

-
message: "#^Parameter \\#1 \\$json of function json_decode expects string, string\\|false given\\.$#"
count: 1
Expand Down
20 changes: 20 additions & 0 deletions src/Database/NotificationDatabaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,15 @@ public static function upsert(Notification $notification)
*/
do_action('notification/data/save', $notification);

// Get information about created_at currently saved in db
$createdAt = DatabaseService::db()->get_var(
DatabaseService::db()->prepare(
'SELECT created_at FROM %i WHERE hash = %s',
self::getNotificationsTableName(),
$notification->getHash()
)
);

// Insert main notification object.
DatabaseService::db()->replace(
self::getNotificationsTableName(),
Expand All @@ -150,12 +159,16 @@ public static function upsert(Notification $notification)
'title' => $notification->getTitle(),
'trigger_slug' => $notification->getTrigger() ? $notification->getTrigger()->getSlug() : '',
'enabled' => $notification->isEnabled(),
'created_at' => $createdAt ?? current_time('mysql', true),
'updated_at' => current_time('mysql', true),
],
[
'%s',
'%s',
'%s',
'%d',
'%s',
'%s',
]
);

Expand All @@ -170,12 +183,14 @@ public static function upsert(Notification $notification)
'slug' => $carrier->getSlug(),
'data' => wp_json_encode($carrier->getData(), JSON_UNESCAPED_UNICODE),
'enabled' => $carrier->isEnabled(),
'updated_at' => current_time('mysql', true),
],
[
'%s',
'%s',
'%s',
'%d',
'%s',
]
);
}
Expand All @@ -190,6 +205,7 @@ public static function upsert(Notification $notification)
'notification_hash' => $notification->getHash(),
'slug' => $extraKey,
'data' => wp_json_encode($extraData, JSON_UNESCAPED_UNICODE),
'updated_at' => current_time('mysql', true),
]
);
}
Expand Down Expand Up @@ -260,6 +276,10 @@ public static function get($hash)

$notificationData['trigger'] = $notificationData['trigger_slug'];

// Set version based on creation or last update date.
$versionDate = $notificationData['updated_at'] ?? $notificationData['created_at'] ?? 'now';
$notificationData['version'] = strtotime($versionDate);

$carriersDataRaw = DatabaseService::db()->get_results(
DatabaseService::db()->prepare(
'SELECT * FROM %i WHERE notification_hash = %s',
Expand Down

0 comments on commit c73cc27

Please sign in to comment.