123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- <?php
- if (!defined('STATUSNET')) { exit(1); }
- class ShareNoticePlugin extends Plugin
- {
- public $targets = array(
- array('Twitter'),
- array('Facebook'),
- array('StatusNet', array('baseurl' => 'http://identi.ca'))
- );
- public function onEndShowStylesheets(Action $action) {
- $action->cssLink($this->path('css/sharenotice.css'));
- return true;
- }
- function onStartShowNoticeItem($item)
- {
- $notice = $item->notice;
- $out = $item->out;
- $out->elementStart('ul', array('class' => 'notice-share'));
- foreach ($this->targets as $data) {
- $type = $data[0];
- $args = (count($data) > 1) ? $data[1] : array();
- $target = $this->getShareTarget($type, $notice, $args);
- $this->showShareTarget($out, $target);
- }
- $out->elementEnd('ul');
- }
- private function getShareTarget($type, $notice, $args)
- {
- $class = ucfirst($type) . 'ShareTarget';
- return new $class($notice, $args);
- }
- private function showShareTarget(HTMLOutputter $out, NoticeShareTarget $target)
- {
- $class = $target->getClass();
- $text = $target->getText();
- $url = $target->targetUrl();
- $out->elementStart('li', array('class' => 'notice-share-' . $class));
- $out->elementStart('a', array(
- 'href' => $url,
- 'title' => $text,
- 'target' => '_blank'
- ));
- $out->element('span', array(), $text);
- $out->elementEnd('a');
- $out->elementEnd('li');
- }
- }
- abstract class NoticeShareTarget
- {
- protected $notice;
- public function __construct($notice)
- {
- $this->notice = $notice;
- }
- public abstract function getClass();
- public abstract function getText();
- public abstract function targetUrl();
- }
- abstract class GenericNoticeShareTarget extends NoticeShareTarget
- {
- protected function maxLength()
- {
- return 140;
- }
- protected function statusText()
- {
-
- $pattern = _m('"%s"');
- $url = $this->notice->getUrl();
- $suffix = ' ' . $url;
- $room = $this->maxLength() - mb_strlen($suffix) - (mb_strlen($pattern) - mb_strlen('%s'));
- $content = $this->notice->content;
-
- $truncation_symbol = _m('…');
- $truncation_symbol_length = mb_strlen($truncation_symbol);
- if (mb_strlen($content) > $room) {
- $content = mb_substr($content, 0, $room - $truncation_symbol_length) . $truncation_symbol;
- }
- return sprintf($pattern, $content) . $suffix;
- }
- }
- class TwitterShareTarget extends GenericNoticeShareTarget
- {
- public function getClass()
- {
- return 'twitter';
- }
- public function getText()
- {
-
- return _m('Share on Twitter');
- }
- public function targetUrl()
- {
- $args = array(
- 'status' => $this->statusText()
- );
- return 'http://twitter.com/home?' .
- http_build_query($args, null, '&');
- }
- }
- class StatusNetShareTarget extends GenericNoticeShareTarget
- {
- protected $baseurl;
- public function __construct($notice, $args)
- {
- parent::__construct($notice);
- $this->baseurl = $args['baseurl'];
- }
- public function getClass()
- {
- return 'statusnet';
- }
- public function getText()
- {
- $host = parse_url($this->baseurl, PHP_URL_HOST);
-
-
- return sprintf(_m('Share on %s'), $host);
- }
- public function targetUrl()
- {
- $args = array(
- 'status_textarea' => $this->statusText()
- );
- return $this->baseurl . '/notice/new?' .
- http_build_query($args, null, '&');
- }
- }
- class FacebookShareTarget extends NoticeShareTarget
- {
- public function getClass()
- {
- return 'facebook';
- }
- public function getText()
- {
-
- return _m('Share on Facebook');
- }
- public function targetUrl()
- {
- $args = array(
- 'u' => $this->notice->getUrl(),
-
- 't' => sprintf(_m('"%s"'), $this->notice->content),
- );
- return 'http://www.facebook.com/sharer.php?' .
- http_build_query($args, null, '&');
- }
-
- function onPluginVersion(&$versions)
- {
- $url = 'http://status.net/wiki/Plugin:ShareNotice';
- $versions[] = array('name' => 'ShareNotice',
- 'version' => GNUSOCIAL_VERSION,
- 'author' => 'Brion Vibber',
- 'homepage' => $url,
- 'rawdescription' =>
-
- _m('This plugin allows sharing of notices to Twitter, Facebook and other platforms.'));
- return true;
- }
- }
|