Skip to content

A platform for community discussion. Free, open, simple.

FIX: Apply affiliate links to localized posts (#41770)

Régis Hanolf0d344d11daf
diff --git a/plugins/discourse-affiliate/lib/affiliate_processor.rb b/plugins/discourse-affiliate/lib/affiliate_processor.rb
index d35e3d85e56..e59a841c80e 100644
--- a/plugins/discourse-affiliate/lib/affiliate_processor.rb
+++ b/plugins/discourse-affiliate/lib/affiliate_processor.rb
@@ -57,6 +57,10 @@ class AffiliateProcessor
@rules = rules
end
+ def self.apply_to_doc(doc)
+ doc.css("a[href]").each { |a| a["href"] = apply(a["href"]) }
+ end
+
def self.apply(url)
uri = URI.parse(url)
diff --git a/plugins/discourse-affiliate/plugin.rb b/plugins/discourse-affiliate/plugin.rb
index 5273c2cf09b..2220f98fb3d 100644
--- a/plugins/discourse-affiliate/plugin.rb
+++ b/plugins/discourse-affiliate/plugin.rb
@@ -14,8 +14,6 @@ register_svg_icon "handshake"
after_initialize do
require_relative "lib/affiliate_processor"
- on(:post_process_cooked) do |doc, post|
- doc.css("a[href]").each { |a| a["href"] = AffiliateProcessor.apply(a["href"]) }
- true
- end
+ on(:post_process_cooked) { |doc| AffiliateProcessor.apply_to_doc(doc) }
+ on(:post_process_localized_cooked) { |doc| AffiliateProcessor.apply_to_doc(doc) }
end
diff --git a/plugins/discourse-affiliate/spec/integration/content_localization_spec.rb b/plugins/discourse-affiliate/spec/integration/content_localization_spec.rb
new file mode 100644
index 00000000000..aacf43d1beb
--- /dev/null
+++ b/plugins/discourse-affiliate/spec/integration/content_localization_spec.rb
@@ -0,0 +1,70 @@
+# frozen_string_literal: true
+
+describe "Affiliate links on localized posts" do
+ fab!(:admin)
+ fab!(:localizer_group) { Fabricate(:group, users: [admin]) }
+
+ let(:amazon_url) { "https://www.amazon.de/gp/product/B0C3WGSSWC" }
+ let(:affiliate_tag) { "tag=discourse-21" }
+
+ before do
+ enable_current_plugin
+ stub_request(:get, /amazon\.de/).to_return(status: 200, body: "")
+ stub_request(:head, /amazon\.de/).to_return(status: 200, body: "")
+ Jobs.run_immediately!
+ SiteSetting.affiliate_amazon_de = "discourse-21"
+ SiteSetting.content_localization_enabled = true
+ SiteSetting.content_localization_allowed_groups = localizer_group.id.to_s
+ end
+
+ def create_post_with_link
+ create_post(raw: "Great deal at #{amazon_url}", user: admin).reload
+ end
+
+ it "applies affiliate codes to a newly created localization" do
+ post = create_post_with_link
+
+ localization =
+ PostLocalizationCreator.create(
+ post: post,
+ locale: "ja",
+ raw: "お得な情報 #{amazon_url}",
+ user: admin,
+ )
+
+ expect(post.reload.cooked).to include(affiliate_tag)
+ expect(localization.reload.cooked).to include(affiliate_tag)
+ end
+
+ it "applies affiliate codes to an updated localization" do
+ post = create_post_with_link
+ PostLocalizationCreator.create(post: post, locale: "ja", raw: "お得な情報", user: admin)
+
+ localization =
+ PostLocalizationUpdater.update(
+ post: post,
+ locale: "ja",
+ raw: "更新されたお得な情報 #{amazon_url}",
+ user: admin,
+ )
+
+ expect(localization.reload.cooked).to include(affiliate_tag)
+ end
+
+ it "applies affiliate codes to an existing localization when it is recooked" do
+ post = create_post_with_link
+ localization =
+ PostLocalizationCreator.create(
+ post: post,
+ locale: "ja",
+ raw: "お得な情報 #{amazon_url}",
+ user: admin,
+ )
+ localization.update_column(:cooked, PrettyText.cook(localization.raw))
+ expect(localization.reload.cooked).not_to include(affiliate_tag)
+
+ Jobs::ProcessLocalizedCooked.new.execute(post_localization_id: localization.id, recook: true)
+
+ expect(localization.reload.cooked).to include(affiliate_tag)
+ end
+end