From dff643d0b2cd0195e3a3b14a1cddc2162f6f6abe Mon Sep 17 00:00:00 2001 From: Jakub Mikita Date: Mon, 27 May 2024 11:50:33 +0200 Subject: [PATCH] refactor: notification duplicator --- phpstan-baseline.neon | 10 ------ src/Admin/NotificationDuplicator.php | 51 +++++++++++++++------------- 2 files changed, 27 insertions(+), 34 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 171f1e62..9d2329cc 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -735,16 +735,6 @@ parameters: count: 1 path: src/Admin/NotificationDuplicator.php - - - message: "#^Cannot access property \\$post_title on WP_Post\\|null\\.$#" - count: 1 - path: src/Admin/NotificationDuplicator.php - - - - message: "#^Parameter \\#1 \\$postarr of function wp_insert_post expects array\\{ID\\?\\: int, post_author\\?\\: int, post_date\\?\\: string, post_date_gmt\\?\\: string, post_content\\?\\: string, post_content_filtered\\?\\: string, post_title\\?\\: string, post_excerpt\\?\\: string, \\.\\.\\.\\}, array\\{post_title\\: non\\-falsy\\-string, post_content\\: mixed, post_status\\: 'draft', post_type\\: 'notification'\\} given\\.$#" - count: 1 - path: src/Admin/NotificationDuplicator.php - - message: "#^Parameter \\#1 \\$string of function html_entity_decode expects string, string\\|null given\\.$#" count: 1 diff --git a/src/Admin/NotificationDuplicator.php b/src/Admin/NotificationDuplicator.php index 7a21c4c6..1c5da79a 100644 --- a/src/Admin/NotificationDuplicator.php +++ b/src/Admin/NotificationDuplicator.php @@ -10,8 +10,8 @@ namespace BracketSpace\Notification\Admin; -use function BracketSpace\Notification\adaptNotificationFrom; -use function BracketSpace\Notification\swapNotificationAdapter; +use BracketSpace\Notification\Database\NotificationDatabaseService; +use BracketSpace\Notification\Integration\WordPressIntegration; /** * Notification duplicator class @@ -61,39 +61,42 @@ public function notificationDuplicate() { check_admin_referer('duplicate_notification', 'nonce'); - if (!isset($_GET['duplicate'])) { + if (! isset($_GET['duplicate'])) { exit; } // Get the source notification post. $source = get_post(intval(wp_unslash($_GET['duplicate']))); - $wp = adaptNotificationFrom('WordPress', $source); - /** - * JSON Adapter - * - * @var \BracketSpace\Notification\Defaults\Adapter\JSON - */ - $json = swapNotificationAdapter('JSON', $wp); + if (get_post_type($source) !== 'notification' || ! $source instanceof \WP_Post ) { + wp_die("You cannot duplicate post that's not a Notification post"); + } - $json->refreshHash(); - $json->setEnabled(false); + $notification = WordPressIntegration::postToNotification($source); - if (get_post_type($source) !== 'notification') { - wp_die('You cannot duplicate post that\'s not Notification post'); + if ($notification === null) { + wp_die("It doesn't seem that Notification exist anymore"); } - $newId = wp_insert_post( - [ - // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps - 'post_title' => sprintf('(%s) %s', __('Duplicate', 'notification'), $source->post_title), - 'post_content' => wp_slash($json->save(JSON_UNESCAPED_UNICODE)), - 'post_status' => 'draft', - 'post_type' => 'notification', - ] - ); + $newNotification = clone $notification; + $newNotification->refreshHash(); + $newNotification->setEnabled(false); + + // Create duplicated Notification. + do_action('notification/data/save', $newNotification); + NotificationDatabaseService::upsert($newNotification); + do_action('notification/data/saved', $newNotification); + + // Create duplicated WP_Post. + $postId = wp_insert_post([ + 'post_title' => $newNotification->getTitle(), + 'post_name' => $newNotification->getHash(), + 'post_content' => '', + 'post_status' => 'draft', + 'post_type' => 'notification', + ]); - wp_safe_redirect(html_entity_decode(get_edit_post_link($newId))); + wp_safe_redirect(html_entity_decode(get_edit_post_link($postId))); exit; } }