SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/SocialNetworking.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Helpers;
  15.  
  16. use Kohana;
  17.  
  18.  
  19. /**
  20. * Has methods for outputting links for submitting to a number of social networking sites
  21. **/
  22. class SocialNetworking
  23. {
  24. private static $url;
  25. private static $title;
  26. private static $desc;
  27.  
  28.  
  29. /**
  30.   * Set the share details to use when submitting to the various websites.
  31.   **/
  32. public static function details($title, $desc, $url = null)
  33. {
  34. if (! $url) $url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
  35.  
  36. $title = Text::limitChars(strip_tags($title), 100, '...');
  37. $desc = Text::limitChars(strip_tags($desc), 1000, '...');
  38.  
  39. self::$url = $url;
  40. self::$title = $title;
  41. self::$desc = $desc;
  42. }
  43.  
  44.  
  45. /**
  46.   * Checks if there are details available
  47.   **/
  48. public static function hasdetails()
  49. {
  50. if (self::$url and self::$title) return true;
  51. return false;
  52. }
  53.  
  54.  
  55. public static function getUrl()
  56. {
  57. return self::$url;
  58. }
  59.  
  60. public static function getTitle()
  61. {
  62. return self::$title;
  63. }
  64.  
  65. public static function getDesc()
  66. {
  67. return self::$desc;
  68. }
  69.  
  70.  
  71. /**
  72.   * Get the A tag for a facebook share link
  73.   **/
  74. public static function facebookLink($class = '')
  75. {
  76. $share_url = 'http://www.facebook.com/sharer.php?u=' . Enc::url(self::$url) . '&t=' . Enc::url(self::$title);
  77.  
  78. $share_url = Enc::html($share_url);
  79. $class = Enc::html($class);
  80.  
  81. return "<a href=\"{$share_url}\" onclick=\"window.open(this.href, 'Share', 'width=800,height=500'); return false;\" class=\"{$class}\">";
  82. }
  83.  
  84. /**
  85.   * Get a facebook share button
  86.   **/
  87. public static function facebook($icon = null)
  88. {
  89. if (! $icon) $icon = 'SKIN/images/icon_facebook-white.svg';
  90.  
  91. echo "<div class='share-link share-link--facebook'>";
  92. echo self::facebookLink();
  93. echo "<img src=\"{$icon}\" alt=\"Facebook\" title=\"Facebook\">";
  94. echo "</a>";
  95. echo "</div>";
  96. }
  97.  
  98.  
  99. /**
  100.   * Get the A tag for a twitter share link
  101.   *
  102.   * @param string $tweet The content of the tweet
  103.   * @param string $link The URL of the link you're sharing
  104.   * @param string $via_handle The Twitter handle of the company / user / account the post is shared via
  105.   * @param string $class classes you want added to the <a> tag example: button
  106.   * @return string
  107.   *
  108.   **/
  109. public static function twitterLink($tweet = null, $link = '', $via_handle = '', $class = '')
  110. {
  111. if (! $tweet) $tweet = self::$title . ' - ' . Kohana::config('sprout.site_title');
  112.  
  113. if (! $link) $link = self::$url;
  114.  
  115. $share_url = 'http://twitter.com/intent/tweet?text=' . Enc::url($tweet);
  116.  
  117. if($link) {
  118. $share_url .= '&url=' . Enc::url($link);
  119. }
  120.  
  121. if($via_handle) {
  122. $share_url .= '&via=' . Enc::url($via_handle);
  123. }
  124.  
  125. $share_url = Enc::html($share_url);
  126. $class = Enc::html($class);
  127.  
  128. return "<a href=\"{$share_url}\" onclick=\"window.open(this.href, 'Share', 'width=800,height=500'); return false;\" class=\"{$class}\">";
  129. }
  130.  
  131. /**
  132.   * Output a twitter share button
  133.   **/
  134. public static function twitter($icon = null, $tweet = null)
  135. {
  136. if (! $icon) $icon = 'SKIN/images/icon_twitter-white.svg';
  137.  
  138. echo "<div class='share-link share-link--twitter'>";
  139. echo self::twitterLink($tweet);
  140. echo "<img src=\"{$icon}\" alt=\"Twitter\" title=\"Twitter\">";
  141. echo "</a>";
  142. echo "</div>";
  143. }
  144.  
  145. /**
  146.   * Get the A tag for a linkedIn share link
  147.   **/
  148. public static function linkedinLink($class = '')
  149. {
  150. $url = Enc::url(self::$url);
  151. $title = Enc::url(self::$title);
  152. $desc = Enc::url(self::$desc);
  153.  
  154. $share_url = "http://www.linkedin.com/shareArticle?mini=true&url={$url}&title={$title}&summary={$desc}";
  155. $share_url = Enc::html($share_url);
  156.  
  157. $class = enc::html($class);
  158.  
  159. return "<a href=\"{$share_url}\" onclick=\"window.open(this.href, 'Share', 'width=800,height=500'); return false;\" class=\"{$class}\">";
  160. }
  161.  
  162.  
  163. /**
  164.   * Output a linkedin share button
  165.   **/
  166. public static function linkedin($icon = null)
  167. {
  168. $url = Enc::url(self::$url);
  169. $title = Enc::url(self::$title);
  170. $desc = Enc::url(self::$desc);
  171.  
  172. if (! $icon) $icon = 'SKIN/images/icon_linkedin-white.svg';
  173.  
  174. $share_url = "http://www.linkedin.com/shareArticle?mini=true&url={$url}&title={$title}&summary={$desc}";
  175. $share_url = Enc::html($share_url);
  176.  
  177. echo "<div class='share-link share-link--linkedin'>";
  178. echo "<a href=\"{$share_url}\" onclick=\"window.open(this.href, 'Share', 'width=800,height=500'); return false;\">";
  179. echo "<img src=\"{$icon}\" alt=\"LinkedIn\" title=\"LinkedIn\">";
  180. echo "</a>";
  181. echo "</div>";
  182. }
  183.  
  184.  
  185. /**
  186.   * Get the A tag for an email share link
  187.   **/
  188. public static function emailLink($class = '')
  189. {
  190. $share_url = 'email_share/share?url=' . Enc::url(self::$url) . '&title=' . Enc::url(self::$title);
  191. $share_url = Enc::html($share_url);
  192. $class = enc::html($class);
  193.  
  194. return '<a href="' . $share_url . '" class="' . $class . '">';
  195. }
  196.  
  197. /**
  198.   * Output a email share button
  199.   **/
  200. public static function email($icon = null)
  201. {
  202. if (! $icon) $icon = 'SKIN/images/icon_email-white.svg';
  203.  
  204. echo "<div class='share-link share-link--email'>";
  205. echo self::emailLink();
  206. echo "<img src=\"{$icon}\" alt=\"Email\" title=\"Email\">";
  207. echo "</a>";
  208. echo "</div>";
  209. }
  210.  
  211.  
  212. /**
  213.   * Return a link to the main page for a given social media platform
  214.   *
  215.   * @param string $type The network type to display, e.g. 'facebook'
  216.   * @param bool $new_window Should the link be in a new window?
  217.   * @return string The opening A link HTML
  218.   **/
  219. public static function pageLink($type, $new_window = true)
  220. {
  221. $link = Kohana::config('sprout.social_media.' . $type);
  222.  
  223. if (! $type or ! $link) {
  224. $link = 'javascript:;" style="box-shadow: 0 0 5px 5px #f00" title="no link target set in skin config!';
  225. }
  226. $out = "<a href=\"{$link}\"";
  227. if ($new_window) $out .= ' target="_blank" ';
  228. $out .= ">";
  229.  
  230. return $out;
  231. }
  232.  
  233. }
  234.  
  235.  
  236.