-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: replace permalinks to posts that are redirected. (#1264)
Co-authored-by: Sebastian Thulin <sebastian.thulin@helsingborg.se>
- Loading branch information
1 parent
78eb1ad
commit 0c6043d
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Municipio\PostDecorators; | ||
|
||
use WpService\Contracts\GetPostMeta; | ||
|
||
/** | ||
* ApplySeoRedirect class. | ||
* | ||
* This class is a PostDecorator implementation that replaces the permalink to the redirect URL if a redirect is set. | ||
*/ | ||
class ApplySeoRedirect implements PostDecorator | ||
{ | ||
/** | ||
* @param GetPostMeta $wpService The WordPress service for retrieving post meta. | ||
* @param PostDecorator|null $inner The inner post decorator. Defaults to a NullDecorator. | ||
*/ | ||
public function __construct(private GetPostMeta $wpService, private ?PostDecorator $inner = new NullDecorator()) | ||
{ | ||
} | ||
|
||
/** | ||
* Applies the SEO redirect to the post. | ||
* | ||
* @param \WP_Post $post The post to replace the url for. | ||
* @return \WP_Post The post with permalink replaced. | ||
*/ | ||
public function apply(\WP_Post $post): \WP_Post | ||
{ | ||
$post = $this->inner->apply($post); | ||
|
||
$seoRedirectMetaUrl = $this->wpService->getPostMeta($post->ID, 'redirect', true); | ||
|
||
if(filter_var($seoRedirectMetaUrl, FILTER_VALIDATE_URL)) { | ||
$post->permalink = $seoRedirectMetaUrl; | ||
} | ||
|
||
return $post; | ||
} | ||
} |