Source of file ShareThisOptions.php
Size: 8,958 Bytes - Last Modified: 2021-12-23T10:46:26+00:00
/var/www/docs.ssmods.com/process/src/src/Data/ShareThisOptions.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 | <?php namespace SunnysideUp\ShareThis\Data; use SilverStripe\Control\Director; use SilverStripe\Core\Config\Config; use SilverStripe\Core\Injector\Injectable; use SilverStripe\Dev\Debug; /** * @author nicolaas [at] sunnysideup.co.nz */ class ShareThisOptions { use Injectable; /** * @var array */ private static $page_specific_data; /** * @var general_data */ private static $general_data; /** * @var share_all_data */ private static $share_all_data; /** * @var non_encoded_page_url */ private static $non_encoded_page_url; /** * @var encoded_page_url */ private static $encoded_page_url; /** * @var encoded_page_title */ private static $encoded_page_title; /** * @var encoded_page_title_space_encoded */ private static $encoded_page_title_space_encoded; /** * @var encoded_description */ private static $encoded_description; /** * @var icon */ private static $icon; /** * Get's all options * * @return array */ public static function get_all_options($title, $link, $description) { self::set_variables($title, $link, $description); self::$page_specific_data = [ 'email' => [ 'url' => 'mailto:?' . htmlentities('Subject=' . self::$encoded_page_title . '&Body=' . self::$encoded_description . '%0D%0A' . self::$encoded_page_url), 'faicon' => 'fa-send', 'title' => 'Email', ], 'print' => [ 'url' => '#', 'faicon' => 'fa-print', 'click' => 'window.print(); return false;', 'title' => 'Print', ], 'favourites' => [ 'url' => '#', 'faicon' => 'fa-bookmark', 'click' => "sharethis.bookmark('" . self::$encoded_page_url . "', '" . self::$encoded_page_title . "'); return false;", 'title' => 'Add to favourites (Internet Explorer Only)', ], 'delicious' => [ 'url' => 'http://del.icio.us/post?' . htmlentities('url=' . self::$encoded_page_url . '&title=' . self::$encoded_page_title), 'faicon' => 'fa-delicious', 'title' => 'Delicious', ], 'facebook' => [ 'url' => 'http://www.facebook.com/share.php?' . htmlentities('u=' . self::$encoded_page_url . '&title=' . self::$encoded_page_title), 'faicon' => 'fa-facebook-square', 'title' => 'Facebook', ], 'googleplus' => [ 'url' => 'https://plus.google.com/share?url=' . self::$encoded_page_url, 'faicon' => 'fa-google-plus', 'title' => 'Google Plus One', ], 'linkedin' => [ 'url' => 'http://www.linkedin.com/shareArticle?' . htmlentities('mini=true&url=' . self::$encoded_page_url . '&title=' . self::$encoded_page_title . '&source=' . Director::absoluteBaseURL()), 'faicon' => 'fa-linkedin-square', 'title' => 'LinkedIn', ], 'pinterest' => [ 'url' => 'http://pinterest.com/pin/create/bookmarklet/?' . htmlentities('media=html&url=' . self::$encoded_page_url . '&is_video=false&description=' . self::$encoded_page_title), 'faicon' => 'fa-pinterest', 'title' => 'Pinterest', ], 'reddit' => [ 'url' => 'http://reddit.com/submit?' . htmlentities('url=' . self::$encoded_page_url . '&title=' . self::$encoded_page_title), 'faicon' => 'fa-reddit', 'title' => 'Reddit', ], 'twitter' => [ 'url' => 'http://twitter.com/home?status=' . htmlentities(urlencode('currently reading: ') . self::$encoded_page_url), 'faicon' => 'fa-twitter-square', 'title' => 'Twitter', ], 'tumblr' => [ 'url' => 'http://www.tumblr.com/share/link?url=' . htmlentities(self::$encoded_page_url . '&name=' . self::$encoded_page_title), 'faicon' => 'fa-tumblr-square', 'title' => 'Tumblr', ], ]; return self::$page_specific_data; } /** * @param string $title * @param string $link * @param string $description * * @return array */ public static function get_page_specific_data($title, $link, $description = '') { $originalArray = self::$page_specific_data ?: self::get_all_options($title, $link, $description); $inc = Config::inst()->get(ShareThisSTE::class, 'included_icons'); $exc = Config::inst()->get(ShareThisSTE::class, 'excluded_icons'); if (is_array($inc) && count($inc)) { $new_array_of_icons_to_include = []; foreach ($inc as $key => $value) { $new_array_of_icons_to_include[$value] = $value; if (! isset($originalArray[$value])) { Debug::show("Error in ShareIcons::set_icons_to_include, ${key} does not exist in bookmark list"); } } foreach (array_keys($originalArray) as $key) { if (! isset($new_array_of_icons_to_include[$key])) { unset($originalArray[$key]); } } } //which ones do we exclude if (is_array($exc) && count($exc)) { foreach ($exc as $key) { if (! isset($originalArray[$key])) { Debug::show("Error in ShareIcons::set_icons_to_exclude, ${key} does not exist in bookmark list"); } else { unset($originalArray[$key]); } } } if (! $link) { self::$page_specific_data = null; } return $originalArray; } /* summary: (required) utf-8 string, defaults to document.title content: (optional) utf-8 string, defaults to null updated: (optional) ISO 8601 date, defaults to document.lastModified published: (optional) ISO 8601 date, defaults to null author: currently not implemented category: currently not implemented */ public static function get_share_all() { self::$share_all_data = ' <script type="text/javascript" src="http://w.sharethis.com/button/sharethis.js#&type=website"></script> <script type="text/javascript"> SHARETHIS.addEntry( { title:"' . urldecode(self::$encoded_page_title) . '", summary:"' . urldecode(self::$encoded_page_title) . '", url:"' . urldecode(self::$encoded_page_url) . '", icon:"' . urldecode(self::$icon) . '" }, {button:true} ); </script>'; return self::$share_all_data; } /** * Sets general data */ public static function set_general_data() { self::$general_data = null; } /** * Get's generic data */ public static function get_general_data() { if (! self::$general_data) { $array = self::get_page_specific_data('', '', ''); $newArray = []; if (count($array)) { foreach (array_keys($array) as $key) { $newArray[$key] = $key; } } self::$general_data = $newArray; } return self::$general_data; } /** * @return string */ protected function facebookLike() { //see http://developers.facebook.com/docs/reference/plugins/like/ return '<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#appId=231498950207168&xfbml=1"></script><fb:like href="www.test.com" send="false" width="450" show_faces="true" font="lucida grande"></fb:like>'; } /** * Set's variables * @param string $title * @param string $link * @param string $description */ private static function set_variables($title, $link, $description) { self::$icon = urlencode(Director::absoluteBaseURL() . 'favicon.ico'); self::$non_encoded_page_url = Director::absoluteURL($link); self::$encoded_page_url = urlencode(self::$non_encoded_page_url); self::$encoded_page_title = urlencode($title); self::$encoded_page_title_space_encoded = str_replace('+', '%20', urlencode($title)); if ($description) { self::$encoded_description = urlencode($description); } else { self::$encoded_description = self::$encoded_page_title; } } } |