From 543c0e7e1a761b01fd505ae078f6cc380837c44b Mon Sep 17 00:00:00 2001 From: Richard Korthuis Date: Mon, 17 Mar 2025 12:17:09 +0100 Subject: [PATCH] Use the correct portal URL if the Show On functionality is used. (Partly copied from PDC Base). --- src/Base/Models/PortalLinkGenerator.php | 28 +++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/Base/Models/PortalLinkGenerator.php b/src/Base/Models/PortalLinkGenerator.php index d68ad31..fb76454 100644 --- a/src/Base/Models/PortalLinkGenerator.php +++ b/src/Base/Models/PortalLinkGenerator.php @@ -3,6 +3,7 @@ namespace OWC\OpenPub\Base\Models; use OWC\OpenPub\Base\Settings\SettingsPageOptions; +use WP_Term; class PortalLinkGenerator { @@ -43,6 +44,11 @@ public function generateBasePortalLink(): string private function createPortalSlug(): self { $portalURL = $this->pubSettings->getPortalURL(); + + if ($this->pubSettings->useShowOn()) { + $portalURL = $this->getShowOnPortalURL(); + } + $portalSlug = $this->pubSettings->getPortalItemSlug(); $this->updatePortalURL($portalURL); @@ -51,6 +57,28 @@ private function createPortalSlug(): self return $this; } + private function getShowOnPortalURL(): string + { + $terms = wp_get_object_terms($this->post->getID(), 'openpub-show-on'); + + if (! is_array($terms) || empty($terms)) { + return ''; + } + + $portalURL = reset($terms); + if (isset($_GET['source'])) { + foreach( $terms as $term ) { + if ($term->slug === $_GET['source']) { + $portalURL = $term; + break; + } + } + } + $portalURL = $portalURL instanceof WP_Term ? $portalURL->name : ''; + + return wp_http_validate_url($portalURL) ? $portalURL : '/'; + } + private function appendPostSlug(): self { if (! empty($this->post->getPostName())) {