Skip to content

Commit

Permalink
Merge pull request #1 from ArthurHoaro/check-tweet
Browse files Browse the repository at this point in the history
Add a tweet button in editlink template
  • Loading branch information
ArthurHoaro authored Dec 8, 2016
2 parents 7b2f280 + 2f8f2ba commit 1349129
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
4 changes: 4 additions & 0 deletions shaarli2twitter/edit_link.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div class="tweet" id="tweet-checkbox">
<input type="checkbox" %s name="tweet" id="tweet">
<label for="tweet">Tweet </label>
</div>
6 changes: 6 additions & 0 deletions shaarli2twitter/shaarli2twitter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var privateInput = document.getElementsByName('lf_private')[0];
var tweetInput = document.getElementsByName('tweet')[0];

privateInput.addEventListener('click', function(event) {
tweetInput.disabled = privateInput.checked;
});
42 changes: 41 additions & 1 deletion shaarli2twitter/shaarli2twitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ function shaarli2twitter_init($conf)
}
}

/**
* Add the JS file: disable the tweet button if the link is set to private.
*
* @param array $data New link values.
* @param ConfigManager $conf instance.
*
* @return array $data with the JS file.
*/
function hook_shaarli2twitter_render_footer($data, $conf)
{
if ($data['_PAGE_'] == Router::$PAGE_EDITLINK) {
$data['js_files'][] = PluginManager::$PLUGINS_PATH . '/shaarli2twitter/shaarli2twitter.js';
}
return $data;
}

/**
* Hook save link: will automatically publish a tweet when a new public link is shaared.
*
Expand All @@ -54,7 +70,7 @@ function shaarli2twitter_init($conf)
function hook_shaarli2twitter_save_link($data, $conf)
{
// No tweet without config, for private links, or on edit.
if (! is_config_valid($conf) || $data['updated'] != false || $data['private']) {
if (! is_config_valid($conf) || $data['updated'] != false || $data['private'] || ! isset($_POST['tweet'])) {
return $data;
}

Expand Down Expand Up @@ -86,6 +102,30 @@ function hook_shaarli2twitter_save_link($data, $conf)
return $link;
}

/**
* Hook render_editlink: add a checkbox to tweet the new link or not.
*
* @param array $data New link values.
* @param ConfigManager $conf instance.
*
* @return array $data with `edit_link_plugin` placeholder filled.
*/
function hook_shaarli2twitter_render_editlink($data, $conf)
{
if (! $data['link_is_new'] || ! is_config_valid($conf)) {
return $data;
}

$private = $conf->get('privacy.default_private_links', false);

$html = file_get_contents(PluginManager::$PLUGINS_PATH . '/shaarli2twitter/edit_link.html');
$html = sprintf($html, $private ? '' : 'checked="checked"');

$data['edit_link_plugin'][] = $html;

return $data;
}

/**
* Use TwitterAPIExchange to publish the tweet.
*
Expand Down

0 comments on commit 1349129

Please sign in to comment.