Skip to content

Commit

Permalink
Download options can be configured per-network.
Browse files Browse the repository at this point in the history
- 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).
  • Loading branch information
AmauryCarrade committed Jul 11, 2018
1 parent bef866a commit 5012004
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 72 deletions.
8 changes: 5 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
## 11-07-2018

1. [](#new)
* Added [Pleroma](https://pleroma.social) support (using the `toot` shortcode).

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

# v1.0.1
## 07-07-2018
Expand Down
47 changes: 33 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,34 +54,51 @@ built_in_js: true
# the default embeds to work, check this.
include_font_awesome_5: true


# Options for all networks
#
# -- theme --
# The embed theme: with the default CSS, matches the light & dark
# themes for the related network, except for Instagram (which does not
# have a dark theme), where the dark theme colours are inspired by Twitter.
#
# -- downloaded_content --
# The plugin can download images, videos, and GIFs, to ensure a
# complete independence from the social networks, at the cost of
# some disk space.
downloaded_content:
images: true
videos: true

# Options for Twitter: embed theme (with the default CSS, matches the
# light & dark Twitter themes) and API credentials required to load
# tweets (an explanation on how to create an application is available
# below).
# some disk space. This includes avatars and custom emojis (if any).


twitter:
theme: light

# Please not that if false, Firefox will block Twitter images from loading by default (for privacy reasons).
download_content:
images: true
videos: true

# API credentials required to load tweets (an explanation on how to create an application is available below).
consumer_key: null
consumer_secret: null
access_token: null
access_token_secret: null

# Options for Mastodon: embed theme (with the default CSS, matches the
# light & dark Mastodon themes).

mastodon:
theme: light

# Options for Instagram: embed theme (with the default CSS, the light theme
# matches Instagram, and the dark one was created from the light one and
# some inspiration from Twitter own dark theme).
download_content:
images: true
videos: true


instagram:
theme: light

# Please not that if false, Firefox will block Instagram images from loading by default (for privacy reasons).
# Please also note that as Instagram's images CDN URLs may expire, you should keep that to true if possible.
download_content:
images: true
videos: true
```

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.
Expand All @@ -94,6 +111,8 @@ To use Twitter embeds, you'll need to register an application. This is done in a
2. Then, click the **Keys and access tokens** tab and at the bottom of the page, click **Create my Access Token**.
3. Finally, copy the credentials in the configuration file (or in the admin).

If not configured, you'll see an error if you try to embed a tweet, but other embeds will work fine.

### Customization

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.
Expand Down
82 changes: 61 additions & 21 deletions blueprints.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Static Social Embeds
version: 1.0.1
version: 1.1.0
description: |
Embeds social status (like tweets, instagram posts, toots, etc.) in articles without using their embed iframe,
but rather statically without any dependency to the service.
Expand Down Expand Up @@ -87,26 +87,6 @@ form:
downloaded_content_help:
type: spacer
text: PLUGIN_SSE.ADMIN.DOWNLOADS.HELP_2
downloaded_content.images:
type: toggle
label: PLUGIN_SSE.ADMIN.DOWNLOADS.IMAGES
highlight: 1
default: 1
options:
1: PLUGIN_ADMIN.YES
0: PLUGIN_ADMIN.NO
validate:
type: bool
downloaded_content.videos:
type: toggle
label: PLUGIN_SSE.ADMIN.DOWNLOADS.VIDEOS
highlight: 1
default: 1
options:
1: PLUGIN_ADMIN.YES
0: PLUGIN_ADMIN.NO
validate:
type: bool
section_twitter:
type: section
title: PLUGIN_SSE.ADMIN.TWITTER.TITLE
Expand All @@ -130,6 +110,26 @@ form:
options:
light: PLUGIN_SSE.ADMIN.LIGHT
dark: PLUGIN_SSE.ADMIN.DARK
twitter.download_content.images:
type: toggle
label: PLUGIN_SSE.ADMIN.DOWNLOADS.IMAGES
highlight: 1
default: 1
options:
1: PLUGIN_ADMIN.YES
0: PLUGIN_ADMIN.NO
validate:
type: bool
twitter.download_content.videos:
type: toggle
label: PLUGIN_SSE.ADMIN.DOWNLOADS.VIDEOS
highlight: 1
default: 1
options:
1: PLUGIN_ADMIN.YES
0: PLUGIN_ADMIN.NO
validate:
type: bool
twitter_column_2:
type: column
fields:
Expand Down Expand Up @@ -169,6 +169,26 @@ form:
options:
light: PLUGIN_SSE.ADMIN.LIGHT
dark: PLUGIN_SSE.ADMIN.DARK
mastodon.download_content.images:
type: toggle
label: PLUGIN_SSE.ADMIN.DOWNLOADS.IMAGES
highlight: 1
default: 1
options:
1: PLUGIN_ADMIN.YES
0: PLUGIN_ADMIN.NO
validate:
type: bool
mastodon.download_content.videos:
type: toggle
label: PLUGIN_SSE.ADMIN.DOWNLOADS.VIDEOS
highlight: 1
default: 1
options:
1: PLUGIN_ADMIN.YES
0: PLUGIN_ADMIN.NO
validate:
type: bool
section_instagram:
type: section
title: PLUGIN_SSE.ADMIN.INSTAGRAM.TITLE
Expand All @@ -186,3 +206,23 @@ form:
options:
light: PLUGIN_SSE.ADMIN.LIGHT
dark: PLUGIN_SSE.ADMIN.DARK
instagram.download_content.images:
type: toggle
label: PLUGIN_SSE.ADMIN.DOWNLOADS.IMAGES
highlight: 1
default: 1
options:
1: PLUGIN_ADMIN.YES
0: PLUGIN_ADMIN.NO
validate:
type: bool
instagram.download_content.videos:
type: toggle
label: PLUGIN_SSE.ADMIN.DOWNLOADS.VIDEOS
highlight: 1
default: 1
options:
1: PLUGIN_ADMIN.YES
0: PLUGIN_ADMIN.NO
validate:
type: bool
38 changes: 24 additions & 14 deletions classes/SSEShortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,26 @@ abstract class SSEShortcode extends Shortcode
const CACHE_PREFIX = 'sse';
const TEMPLATES_DIRECTORY = 'partials/static-social-embeds/';

/**
* @var string The shortcode name, as returned by getShortcodeName().
* @see SSEShortcode::getShortcodeName()
*/
private $shortcode_name;

/** @var string The cache directory for API data */
protected $cache_dir;
private $cache_dir;

/** @var string The cache directory for downloaded images/videos */
protected $images_dir;
private $images_dir;

/** @var string The public path to the cache directory for downloaded images/videos */
protected $images_path;
private $images_path;

/** @var string The temporary directory for downloaded medias */
protected $tmp_dir;
private $tmp_dir;

/** @var FilesystemCache The cache driver for API data */
protected $cache;
private $cache;


/**
Expand All @@ -36,6 +42,8 @@ public function __construct()
{
parent::__construct();

$this->shortcode_name = $this->getShortcodeName();

/** @var $locator ResourceLocatorInterface */
$locator = $this->grav['locator'];

Expand Down Expand Up @@ -79,7 +87,7 @@ abstract protected function getData($url);
private function getDataCached($url)
{
/** @var $cache Cache */
$cache_id = self::CACHE_PREFIX . '-' . $this->getShortcodeName() . '-' . $url;
$cache_id = self::CACHE_PREFIX . '-' . $this->shortcode_name . '-' . $url;

if ($this->cache->contains($cache_id))
{
Expand All @@ -100,14 +108,14 @@ private function getDataCached($url)
*/
public function init()
{
$this->shortcode->getHandlers()->add($this->getShortcodeName(), function(ShortcodeInterface $sc)
$this->shortcode->getHandlers()->add($this->shortcode_name, function(ShortcodeInterface $sc)
{
$template_context = array_merge($this->getDataCached(trim($sc->getBbCode())), [
'config' => $this->config->get('plugins.static-social-embeds')
]);

return $this->grav['twig']->processTemplate(
self::TEMPLATES_DIRECTORY . $this->getShortcodeName() . '.html.twig',
self::TEMPLATES_DIRECTORY . $this->shortcode_name . '.html.twig',
$template_context
);
});
Expand Down Expand Up @@ -151,29 +159,31 @@ protected function fetchMedia($url, $format = 'auto')


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

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

$cache_permissions = $this->config->get('system.images.cache_perms', '0755');

$ch = curl_init();
$tmp_file_path = $this->tmp_dir . '/' . sha1($url) . '.' . $extension;
$tmp_dir_name = dirname($tmp_file_path);

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

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

curl_setopt_array($ch, [
CURLOPT_FILE => $tmp_file,
CURLOPT_FILE => $tmp_file,
CURLOPT_TIMEOUT => 3600,
CURLOPT_URL => $url
CURLOPT_URL => $url
]);

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

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

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

Expand Down
9 changes: 6 additions & 3 deletions languages/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ PLUGIN_SSE:
avatars).<br /><br />
<em>If this is disabled, Firefox users will not see images by default, as Firefox blocks images and videos loaded
from other domains for privacy reasons. They will have to disable the privacy protection by clicking the shield
in their address bar.</em>
from social network domains for privacy reasons. They will have to disable the privacy protection by clicking the
shield in their address bar.</em>
HELP_2: |
Embeds data is (always) stored in <code>cache/static-social-embeds/</code>.<br />
You can delete this folder at any time: the content will be re-downloaded on-demand.<br /><br />
Expand Down Expand Up @@ -143,7 +143,10 @@ PLUGIN_SSE:
Embed <a href="https://www.instagram.com">Instagram</a> posts in articles using
<code>[instagram="instagramPostURL"]</code> with blanks lines before and after.<br />
As posts are retrieved using non-authenticated requests, you cannot embed Instagram posts from private
accounts.
accounts.<br /><br />
<em>Please note that Instagram images CDN URLs may change over time, so to be on the safe side, you should keep
the download options enabled if possible.</em>
THEME:
LABEL: Instagram theme
HELP: The embeds theme. This option is only guaranteed to be followed with the built-in CSS.
9 changes: 6 additions & 3 deletions languages/fr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ PLUGIN_SSE:
par vidéo, et quasiment rien pour les avatars).<br /><br />
<em>En cas de désactivation, les utilisateurs de Firefox ne verront pas les images par défaut, car Firefox
bloque le chargement des contenus depuis d'autres domaines pour des raisons de protection de la vie privée. Iels
devront alors désactiver la protection en cliquant sur le bouclier dans leur barre d'adresse.</em>
bloque le chargement des contenus depuis les domaines des grands réseaux sociaux pour des raisons de protection
de la vie privée. Iels devront alors désactiver la protection en cliquant sur le bouclier dans leur barre d'adresse.</em>
HELP_2: |
Les données des posts sont (toujours) mises en cache dans le dossier <code>cache/static-social-embeds/</code>.<br />
Vous pouvez supprimer ce dossier quand vous le voulez&nbsp;: les données sont re-téléchargées à la demande.<br /><br />
Expand Down Expand Up @@ -136,7 +136,10 @@ PLUGIN_SSE:
Intégrez des publications d'<a href="https://www.instagram.com">Instagram</a> dans vos articles en écrivant
<code>[instagram="URL du post Instagram"]</code> entouré de lignes blanches de part et d'autre.<br />
Les posts étant récupérés via une requête non-authentifiée, vous ne pouvez pas inclure des posts Instagram
extraits de comptes privés.
extraits de comptes privés.<br /><br />
<em>Les adresses des images et vidéos d'Instagram pouvant varier avec le temps, nous vous recommandons, afin
d'être tranquille, d'activer le téléchargement des médias.</em>
THEME:
LABEL: Thème des publications Instagram
HELP: Le thème des publications intégrées. Cette option n'est garantie que si le CSS intégré est utilisé.
Loading

0 comments on commit 5012004

Please sign in to comment.