From 2fbab91fd5b32a1bdf2fabd7e4c77fde295415dc Mon Sep 17 00:00:00 2001 From: Sebastian Thulin Date: Mon, 2 Dec 2024 13:04:16 +0100 Subject: [PATCH] fix: get real links --- source/php/LinkUpdater/LinkUpdater.php | 170 ++++++++---------- .../php/LinkUpdater/LinkUpdaterInterface.php | 2 +- 2 files changed, 78 insertions(+), 94 deletions(-) diff --git a/source/php/LinkUpdater/LinkUpdater.php b/source/php/LinkUpdater/LinkUpdater.php index 6a133cb..b2e0b85 100644 --- a/source/php/LinkUpdater/LinkUpdater.php +++ b/source/php/LinkUpdater/LinkUpdater.php @@ -1,5 +1,4 @@ -wpService->addAction('wp_after_insert_post', array($this, 'updateLinks'), 10, 3); + private ?string $storedPermalink = null; + + public function __construct(private WpService $wpService, private Config $config, private Database $database) + { + } + + /** + * Add hooks for the link updater + * @return void + */ + public function addHooks(): void + { + //Fetches the previous permalink before the post is updated + $this->wpService->addFilter('pre_post_update', [$this, 'beforeUpdateLinks'], 10, 2); + + //Updates the links in the post content if the post name has changed + $this->wpService->addAction('post_updated', [$this, 'updateLinks'], 10, 3); + } + + /** + * Before the post is updated, store the post data + * @param int $postId + * @param array $post + * @return void + */ + public function beforeUpdateLinks(int $postId, array $post): void + { + $this->storedPermalink = $this->wpService->getPermalink($postId); + } + + /** + * Update the links in the post content if the post name has changed + * @param int $id + * @param WP_Post $postBefore + * @param WP_Post $postAfter + * @return void + */ + public function updateLinks(int $postId, WP_Post $postBefore, WP_Post $postAfter): void + { + if ($this->storedPermalink === null) { + return; } - /** - * Update the links in the post content if the post name has changed - * @param array $data - * @param array $post - * @return bool - */ - public function updateLinks(int|WP_Post $post, bool $isUpdate, null|WP_Post $postBefore): void - { - if(!$isUpdate) { - return; - } - - if(!is_a($post, 'WP_Post') && is_numeric($post)) { - $post = $this->wpService->getPost($post); - } + $previousPermalink = $this->storedPermalink; + $currentPermalink = $this->wpService->getPermalink($postId); - if($this->linkHasChanged($post, $postBefore) && $this->shouldReplaceForPosttype($this->wpService->getPostType($post))) { - $this->replaceLinks( - $this->createPermalink($post->ID, $postBefore->post_name), - $this->createPermalink($post->ID, $post->post_name) - ); + 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); } } - - /** - * Replace the old link with the new link in posts that contains the link - * @param string $newLink - * @param string $oldLink - * @return int - */ - private function replaceLinks(string $newLink, string $oldLink): int - { - + } + + /** + * Replace the old link with the new link in the post content + * @param string $oldLink + * @param string $newLink + * @return int + */ + private function replaceLinks(string $oldLink, string $newLink): int + { $db = $this->database->getInstance(); - $db->query( - $db->prepare( - "UPDATE $db->posts - SET post_content = REPLACE(post_content, %s, %s) - WHERE post_content LIKE %s", - $oldLink, - $newLink, - '%' . $db->esc_like($oldLink) . '%' - ) + $db->prepare( + "UPDATE $db->posts + SET post_content = REPLACE(post_content, %s, %s) + WHERE post_content LIKE %s", + $oldLink, + $newLink, + '%' . $db->esc_like($oldLink) . '%' + ) ); - return $db->rows_affected; - } - - /** - * Create a permalink from the post id and post name - * @param int $postId - * @param string $postName - * @return string - */ - public function createPermalink(int $postId, string $postName): string - { - $permalink = preg_replace('/[^\/]+\/?$/', - $postName, - $this->wpService->getPermalink($postId) - ); - - $permalink = rtrim($permalink, '/'); - - return $permalink; - } - - /** - * Check if the link has changed - * @param WP_Post $post The newly submitted data - * @param WP_Post $postBefore The stored post data - * @return bool - */ - public function linkHasChanged(WP_Post $post, WP_Post $postBefore): bool - { - return $post->post_name !== $postBefore->post_name; - } - - /** - * Check if the link should be replaced for the post type - * @param string $postType - * @return bool - */ - private function shouldReplaceForPosttype(string $postType): bool - { - return !in_array($postType, $this->config->linkUpdaterBannedPostTypes()); - } + } + + /** + * Check if the post type should be replaced + * @param string $postType + * @return bool + */ + private function shouldReplaceForPosttype(string $postType): bool + { + return !in_array($postType, $this->config->linkUpdaterBannedPostTypes()); + } } \ No newline at end of file diff --git a/source/php/LinkUpdater/LinkUpdaterInterface.php b/source/php/LinkUpdater/LinkUpdaterInterface.php index 6fe7e91..f632e16 100644 --- a/source/php/LinkUpdater/LinkUpdaterInterface.php +++ b/source/php/LinkUpdater/LinkUpdaterInterface.php @@ -4,5 +4,5 @@ use WP_Post; interface LinkUpdaterInterface { - public function updateLinks(int|WP_Post $post, bool $isUpdate, null|WP_Post $postBefore): void; + public function updateLinks(int $postId, WP_Post $postBefore, WP_Post $postAfter): void; } \ No newline at end of file