From d0788b96ae74eb2d48a75c7147e7c6a5457977da Mon Sep 17 00:00:00 2001 From: Joe Mooring Date: Thu, 1 Feb 2024 13:45:31 -0800 Subject: [PATCH] tpl/tplimpl: Update embedded instagram, twitter, and vimeo shortcodes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Replace data.GetJSON calls with resources.GetRemote - Remove usage of Facebook’s oEmbed Read feature Fixes #11971 --- .../templates/shortcodes/instagram.html | 254 +++++++++++++++++- .../shortcodes/instagram_simple.html | 68 +---- .../templates/shortcodes/twitter.html | 34 +-- .../templates/shortcodes/twitter_simple.html | 39 ++- .../templates/shortcodes/vimeo_simple.html | 68 +++-- 5 files changed, 319 insertions(+), 144 deletions(-) diff --git a/tpl/tplimpl/embedded/templates/shortcodes/instagram.html b/tpl/tplimpl/embedded/templates/shortcodes/instagram.html index a6cd002df..5d940d67b 100644 --- a/tpl/tplimpl/embedded/templates/shortcodes/instagram.html +++ b/tpl/tplimpl/embedded/templates/shortcodes/instagram.html @@ -1,18 +1,244 @@ {{- $pc := site.Config.Privacy.Instagram -}} {{- if not $pc.Disable -}} - {{ $accessToken := site.Config.Services.Instagram.AccessToken }} - {{- if not $accessToken -}} - {{- erroridf "error-missing-instagram-accesstoken" "instagram shortcode: Missing config value for services.instagram.accessToken. This can be set in config.toml, but it is recommended to configure this via the HUGO_SERVICES_INSTAGRAM_ACCESSTOKEN OS environment variable. If you are using a Client Access Token, remember that you must combine it with your App ID using a pipe symbol (|) otherwise the request will fail." -}} + {{- with .Get 0 -}} + {{- template "render-instagram" (dict "id" . "pc" $pc) -}} {{- else -}} - {{- if $pc.Simple -}} - {{ template "_internal/shortcodes/instagram_simple.html" . }} - {{- else -}} - {{ $id := .Get 0 }} - {{ $hideCaption := cond (eq (.Get 1) "hidecaption") "1" "0" }} - {{ $headers := dict "Authorization" (printf "Bearer %s" $accessToken) }} - {{ with getJSON "https://graph.facebook.com/v8.0/instagram_oembed/?url=https://instagram.com/p/" $id "/&hidecaption=" $hideCaption $headers }} - {{ .html | safeHTML }} - {{ end }} - {{- end -}} + {{- errorf "The %q shortocde requires a single positional parameter, the ID of the Instagram post. See %s" .Name .Position -}} {{- end -}} -{{- end -}} \ No newline at end of file +{{- end -}} + +{{- define "render-instagram" -}} +
+
+ +
+
+
+
+
+
+
+
+
+ + + + + + + + + +
+
+
+ View this post on Instagram +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ {{- if not .pc.Simple -}} + + {{- end -}} +{{- end -}} diff --git a/tpl/tplimpl/embedded/templates/shortcodes/instagram_simple.html b/tpl/tplimpl/embedded/templates/shortcodes/instagram_simple.html index f219181ac..13e53f0ac 100644 --- a/tpl/tplimpl/embedded/templates/shortcodes/instagram_simple.html +++ b/tpl/tplimpl/embedded/templates/shortcodes/instagram_simple.html @@ -1,67 +1 @@ -{{- $pc := .Page.Site.Config.Privacy.Instagram -}} -{{- $sc := .Page.Site.Config.Services.Instagram -}} -{{- if not $pc.Disable -}} - {{ $accessToken := site.Config.Services.Instagram.AccessToken }} - {{- if not $accessToken -}} - {{- erroridf "error-missing-instagram-accesstoken" "instagram shortcode: Missing config value for services.instagram.accessToken. This can be set in config.toml, but it is recommended to configure this via the HUGO_SERVICES_INSTAGRAM_ACCESSTOKEN OS environment variable. If you are using a Client Access Token, remember that you must combine it with your App ID using a pipe symbol (|) otherwise the request will fail." -}} - {{- else -}} - {{- $id := .Get 0 -}} - {{- $headers := dict "Authorization" (printf "Bearer %s" $accessToken) -}} - {{- $item := getJSON "https://graph.facebook.com/v8.0/instagram_oembed/?url=https://instagram.com/p/" $id "/&maxwidth=640&omitscript=true" $headers -}} - {{- $class1 := "__h_instagram" -}} - {{- $class2 := "s_instagram_simple" -}} - {{- $hideCaption := (eq (.Get 1) "hidecaption") -}} - {{ with $item }} - {{- $mediaURL := printf "https://instagram.com/p/%s/" $id | safeURL -}} - {{- if not $sc.DisableInlineCSS -}} - {{ template "__h_simple_instagram_css" $ }} - {{- end -}} -
- - - Instagram Image - -
- {{ if not $hideCaption }} -

- - {{ $item.author_name }} - - {{ $item.title}} -

- {{ end }} - - View More on Instagram - -
-
- {{ end }} - {{- end -}} -{{- end -}} - -{{ define "__h_simple_instagram_css" }} - {{ if not (.Page.Scratch.Get "__h_simple_instagram_css") }} - {{/* Only include once */}} - {{ .Page.Scratch.Set "__h_simple_instagram_css" true }} - - {{ end }} -{{ end }} \ No newline at end of file +{{- errorf `The %q shortcode was removed in v0.123.0. Use the "instagram" shortcode instead. See %s` .Name .Position -}} diff --git a/tpl/tplimpl/embedded/templates/shortcodes/twitter.html b/tpl/tplimpl/embedded/templates/shortcodes/twitter.html index 2f1d2323e..ba5a851ee 100644 --- a/tpl/tplimpl/embedded/templates/shortcodes/twitter.html +++ b/tpl/tplimpl/embedded/templates/shortcodes/twitter.html @@ -3,25 +3,12 @@ {{- if $pc.Simple -}} {{- template "_internal/shortcodes/twitter_simple.html" . -}} {{- else -}} - {{- $msg1 := "The %q shortcode requires two named parameters: user and id. See %s" -}} - {{- $msg2 := "The %q shortcode will soon require two named parameters: user and id. See %s" -}} - {{- if .IsNamedParams -}} - {{- $id := .Get "id" -}} - {{- $user := .Get "user" -}} - {{- if and $id $user -}} - {{- template "render-tweet" (dict "id" $id "user" $user "dnt" $pc.EnableDNT) -}} - {{- else -}} - {{- errorf $msg1 .Name .Position -}} - {{- end -}} + {{- $id := or (.Get "id") "" -}} + {{- $user := or (.Get "user") "" -}} + {{- if and $id $user -}} + {{- template "render-tweet" (dict "id" $id "user" $user "dnt" $pc.EnableDNT "name" .Name "position" .Position) -}} {{- else -}} - {{- $id := .Get 1 -}} - {{- $user := .Get 0 -}} - {{- if eq 1 (len .Params) -}} - {{- $id = .Get 0 -}} - {{- $user = "x" -}} {{/* This triggers a redirect. It works, but may not work forever. */}} - {{- warnf $msg2 .Name .Position -}} - {{- end -}} - {{- template "render-tweet" (dict "id" $id "user" $user "dnt" $pc.EnableDNT) -}} + {{- errorf "The %q shortcode requires two named parameters: user and id. See %s" .Name .Position -}} {{- end -}} {{- end -}} {{- end -}} @@ -30,6 +17,13 @@ {{- $url := printf "https://twitter.com/%v/status/%v" .user .id -}} {{- $query := querify "url" $url "dnt" .dnt -}} {{- $request := printf "https://publish.twitter.com/oembed?%s" $query -}} - {{- $json := getJSON $request -}} - {{- $json.html | safeHTML -}} + {{- with resources.GetRemote $request -}} + {{- with .Err -}} + {{- errorf "%s" . -}} + {{- else -}} + {{- (. | transform.Unmarshal).html | safeHTML -}} + {{- end -}} + {{- else -}} + {{- warnidf "shortcode-twitter-getremote" "The %q shortcode was unable to retrieve the remote data. See %s" .name .position -}} + {{- end -}} {{- end -}} diff --git a/tpl/tplimpl/embedded/templates/shortcodes/twitter_simple.html b/tpl/tplimpl/embedded/templates/shortcodes/twitter_simple.html index 0127fbe22..1f3b3c523 100644 --- a/tpl/tplimpl/embedded/templates/shortcodes/twitter_simple.html +++ b/tpl/tplimpl/embedded/templates/shortcodes/twitter_simple.html @@ -1,25 +1,12 @@ {{- $pc := .Page.Site.Config.Privacy.Twitter -}} {{- $sc := .Page.Site.Config.Services.Twitter -}} {{- if not $pc.Disable -}} - {{- $msg1 := "The %q shortcode requires two named parameters: user and id. See %s" -}} - {{- $msg2 := "The %q shortcode will soon require two named parameters: user and id. See %s" -}} - {{- if .IsNamedParams -}} - {{- $id := .Get "id" -}} - {{- $user := .Get "user" -}} - {{- if and $id $user -}} - {{- template "render-simple-tweet" (dict "id" $id "user" $user "dnt" $pc.EnableDNT "disableInlineCSS" $sc.DisableInlineCSS "ctx" .) -}} - {{- else -}} - {{- errorf $msg1 .Name .Position -}} - {{- end -}} + {{- $id := or (.Get "id") "" -}} + {{- $user := or (.Get "user") "" -}} + {{- if and $id $user -}} + {{- template "render-simple-tweet" (dict "id" $id "user" $user "dnt" $pc.EnableDNT "name" .Name "position" .Position) -}} {{- else -}} - {{- $id := .Get 1 -}} - {{- $user := .Get 0 -}} - {{- if eq 1 (len .Params) -}} - {{- $id = .Get 0 -}} - {{- $user = "x" -}} {{/* This triggers a redirect. It works, but may not work forever. */}} - {{- warnf $msg2 .Name .Position -}} - {{- end -}} - {{- template "render-simple-tweet" (dict "id" $id "user" $user "dnt" $pc.EnableDNT "disableInlineCSS" $sc.DisableInlineCSS "ctx" .) -}} + {{- errorf "The %q shortcode requires two named parameters: user and id. See %s" .Name .Position -}} {{- end -}} {{- end -}} @@ -27,17 +14,21 @@ {{- $url := printf "https://twitter.com/%v/status/%v" .user .id -}} {{- $query := querify "url" $url "dnt" .dnt "omit_script" true -}} {{- $request := printf "https://publish.twitter.com/oembed?%s" $query -}} - {{- $json := getJSON $request -}} - {{- if not .disableInlineCSS -}} - {{- template "__h_simple_twitter_css" .ctx -}} - {{- end }} - {{ $json.html | safeHTML -}} + {{- with resources.GetRemote $request -}} + {{- with .Err -}} + {{- errorf "%s" . -}} + {{- else -}} + {{- (. | transform.Unmarshal).html | safeHTML -}} + {{- end -}} + {{- else -}} + {{- warnidf "shortcode-twitter-simple-getremote" "The %q shortcode was unable to retrieve the remote data. See %s" .name .position -}} + {{- end -}} {{- end -}} {{- define "__h_simple_twitter_css" -}} {{- if not (.Page.Scratch.Get "__h_simple_twitter_css") -}} {{/* Only include once */}} - {{- .Page.Scratch.Set "__h_simple_twitter_css" true }} + {{- .Page.Scratch.Set "__h_simple_twitter_css" true -}}