Posted By

DaveChild on 04/17/09


Tagged

php modx


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

1man
atma
jealousy


MODx Snippet: Tweet This


 / Published in: PHP
 

URL: http://www.addedbytes.com

This snippet returns a link to twitter for the user to tweet the current page. URL is TinyURLed automatically (and tinyurls are cached indefinitely in assets/cache/tweetthis.txt).

Simply paste code into a new snippet called "TweetThis". Call it like so:

[[TweetThis? &name=`@DaveChild` &text=`Tweet This!` &id=`7` ]]

All parameters are optional. ID defaults to current document.

  1. <?php
  2. /**
  3.  * @name TweetThis
  4.  * @author Dave Child <[email protected]>
  5.  * @license GPLv3
  6.  * @version 0.3
  7.  *
  8.  * This snippet returns a link to twitter for the user to tweet the current
  9.  * page. URL is sent through TinyURL automatically (TinyURLs are cached
  10.  * indefinitely in assets/cache/tweetthis.txt). Link is given the class
  11.  * "tweetthis".
  12.  *
  13.  * &name=`@DaveChild`
  14.  * Pass through a name to the snippet. You can use your own Twitter ID by
  15.  * prefixing the name with an @. Optional.
  16.  *
  17.  * &text=`Tweet This!`
  18.  * Text to use for the link. Optional.
  19.  *
  20.  * &id=`7`
  21.  * Document ID (defaults to current document). Optional.
  22.  */
  23.  
  24. // ---------------------------------------------------
  25. // Check input variables
  26. // ---------------------------------------------------
  27. $name = (isset($name)) ? $name : '';
  28. $text = (isset($text)) ? $text : 'Tweet This!';
  29. $id = (isset($id)) ? $id : $modx->documentIdentifier;
  30.  
  31. // ---------------------------------------------------
  32. // Get document information
  33. // ---------------------------------------------------
  34. $thisDoc = $modx->getPageInfo($id);
  35.  
  36. // ---------------------------------------------------
  37. // Define getTinyUrl function
  38. // ---------------------------------------------------
  39. if (!function_exists('getTinyUrl')) {
  40. function getTinyUrl($id) {
  41. global $modx;
  42. $cacheFile = $modx->config['base_path'] . 'assets/cache/tweetthis.txt';
  43. if (file_exists($cacheFile)) {
  44. $tinyurls = unserialize(file_get_contents($cacheFile));
  45. if ($tinyurls[$id]) {
  46. return $tinyurls[$id];
  47. }
  48. } else {
  49. $tinyurls = array();
  50. }
  51. $url = $modx->makeUrl($id, '', '', 'full');
  52. $ch = curl_init();
  53. curl_setopt ($ch, CURLOPT_URL, 'http://tinyurl.com/api-create.php?url=' . $url);
  54. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  55. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
  56. $tinyurl = curl_exec($ch);
  57. curl_close($ch);
  58. if ($tinyurl) {
  59. $tinyurls[$id] = $tinyurl;
  60. } else {
  61. return $url;
  62. }
  63. $fp = fopen($cacheFile, 'w');
  64. fwrite($fp, serialize($tinyurls));
  65. fclose($fp);
  66. return $tinyurl;
  67. }
  68. }
  69.  
  70. // ---------------------------------------------------
  71. // Build link
  72. // ---------------------------------------------------
  73. $strTweetThis = '<a class="tweetthis" href="http://twitter.com/home?status=' . urlencode($thisDoc['pagetitle']);
  74. if ($name <> '') {
  75. $strTweetThis .= '%20by%20' . urlencode($name);
  76. }
  77. $strTweetThis .= ':%20' . urlencode(getTinyUrl($id)) .'">' . $text . '</a>';
  78.  
  79. return $strTweetThis;
  80. ?>

Report this snippet  

You need to login to post a comment.