Skip to content

Commit

Permalink
fix: clear the affected posts cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Thulin committed Dec 2, 2024
1 parent 65aa2ec commit 06e4dc3
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions source/php/LinkUpdater/LinkUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ private function isValidReplaceRequest(string $oldLink, string $newLink): bool
private function replaceLinks(string $oldLink, string $newLink): int
{
$db = $this->database->getInstance();

//Get the post ids that contain the old link
$postIds = $db->get_col(
$db->prepare(
"SELECT ID
FROM $db->posts
WHERE post_content LIKE %s",
'%' . $db->esc_like($oldLink) . '%'
)
);

//Update the post content
$db->query(
$db->prepare(
"UPDATE $db->posts
Expand All @@ -111,9 +123,26 @@ private function replaceLinks(string $oldLink, string $newLink): int
'%' . $db->esc_like($oldLink) . '%'
)
);

//Clear the object cache for the post ids
$this->clearObjectCache($postIds);

//Return the number of rows affected
return $db->rows_affected;
}

/**
* Clear the object cache for the post ids
*
* @param array $postIds
*/
private function clearObjectCache(array $postIds): void
{
foreach ($postIds as $postId) {
$this->wpService->cleanPostCache($postId);
}
}

/**
* Check if the post type should be replaced
* @param string $postType
Expand Down

0 comments on commit 06e4dc3

Please sign in to comment.