Skip to content

Commit 5012004

Browse files
committed
Download options can be configured per-network.
- Download options can be configured per-network. Key changed from `downloaded_content` to `download_content` and moved to each network section. - Better explanations regarding Firefox privacy shield and Instagram changing URLs: its for well known social networks only (and others, but not in our context).
1 parent bef866a commit 5012004

File tree

7 files changed

+166
-72
lines changed

7 files changed

+166
-72
lines changed

CHANGELOG.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
## 11-07-2018
33

44
1. [](#new)
5-
* Added [Pleroma](https://pleroma.social) support (using the `toot` shortcode).
6-
5+
* Added [Pleroma](https://pleroma.social) support (using the same `toot` shortcode as Mastodon).
6+
1. [](#improved)
7+
* Download options can be configured per-network.
8+
* Better explanations regarding Firefox privacy shield and Instagram changing URLs.
79
1. [](#bugfix)
8-
* Fixed some Mastodon URLs badly parsed.
10+
* Fixed some Mastodon URLs wrongly parsed.
911

1012
# v1.0.1
1113
## 07-07-2018

README.md

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,34 +54,51 @@ built_in_js: true
5454
# the default embeds to work, check this.
5555
include_font_awesome_5: true
5656

57+
58+
# Options for all networks
59+
#
60+
# -- theme --
61+
# The embed theme: with the default CSS, matches the light & dark
62+
# themes for the related network, except for Instagram (which does not
63+
# have a dark theme), where the dark theme colours are inspired by Twitter.
64+
#
65+
# -- downloaded_content --
5766
# The plugin can download images, videos, and GIFs, to ensure a
5867
# complete independence from the social networks, at the cost of
59-
# some disk space.
60-
downloaded_content:
61-
images: true
62-
videos: true
63-
64-
# Options for Twitter: embed theme (with the default CSS, matches the
65-
# light & dark Twitter themes) and API credentials required to load
66-
# tweets (an explanation on how to create an application is available
67-
# below).
68+
# some disk space. This includes avatars and custom emojis (if any).
69+
70+
6871
twitter:
6972
theme: light
73+
74+
# Please not that if false, Firefox will block Twitter images from loading by default (for privacy reasons).
75+
download_content:
76+
images: true
77+
videos: true
78+
79+
# API credentials required to load tweets (an explanation on how to create an application is available below).
7080
consumer_key: null
7181
consumer_secret: null
7282
access_token: null
7383
access_token_secret: null
7484

75-
# Options for Mastodon: embed theme (with the default CSS, matches the
76-
# light & dark Mastodon themes).
85+
7786
mastodon:
7887
theme: light
7988

80-
# Options for Instagram: embed theme (with the default CSS, the light theme
81-
# matches Instagram, and the dark one was created from the light one and
82-
# some inspiration from Twitter own dark theme).
89+
download_content:
90+
images: true
91+
videos: true
92+
93+
8394
instagram:
8495
theme: light
96+
97+
# Please not that if false, Firefox will block Instagram images from loading by default (for privacy reasons).
98+
# Please also note that as Instagram's images CDN URLs may expire, you should keep that to true if possible.
99+
download_content:
100+
images: true
101+
videos: true
85102
```
86103
87104
Themes can be `light` or `dark` as for now and for the built-in CSS. If you want to add your own, themes only add a CSS class to the embeds containers: `sse-theme-[thetheme]` (e.g. `sse-theme-light` or `sse-theme-dark`); you can put anything there and reference it on your CSS file.
@@ -94,6 +111,8 @@ To use Twitter embeds, you'll need to register an application. This is done in a
94111
2. Then, click the **Keys and access tokens** tab and at the bottom of the page, click **Create my Access Token**.
95112
3. Finally, copy the credentials in the configuration file (or in the admin).
96113

114+
If not configured, you'll see an error if you try to embed a tweet, but other embeds will work fine.
115+
97116
### Customization
98117

99118
If you want to customize the embeds' HTML code, you can override the `partials/static-social-embeds` templates. The template name is the same as the shortcode name (see below), e.g. `toot.html.twig` for Mastodon embeds template. Templates context vary for different social networks; checkout built-in templates or shortcode classes to know which variables you can use.

blueprints.yaml

Lines changed: 61 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: Static Social Embeds
2-
version: 1.0.1
2+
version: 1.1.0
33
description: |
44
Embeds social status (like tweets, instagram posts, toots, etc.) in articles without using their embed iframe,
55
but rather statically without any dependency to the service.
@@ -87,26 +87,6 @@ form:
8787
downloaded_content_help:
8888
type: spacer
8989
text: PLUGIN_SSE.ADMIN.DOWNLOADS.HELP_2
90-
downloaded_content.images:
91-
type: toggle
92-
label: PLUGIN_SSE.ADMIN.DOWNLOADS.IMAGES
93-
highlight: 1
94-
default: 1
95-
options:
96-
1: PLUGIN_ADMIN.YES
97-
0: PLUGIN_ADMIN.NO
98-
validate:
99-
type: bool
100-
downloaded_content.videos:
101-
type: toggle
102-
label: PLUGIN_SSE.ADMIN.DOWNLOADS.VIDEOS
103-
highlight: 1
104-
default: 1
105-
options:
106-
1: PLUGIN_ADMIN.YES
107-
0: PLUGIN_ADMIN.NO
108-
validate:
109-
type: bool
11090
section_twitter:
11191
type: section
11292
title: PLUGIN_SSE.ADMIN.TWITTER.TITLE
@@ -130,6 +110,26 @@ form:
130110
options:
131111
light: PLUGIN_SSE.ADMIN.LIGHT
132112
dark: PLUGIN_SSE.ADMIN.DARK
113+
twitter.download_content.images:
114+
type: toggle
115+
label: PLUGIN_SSE.ADMIN.DOWNLOADS.IMAGES
116+
highlight: 1
117+
default: 1
118+
options:
119+
1: PLUGIN_ADMIN.YES
120+
0: PLUGIN_ADMIN.NO
121+
validate:
122+
type: bool
123+
twitter.download_content.videos:
124+
type: toggle
125+
label: PLUGIN_SSE.ADMIN.DOWNLOADS.VIDEOS
126+
highlight: 1
127+
default: 1
128+
options:
129+
1: PLUGIN_ADMIN.YES
130+
0: PLUGIN_ADMIN.NO
131+
validate:
132+
type: bool
133133
twitter_column_2:
134134
type: column
135135
fields:
@@ -169,6 +169,26 @@ form:
169169
options:
170170
light: PLUGIN_SSE.ADMIN.LIGHT
171171
dark: PLUGIN_SSE.ADMIN.DARK
172+
mastodon.download_content.images:
173+
type: toggle
174+
label: PLUGIN_SSE.ADMIN.DOWNLOADS.IMAGES
175+
highlight: 1
176+
default: 1
177+
options:
178+
1: PLUGIN_ADMIN.YES
179+
0: PLUGIN_ADMIN.NO
180+
validate:
181+
type: bool
182+
mastodon.download_content.videos:
183+
type: toggle
184+
label: PLUGIN_SSE.ADMIN.DOWNLOADS.VIDEOS
185+
highlight: 1
186+
default: 1
187+
options:
188+
1: PLUGIN_ADMIN.YES
189+
0: PLUGIN_ADMIN.NO
190+
validate:
191+
type: bool
172192
section_instagram:
173193
type: section
174194
title: PLUGIN_SSE.ADMIN.INSTAGRAM.TITLE
@@ -186,3 +206,23 @@ form:
186206
options:
187207
light: PLUGIN_SSE.ADMIN.LIGHT
188208
dark: PLUGIN_SSE.ADMIN.DARK
209+
instagram.download_content.images:
210+
type: toggle
211+
label: PLUGIN_SSE.ADMIN.DOWNLOADS.IMAGES
212+
highlight: 1
213+
default: 1
214+
options:
215+
1: PLUGIN_ADMIN.YES
216+
0: PLUGIN_ADMIN.NO
217+
validate:
218+
type: bool
219+
instagram.download_content.videos:
220+
type: toggle
221+
label: PLUGIN_SSE.ADMIN.DOWNLOADS.VIDEOS
222+
highlight: 1
223+
default: 1
224+
options:
225+
1: PLUGIN_ADMIN.YES
226+
0: PLUGIN_ADMIN.NO
227+
validate:
228+
type: bool

classes/SSEShortcode.php

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,26 @@ abstract class SSEShortcode extends Shortcode
1313
const CACHE_PREFIX = 'sse';
1414
const TEMPLATES_DIRECTORY = 'partials/static-social-embeds/';
1515

16+
/**
17+
* @var string The shortcode name, as returned by getShortcodeName().
18+
* @see SSEShortcode::getShortcodeName()
19+
*/
20+
private $shortcode_name;
21+
1622
/** @var string The cache directory for API data */
17-
protected $cache_dir;
23+
private $cache_dir;
1824

1925
/** @var string The cache directory for downloaded images/videos */
20-
protected $images_dir;
26+
private $images_dir;
2127

2228
/** @var string The public path to the cache directory for downloaded images/videos */
23-
protected $images_path;
29+
private $images_path;
2430

2531
/** @var string The temporary directory for downloaded medias */
26-
protected $tmp_dir;
32+
private $tmp_dir;
2733

2834
/** @var FilesystemCache The cache driver for API data */
29-
protected $cache;
35+
private $cache;
3036

3137

3238
/**
@@ -36,6 +42,8 @@ public function __construct()
3642
{
3743
parent::__construct();
3844

45+
$this->shortcode_name = $this->getShortcodeName();
46+
3947
/** @var $locator ResourceLocatorInterface */
4048
$locator = $this->grav['locator'];
4149

@@ -79,7 +87,7 @@ abstract protected function getData($url);
7987
private function getDataCached($url)
8088
{
8189
/** @var $cache Cache */
82-
$cache_id = self::CACHE_PREFIX . '-' . $this->getShortcodeName() . '-' . $url;
90+
$cache_id = self::CACHE_PREFIX . '-' . $this->shortcode_name . '-' . $url;
8391

8492
if ($this->cache->contains($cache_id))
8593
{
@@ -100,14 +108,14 @@ private function getDataCached($url)
100108
*/
101109
public function init()
102110
{
103-
$this->shortcode->getHandlers()->add($this->getShortcodeName(), function(ShortcodeInterface $sc)
111+
$this->shortcode->getHandlers()->add($this->shortcode_name, function(ShortcodeInterface $sc)
104112
{
105113
$template_context = array_merge($this->getDataCached(trim($sc->getBbCode())), [
106114
'config' => $this->config->get('plugins.static-social-embeds')
107115
]);
108116

109117
return $this->grav['twig']->processTemplate(
110-
self::TEMPLATES_DIRECTORY . $this->getShortcodeName() . '.html.twig',
118+
self::TEMPLATES_DIRECTORY . $this->shortcode_name . '.html.twig',
111119
$template_context
112120
);
113121
});
@@ -151,29 +159,31 @@ protected function fetchMedia($url, $format = 'auto')
151159

152160

153161
// Are we allowed to cache images?
154-
if ($format == 'image' && !$this->config->get('plugins.static-social-embeds.downloaded_content.images'))
162+
if ($format == 'image' && !$this->config->get('plugins.static-social-embeds.' . $this->shortcode_name . '.download_content.images'))
155163
{
156164
return $url;
157165
}
158166

159167
// Or videos?
160-
if ($format == 'video' && !$this->config->get('plugins.static-social-embeds.downloaded_content.videos'))
168+
if ($format == 'video' && !$this->config->get('plugins.static-social-embeds.' . $this->shortcode_name . '.download_content.videos'))
161169
{
162170
return $url;
163171
}
164172

173+
$cache_permissions = $this->config->get('system.images.cache_perms', '0755');
174+
165175
$ch = curl_init();
166176
$tmp_file_path = $this->tmp_dir . '/' . sha1($url) . '.' . $extension;
167177
$tmp_dir_name = dirname($tmp_file_path);
168178

169-
if (!is_dir($tmp_dir_name)) mkdir($tmp_dir_name, $this->config->get('system.images.cache_perms', '0755'), true);
179+
if (!is_dir($tmp_dir_name)) mkdir($tmp_dir_name, $cache_permissions, true);
170180

171181
$tmp_file = fopen($tmp_file_path, 'w');
172182

173183
curl_setopt_array($ch, [
174-
CURLOPT_FILE => $tmp_file,
184+
CURLOPT_FILE => $tmp_file,
175185
CURLOPT_TIMEOUT => 3600,
176-
CURLOPT_URL => $url
186+
CURLOPT_URL => $url
177187
]);
178188

179189
curl_exec($ch);
@@ -193,7 +203,7 @@ protected function fetchMedia($url, $format = 'auto')
193203
$storage_file_path = '/' . implode('/', $storage_file_path) . '/' . $storage_file_name;
194204
$storage_file_dir = dirname($this->images_dir . $storage_file_path);
195205

196-
if (!is_dir($storage_file_dir)) mkdir($storage_file_dir, $this->config->get('system.images.cache_perms', '0755'), true);
206+
if (!is_dir($storage_file_dir)) mkdir($storage_file_dir, $cache_permissions, true);
197207

198208
rename($tmp_file_path, $this->images_dir . $storage_file_path);
199209

languages/en.yaml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ PLUGIN_SSE:
8383
avatars).<br /><br />
8484
8585
<em>If this is disabled, Firefox users will not see images by default, as Firefox blocks images and videos loaded
86-
from other domains for privacy reasons. They will have to disable the privacy protection by clicking the shield
87-
in their address bar.</em>
86+
from social network domains for privacy reasons. They will have to disable the privacy protection by clicking the
87+
shield in their address bar.</em>
8888
HELP_2: |
8989
Embeds data is (always) stored in <code>cache/static-social-embeds/</code>.<br />
9090
You can delete this folder at any time: the content will be re-downloaded on-demand.<br /><br />
@@ -143,7 +143,10 @@ PLUGIN_SSE:
143143
Embed <a href="https://www.instagram.com">Instagram</a> posts in articles using
144144
<code>[instagram="instagramPostURL"]</code> with blanks lines before and after.<br />
145145
As posts are retrieved using non-authenticated requests, you cannot embed Instagram posts from private
146-
accounts.
146+
accounts.<br /><br />
147+
148+
<em>Please note that Instagram images CDN URLs may change over time, so to be on the safe side, you should keep
149+
the download options enabled if possible.</em>
147150
THEME:
148151
LABEL: Instagram theme
149152
HELP: The embeds theme. This option is only guaranteed to be followed with the built-in CSS.

languages/fr.yaml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ PLUGIN_SSE:
8585
par vidéo, et quasiment rien pour les avatars).<br /><br />
8686
8787
<em>En cas de désactivation, les utilisateurs de Firefox ne verront pas les images par défaut, car Firefox
88-
bloque le chargement des contenus depuis d'autres domaines pour des raisons de protection de la vie privée. Iels
89-
devront alors désactiver la protection en cliquant sur le bouclier dans leur barre d'adresse.</em>
88+
bloque le chargement des contenus depuis les domaines des grands réseaux sociaux pour des raisons de protection
89+
de la vie privée. Iels devront alors désactiver la protection en cliquant sur le bouclier dans leur barre d'adresse.</em>
9090
HELP_2: |
9191
Les données des posts sont (toujours) mises en cache dans le dossier <code>cache/static-social-embeds/</code>.<br />
9292
Vous pouvez supprimer ce dossier quand vous le voulez&nbsp;: les données sont re-téléchargées à la demande.<br /><br />
@@ -136,7 +136,10 @@ PLUGIN_SSE:
136136
Intégrez des publications d'<a href="https://www.instagram.com">Instagram</a> dans vos articles en écrivant
137137
<code>[instagram="URL du post Instagram"]</code> entouré de lignes blanches de part et d'autre.<br />
138138
Les posts étant récupérés via une requête non-authentifiée, vous ne pouvez pas inclure des posts Instagram
139-
extraits de comptes privés.
139+
extraits de comptes privés.<br /><br />
140+
141+
<em>Les adresses des images et vidéos d'Instagram pouvant varier avec le temps, nous vous recommandons, afin
142+
d'être tranquille, d'activer le téléchargement des médias.</em>
140143
THEME:
141144
LABEL: Thème des publications Instagram
142145
HELP: Le thème des publications intégrées. Cette option n'est garantie que si le CSS intégré est utilisé.

0 commit comments

Comments
 (0)