Return to Snippet

Revision: 13280
at April 18, 2009 04:10 by DaveChild


Updated Code
<?php
/**
 * @name TweetThis
 * @author Dave Child <[email protected]>
 * @license GPLv3
 * @version 0.3
 * 
 * This snippet returns a link to twitter for the user to tweet the current
 * page. URL is sent through TinyURL automatically (TinyURLs are cached
 * indefinitely in assets/cache/tweetthis.txt). Link is given the class
 * "tweetthis".
 * 
 * &name=`@DaveChild`
 * Pass through a name to the snippet. You can use your own Twitter ID by
 * prefixing the name with an @. Optional.
 * 
 * &text=`Tweet This!` 
 * Text to use for the link. Optional.
 * 
 * &id=`7` 
 * Document ID (defaults to current document). Optional.
 */

// ---------------------------------------------------
//  Check input variables
// ---------------------------------------------------
$name = (isset($name)) ? $name : '';
$text = (isset($text)) ? $text : 'Tweet This!';
$id = (isset($id)) ? $id : $modx->documentIdentifier;

// ---------------------------------------------------
//  Get document information
// ---------------------------------------------------
$thisDoc = $modx->getPageInfo($id);

// ---------------------------------------------------
//  Define getTinyUrl function
// ---------------------------------------------------
if (!function_exists('getTinyUrl')) {
    function getTinyUrl($id) {
        global $modx;
        $cacheFile = $modx->config['base_path'] . 'assets/cache/tweetthis.txt';
        if (file_exists($cacheFile)) {
            $tinyurls = unserialize(file_get_contents($cacheFile));
            if ($tinyurls[$id]) {
                return $tinyurls[$id];
            }
        } else {
            $tinyurls = array();
        }        
        $url = $modx->makeUrl($id, '', '', 'full');
        $ch = curl_init();
        curl_setopt ($ch, CURLOPT_URL, 'http://tinyurl.com/api-create.php?url=' . $url);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
        $tinyurl = curl_exec($ch);
        curl_close($ch);
        if ($tinyurl) {
            $tinyurls[$id] = $tinyurl;
        } else {
            return $url;
        }
        $fp = fopen($cacheFile, 'w');
        fwrite($fp, serialize($tinyurls));
        fclose($fp);
        return $tinyurl;
    }
}

// ---------------------------------------------------
//  Build link
// ---------------------------------------------------
$strTweetThis = '<a class="tweetthis" href="http://twitter.com/home?status=' . urlencode($thisDoc['pagetitle']);
if ($name <> '') {
    $strTweetThis .= '%20by%20' . urlencode($name);
}
$strTweetThis .= ':%20' . urlencode(getTinyUrl($id)) .'">' . $text . '</a>';

return $strTweetThis;
?>

Revision: 13279
at April 17, 2009 08:54 by DaveChild


Updated Code
<?php
/**
 * @name TweetThis
 * @author Dave Child <[email protected]>
 * @license GPLv3
 * @version 0.2
 * 
 * This snippet returns a link to twitter for the user to tweet the current
 * page. URL is sent through TinyURL automatically (TinyURLs are cached
 * indefinitely in assets/cache/tweetthis.txt). Link is given the class
 * "tweetthis".
 * 
 * &name=`@DaveChild`
 * Pass through a name to the snippet. You can use your own Twitter ID by
 * prefixing the name with an @. Optional.
 * 
 * &text=`Tweet This!` 
 * Text to use for the link. Optional.
 */

// ---------------------------------------------------
//  Check input variables
// ---------------------------------------------------
$name = (isset($name)) ? $name : '';
$text = (isset($text)) ? $text : 'Tweet This!';

// ---------------------------------------------------
//  Get document information
// ---------------------------------------------------
$thisDoc = $modx->getPageInfo($modx->documentIdentifier);

// ---------------------------------------------------
//  Define getTinyUrl function
// ---------------------------------------------------
if (!function_exists('getTinyUrl')) {
    function getTinyUrl() {
        global $modx;
        $cacheFile = $modx->config['base_path'] . 'assets/cache/tweetthis.txt';
        if (file_exists($cacheFile)) {
            $tinyurls = unserialize(file_get_contents($cacheFile));
            if ($tinyurls[$modx->documentIdentifier]) {
                return $tinyurls[$modx->documentIdentifier];
            }
        } else {
            $tinyurls = array();
        }        
        $url = $modx->makeUrl($modx->documentIdentifier, '', '', 'full');
        $ch = curl_init();
        curl_setopt ($ch, CURLOPT_URL, 'http://tinyurl.com/api-create.php?url=' . $url);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
        $tinyurl = curl_exec($ch);
        curl_close($ch);
        if ($tinyurl) {
            $tinyurls[$modx->documentIdentifier] = $tinyurl;
        } else {
            return $url;
        }
        $fp = fopen($cacheFile, 'w');
        fwrite($fp, serialize($tinyurls));
        fclose($fp);
        return $tinyurl;
    }
}

// ---------------------------------------------------
//  Build link
// ---------------------------------------------------
$strTweetThis = '<a class="tweetthis" href="http://twitter.com/home?status=' . urlencode($thisDoc['pagetitle']);
if ($name <> '') {
    $strTweetThis .= '%20by%20' . urlencode($name);
}
$strTweetThis .= ':%20' . urlencode(getTinyUrl()) .'">' . $text . '</a>';

return $strTweetThis;
?>

Revision: 13278
at April 17, 2009 08:40 by DaveChild


Initial Code
<?php
/**
 * @name TweetThis
 * @author Dave Child <[email protected]>
 * @license GPLv3
 * @version 0.1
 * 
 * This snippet returns a link to twitter for the user to tweet the current
 * page. URL is sent through TinyURL automatically (TinyURLs are cached
 * indefinitely in assets/cache/tweetthis.txt). Link is given the class
 * "tweetthis".
 * 
 * &name=`@DaveChild`
 * Pass through a name to the snippet. You can use your own Twitter ID by
 * prefixing the name with an @. Optional.
 * 
 * &text=`Tweet This!` 
 * Text to use for the link. Optional.
 */

// ---------------------------------------------------
//  Check input variables
// ---------------------------------------------------
$name = (isset($name)) ? $name : '';
$text = (isset($text)) ? $text : 'Tweet This!';

// ---------------------------------------------------
//  Get document information
// ---------------------------------------------------
$thisDoc = $modx->getPageInfo($modx->documentIdentifier);
$url = $modx->makeUrl($modx->documentIdentifier, '', '', 'full');

// ---------------------------------------------------
//  Define getTinyUrl function
// ---------------------------------------------------
if (!function_exists('getTinyUrl')) {
    function getTinyUrl($url) {
        global $modx;
        $cacheFile = $modx->config['base_path'] . 'assets/cache/tweetthis.txt';
        if (file_exists($cacheFile)) {
            $tinyurls = unserialize(file_get_contents($cacheFile));
            if ($tinyurls[$url]) {
                return $tinyurls[$url];
            }
        } else {
            $tinyurls = array();
        }
        $ch = curl_init();
        curl_setopt ($ch, CURLOPT_URL, 'http://tinyurl.com/api-create.php?url=' . $url);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
        $tinyurl = curl_exec($ch);
        curl_close($ch);
        if ($tinyurl) {
            $tinyurls[$url] = $tinyurl;
        } else {
            return $url;
        }
        $fp = fopen($cacheFile, 'w');
        fwrite($fp, serialize($tinyurls));
        fclose($fp);
        return $tinyurl;
    }
}

// ---------------------------------------------------
//  Build link
// ---------------------------------------------------
$strTweetThis = '<a class="tweetthis" href="http://twitter.com/home?status=' . urlencode($thisDoc['pagetitle']);
if ($name <> '') {
    $strTweetThis .= '%20by%20' . urlencode($name);
}
$strTweetThis .= ':%20' . urlencode(getTinyUrl($url)) .'">' . $text . '</a>';

return $strTweetThis;
?>

Initial URL
http://www.addedbytes.com

Initial Description
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.

Initial Title
MODx Snippet: Tweet This

Initial Tags
php

Initial Language
PHP