From 1ef0366281f8acc5358d27f10872cd5f5d89d240 Mon Sep 17 00:00:00 2001 From: Tetrakern <26898880+Tetrakern@users.noreply.github.com> Date: Tue, 18 Jun 2024 12:00:24 +0200 Subject: [PATCH] Only load ePUB script if required --- functions.php | 25 ++++++++++++++++++++++++- includes/functions/_module-epub.php | 15 --------------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/functions.php b/functions.php index 3004001d..2c408c31 100644 --- a/functions.php +++ b/functions.php @@ -59,6 +59,17 @@ function fictioneer_add_oauth2_endpoint() { } add_action( 'init', 'fictioneer_add_oauth2_endpoint', 10 ); +/** + * Adds route to ePUB script + * + * @since 4.0.0 + */ + +function fictioneer_add_epub_download_endpoint() { + add_rewrite_endpoint( FICTIONEER_EPUB_ENDPOINT, EP_ROOT ); +} +add_action( 'init', 'fictioneer_add_epub_download_endpoint', 10 ); + /* * Strings */ @@ -470,7 +481,19 @@ require_once __DIR__ . '/includes/functions/_module-seo.php'; */ if ( get_option( 'fictioneer_enable_epubs' ) ) { - require_once __DIR__ . '/includes/functions/_module-epub.php'; + if ( wp_doing_ajax() && ( $_GET['action'] ?? 0 ) === 'fictioneer_ajax_download_epub' ) { + require_once __DIR__ . '/includes/functions/_module-epub.php'; + } else { + add_action( + 'template_redirect', + function () { + if ( ! is_null( get_query_var( FICTIONEER_EPUB_ENDPOINT, null ) ) ) { + require_once __DIR__ . '/includes/functions/_module-epub.php'; + } + }, + 1 + ); + } } /** diff --git a/includes/functions/_module-epub.php b/includes/functions/_module-epub.php index 8f284fe0..fc5d5491 100644 --- a/includes/functions/_module-epub.php +++ b/includes/functions/_module-epub.php @@ -1,20 +1,5 @@