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
52 changes: 52 additions & 0 deletions includes/class-post-collection-integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ public function app_loaded( $app ) {

add_action( 'post_collection_app_item_actions', array( $this, 'item_actions' ), 10, 2 );
add_action( 'post_collection_app_selection_actions', array( $this, 'selection_actions' ), 10, 2 );
add_action( 'post_collection_app_download_urls', array( $this, 'download_urls' ), 10, 2 );

if ( function_exists( 'wp_app_enqueue_style' ) ) {
$file = 'post-collection-e-reader.css';
$path = SEND_TO_E_READER_PLUGIN_DIR . $file;
wp_app_enqueue_style(
'send-to-e-reader-post-collection',
plugins_url( $file, SEND_TO_E_READER_PLUGIN_DIR . 'send-to-e-reader.php' ),
array(),
file_exists( $path ) ? filemtime( $path ) : SEND_TO_E_READER_VERSION,
\PostCollection\Post_Collection_App::PATH
);
}

if ( function_exists( 'wp_app_enqueue_script' ) ) {
$file = 'post-collection-e-reader.js';
Expand Down Expand Up @@ -242,6 +255,45 @@ private function get_action_label( $ereader, $context ) {
);
}

/**
* Render the e-reader download URLs on a Post Collection collection page.
*
* @param \PostCollection\Post_Collection_App $app The app instance.
* @param \WP_Term $collection The collection in context.
*/
public function download_urls( $app, $collection ) {
if ( ! $app->can_manage_collections() || ! $collection instanceof \WP_Term ) {
return;
}

$url_var = $this->send_to_e_reader->get_download_url_var();
$settings_url = admin_url( 'admin.php?page=send-to-e-reader-settings' );
$base_url = $app->get_collection_url( $collection );
$urls = array(
'list' => __( 'Pick articles on the e-reader', 'send-to-e-reader' ),
'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' ),
);
?>
<details class="pc-e-reader-download-urls">
<summary>
<strong><?php esc_html_e( 'E-reader URLs', 'send-to-e-reader' ); ?></strong>
<a class="pc-e-reader-settings-link" href="<?php echo esc_url( $settings_url ); ?>"><?php esc_html_e( 'URL settings', 'send-to-e-reader' ); ?></a>
</summary>
<ul>
<?php foreach ( $urls as $selection => $label ) : ?>
<li>
<?php $url = add_query_arg( $url_var, $selection, $base_url ); ?>
<span><?php echo esc_html( $label ); ?></span>
<a href="<?php echo esc_url( $url ); ?>"><?php echo esc_html( $url ); ?></a>
</li>
<?php endforeach; ?>
</ul>
</details>
<?php
}

/**
* Answer an app request that asks for an ePub.
*
Expand Down
50 changes: 50 additions & 0 deletions post-collection-e-reader.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
.pc-e-reader-download-urls {
margin: 16px 0;
padding: 10px 12px;
border: 1px solid var(--pc-color-border);
border-radius: 8px;
background: var(--pc-color-surface);
}

.pc-e-reader-download-urls summary {
display: flex;
flex-wrap: wrap;
align-items: baseline;
justify-content: space-between;
gap: 8px 16px;
cursor: pointer;
}

.pc-e-reader-download-urls .pc-e-reader-settings-link {
font-size: 0.9em;
font-weight: 400;
color: var(--pc-color-muted);
}

.pc-e-reader-download-urls .pc-e-reader-settings-link:hover,
.pc-e-reader-download-urls .pc-e-reader-settings-link:focus {
color: var(--pc-color-primary);
}

.pc-e-reader-download-urls ul {
display: grid;
gap: 6px;
margin: 10px 0 0;
padding: 0;
list-style: none;
}

.pc-e-reader-download-urls li {
display: grid;
grid-template-columns: minmax(130px, max-content) minmax(0, 1fr);
gap: 8px 14px;
align-items: baseline;
}

.pc-e-reader-download-urls li span {
color: var(--pc-color-muted);
}

.pc-e-reader-download-urls li a {
overflow-wrap: anywhere;
}
56 changes: 56 additions & 0 deletions tests/Test_Post_Collection_Integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
use Send_To_E_Reader\Post_Collection_Integration;
use Send_To_E_Reader\Send_To_E_Reader;

if ( ! class_exists( 'WP_Term' ) ) {
class WP_Term {
public $term_id = 0;
public $name = '';
public $slug = '';
}
}

/**
* Test class for Post_Collection_Integration.
*/
Expand Down Expand Up @@ -85,6 +93,7 @@ private function get_integration() {
private function get_app_spy() {
return new class() {
public $args = array();
public $can_manage = true;

public function get_unread_posts( $collection = null, array $args = array() ) {
$this->args = $args;
Expand All @@ -97,6 +106,14 @@ public function query_app_posts( $collection = null, array $args = array() ) {

return array();
}

public function can_manage_collections() {
return $this->can_manage;
}

public function get_collection_url( $collection, $post_id = null ) {
return 'https://example.com/post-collection/' . rawurlencode( $collection->slug ) . '/';
}
};
}

Expand Down Expand Up @@ -229,4 +246,43 @@ public function test_the_download_url_only_accepts_known_selections() {
$_GET['epubsecret'] = array( '4', '0', 'seven', '9' );
$this->assertSame( array( array( 4, 9 ), null ), $this->call( $integration, 'get_download_request' ) );
}

/**
* Test that the integration exposes collection-specific download URLs.
*/
public function test_download_urls_are_exposed_on_the_collection_frontend() {
update_option( Send_To_E_Reader::DOWNLOAD_PASSWORD_OPTION, 'secret' );
$integration = $this->get_integration();
$app = $this->get_app_spy();
$collection = new WP_Term();
$collection->term_id = 12;
$collection->name = 'Collected Posts';
$collection->slug = 'collected-posts';

ob_start();
$integration->download_urls( $app, $collection );
$output = ob_get_clean();

$this->assertStringContainsString( 'E-reader URLs', $output );
$this->assertStringContainsString( 'URL settings', $output );
$this->assertStringContainsString( 'https://example.com/post-collection/collected-posts/?epubsecret=list', $output );
$this->assertStringContainsString( 'https://example.com/post-collection/collected-posts/?epubsecret=unread', $output );
$this->assertStringContainsString( 'https://example.com/wp-admin/admin.php?page=send-to-e-reader-settings', $output );
}

/**
* Test that visitors never see the password-bearing download URLs.
*/
public function test_download_urls_are_only_exposed_to_collection_managers() {
$integration = $this->get_integration();
$app = $this->get_app_spy();
$app->can_manage = false;
$collection = new WP_Term();

ob_start();
$integration->download_urls( $app, $collection );
$output = ob_get_clean();

$this->assertSame( '', $output );
}
}