From b5cee3d93536433997caa10c5febd7bdbd2e675f Mon Sep 17 00:00:00 2001 From: Dmitri Don Date: Tue, 31 Mar 2026 01:33:38 +0300 Subject: [PATCH] Revert "Revert "Cookie consent fix and sitemap improvements"" --- lib/modules/legal/legal.ex | 40 +++++++-- lib/modules/sitemap/sources/publishing.ex | 16 +++- lib/modules/sitemap/sources/static.ex | 23 ++++- .../components/core/cookie_consent.ex | 89 ++++++------------- .../components/layout_wrapper.ex | 2 + 5 files changed, 100 insertions(+), 70 deletions(-) diff --git a/lib/modules/legal/legal.ex b/lib/modules/legal/legal.ex index 20121c2a1..e2169761d 100644 --- a/lib/modules/legal/legal.ex +++ b/lib/modules/legal/legal.ex @@ -38,6 +38,7 @@ defmodule PhoenixKit.Modules.Legal do alias PhoenixKit.Modules.Legal.PageType alias PhoenixKit.Modules.Legal.TemplateGenerator alias PhoenixKit.Settings + alias PhoenixKit.Utils.Routes @enabled_key "legal_enabled" @module_name "legal" @@ -551,12 +552,26 @@ defmodule PhoenixKit.Modules.Legal do - google_consent_mode: boolean - hide_for_authenticated: boolean - frameworks: list of framework IDs - - cookie_policy_url: string - - privacy_policy_url: string + - cookie_policy_url: string (backward compat, derived from published pages) + - privacy_policy_url: string (backward compat, derived from published pages) + - legal_links: list of %{title: string, url: string} for all published legal pages + - legal_index_url: string """ @spec get_consent_widget_config() :: map() def get_consent_widget_config do - prefix = PhoenixKit.Config.get_url_prefix() + legal_links = get_published_legal_links() + + cookie_policy_url = + case Enum.find(legal_links, &String.ends_with?(&1.url, "/cookie-policy")) do + %{url: url} -> url + nil -> Routes.path("/legal/cookie-policy") + end + + privacy_policy_url = + case Enum.find(legal_links, &String.ends_with?(&1.url, "/privacy-policy")) do + %{url: url} -> url + nil -> Routes.path("/legal/privacy-policy") + end %{ enabled: consent_widget_enabled?(), @@ -567,11 +582,26 @@ defmodule PhoenixKit.Modules.Legal do policy_version: get_auto_policy_version(), google_consent_mode: google_consent_mode_enabled?(), frameworks: get_selected_frameworks(), - cookie_policy_url: "#{prefix}/legal/cookie-policy", - privacy_policy_url: "#{prefix}/legal/privacy-policy" + cookie_policy_url: cookie_policy_url, + privacy_policy_url: privacy_policy_url, + legal_links: legal_links, + legal_index_url: Routes.path("/legal") } end + @doc """ + Returns a list of all published legal pages as link maps. + + Each map has `:title` and `:url` keys. Used by the cookie consent widget + to render dynamic links to all published legal pages. + """ + @spec get_published_legal_links() :: list(%{title: String.t(), url: String.t()}) + def get_published_legal_links do + list_generated_pages() + |> Enum.filter(&(&1.status == "published")) + |> Enum.map(&%{title: &1.title, url: Routes.path("/legal/#{&1.slug}")}) + end + @doc """ Check if there are unpublished legal pages that are required. diff --git a/lib/modules/sitemap/sources/publishing.ex b/lib/modules/sitemap/sources/publishing.ex index afa5f6b20..7f6a8fd32 100644 --- a/lib/modules/sitemap/sources/publishing.ex +++ b/lib/modules/sitemap/sources/publishing.ex @@ -158,7 +158,7 @@ defmodule PhoenixKit.Modules.Sitemap.Sources.Publishing do UrlEntry.new(%{ loc: url, - lastmod: nil, + lastmod: latest_post_date(slug, language), changefreq: "daily", priority: 0.7, title: name, @@ -384,6 +384,20 @@ defmodule PhoenixKit.Modules.Sitemap.Sources.Publishing do end end + # Latest lastmod among published posts in a group (for group listing pages) + defp latest_post_date(group_slug, language) do + post_language = language || get_default_language() + + Publishing.list_posts(group_slug, post_language) + |> Enum.filter(&published?/1) + |> Enum.reject(&excluded?/1) + |> Enum.map(&get_post_lastmod/1) + |> Enum.reject(&is_nil/1) + |> Enum.max(Date, fn -> nil end) + rescue + _ -> nil + end + defp get_post_lastmod(post) do case post do # Check metadata fields first (PhoenixKit Publishing uses published_at) diff --git a/lib/modules/sitemap/sources/static.ex b/lib/modules/sitemap/sources/static.ex index 636032418..45408c3fa 100644 --- a/lib/modules/sitemap/sources/static.ex +++ b/lib/modules/sitemap/sources/static.ex @@ -199,7 +199,7 @@ defmodule PhoenixKit.Modules.Sitemap.Sources.Static do UrlEntry.new(%{ loc: url, - lastmod: Date.utc_today(), + lastmod: static_lastmod(path), changefreq: Map.get(config, "changefreq", "weekly"), priority: Map.get(config, "priority", 0.5), title: Map.get(config, "title", path), @@ -224,7 +224,7 @@ defmodule PhoenixKit.Modules.Sitemap.Sources.Static do UrlEntry.new(%{ loc: url, - lastmod: Date.utc_today(), + lastmod: static_lastmod(path), changefreq: Map.get(config, "changefreq", "weekly"), priority: Map.get(config, "priority", 0.5), title: Map.get(config, "title", path), @@ -237,6 +237,25 @@ defmodule PhoenixKit.Modules.Sitemap.Sources.Static do end end + # For homepage, use the latest published content date across all publishing groups. + # For other static pages, use today's date as a reasonable approximation. + defp static_lastmod("/") do + alias PhoenixKit.Modules.Sitemap.Sources.Publishing + + if Code.ensure_loaded?(Publishing) and function_exported?(Publishing, :collect, 1) do + Publishing.collect([]) + |> Enum.map(& &1.lastmod) + |> Enum.reject(&is_nil/1) + |> Enum.max(Date, fn -> Date.utc_today() end) + else + Date.utc_today() + end + rescue + _ -> Date.utc_today() + end + + defp static_lastmod(_path), do: Date.utc_today() + # Resolve path from config: explicit path OR via RouteResolver defp resolve_path(%{"path" => path}) when is_binary(path) and path != "" do path diff --git a/lib/phoenix_kit_web/components/core/cookie_consent.ex b/lib/phoenix_kit_web/components/core/cookie_consent.ex index ed04a93b4..1e9c5e2eb 100644 --- a/lib/phoenix_kit_web/components/core/cookie_consent.ex +++ b/lib/phoenix_kit_web/components/core/cookie_consent.ex @@ -75,6 +75,13 @@ defmodule PhoenixKitWeb.Components.Core.CookieConsent do attr :policy_version, :string, default: "1.0", doc: "Policy version for consent tracking" attr :cookie_policy_url, :string, default: "/legal/cookie-policy" attr :privacy_policy_url, :string, default: "/legal/privacy-policy" + + attr :legal_links, :list, + default: [], + doc: "Dynamic list of %{title, url} for published legal pages" + + attr :legal_index_url, :string, default: "/legal", doc: "URL to legal pages index" + attr :google_consent_mode, :boolean, default: false, doc: "Enable Google Consent Mode v2" attr :class, :string, default: "" @@ -173,7 +180,7 @@ defmodule PhoenixKitWeb.Components.Core.CookieConsent do } .pk-glass { - background: oklch(var(--b1) / 0.95); + background: oklch(var(--b1) / 0.98); backdrop-filter: blur(20px) saturate(180%); -webkit-backdrop-filter: blur(20px) saturate(180%); border: 1px solid var(--pk-border); @@ -190,25 +197,6 @@ defmodule PhoenixKitWeb.Components.Core.CookieConsent do transform: translateY(-2px); box-shadow: 0 4px 12px oklch(var(--bc) / 0.1); } - - .pk-toggle-track { - background: var(--pk-border); - transition: background-color 0.2s ease; - } - - .pk-toggle-track.active { - background: var(--pk-primary); - } - - .pk-toggle-thumb { - background: var(--pk-bg); - box-shadow: 0 1px 3px oklch(var(--bc) / 0.2); - transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1); - } - - input:checked + .pk-toggle-track .pk-toggle-thumb { - transform: translateX(20px); - } <%!-- Floating Icon (only for opt-in frameworks) --%> @@ -254,17 +242,16 @@ defmodule PhoenixKitWeb.Components.Core.CookieConsent do

{gettext("We value your privacy")}

-

+

{gettext( "We use cookies to enhance your browsing experience and analyze our traffic." )} {" "} - {gettext("Cookie Policy")} + {gettext("Legal")}

@@ -308,7 +295,7 @@ defmodule PhoenixKitWeb.Components.Core.CookieConsent do > <%!-- Backdrop --%>
@@ -328,7 +315,7 @@ defmodule PhoenixKitWeb.Components.Core.CookieConsent do

{gettext("Privacy Preferences")}

-

+

{gettext("Manage your cookie settings")}

@@ -356,7 +343,7 @@ defmodule PhoenixKitWeb.Components.Core.CookieConsent do <%= for category <- @categories do %>
@@ -374,34 +361,21 @@ defmodule PhoenixKitWeb.Components.Core.CookieConsent do <% end %>
-

+

{category.description}

- <%!-- Custom Toggle --%> - + <%!-- Toggle --%> + <% end %> @@ -411,21 +385,12 @@ defmodule PhoenixKitWeb.Components.Core.CookieConsent do
<%!-- Policy Links --%> -
- - {gettext("Privacy Policy")} - - + diff --git a/lib/phoenix_kit_web/components/layout_wrapper.ex b/lib/phoenix_kit_web/components/layout_wrapper.ex index 5f77299a7..e557c0c41 100644 --- a/lib/phoenix_kit_web/components/layout_wrapper.ex +++ b/lib/phoenix_kit_web/components/layout_wrapper.ex @@ -700,6 +700,8 @@ defmodule PhoenixKitWeb.Components.LayoutWrapper do policy_version={config.policy_version} cookie_policy_url={config.cookie_policy_url} privacy_policy_url={config.privacy_policy_url} + legal_links={config.legal_links} + legal_index_url={config.legal_index_url} google_consent_mode={config.google_consent_mode} /> <% end %>