Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions includes/class-e-reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ protected function get_content( $format, \WP_Post $post ) {
$format . '/footer',
null,
array(
'url' => get_permalink( $post ),
'url' => $this->get_post_source_url( $post ),
)
);
$content = ob_get_contents();
Expand All @@ -161,6 +161,16 @@ protected function get_content( $format, \WP_Post $post ) {
return $content;
}

/**
* Resolve the source URL for the chapter footer.
*
* @param \WP_Post $post The post.
* @return string
*/
protected function get_post_source_url( \WP_Post $post ) {
return apply_filters( 'send_to_e_reader_post_url', get_permalink( $post ), $post );
}

/**
* Get the date to show for a post in generated e-reader content.
*
Expand Down Expand Up @@ -192,7 +202,7 @@ protected function update_author_name( \WP_Post $post ) {
if ( $override_author_name ) {
$author_name = $override_author_name;
}
$post->author_name = $author_name;
$post->author_name = apply_filters( 'send_to_e_reader_post_author_name', $author_name, $post );
}
return $post->author_name;
}
Expand Down
3 changes: 2 additions & 1 deletion templates/plain-list.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,14 +313,15 @@
</header>
<ul class="post-list">
<?php foreach ( $args['posts'] as $post ) : ?>
<?php $post_url = apply_filters( 'send_to_e_reader_post_url', get_permalink( $post ), $post ); ?>
<li class="post-item">
<input type="checkbox" name="<?php echo esc_attr( $args['inputname'] ); ?>[]" value="<?php echo esc_attr( $post->ID ); ?>" <?php checked( isset( $args['unsent'][ $post->ID ] ) ); ?> aria-label="<?php echo esc_attr( get_the_title( $post ) ); ?>" />
<div>
<p class="post-title"><?php echo esc_html( get_the_title( $post ) ); ?></p>
<p class="post-meta"><?php echo esc_html( $get_post_display_date( $post ) ); ?></p>
<p class="post-summary"><?php echo esc_html( wp_trim_words( wp_strip_all_tags( $post->post_content ) ) ); ?></p>
<div class="item-actions">
<a class="post-url" href="<?php echo esc_url( get_the_permalink( $post ) ); ?>"><?php echo esc_html( get_the_permalink( $post ) ); ?></a>
<a class="post-url" href="<?php echo esc_url( $post_url ); ?>"><?php echo esc_html( $post_url ); ?></a>
<a href="#" data-send-to-e-reader-action="move-up"><?php esc_html_e( 'Move up', 'send-to-e-reader' ); ?></a>
<a href="#" data-send-to-e-reader-action="move-down"><?php esc_html_e( 'Move down', 'send-to-e-reader' ); ?></a>
</div>
Expand Down
34 changes: 34 additions & 0 deletions tests/Test_E_Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class Test_E_Reader extends TestCase {

public function tearDown(): void {
remove_all_filters( 'friends_override_author_name' );
remove_all_filters( 'send_to_e_reader_post_url' );
remove_all_filters( 'send_to_e_reader_post_author_name' );
unset( $GLOBALS['_test_upload_basedir'], $GLOBALS['_test_upload_baseurl'] );
parent::tearDown();
}
Expand Down Expand Up @@ -186,6 +188,38 @@ function () {
$this->assertSame( 'Zetaphor', $post->author_name );
}

public function test_chapter_url_and_author_can_be_filtered() {
$post = new \WP_Post();
$post->ID = 125;
$post->post_author = 1;
$ereader = new E_Reader_Download( 'Test' );
$url = new \ReflectionMethod( $ereader, 'get_post_source_url' );
$url->setAccessible( true );
$this->assertSame( get_permalink( $post ), $url->invoke( $ereader, $post ) );
add_filter(
'send_to_e_reader_post_url',
function ( $url, $item ) use ( $post ) {
$this->assertSame( $post, $item );
return 'https://original.example/article/';
},
10,
2
);
add_filter(
'send_to_e_reader_post_author_name',
function ( $author, $item ) use ( $post ) {
$this->assertSame( $post, $item );
return 'Alexander Aidun';
},
10,
2
);
$author = new \ReflectionMethod( $ereader, 'update_author_name' );
$author->setAccessible( true );
$this->assertSame( 'Alexander Aidun', $author->invoke( $ereader, $post ) );
$this->assertSame( 'https://original.example/article/', $url->invoke( $ereader, $post ) );
}

/**
* Test collected article dates replace the saved post date in chapter bylines.
*/
Expand Down
13 changes: 13 additions & 0 deletions tests/Test_Send_To_E_Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Test_Send_To_E_Reader extends TestCase {
public function tearDown(): void {
remove_all_filters( 'friends_override_author_name' );
remove_all_filters( 'send_to_e_reader_post_content' );
remove_all_filters( 'send_to_e_reader_post_url' );
remove_all_filters( 'static_archive_post_types' );
remove_all_filters( 'static_archive_post_html' );
remove_all_filters( 'send_to_e_reader_user_can_send' );
Expand Down Expand Up @@ -259,11 +260,23 @@ public function test_plain_list_shows_original_article_date() {
'inputname' => 'epubsecret',
);

add_filter(
'send_to_e_reader_post_url',
function ( $url, $item ) use ( $post ) {
$this->assertSame( $post, $item );
return 'https://original.example/article/?a=1&b=2';
},
10,
2
);

ob_start();
include FRIENDS_SEND_TO_E_READER_PLUGIN_DIR . 'templates/plain-list.php';
$output = ob_get_clean();

$this->assertStringContainsString( 'Collected Link', $output );
$this->assertStringContainsString( '<a class="post-url" href="https://original.example/article/?a=1&amp;b=2">https://original.example/article/?a=1&amp;b=2</a>', $output );
$this->assertStringNotContainsString( get_permalink( $post ), $output );
$this->assertStringContainsString( 'June 29, 2026', $output );
$this->assertStringContainsString( '1 articles (1 selected)', $output );
$this->assertStringContainsString( '<form method="post">', $output );
Expand Down
Loading