From ca109ba3a8a08e661fd2484454b1983c3418f15d Mon Sep 17 00:00:00 2001
From: Yassine Doghri <yassine@doghri.fr>
Date: Thu, 29 Sep 2022 13:34:28 +0000
Subject: [PATCH] fix(premium-podcasts): display unlock button in embed when
 premium episode

---
 app/Controllers/EpisodeController.php           | 17 +++++++++++++----
 .../Filters/PodcastUnlockFilter.php             |  5 +++++
 themes/cp_app/embed.php                         |  4 ++++
 themes/cp_app/episode/_partials/card.php        |  2 +-
 .../cp_app/episode/_partials/preview_card.php   |  2 +-
 5 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/app/Controllers/EpisodeController.php b/app/Controllers/EpisodeController.php
index 5465474e12..3d30b4e54c 100644
--- a/app/Controllers/EpisodeController.php
+++ b/app/Controllers/EpisodeController.php
@@ -177,10 +177,19 @@ class EpisodeController extends BaseController
             $session->set('embed_domain', parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST));
         }
 
-        $locale = service('request')
-            ->getLocale();
-
-        $cacheName = "page_podcast#{$this->podcast->id}_episode#{$this->episode->id}_embed_{$theme}_{$locale}";
+        $cacheName = implode(
+            '_',
+            array_filter([
+                'page',
+                "podcast#{$this->podcast->id}",
+                "episode#{$this->episode->id}",
+                'embed',
+                $theme,
+                service('request')
+                    ->getLocale(),
+                is_unlocked($this->podcast->handle) ? 'unlocked' : null,
+            ]),
+        );
 
         if (! ($cachedView = cache($cacheName))) {
             $themeData = EpisodeModel::$themes[$theme];
diff --git a/modules/PremiumPodcasts/Filters/PodcastUnlockFilter.php b/modules/PremiumPodcasts/Filters/PodcastUnlockFilter.php
index a46348e5fa..74468fa2e5 100644
--- a/modules/PremiumPodcasts/Filters/PodcastUnlockFilter.php
+++ b/modules/PremiumPodcasts/Filters/PodcastUnlockFilter.php
@@ -67,6 +67,11 @@ class PodcastUnlockFilter implements FilterInterface
             return;
         }
 
+        // Episode should be embeddable even if it is premium
+        if ($current === route_to('embed', $episode->podcast->handle, $episode->slug)) {
+            return;
+        }
+
         // if podcast is locked then send to the unlock form
         /** @var PremiumPodcasts $premiumPodcasts */
         $premiumPodcasts = service('premium_podcasts');
diff --git a/themes/cp_app/embed.php b/themes/cp_app/embed.php
index 478e0b2c91..26621ea048 100644
--- a/themes/cp_app/embed.php
+++ b/themes/cp_app/embed.php
@@ -33,6 +33,9 @@
         <a href="<?= $episode->link ?>" class="flex flex-col items-start text-sm" style="color: <?= $themeData['text'] ?>;" target="_blank" rel="noopener noreferrer">
             <h1 class="font-semibold leading-tight opacity-100 line-clamp-2 hover:opacity-75"><?= esc($episode->title) ?></h1>
         </a>
+        <?php if ($episode->is_premium && ! is_unlocked($podcast->handle)): ?>
+            <Button variant="primary" class="mt-auto mb-2" iconLeft="lock" uri="<?= $episode->link ?>" target="_blank" rel="noopener noreferrer"><?= lang('PremiumPodcasts.unlock') ?></Button>
+        <?php else: ?>
         <vm-player
                 id="castopod-vm-player"
                 theme="<?= str_starts_with($theme, 'dark') ? 'dark' : 'light' ?>"
@@ -60,6 +63,7 @@
                 </vm-controls>
             </vm-ui>
         </vm-player>
+        <?php endif; ?>
     </div>
 </body>
 
diff --git a/themes/cp_app/episode/_partials/card.php b/themes/cp_app/episode/_partials/card.php
index 165b00c0f9..d2e4e8781c 100644
--- a/themes/cp_app/episode/_partials/card.php
+++ b/themes/cp_app/episode/_partials/card.php
@@ -17,7 +17,7 @@
             </div>
             <h2 class="flex-1 mt-1 font-semibold leading-tight line-clamp-2"><a class="hover:underline" href="<?= $episode->link ?>"><?= esc($episode->title) ?></a></h2>
         </div>
-        <?php if ($episode->is_premium && ! subscription($podcast->handle)): ?>
+        <?php if ($episode->is_premium && ! is_unlocked($podcast->handle)): ?>
             <a href="<?= route_to('episode', $episode->podcast->handle, $episode->slug) ?>" class="p-3 rounded-full bg-brand bg-accent-base text-accent-contrast hover:bg-accent-hover focus:ring-accent" title="<?= lang('PremiumPodcasts.unlock_episode') ?>" data-tooltip="bottom">
                 <Icon glyph="lock" class="text-xl" />
             </a>
diff --git a/themes/cp_app/episode/_partials/preview_card.php b/themes/cp_app/episode/_partials/preview_card.php
index 0d425b5663..5072a99dc6 100644
--- a/themes/cp_app/episode/_partials/preview_card.php
+++ b/themes/cp_app/episode/_partials/preview_card.php
@@ -17,7 +17,7 @@
         </div>
         <a href="<?= $episode->link ?>" class="flex items-baseline font-semibold line-clamp-2" title="<?= esc($episode->title) ?>"><?= esc($episode->title) ?></a>
     </div>
-    <?php if ($episode->is_premium && ! subscription($episode->podcast->handle)): ?>
+    <?php if ($episode->is_premium && ! is_unlocked($episode->podcast->handle)): ?>
         <a href="<?= route_to('episode', $episode->podcast->handle, $episode->slug) ?>" class="p-3 mr-4 rounded-full bg-brand bg-accent-base text-accent-contrast hover:bg-accent-hover focus:ring-accent" title="<?= lang('PremiumPodcasts.unlock_episode') ?>" data-tooltip="bottom">
             <Icon glyph="lock" class="text-xl" />
         </a>
-- 
GitLab