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
8 changes: 5 additions & 3 deletions includes/class-e-reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ protected function generate_file( array $posts, $title = null, $author = null )
$this->ebook_title = $this->strip_emojis( $this->ebook_title );
$this->ebook_author = $this->strip_emojis( $this->ebook_author );

$url = home_url( '?' . implode( '-', array_map( 'intval', array_column( $posts, 'ID' ) ) ) );
$url = home_url( '?' . implode( '-', array_map( 'intval', array_column( $posts, 'ID' ) ) ) );
$generation_id = str_replace( '.', '-', uniqid( 'generated-', true ) );

foreach ( $posts as $post ) {
$post_title = $post->post_title;
Expand All @@ -253,8 +254,9 @@ protected function generate_file( array $posts, $title = null, $author = null )
$this->ebook_author,
$chapters,
array(
'identifier' => $url,
'source_url' => $url,
'filename_suffix' => $generation_id,
'identifier' => $url . '#' . $generation_id,
'source_url' => $url,
)
);
}
Expand Down
14 changes: 11 additions & 3 deletions includes/class-epub-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static function build_file( $title, $author, array $chapters, array $args
}

$args['base_dir'] = $dir;
$filename = self::build_book_filename( $title, $author );
$filename = self::build_book_filename( $title, $author, isset( $args['filename_suffix'] ) ? $args['filename_suffix'] : '' );
$book = self::build_book( $title, $author, $chapters, $args );

if ( false === $book->saveBook( $filename . '.epub', $dir ) ) {
Expand Down Expand Up @@ -205,12 +205,20 @@ private static function build_book( $title, $author, array $chapters, array $arg
*
* @param string $title The book title.
* @param string $author The book author.
* @param string $suffix Optional filename suffix.
* @return string
*/
private static function build_book_filename( $title, $author ) {
private static function build_book_filename( $title, $author, $suffix = '' ) {
$filename = sanitize_title( substr( (string) $author, 0, 40 ) . ' - ' . substr( (string) $title, 0, 100 ) );
$suffix = sanitize_title( (string) $suffix );
if ( ! $filename ) {
$filename = 'ebook';
}
if ( $suffix ) {
$filename .= '-' . $suffix;
}

return $filename ? $filename : 'ebook';
return $filename;
}

/**
Expand Down
13 changes: 9 additions & 4 deletions includes/class-post-collection-integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Post_Collection_Integration {
*
* @var array
*/
private static $selections = array( 'unread', 'new', 'all', 'last', 'list' );
private static $selections = array( 'unread', 'new', 'all', 'last', 'list', 'compact' );

/**
* Constructor.
Expand Down Expand Up @@ -274,6 +274,7 @@ public function download_urls( $app, $collection ) {
'unread' => __( 'Unread articles', 'send-to-e-reader' ),
'unread-10' => __( '10 most recent unread articles', 'send-to-e-reader' ),
'new' => __( 'Articles not yet sent', 'send-to-e-reader' ),
'compact' => __( 'Compact picker on the e-reader', 'send-to-e-reader' ),
);
?>
<details class="pc-e-reader-download-urls">
Expand Down Expand Up @@ -306,14 +307,15 @@ public function maybe_download( $app, $collection = null ) {
return;
}

Send_To_E_Reader::prevent_response_caching();
list( $selection, $limit ) = $request;

// The download password stands in for being logged in here, the same way
// it does on the Friends frontend: whoever knows it gets the private
// posts too, because that is the point of pulling the unread articles
// onto a device that cannot log in.
if ( 'list' === $selection ) {
$this->render_list( $app, $collection );
if ( in_array( $selection, array( 'list', 'compact' ), true ) ) {
$this->render_list( $app, $collection, 'compact' === $selection );
exit;
}

Expand Down Expand Up @@ -343,8 +345,9 @@ public function maybe_download( $app, $collection = null ) {
*
* @param \PostCollection\Post_Collection_App $app The app instance.
* @param \WP_Term|null $collection The collection in context.
* @param bool $compact Whether to render the compact list view.
*/
private function render_list( $app, $collection ) {
private function render_list( $app, $collection, $compact = false ) {
$posts = array();
$unsent = array();
foreach ( $this->get_posts( $app, 'last', $collection, true, 50 ) as $post ) {
Expand All @@ -370,6 +373,7 @@ private function render_list( $app, $collection ) {
'unsent' => $unsent,
'posts' => $posts,
'inputname' => $this->send_to_e_reader->get_download_url_var(),
'compact' => $compact,
)
);
}
Expand Down Expand Up @@ -610,6 +614,7 @@ public function settings_download_urls( $download_password ) {
'all' => __( 'All collected articles:', 'send-to-e-reader' ),
'last' => __( 'The last 10 collected articles:', 'send-to-e-reader' ),
'list' => __( 'A list to pick from:', 'send-to-e-reader' ),
'compact' => __( 'A compact list to pick from:', 'send-to-e-reader' ),
);

$base = wp_parse_url( $app->get_home_url(), PHP_URL_PATH );
Expand Down
23 changes: 18 additions & 5 deletions includes/class-send-to-e-reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -683,10 +683,7 @@ public function get_unsent_posts( $query_vars = array() ) {
$query_vars = $wp_query->query_vars;
}

// Prevent super cache from caching this page.
if ( ! defined( 'DONOTCACHEPAGE' ) ) {
define( 'DONOTCACHEPAGE', true );
}
self::prevent_response_caching();

$query = new \WP_Query(
array_merge(
Expand All @@ -701,6 +698,19 @@ public function get_unsent_posts( $query_vars = array() ) {
return $query->get_posts();
}

/**
* Prevent page-cache and browser-cache layers from storing dynamic e-reader responses.
*/
public static function prevent_response_caching() {
if ( ! defined( 'DONOTCACHEPAGE' ) ) {
define( 'DONOTCACHEPAGE', true );
}

if ( function_exists( 'nocache_headers' ) && ! headers_sent() ) {
nocache_headers();
}
}

/**
* Get the meta query that selects posts by their e-reader sent status.
*
Expand Down Expand Up @@ -1115,13 +1125,15 @@ public function enable_download_via_url( $viewable ) {
'all',
'last',
'list',
'compact',
),
true
)
) {
return $viewable;
}

self::prevent_response_caching();
$this->download_request = $request_value;
return true;
}
Expand All @@ -1146,7 +1158,7 @@ public function download_via_url( $template ) {
return $template;
}

if ( 'list' === $this->download_request ) {
if ( in_array( $this->download_request, array( 'list', 'compact' ), true ) ) {
$unsent = array();
foreach ( $this->get_unsent_posts() as $post ) {
if ( in_array( get_post_format( $post ), array( 'video' ), true ) ) {
Expand Down Expand Up @@ -1185,6 +1197,7 @@ public function download_via_url( $template ) {
'unsent' => $unsent,
'posts' => $posts,
'inputname' => 'epub' . get_option( self::DOWNLOAD_PASSWORD_OPTION, hash( 'crc32', wp_salt( 'nonce' ), false ) ),
'compact' => 'compact' === $this->download_request,
)
);
exit;
Expand Down
25 changes: 25 additions & 0 deletions plain-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@
document.querySelectorAll('input[type="checkbox"]').forEach(function (checkbox) {
checkbox.checked = checked;
});
updateSelectionCount();
}

function updateSelectionCount() {
var count = document.querySelector('[data-send-to-e-reader-selection-count]');
var checkboxes = document.querySelectorAll('input[type="checkbox"]');
var selected = document.querySelectorAll('input[type="checkbox"]:checked').length;

if (!count) {
return;
}

count.textContent = count.getAttribute('data-selected-template')
.replace('%1$d', checkboxes.length)
.replace('%2$d', selected);
}

function reverseList() {
Expand Down Expand Up @@ -63,4 +78,14 @@
break;
}
});

document.addEventListener('change', function (event) {
if (!event.target.matches('input[type="checkbox"]')) {
return;
}

updateSelectionCount();
});

updateSelectionCount();
}());
9 changes: 5 additions & 4 deletions templates/admin/ereader-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
<?php
foreach (
array(
'all' => __( 'All posts from this friend:', 'send-to-e-reader' ),
'last' => __( 'The last 10 posts from this friend:', 'send-to-e-reader' ),
'new' => __( 'Posts not yet sent from this friend:', 'send-to-e-reader' ),
'list' => __( 'List last for manual selection from this friend:', 'send-to-e-reader' ),
'all' => __( 'All posts from this friend:', 'send-to-e-reader' ),
'last' => __( 'The last 10 posts from this friend:', 'send-to-e-reader' ),
'new' => __( 'Posts not yet sent from this friend:', 'send-to-e-reader' ),
'list' => __( 'List last for manual selection from this friend:', 'send-to-e-reader' ),
'compact' => __( 'Compact list for manual selection from this friend:', 'send-to-e-reader' ),
) as $key => $description
) :
?>
Expand Down
Loading