Skip to content

Commit 353ff5e

Browse files
authored
Enusre that exluded links are not rendered in teh links data attribute (#317)
* Enusre that exluded links are not rendered in teh links data attribute * Use the link collections is_empty() helper
1 parent 8b6fca9 commit 353ff5e

2 files changed

Lines changed: 130 additions & 2 deletions

File tree

src/WP_Post/WP_Post_Controller.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,9 @@ public function render_block( string $block_content, array $block ): string { //
306306
}
307307

308308
// Compile the data.
309-
$links = $this->link_repository->get_links_for_post( $post_id );
309+
$links = $this->link_repository->get_links_for_post( $post_id, true );
310310

311-
if ( empty( $links ) ) {
311+
if ( $links->is_empty() ) {
312312
return $block_content;
313313
}
314314

tests/WP_Post/Test_WP_Post_Controller.php

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Internet_Archive\Wayback_Machine_Link_Fixer\Tests\Processor;
1313

1414
use Internet_Archive\Wayback_Machine_Link_Fixer\Settings\Settings;
15+
use Internet_Archive\Wayback_Machine_Link_Fixer\Link\Link_Exclusion;
1516
use Internet_Archive\Wayback_Machine_Link_Fixer\Link\Link_Repository;
1617
use Internet_Archive\Wayback_Machine_Link_Fixer\WP_Post\WP_Post_Controller;
1718

@@ -618,6 +619,133 @@ public function test_excluded_post_does_not_render_link_data(): void {
618619
\delete_option( Settings::LINK_FIXER_EXCLUDED_POSTS );
619620
}
620621

622+
/**
623+
* @testdox When a link matches a global exclusion pattern, it should not appear in the render_block data attribute output.
624+
*
625+
* @return void
626+
*/
627+
public function test_excluded_link_not_included_in_render_block_data(): void {
628+
// Set the option to render the HTML link output.
629+
update_option( Settings::FIXER_OPTION, Settings::FIXER_OPTION_REPLACE_LINK );
630+
631+
// Add a global exclusion pattern that matches one of the links.
632+
update_option( Settings::LINK_EXCLUSIONS, array( '*excluded-domain.com*' ) );
633+
634+
// Reset the Link_Exclusion static cache so it picks up the new option.
635+
$reflection = new \ReflectionClass( Link_Exclusion::class );
636+
$property = $reflection->getProperty( 'exclusions' );
637+
$property->setAccessible( true );
638+
$property->setValue( null, null );
639+
640+
$post_id = self::factory()->post->create();
641+
642+
// Add content with two links — one that will be excluded, one that won't.
643+
$content = 'Link one <a href="https://excluded-domain.com/page">excluded</a> and link two <a href="https://allowed-domain.com/page">allowed</a>';
644+
wp_update_post(
645+
array(
646+
'ID' => $post_id,
647+
'post_content' => $content,
648+
'post_type' => 'post',
649+
)
650+
);
651+
652+
$GLOBALS['post'] = get_post( $post_id );
653+
654+
// Render the block.
655+
$rendered = do_blocks( $GLOBALS['post']->post_content );
656+
657+
// The data attribute should be present (we still have one non-excluded link).
658+
$this->assertStringContainsString( 'data-iawmlf-post-links', $rendered );
659+
660+
// Extract the JSON from the data attribute.
661+
preg_match( "/data-iawmlf-post-links='([^']*)'/", $rendered, $matches );
662+
$this->assertNotEmpty( $matches, 'Should find the data attribute in rendered output.' );
663+
664+
$links_data = json_decode( html_entity_decode( $matches[1] ), true );
665+
$this->assertIsArray( $links_data );
666+
667+
// Collect all hrefs from the links data.
668+
$hrefs = array_column( $links_data, 'href' );
669+
670+
// The excluded link should NOT be in the data.
671+
$this->assertNotContains( 'https://excluded-domain.com/page', $hrefs, 'Excluded link should not appear in render_block data.' );
672+
673+
// The allowed link SHOULD be in the data.
674+
$this->assertContains( 'https://allowed-domain.com/page', $hrefs, 'Non-excluded link should appear in render_block data.' );
675+
676+
// Clean up.
677+
unset( $GLOBALS['post'] );
678+
\delete_option( Settings::LINK_EXCLUSIONS );
679+
680+
// Reset the Link_Exclusion static cache.
681+
$property->setValue( null, null );
682+
}
683+
684+
/**
685+
* @testdox When a link matches a global exclusion pattern and another does not, only the non-excluded link should appear in the render_block data attribute output.
686+
*
687+
* @return void
688+
*/
689+
public function test_excluded_link_filtered_from_render_block_data_with_mixed_links(): void {
690+
// Set the option to render the HTML link output.
691+
update_option( Settings::FIXER_OPTION, Settings::FIXER_OPTION_REPLACE_LINK );
692+
693+
// Add a global exclusion pattern that matches one of the links.
694+
update_option( Settings::LINK_EXCLUSIONS, array( '*excluded-domain.com*' ) );
695+
696+
// Reset the Link_Exclusion static cache so it picks up the new option.
697+
$reflection = new \ReflectionClass( Link_Exclusion::class );
698+
$property = $reflection->getProperty( 'exclusions' );
699+
$property->setAccessible( true );
700+
$property->setValue( null, null );
701+
702+
$post_id = self::factory()->post->create();
703+
704+
// Add content with two links — one excluded, one allowed.
705+
$content = 'Link one <a href="https://excluded-domain.com/page1">excluded</a> and link two <a href="https://kept-domain.com/page">kept</a>';
706+
wp_update_post(
707+
array(
708+
'ID' => $post_id,
709+
'post_content' => $content,
710+
'post_type' => 'post',
711+
)
712+
);
713+
714+
$GLOBALS['post'] = get_post( $post_id );
715+
716+
// Render the block.
717+
$rendered = do_blocks( $GLOBALS['post']->post_content );
718+
719+
// The data attribute should be present (we still have one non-excluded link).
720+
$this->assertStringContainsString( 'data-iawmlf-post-links', $rendered );
721+
722+
// Extract the JSON from the data attribute.
723+
preg_match( "/data-iawmlf-post-links='([^']*)'/", $rendered, $matches );
724+
$this->assertNotEmpty( $matches, 'Should find the data attribute in rendered output.' );
725+
726+
$links_data = json_decode( html_entity_decode( $matches[1] ), true );
727+
$this->assertIsArray( $links_data );
728+
729+
// Should only have 1 link (the non-excluded one).
730+
$this->assertCount( 1, $links_data, 'Only the non-excluded link should be in the data.' );
731+
732+
// Collect all hrefs from the links data.
733+
$hrefs = array_column( $links_data, 'href' );
734+
735+
// The excluded link should NOT be in the data.
736+
$this->assertNotContains( 'https://excluded-domain.com/page1', $hrefs, 'Excluded link should not appear in render_block data.' );
737+
738+
// The allowed link SHOULD be in the data.
739+
$this->assertContains( 'https://kept-domain.com/page', $hrefs, 'Non-excluded link should appear in render_block data.' );
740+
741+
// Clean up.
742+
unset( $GLOBALS['post'] );
743+
\delete_option( Settings::LINK_EXCLUSIONS );
744+
745+
// Reset the Link_Exclusion static cache.
746+
$property->setValue( null, null );
747+
}
748+
621749
/**
622750
* @testdox When an option is selected to do nothing, it should not render out the HTML link output.
623751
*

0 commit comments

Comments
 (0)