diff --git a/includes/class-post-collection-integration.php b/includes/class-post-collection-integration.php index 444ad22..3392e16 100644 --- a/includes/class-post-collection-integration.php +++ b/includes/class-post-collection-integration.php @@ -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'; @@ -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' ), + ); + ?> +
+ + + + + +
+ args = $args; @@ -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 ) . '/'; + } }; } @@ -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 ); + } }