Skip to content

Commit

Permalink
fix: get real links
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Thulin committed Dec 2, 2024
1 parent c806bd5 commit 2fbab91
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 94 deletions.
170 changes: 77 additions & 93 deletions source/php/LinkUpdater/LinkUpdater.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

<?php

namespace BrokenLinkDetector\LinkUpdater;

Expand All @@ -11,104 +10,89 @@

class LinkUpdater implements LinkUpdaterInterface, Hookable
{
public function __construct(private WpService $wpService, private Config $config, private Database $database)
{
}

/**
* Add hooks for the link updater
* @return void
*/
public function addHooks(): void
{
$this->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());
}
}
2 changes: 1 addition & 1 deletion source/php/LinkUpdater/LinkUpdaterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 2fbab91

Please sign in to comment.