Skip to content

Commit

Permalink
refactor: notification duplicator
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubmikita committed May 27, 2024
1 parent 28aba9b commit dff643d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 34 deletions.
10 changes: 0 additions & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 27 additions & 24 deletions src/Admin/NotificationDuplicator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}

0 comments on commit dff643d

Please sign in to comment.