Skip to content

Commit da50f52

Browse files
committed
Auto-link URLs in plain text content
Use WordPress's make_clickable() function to automatically convert URLs in plain text content to clickable links. This only applies to plain text content, not HTML content which is preserved as-is. Fixes #303
1 parent 36b541c commit da50f52

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

includes/rest/class-endpoint-controller.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,8 @@ protected function mp_to_wp( $mf2 ) {
831831
$args['post_content'] = $content['html'] ? $content['html'] :
832832
\htmlspecialchars( $content['value'] );
833833
} elseif ( $content ) {
834-
$args['post_content'] = \htmlspecialchars( $content );
834+
// Auto-link URLs in plain text content.
835+
$args['post_content'] = \make_clickable( \htmlspecialchars( $content ) );
835836
}
836837
}
837838

tests/test_endpoint.php

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,8 +478,9 @@ public function test_update_add_without_content() {
478478
$this->assertEquals( 'bar', $tags[0]->name );
479479
$mf2 = $this->query_source( $post->ID );
480480
$this->assertArrayHasKey( 'published', $mf2['properties'] );
481-
// We have confirmed it exists now compare everything but this.
481+
// We have confirmed it exists now compare everything but these time properties.
482482
unset( $mf2['properties']['published'] );
483+
unset( $mf2['properties']['updated'] );
483484
$this->assertEquals(
484485
array(
485486
'properties' => array(
@@ -815,4 +816,86 @@ function test_create_publish_default_status() {
815816
$post = self::check_create( self::create_json_request( $input ) );
816817
$this->assertEquals( 'publish', $post->post_status );
817818
}
819+
820+
function test_create_plain_text_autolinks_urls() {
821+
$input = array(
822+
'type' => array( 'h-entry' ),
823+
'properties' => array(
824+
'content' => array( 'Check out https://example.com for more info' ),
825+
),
826+
);
827+
$post = self::check_create( self::create_json_request( $input ) );
828+
// URLs in plain text content should be auto-linked.
829+
$this->assertStringContainsString( '<a href="https://example.com"', $post->post_content );
830+
$this->assertStringContainsString( 'https://example.com</a>', $post->post_content );
831+
}
832+
833+
function test_create_plain_text_autolinks_multiple_urls() {
834+
$input = array(
835+
'type' => array( 'h-entry' ),
836+
'properties' => array(
837+
'content' => array( 'Visit https://example.com and http://test.org today' ),
838+
),
839+
);
840+
$post = self::check_create( self::create_json_request( $input ) );
841+
// Both URLs should be auto-linked.
842+
$this->assertStringContainsString( '<a href="https://example.com"', $post->post_content );
843+
$this->assertStringContainsString( '<a href="http://test.org"', $post->post_content );
844+
}
845+
846+
function test_create_html_content_not_autolinked() {
847+
$input = array(
848+
'type' => array( 'h-entry' ),
849+
'properties' => array(
850+
'content' => array(
851+
array( 'html' => '<p>Visit https://example.com today</p>' ),
852+
),
853+
),
854+
);
855+
$post = self::check_create( self::create_json_request( $input ) );
856+
// HTML content should not be auto-linked (preserved as-is).
857+
$this->assertEquals( '<p>Visit https://example.com today</p>', $post->post_content );
858+
}
859+
860+
function test_create_html_content_existing_link_preserved() {
861+
$input = array(
862+
'type' => array( 'h-entry' ),
863+
'properties' => array(
864+
'content' => array(
865+
array( 'html' => '<p>Visit <a href="https://example.com">my site</a> today</p>' ),
866+
),
867+
),
868+
);
869+
$post = self::check_create( self::create_json_request( $input ) );
870+
// Existing links in HTML content should be preserved.
871+
$this->assertEquals( '<p>Visit <a href="https://example.com">my site</a> today</p>', $post->post_content );
872+
}
873+
874+
function test_create_html_content_code_block_preserved() {
875+
$input = array(
876+
'type' => array( 'h-entry' ),
877+
'properties' => array(
878+
'content' => array(
879+
array( 'html' => '<p>Example: <code>https://example.com/api</code></p>' ),
880+
),
881+
),
882+
);
883+
$post = self::check_create( self::create_json_request( $input ) );
884+
// URLs in code blocks should not be auto-linked.
885+
$this->assertEquals( '<p>Example: <code>https://example.com/api</code></p>', $post->post_content );
886+
}
887+
888+
function test_create_html_content_pre_block_preserved() {
889+
$input = array(
890+
'type' => array( 'h-entry' ),
891+
'properties' => array(
892+
'content' => array(
893+
array( 'html' => '<pre>curl https://example.com/api</pre>' ),
894+
),
895+
),
896+
);
897+
$post = self::check_create( self::create_json_request( $input ) );
898+
// URLs in pre blocks should not be auto-linked.
899+
$this->assertEquals( '<pre>curl https://example.com/api</pre>', $post->post_content );
900+
}
818901
}

0 commit comments

Comments
 (0)