Skip to content

Commit

Permalink
refactor: notification synchronizer
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubmikita committed Jun 2, 2024
1 parent d66e5ee commit 6449775
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 49 deletions.
12 changes: 2 additions & 10 deletions resources/templates/sync/actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,10 @@
?>

<div class="button-group">
<a
href="#"
class="button button-secondary button-icon notification-sync-all"
data-type="json"
>
<a href="#" class="button button-secondary button-icon notification-sync-all" data-type="json">
<span class="dashicons dashicons-download"></span> <?php esc_html_e('Save all to JSON'); ?>
</a>
<a
href="#"
class="button button-secondary button-icon notification-sync-all"
data-type="wordpress"
>
<a href="#" class="button button-secondary button-icon notification-sync-all" data-type="wordpress">
<span class="dashicons dashicons-upload"></span> <?php esc_html_e('Load all to WordPress'); ?>
</a>
</div>
4 changes: 2 additions & 2 deletions resources/templates/sync/disabled.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
// Translators: %s Function name.
__(
sprintf(
'Synchronization is disabled. You can enable it with %s function',
'<code>notification_sync()</code>'
'Synchronization is disabled. You can enable it with %s',
'<code>\BracketSpace\Notification\Core\Sync::enable()</code>'
),
'notification'
)
Expand Down
2 changes: 1 addition & 1 deletion resources/templates/sync/notifications-empty.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
* @var \BracketSpace\Notification\Dependencies\Micropackage\Templates\Template $this Template instance.
*/

esc_html_e('All Notifications are synchronized and up to date.');
esc_html_e('All Notifications are synchronized and up to date.', 'notification');
19 changes: 4 additions & 15 deletions resources/templates/sync/notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,12 @@
<tr>
<td class="title">
<?php if (isset($item['post_id'])) : ?>
<a
href="
<?php
echo esc_url(
(string)get_edit_post_link(
$item['post_id'],
'admin'
)
);
?>
"
>
<a href="<?php echo esc_url((string)get_edit_post_link($item['post_id'], 'admin')); ?>">
<?php endif ?>
<?php echo esc_html($item['notification']->getTitle()); ?>
<?php if (isset($item['post_id'])) : ?>
</a>
<?php endif ?>
<?php if (isset($item['post_id'])) : ?>
</a>
<?php endif ?>
</td>
<td class="hash">
<code><?php echo esc_html($hash); ?></code>
Expand Down
3 changes: 1 addition & 2 deletions src/Admin/Sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@ public function settings($settings)
],
'render' => [new CoreFields\Message(), 'input'],
'sanitize' => [new CoreFields\Message(), 'sanitize'],
'description' => __('Bulk actions for the table below.'),
]
);

if (!CoreSync::isSyncing()) {
if (! CoreSync::isSyncing()) {
return;
}

Expand Down
17 changes: 7 additions & 10 deletions src/Core/Sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Sync
*/
public static function getAllJson()
{
if (!self::isSyncing()) {
if (! self::isSyncing()) {
return [];
}

Expand Down Expand Up @@ -87,7 +87,7 @@ public function loadLocalJson()
$notification = Notification::from('json', $json);

if ($notification->isEnabled()) {
Register::notification($notification);
Register::notificationIfNewer($notification);
}
} catch (\Throwable $e) {
// Do nothing.
Expand Down Expand Up @@ -170,7 +170,7 @@ public function deleteLocalJson($postId)
//phpcs:ignore SlevomatCodingStandard.TypeHints.NullableTypeForNullDefaultValue.NullabilitySymbolRequired
public static function enable(string $path = null)
{
if (!$path) {
if (! $path) {
$path = trailingslashit(get_stylesheet_directory()) . 'notifications';
}

Expand All @@ -187,11 +187,11 @@ public static function enable(string $path = null)

$fs = static::getSyncFs();

if (!$fs) {
if (! $fs) {
return;
}

if (!$fs->exists('') || !$fs->is_dir('')) {
if (! $fs->exists('') || ! $fs->is_dir('')) {
$fs->mkdir('');
}

Expand All @@ -200,10 +200,7 @@ public static function enable(string $path = null)
}

$fs->touch('index.php');
$fs->put_contents(
'index.php',
'<?php' . "\r\n" . '// Keep this file here.' . "\r\n"
);
$fs->put_contents('index.php', '<?php' . "\r\n" . '// Keep this file here.' . "\r\n");
}

/**
Expand Down Expand Up @@ -236,7 +233,7 @@ public static function getSyncPath()
*/
public static function getSyncFs()
{
if (!static::isSyncing()) {
if (! static::isSyncing()) {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Integration/WordPressIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function loadDatabaseNotifications()
*/
$notifications = $cache->collect(static fn() => NotificationDatabaseService::getAll());

array_map([Register::class, 'notification'], $notifications);
array_map([Register::class, 'notificationIfNewer'], $notifications);
}

/**
Expand Down
10 changes: 2 additions & 8 deletions src/Utils/Settings/Fields/SyncTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,7 @@ public function input($field)

$wpNotification = $collection[$notification->getHash()]['notification'];

if (
version_compare(
(string)$wpNotification->getVersion(),
(string)$notification->getVersion(),
'>='
)
) {
if (version_compare((string)$wpNotification->getVersion(), (string)$notification->getVersion(), '>=')) {
$collection[$notification->getHash()]['up_to_date'] = true;
}
} else {
Expand All @@ -85,7 +79,7 @@ public function input($field)

// Filter synchronized.
foreach ($collection as $key => $data) {
if (!$data['up_to_date']) {
if (! $data['up_to_date']) {
continue;
}

Expand Down

0 comments on commit 6449775

Please sign in to comment.