Skip to content

Commit

Permalink
Remove TWITTER_USE_PERMALINK and add a permalink placeholder
Browse files Browse the repository at this point in the history
The placeholder is second priority, after url.

fixes #5
  • Loading branch information
ArthurHoaro committed Jan 20, 2017
1 parent f89797c commit 1a68fc0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 21 deletions.
10 changes: 1 addition & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,6 @@ You now have everything required to set up shaarli2twitter plugin.

## Settings

### TWITTER_USE_PERMALINK

Enable this setting to tweet links to Shaarli permalinks instead of direct links.

*Values*:

- `0`: disabled, tweet direct links
- `1`: enabled, tweet Shaarli's permalink

### TWITTER_TWEET_FORMAT

This setting shows the format of tweets. You can use placeholders which will be filled
Expand All @@ -75,6 +66,7 @@ until the 140 chars limit is reached. Values may be truncated if the limit is re
Available placeholders, in order of priority:

* `${url}`: Shaare URL (will be automatically replaced as `t.co` links).
* `${permalink}`: Shaare permalink (will be automatically replaced as `t.co` links).
* `${title}`: Shaare title.
* `${tags}`: List of shaare tags displayed as hashtags (`#tag1 #tag2`...).
* `${description}`: Shaare description.
Expand Down
5 changes: 2 additions & 3 deletions shaarli2twitter/shaarli2twitter.meta
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
description="Automatically publish your Shaares to your Twitter account. Read <a href="#">the documentation</a> for configuration."
parameters="TWITTER_API_KEY;TWITTER_API_SECRET;TWITTER_ACCESS_TOKEN;TWITTER_ACCESS_TOKEN_SECRET;TWITTER_USE_PERMALINK;TWITTER_TWEET_FORMAT"
parameters="TWITTER_API_KEY;TWITTER_API_SECRET;TWITTER_ACCESS_TOKEN;TWITTER_ACCESS_TOKEN_SECRET;TWITTER_TWEET_FORMAT"
parameter.TWITTER_API_KEY="Consumer Key (API Key)"
parameter.TWITTER_API_SECRET="Consumer Secret (API Secret)"
parameter.TWITTER_ACCESS_TOKEN="Access Token"
parameter.TWITTER_ACCESS_TOKEN_SECRET="Access Token Secret"
parameter.TWITTER_TWEET_FORMAT="Placeholders: \${title} \${url} \${description} \${tags} (as hashtags)"
parameter.TWITTER_USE_PERMALINK="Tweet links to Shaarli instead of direct links (1: enabled, 0: disabled)"
parameter.TWITTER_TWEET_FORMAT="Placeholders: \${url} \${permalink} \${title} \${tags} \${description}"
15 changes: 6 additions & 9 deletions shaarli2twitter/shaarli2twitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,7 @@ function hook_shaarli2twitter_save_link($data, $conf)
$data['tags'][$i] = '#'. $data['tags'][$i];
}

// URL: notes becomes permalinks, and all permalinks if the option is enabled
if ($conf->get('plugins.TWITTER_USE_PERMALINK') || startsWith($data['url'], '?')) {
$data['url'] = index_url($_SERVER) . '?' . $data['shorturl'];
}
$data['permalink'] = index_url($_SERVER) . '?' . $data['shorturl'];

$format = $conf->get('plugins.TWITTER_TWEET_FORMAT', DEFAULT_FORMAT);
$tweet = format_tweet($data, $format);
Expand Down Expand Up @@ -176,12 +173,12 @@ function tweet($conf, $tweet)
function format_tweet($link, $format)
{
// Tweets are limited to 140 chars, we need to prioritize what will be displayed
$priorities = array('url', 'title', 'tags', 'description');
$priorities = array('url', 'permalink', 'title', 'tags', 'description');

$tweet = $format;
foreach ($priorities as $priority) {
if (get_current_length($format) >= TWEET_LENGTH) {
return substr(remove_remaining_placeholders($tweet), 0, TWEET_LENGTH);
if (get_current_length($tweet) >= TWEET_LENGTH) {
return remove_remaining_placeholders($tweet);
}

$tweet = replace_placeholder($tweet, $priority, $link[$priority]);
Expand All @@ -207,9 +204,9 @@ function replace_placeholder($tweet, $placeholder, $value)

$current = get_current_length($tweet);
// Tweets URL have a fixed size due to t.co
$valueLength = ($placeholder != 'url') ? strlen($value) : TWEET_URL_LENGTH;
$valueLength = ($placeholder != 'url' && $placeholder != 'permalink') ? strlen($value) : TWEET_URL_LENGTH;
if ($current + $valueLength > TWEET_LENGTH) {
$value = substr($value, 0, TWEET_LENGTH - $current - 1) . '';
$value = substr($value, 0, TWEET_LENGTH - $current - 3) . '';
}
return str_replace('${'. $placeholder .'}', $value, $tweet);
}
Expand Down

0 comments on commit 1a68fc0

Please sign in to comment.