From 4062161e4a5959862ec78fdccf132be36288309f Mon Sep 17 00:00:00 2001 From: Mario Santos Date: Mon, 17 Oct 2022 14:39:33 +0200 Subject: [PATCH 1/3] Add filter to add the wp-link attribute to links --- wp-directives.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/wp-directives.php b/wp-directives.php index 526e22c8..04b53e69 100644 --- a/wp-directives.php +++ b/wp-directives.php @@ -1,4 +1,5 @@ next_tag('a'); + $w->set_attribute('wp-link', 'true'); + return $w; +} + +add_filter('render_block', 'add_wp_link_attribute', 10, 2); From d2ecd1c284a49c996e00705c37db45dc9e0bb86c Mon Sep 17 00:00:00 2001 From: Mario Santos Date: Mon, 17 Oct 2022 16:18:43 +0200 Subject: [PATCH 2/3] Add the wp-link recursively and add host check --- wp-directives.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/wp-directives.php b/wp-directives.php index 04b53e69..c4364bb5 100644 --- a/wp-directives.php +++ b/wp-directives.php @@ -40,9 +40,19 @@ function wp_directives_register_scripts() function add_wp_link_attribute($block_content) { + $site_url = parse_url(get_site_url()); $w = new WP_HTML_Tag_Processor($block_content); - $w->next_tag('a'); - $w->set_attribute('wp-link', 'true'); + while ($w->next_tag('a')) { + $link = parse_url($w->get_attribute('href')); + + if ($w->get_attribute('target') === '_blank') { + break; + } + + if (is_null($link['host']) || ($link['host'] === $site_url['host'])) { + $w->set_attribute('wp-link', 'true'); + } + }; return $w; } From ac36ece389f08b0ca8caa5d5b58ce0b9284de40e Mon Sep 17 00:00:00 2001 From: Mario Santos Date: Mon, 17 Oct 2022 17:09:17 +0200 Subject: [PATCH 3/3] Return a string in the filter --- wp-directives.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/wp-directives.php b/wp-directives.php index c4364bb5..f947ffde 100644 --- a/wp-directives.php +++ b/wp-directives.php @@ -43,17 +43,16 @@ function add_wp_link_attribute($block_content) $site_url = parse_url(get_site_url()); $w = new WP_HTML_Tag_Processor($block_content); while ($w->next_tag('a')) { - $link = parse_url($w->get_attribute('href')); - if ($w->get_attribute('target') === '_blank') { break; } + $link = parse_url($w->get_attribute('href')); if (is_null($link['host']) || ($link['host'] === $site_url['host'])) { $w->set_attribute('wp-link', 'true'); } }; - return $w; + return (string) $w; } add_filter('render_block', 'add_wp_link_attribute', 10, 2);