1 <?php 2 /* Copyright (c) 2013, Geert Bergman (geert@scrivo.nl) 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. Neither the name of "Scrivo" nor the names of its contributors may be 14 * used to endorse or promote products derived from this software without 15 * specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 * 29 * $Id$ 30 */ 31 32 namespace Scrivo\SocialMedia; 33 34 /** 35 * Implmentation of the most usefull social share buttons using their own API. 36 * All buttons are implemented as iframe elements so they will keep working 37 * correctly when reloaded with AJAX using different parameter values. 38 * 39 * Note that this is not an attempt to create a uniform interface for 40 * different social media buttons, but simply a convenient way to output 41 * them using PHP. The parameters that are used by the buttons are still those 42 * as defined by the original API and therfore differ: (Facebook uses href, 43 * Twitter and LinkedIn use data-url, Titter uses data-count and LinkedIn 44 * uses data-counter, etc.). 45 */ 46 class SocialMediaShareButtons { 47 48 /** 49 * The URL to share. 50 * @var string 51 */ 52 private $url = "http://www.scrivo.nl"; 53 54 /** 55 * The locale to use for the buttons. 56 * @var string 57 */ 58 private $locale = "nl_NL"; 59 60 /** 61 * Construct a ShareButtons object. Using this object you can conveniently 62 * print share buttons on your site, all using the same URL for sharing 63 * and using the same locale. 64 * @param string $url The URL to share. 65 * @param string $locale The locale to use for the buttons. 66 **/ 67 public function __construct($url, $locale="nl_NL") { 68 $this->url = $url; 69 $this->locale = $locale; 70 } 71 72 /** 73 * Get HTML code for the iframe that hosts the button. 74 * @param string $src The value for the iframe src parameter. 75 * @param string $width The CSS width of the iframe. 76 * @param string $height The CSS height of the iframe. 77 * @return string HTML iframe code. 78 **/ 79 private function getIFrame($src, $width, $height) { 80 return "<iframe src=\"{$src}\" scrolling=\"no\" frameborder=\"0\" ". 81 "style=\"display: inline; border:none; overflow:hidden; ". 82 "width:{$width}; height:{$height};\" ". 83 "allowTransparency=\"true\"></iframe>"; 84 } 85 86 /** 87 * Construct a javascript source string for an iframe. For instance: 88 * "javascript: '<html><body>\'t stoepje</body></html>'". 89 * @param string $data The HTML data to display. 90 * @return string A javascript iframe src string. 91 **/ 92 private function jsContent($data) { 93 return "javascript: '".str_replace("'", "\\'", $data)."'"; 94 } 95 96 /** 97 * Collect only the parameters that have keys starting with "data-" and 98 * contain a value. Encode these and return them as a parameter string. 99 * @param array $param A set of button parameters. 100 * @return string A parameterized string. 101 */ 102 private function getDataParameters($param) { 103 $nb = array(); 104 foreach ($param as $k=>$v) { 105 if (substr($k, 0, 5) === "data-" && $v) { 106 $nb[] = $k . "='" . urlencode($v) . "'"; 107 } 108 } 109 return implode(" ", $nb); 110 } 111 112 /** 113 * Get the HTML for a Facebook like button. 114 * 115 * $sb = new SocialMediaShareButtons( 116 * "http://www.scrivo.nl/index.php?p=1093657"); 117 * 118 * echo $sb->getFacebookLikeButton(array( 119 * "action"=>"recommend", "width"=>"125px")); 120 * 121 * @param array $param A set of button parameters. For possible parameter 122 * names and values see the Facebook site. 123 * @return string The HTML button code (iframe). 124 */ 125 public function getFacebookLikeButton($param) { 126 127 $param += array( 128 "href" => $this->url, 129 "send" => "false", 130 "layout" => "button_count", 131 "show_faces" => "false", 132 "font" => "", 133 "colorscheme" => "light", 134 "action" => "like", 135 "locale" => $this->locale, 136 "width" => "125px", 137 "height" => "21px" 138 ); 139 140 $nb = array(); 141 foreach ($param as $k=>$v) { 142 $nb[] = $k . "=" . urlencode($v); 143 } 144 $prms = implode("&",$nb); 145 146 return $this->getIFrame("//www.facebook.com/plugins/like.php?{$prms}", 147 $param["width"], $param["height"]); 148 } 149 150 /** 151 * Get the HTML for a Twitter tweet button. 152 * 153 * $sb = new SocialMediaShareButtons( 154 * "http://www.scrivo.nl/index.php?p=1093657"); 155 * 156 * $sb->getTwitterTweetButton(array("width" => "95px")); 157 * 158 * @param array $param A set of button parameters. For possible parameter 159 * names and values see the Twitter site. 160 * @return string The HTML button code (iframe). 161 */ 162 public function getTwitterTweetButton($param) { 163 164 $param += array( 165 "data-url" => $this->url, 166 "data-via" => "", 167 "data-lang" => substr($this->locale, 0, 2), 168 "data-related" => "", 169 "data-count" => "", 170 "data-hashtags" => "", 171 "width" => "125px", 172 "height" => "21px" 173 ); 174 175 // Template was directly copied from the Twitter site. 176 $template = "<html style='margin:0; padding:0'> 177 <body style='margin:0; padding:0'> 178 <a href='https://twitter.com/share' class='twitter-share-button' 179 ".$this->getDataParameters($param)."></a> 180 <script>!function(d,s,id){ 181 var js,fjs=d.getElementsByTagName(s)[0], 182 p=/^http:/.test(d.location)?'http':'https'; 183 if(!d.getElementById(id)){js=d.createElement(s); 184 js.id=id;js.src=p+'://platform.twitter.com/widgets.js'; 185 fjs.parentNode.insertBefore(js,fjs); 186 }}(document, 'script', 'twitter-wjs');</script> 187 </body></html>"; 188 189 return $this->getIFrame($this->jsContent($template), 190 $param["width"], $param["height"]); 191 } 192 193 /** 194 * Get the HTML for a LinkedIn share button. 195 * 196 * $sb = new SocialMediaShareButtons( 197 * "http://www.scrivo.nl/index.php?p=1093657"); 198 * 199 * echo $sb->getLinkedInShareButton(array("width" => "95px")); 200 * 201 * @param array $param A set of button parameters. For possible parameter 202 * names and values see the LinkedIn site. 203 * @return string The HTML button code (iframe). 204 */ 205 public function getLinkedInShareButton($param) { 206 207 $param += array( 208 "data-url" => $this->url, 209 "data-counter" => "right", 210 "data-showzero" => "true", 211 "locale" => $this->locale, 212 "width" => "125px", 213 "height" => "21px" 214 ); 215 216 // Template was directly copied from the LinkedIn site. 217 $template = "<html style='margin:0; padding:0'> 218 <body style='margin:0; padding:0'> 219 <script src='//platform.linkedin.com/in.js' 220 type='text/javascript'>lang: {$param["locale"]}</script> 221 <script type='IN/Share' ".$this->getDataParameters($param)."> 222 </script> 223 </body></html>"; 224 225 return $this->getIFrame($this->jsContent($template), 226 $param["width"], $param["height"]); 227 } 228 229 } 230 231 ?>
Documentation generated by phpDocumentor 2.0.0a12 and ScrivoDocumentor on August 29, 2013