Skip to content

Commit

Permalink
fix: add valid function
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Thulin committed Dec 2, 2024
1 parent 2fbab91 commit eddc340
Showing 1 changed file with 36 additions and 8 deletions.
44 changes: 36 additions & 8 deletions source/php/LinkUpdater/LinkUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,47 @@ public function beforeUpdateLinks(int $postId, array $post): void
*/
public function updateLinks(int $postId, WP_Post $postBefore, WP_Post $postAfter): void
{
if ($this->storedPermalink === null) {
$previousPermalink = $this->storedPermalink;
$currentPermalink = $this->wpService->getPermalink($postId);

if (!$this->isValidReplaceRequest($previousPermalink, $currentPermalink)) {
return;
}

$previousPermalink = $this->storedPermalink;
$currentPermalink = $this->wpService->getPermalink($postId);
if ($this->shouldReplaceForPosttype($postAfter->post_type)) {
$this->replaceLinks($previousPermalink, $currentPermalink);
}
}

/**
* Validate the replace request
* @param string $oldLink
* @param string $newLink
* @return bool
*/
private function isValidReplaceRequest(string $oldLink, string $newLink): bool
{
//Ensure that the old link is not the same as the new link
if ($oldLink === $newLink) {
return false;
}

if ($previousPermalink != $currentPermalink && $this->shouldReplaceForPosttype($postAfter->post_type)) {
//Ensure that the home url is not replaced
if($this->wpService->homeUrl() !== $previousPermalink) {
$this->replaceLinks($previousPermalink, $currentPermalink);
}
//Ensure that the old link is not empty
if (empty($oldLink)) {
return false;
}

//Ensure that the new link is not empty
if (empty($newLink)) {
return false;
}

//Ensure that the old link is not a home url
if ($this->wpService->homeUrl() === $oldLink) {
return false;
}

return true;
}

/**
Expand Down

0 comments on commit eddc340

Please sign in to comment.