Skip to content

Commit

Permalink
refactor: move notification wp helpers to notification db service class
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubmikita committed May 28, 2024
1 parent 6b4d949 commit f470f46
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 77 deletions.
9 changes: 4 additions & 5 deletions src/Admin/ImportExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
namespace BracketSpace\Notification\Admin;

use BracketSpace\Notification\Core\Notification;
use BracketSpace\Notification\Database\NotificationDatabaseService;
use BracketSpace\Notification\Integration\WordPressIntegration;
use BracketSpace\Notification\Database\NotificationDatabaseService as Db;
use BracketSpace\Notification\Utils\Settings\Fields as SettingFields;
use BracketSpace\Notification\Database\Queries\NotificationQueries;

Expand Down Expand Up @@ -125,7 +124,7 @@ public function prepareNotificationsExportData(array $items = [])
);

foreach ($posts as $post) {
$notification = WordPressIntegration::postToNotification($post);
$notification = Db::postToNotification($post);

if (!($notification instanceof Notification)) {
continue;
Expand Down Expand Up @@ -223,7 +222,7 @@ public function processNotificationsImportRequest($data)
$existingNotification = NotificationQueries::withHash($notification->getHash());

if ($existingNotification === null) {
NotificationDatabaseService::upsert($notification);
Db::upsert($notification);
$added++;
} else {
if ($existingNotification->getVersion() >= $notification->getVersion()) {
Expand All @@ -235,7 +234,7 @@ public function processNotificationsImportRequest($data)
continue;
}

NotificationDatabaseService::upsert($notification);
Db::upsert($notification);
$updated++;
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/Admin/NotificationDuplicator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

namespace BracketSpace\Notification\Admin;

use BracketSpace\Notification\Database\NotificationDatabaseService;
use BracketSpace\Notification\Integration\WordPressIntegration;
use BracketSpace\Notification\Database\NotificationDatabaseService as Db;

/**
* Notification duplicator class
Expand Down Expand Up @@ -72,7 +71,7 @@ public function notificationDuplicate()
wp_die("You cannot duplicate post that's not a Notification post");
}

$notification = WordPressIntegration::postToNotification($source);
$notification = Db::postToNotification($source);

if ($notification === null) {
wp_die("It doesn't seem that Notification exist anymore");
Expand All @@ -84,7 +83,7 @@ public function notificationDuplicate()

// Create duplicated Notification.
do_action('notification/data/save', $newNotification);
NotificationDatabaseService::upsert($newNotification);
Db::upsert($newNotification);
do_action('notification/data/saved', $newNotification);

// Create duplicated WP_Post.
Expand Down
9 changes: 4 additions & 5 deletions src/Admin/PostTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

namespace BracketSpace\Notification\Admin;

use BracketSpace\Notification\Database\NotificationDatabaseService;
use BracketSpace\Notification\Integration\WordPressIntegration;
use BracketSpace\Notification\Database\NotificationDatabaseService as Db;

/**
* PostTable class
Expand Down Expand Up @@ -89,7 +88,7 @@ static function ($label, $slug) {
*/
public function tableColumnContent($column, $postId)
{
$notification = WordPressIntegration::postToNotification($postId);
$notification = Db::postToNotification($postId);

if ($notification === null) {
return;
Expand Down Expand Up @@ -258,7 +257,7 @@ public function handleStatusBulkActions($redirectTo, $doaction, $postIds)
);

foreach ($postIds as $postId) {
$notification = WordPressIntegration::postToNotification($postId);
$notification = Db::postToNotification($postId);

if ($notification === null) {
continue;
Expand All @@ -267,7 +266,7 @@ public function handleStatusBulkActions($redirectTo, $doaction, $postIds)
$notification->setEnabled($doaction === 'enable');

do_action('notification/data/save', $notification);
NotificationDatabaseService::upsert($notification);
Db::upsert($notification);
do_action('notification/data/saved', $notification);
}

Expand Down
23 changes: 8 additions & 15 deletions src/Admin/PostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
namespace BracketSpace\Notification\Admin;

use BracketSpace\Notification\Core\Notification;
use BracketSpace\Notification\Database\NotificationDatabaseService;
use BracketSpace\Notification\Integration\WordPressIntegration;
use BracketSpace\Notification\Database\NotificationDatabaseService as Db;
use BracketSpace\Notification\Store;
use BracketSpace\Notification\Dependencies\Micropackage\Ajax\Response;

Expand Down Expand Up @@ -219,13 +218,13 @@ public function bypassTrash($postId)
*/
public function deleteNotification($postId)
{
$notification = WordPressIntegration::postToNotification($postId);
$notification = Db::postToNotification($postId);

if ($notification === null) {
return;
}

NotificationDatabaseService::delete($notification->getHash());
Db::delete($notification->getHash());
}

/**
Expand Down Expand Up @@ -267,7 +266,7 @@ public function save($postId, $post, $update)

$data = $_POST;

$notification = WordPressIntegration::postToNotification($post) ?? new Notification();
$notification = Db::postToNotification($post) ?? new Notification();

// Hash.
if (isset($data['post_name'])) {
Expand Down Expand Up @@ -327,7 +326,7 @@ public function save($postId, $post, $update)
// Hook into this action if you want to save any Notification Post data.
do_action('notification/data/save', $notification);

NotificationDatabaseService::upsert($notification);
Db::upsert($notification);

do_action_deprecated('notification/data/save/after', [$notification], '[Next]', 'notification/data/saved');
do_action('notification/data/saved', $notification);
Expand Down Expand Up @@ -356,27 +355,21 @@ public function ajaxChangeNotificationStatus()

$ajax->verify_nonce('change_notification_status_' . $data['post_id']);

$notification = WordPressIntegration::postToNotification($data['post_id']);
$notification = Db::postToNotification($data['post_id']);

if ($notification === null) {
$ajax->error($errorMessage);
} else {
$notification->setEnabled($data['status'] === 'true');

do_action('notification/data/save', $notification);
NotificationDatabaseService::upsert($notification);
Db::upsert($notification);
do_action('notification/data/saved', $notification);
}

$ajax->send(true);
}

/**
* --------------------------------------------------
* Notifications.
* --------------------------------------------------
*/

/**
* Gets all Notifications from database.
*
Expand All @@ -392,6 +385,6 @@ public static function getAllNotifications()
'BracketSpace\Notification\Database\NotificationDatabaseService::getAll'
);

return NotificationDatabaseService::getAll();
return Db::getAll();
}
}
8 changes: 4 additions & 4 deletions src/Admin/Screen.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

use BracketSpace\Notification\Core\Notification;
use BracketSpace\Notification\Core\Templates;
use BracketSpace\Notification\Database\NotificationDatabaseService as Db;
use BracketSpace\Notification\Dependencies\Micropackage\Casegnostic\Casegnostic;
use BracketSpace\Notification\Integration\WordPressIntegration;
use BracketSpace\Notification\Interfaces;
use BracketSpace\Notification\Store;
use BracketSpace\Notification\Dependencies\Micropackage\Ajax\Response;
Expand Down Expand Up @@ -78,7 +78,7 @@ public function setupNotification()
return;
}

self::$currentNotification = WordPressIntegration::postToNotification($_GET['post']);
self::$currentNotification = Db::postToNotification($_GET['post']);
}

/**
Expand Down Expand Up @@ -276,7 +276,7 @@ public function renderSaveMetabox($post)
[
'post_id' => $post->ID,
'delete_link_label' => $deleteText,
'notification' => WordPressIntegration::postToNotification($post) ?? new Notification(),
'notification' => Db::postToNotification($post) ?? new Notification(),
]
);
}
Expand Down Expand Up @@ -311,7 +311,7 @@ public function addMergeTagsMetaBox()
*/
public function renderMergeTagsMetabox($post)
{
$notification = WordPressIntegration::postToNotification($post) ?? new Notification();
$notification = Db::postToNotification($post) ?? new Notification();
$trigger = $notification->getTrigger();
$triggerSlug = $trigger ? $trigger->getSlug() : false;

Expand Down
4 changes: 2 additions & 2 deletions src/Api/Controller/RepeaterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace BracketSpace\Notification\Api\Controller;

use BracketSpace\Notification\Core\Notification;
use BracketSpace\Notification\Integration\WordPressIntegration;
use BracketSpace\Notification\Database\NotificationDatabaseService as Db;
use BracketSpace\Notification\Store;

/**
Expand Down Expand Up @@ -102,7 +102,7 @@ public function formFieldData($data = null)
*/
public function getValues($postId, $carrier, $field)
{
$notification = WordPressIntegration::postToNotification($postId);
$notification = Db::postToNotification($postId);

if (! $notification instanceof Notification) {
return [];
Expand Down
4 changes: 2 additions & 2 deletions src/Core/Sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

use BracketSpace\Notification\Dependencies\Micropackage\Casegnostic\Casegnostic;
use BracketSpace\Notification\Dependencies\Micropackage\Filesystem\Filesystem;
use BracketSpace\Notification\Integration\WordPressIntegration;
use BracketSpace\Notification\Database\NotificationDatabaseService as Db;
use function BracketSpace\Notification\addNotification;

/**
Expand Down Expand Up @@ -143,7 +143,7 @@ public function deleteLocalJson($postId)
return;
}

$notification = WordPressIntegration::postToNotification($postId);
$notification = Db::postToNotification($postId);

if (! $notification instanceof Notification) {
return;
Expand Down
31 changes: 31 additions & 0 deletions src/Database/NotificationDatabaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
/**
* Notification Database Service.
*
* This class handles both wp_notification* tables and wp_posts table in sync.
*
* @package notification
*/

Expand All @@ -11,10 +13,13 @@
namespace BracketSpace\Notification\Database;

use BracketSpace\Notification\Core\Notification;
use BracketSpace\Notification\Store\Notification as NotificationStore;
use function BracketSpace\Notification\convertNotificationData;

/**
* This class describes a notification database service.
*
* @since [Next]
*/
class NotificationDatabaseService
{
Expand Down Expand Up @@ -60,6 +65,32 @@ public static function count(): int
);
}

/**
* Translates post ID to Notification object
*
* @param int|\WP_Post $post Notification post object or post ID
* @return ?Notification
*/
public static function postToNotification($post): ?Notification
{
$hash = get_post_field('post_name', $post, 'raw');

return NotificationStore::has($hash) ? NotificationStore::get($hash) : null;
}

/**
* Translates Notification to WP_Post
*
* @param string|Notification $notification Notification object or hash.
* @return ?\WP_Post
*/
public static function notificationToPost($notification): ?\WP_Post
{
$hash = $notification instanceof Notification ? $notification->getHash() : $notification;

return get_page_by_path($hash, OBJECT, 'post');
}

/**
* Upserts the Notification database entry.
*
Expand Down
6 changes: 3 additions & 3 deletions src/Database/Queries/NotificationQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace BracketSpace\Notification\Database\Queries;

use BracketSpace\Notification\Core\Notification;
use BracketSpace\Notification\Integration\WordPressIntegration;
use BracketSpace\Notification\Database\NotificationDatabaseService as Db;

/**
* Notification Queries class
Expand Down Expand Up @@ -49,7 +49,7 @@ public static function all(bool $includingDisabled = false): array
}

foreach ($wpposts as $wppost) {
$notification = WordPressIntegration::postToNotification($wppost);
$notification = Db::postToNotification($wppost);

if (!($notification instanceof Notification)) {
continue;
Expand All @@ -72,6 +72,6 @@ public static function withHash(string $hash)
{
$post = get_page_by_path($hash, OBJECT, 'notification');

return empty($post) ? null : WordPressIntegration::postToNotification($post);
return empty($post) ? null : Db::postToNotification($post);
}
}
Loading

0 comments on commit f470f46

Please sign in to comment.