diff --git a/src/Base/RestAPI/ItemFields/LinksField.php b/src/Base/RestAPI/ItemFields/LinksField.php index 2b389a2..18858dd 100644 --- a/src/Base/RestAPI/ItemFields/LinksField.php +++ b/src/Base/RestAPI/ItemFields/LinksField.php @@ -1,12 +1,13 @@ handlePossibleUrlInShortcode($shortcode); } return [ @@ -37,16 +35,36 @@ public function create(WP_Post $post): array } /** - * Get links of a post, if URL & title are present. + * Extract a URL from a shortcode or return the shortcode itself if no URL is found. * - * @param WP_Post $post - * - * @return array + * Some shortcodes may contain a URL embedded within an HTML element, such as an + * tag. This method attempts to extract the URL from the `href` attribute + * using a regular expression. If no valid `href` is found, the raw shortcode + * string is sanitized and returned as a fallback. + */ + private function handlePossibleUrlInShortcode(string $shortcode): string + { + $regex = '/href=["\']([^"\']+)["\']/'; + + if (preg_match($regex, $shortcode, $matches)) { + return esc_url($matches[1]); + } + + return esc_url($shortcode); + } + + /** + * Get links of a post, if URL & title are present. */ - private function getLinks(WP_Post $post) + private function getLinks(WP_Post $post): array { - return array_filter(get_post_meta($post->ID, '_owc_pdc_links_group', true) ?: [], function ($link) { - return (! empty($link['pdc_links_url']) or ! empty($link['pdc_links_shortcode'])) && (! empty($link['pdc_links_title'])); + $links = get_post_meta($post->ID, '_owc_pdc_links_group', true) ?: []; + + return array_filter($links, function ($link) { + $hasUrlOrShortcode = ! empty($link['pdc_links_url']) || ! empty($link['pdc_links_shortcode']); + $hasTitle = ! empty($link['pdc_links_title']); + + return $hasUrlOrShortcode && $hasTitle; }); } }