From e045c6c91c6fc8c8e760ff501a2a7fe6c10404d3 Mon Sep 17 00:00:00 2001 From: Cameron Date: Thu, 8 Feb 2018 15:43:38 -0800 Subject: [PATCH] Social plugin now provides social share links in admin after creating/updating a news item. --- e107_plugins/_blank/e_admin.php | 16 ++--- e107_plugins/social/e_admin.php | 98 +++++++++++++++++++++++--- e107_plugins/social/e_shortcode.php | 70 +++++++++++++----- e107_themes/bootstrap3/admin_style.css | 2 + 4 files changed, 149 insertions(+), 37 deletions(-) diff --git a/e107_plugins/_blank/e_admin.php b/e107_plugins/_blank/e_admin.php index ff000183f..0d0b489ba 100644 --- a/e107_plugins/_blank/e_admin.php +++ b/e107_plugins/_blank/e_admin.php @@ -11,7 +11,7 @@ class _blank_admin * @param $ui admin-ui object * @return array */ - public function config($ui) + public function config(e_admin_ui $ui) { $action = $ui->getAction(); // current mode: create, edit, list $type = $ui->getEventName(); // 'wmessage', 'news' etc. (core or plugin) @@ -41,19 +41,20 @@ class _blank_admin * @param object $ui admin-ui * @param int|array $id - Primary ID of the record being created/edited/deleted or array data of a batch process. */ - public function process($ui, $id=0) + public function process(e_admin_ui $ui, $id=0) { - $data = $ui->getPosted(); - $type = $ui->getEventName(); + $data = $ui->getPosted(); // ie $_POST field-data + $type = $ui->getEventName(); // eg. 'news' $action = $ui->getAction(); // current mode: create, edit, list, batch + $changed = $ui->getModel()->dataHasChanged(); // true when data has changed from what is in the DB. - if($action == 'delete') + if($action === 'delete') { return; } - if($action == 'batch') + if($action === 'batch') { $arrayOfRecordIds = $id['ids']; $command = $id['cmd']; @@ -68,7 +69,7 @@ class _blank_admin if(!empty($data['x__blank_url'])) { - // Save the data in 'blank' plugin table. . + // eg. Save the data in 'blank' plugin table. . } @@ -86,4 +87,3 @@ class _blank_admin -?> \ No newline at end of file diff --git a/e107_plugins/social/e_admin.php b/e107_plugins/social/e_admin.php index 3ab19b594..5099373f7 100644 --- a/e107_plugins/social/e_admin.php +++ b/e107_plugins/social/e_admin.php @@ -6,7 +6,9 @@ class social_admin { - private $twitterActive = false; + private $_twitter_active = false; + + private $_default_providers = array('facebook-share'=>'fa-facebook', 'twitter'=>'fa-twitter'); function __construct() @@ -15,7 +17,7 @@ class social_admin if(!empty($pref) && !empty($pref['Twitter']) && is_array($pref['Twitter'])) { - $this->twitterActive = vartrue($pref['Twitter']['keys']['key']); + $this->_twitter_active = vartrue($pref['Twitter']['keys']['key']); } } @@ -39,7 +41,7 @@ class social_admin case "page": case "news": - if($this->twitterActive == true) + if($this->_twitter_active == true) { $config['fields']['twitter'] = array ( 'title' =>LAN_SOCIAL_202, 'type' => 'text', 'tab'=>2, 'writeParms'=> array('size'=>'xxlarge', 'placeholder'=>LAN_SOCIAL_203), 'width' => 'auto', 'help' => '', 'readParms' => '', 'class' => 'left', 'thclass' => 'left', ); } @@ -57,22 +59,98 @@ class social_admin * Process Posted Data. * @param $ui admin-ui object */ - public function process($ui, $id=0) + public function process(e_admin_ui $ui, $id=0) { - $data = $ui->getPosted(); + $data = $ui->getPosted(); $action = $ui->getAction(); // current mode: create, edit, list + $type = $ui->getEventName(); // eg. 'news' + $changed = $ui->getModel()->dataHasChanged(); + + $tp = e107::getParser(); + //e107::getHybridAuth('twitter'); - e107::getMessage()->addDebug("e107_plugins/social/e_admin.php :: process method called."); - e107::getMessage()->addDebug("ID: ".$id); - e107::getMessage()->addDebug("Action: ".$action); - e107::getMessage()->addDebug(print_a($data,true)); + // e107::getMessage()->addDebug("e107_plugins/social/e_admin.php :: process method called."); + // e107::getMessage()->addDebug("ID: ".$id); + // e107::getMessage()->addDebug("Action: ".$action); + // e107::getMessage()->addDebug(print_a($data,true)); + + + if($changed === false || $type !== 'news' || intval($data['news_class']) !== e_UC_PUBLIC) // social links only when item is public. + { + return null; + } + + if($action === 'create' || $action === 'edit') + { + $data['news_id'] = $id; + + $shareData = array( + 'title' => $data['news_title'], + 'url' => e107::getUrl()->create('news/view/item', $data, 'full=1'), + 'hashtags' => $data['news_meta_keywords'] + ); + + $message = ' + +
+
+
'.$tp->toImage($data['news_thumbnail'][0], array('w'=>100, 'h'=>100, 'class'=>'media-object')).'
+
+

'.$data['news_title'].'

+

'.$data['news_meta_description']."

".$this->share($shareData).' +
+
+
+ '; + + //FIXME setTitle doesn't work across sessions. + e107::getMessage()->setTitle(LAN_PLUGIN_SOCIAL_NAME." (".LAN_OPTIONAL.")",E_MESSAGE_INFO)->addInfo($message); + } + } + /** + * Build social share links for the admin area. + * @param $data + * @return string + */ + private function share($data) + { + // $pref = e107::pref('social'); + $soc = e107::getScBatch('social'); + $tp = e107::getParser(); + + $providers = /*!empty($pref['sharing_providers']) ? array_keys($pref['sharing_providers']) :*/ $this->_default_providers; + + $links = array(); + + $allProviders = $soc->getProviders(); + + $options = array( + 'twitterAccount' => basename(XURL_TWITTER), + 'hashtags' => $data['hashtags'] + + ); + + foreach($allProviders as $key=>$row) + { + if(!array_key_exists($key,$providers)) + { + continue; + } + + $shareURL = $soc->getShareUrl($key, $row['url'], $data, $options); + + $links[] = "".$tp->toGlyph($providers[$key]).$row['title'].""; + + } + + return implode(" ",$links); + } } -?> \ No newline at end of file diff --git a/e107_plugins/social/e_shortcode.php b/e107_plugins/social/e_shortcode.php index 03e3128c8..a1c557543 100644 --- a/e107_plugins/social/e_shortcode.php +++ b/e107_plugins/social/e_shortcode.php @@ -17,6 +17,7 @@ class social_shortcodes extends e_shortcode public $var; + public function getProviders() { @@ -215,6 +216,55 @@ class social_shortcodes extends e_shortcode } + /** + * @param string $type provider key. eg. facebook, twitter etc. + * @param string $urlScheme The URL scheme. @see getProviders 'url' + * @param array $data + * @param string $data['title'] Title for the URL + * @param string $data['description'] Description for the URL + * @param string $data['media'] + * @param array $options Currently 'twitterAccount' and 'hashtags' are supported. + * @return string + */ + public function getShareUrl($type, $urlScheme, $data=array(), $options=array()) + { + $data = array('u'=> rawurlencode($data['url']), 't'=> rawurlencode($data['title']), 'd' => rawurlencode($data['description']), 'm' => rawurlencode($data['media'])); + + return $this->parseShareUrlScheme($type, $urlScheme, $data, $options); + } + + + /** + * @param string $type + * @param string $providerUrlScheme + * @param array $data Array containing keys: 'u' (URL), 't' (Title), 'd' (Description)', 'm' (Media) + * @param array $options (optional) 'hashtags' and 'twitterAccount' + * @return string + */ + private function parseShareUrlScheme($type, $providerUrlScheme, $data=array(), $options=array()) + { + $pUrl = str_replace("&","&",$providerUrlScheme); + + $shareUrl = e107::getParser()->lanVars($pUrl,$data); + + if($type === 'twitter') + { + if(!empty($options['hashtags'])) + { + $shareUrl .= "&hashtags=".rawurlencode($options['hashtags']); + } + + if(!empty($options['twitterAccount'])) + { + $shareUrl .= "&via=".$options['twitterAccount']; + } + + } + + return $shareUrl; + + } + /** * {SOCIALSHARE: url=x&title=y} * @example {SOCIALSHARE: type=basic} - Show only Email, Facebook, Twitter and Google. @@ -315,25 +365,7 @@ class social_shortcodes extends e_shortcode continue; } - - - $pUrl = str_replace("&","&",$val['url']); - - $shareUrl = $tp->lanVars($pUrl,$data); - - if($k == 'twitter') - { - if(!empty($hashtags)) - { - $shareUrl .= "&hashtags=".rawurlencode($hashtags); - } - - if(!empty($twitterAccount)) - { - $shareUrl .= "&via=".$twitterAccount; - } - - } + $shareUrl = $this->parseShareUrlScheme($k, $val['url'], $data, array('twitterAccount'=>$twitterAccount, 'hashtags'=>$hashtags)); if(!empty($val['mobile'])) { diff --git a/e107_themes/bootstrap3/admin_style.css b/e107_themes/bootstrap3/admin_style.css index 1305d4238..3a72f6fdc 100644 --- a/e107_themes/bootstrap3/admin_style.css +++ b/e107_themes/bootstrap3/admin_style.css @@ -514,10 +514,12 @@ i.s-message-warning { background-position: -777px -37px; width: 32px; height: 32 i.s-message-info { background-position: -1480px 0; width: 32px; height: 32px; } i.s-message-debug { background-position: -1480px 0; width: 32px; height: 32px; } +.s-message .well { background-color: rgba(0,0,0,0.4) } .s-message-body { padding-left: 42px; } .s-message-item {} .s-message-item .btn { margin-top: 15px; } + .e-footer-info { text-align: center } /* ************* Backwards Compatibility CSS *****************/