Topsy. Can also provide automatic trackback comments when someone tweets about your posts. Copyright: Copyright 2009-2010 by Topsy Labs. This software is distributed under the terms of the GNU GPL as defined in LICENSE.txt. */ global $TOPSY_VERSION; $TOPSY_VERSION = '1.2.7'; global $topsy_cache_time_tb, $topsy_cache_time_udata, $topsy_conn_timeout, $topsy_debug; $topsy_cache_time_tb = 300; $topsy_cache_time_udata = 3600; $topsy_conn_timeout = 10.0; $topsy_debug = 0; function topsy_init() { load_plugin_textdomain('topsy', PLUGINDIR . '/' . dirname(plugin_basename(__FILE__)) ); } function shortcode_topsy_retweet_big() { return topsy_generate_retweet_button('big'); } function shortcode_topsy_retweet_small() { return topsy_generate_retweet_button('small'); } add_filter('get_avatar', 'knx_get_avatar', 10, 5); function knx_get_avatar($avatar, $id_or_email, $size='96', $default='', $alt='') { if (!is_object($id_or_email) && $comment_ID = get_comment_ID()) { $id_or_email = get_comment($comment_ID); } if (is_object($id_or_email)) { if (sgk_is_topsy_comment($id_or_email)) { $alt = ''; if (preg_match("/^https?:\/\/(?:[-\w]+\.)?twitter\.com\/(\w{1,15})/", $id_or_email->comment_author_url, $match)) { $tweeter_username = $match[1]; } $default = 'http://img.tweetimag.es/i/' . $tweeter_username . '_n'; return "{$alt}"; } } return $avatar; } function sgk_is_topsy_comment($comment, $ret_ID=false) { if (substr($comment->comment_agent, 0, 5) == 'Topsy') { if ($ret_ID) { return $comment->comment_ID; } return true; } return false; } function topsy_action_admin_menu() { add_submenu_page('options-general.php', 'Topsy Options', 'Topsy', 9, 'topsy', 'topsy_settings_menu'); if( !get_option('topsy_trackbacks_enabled') ) { topsy_settings_reset(); } } function topsy_activation() { add_option('topsy_trackbacks_enabled', 'off'); add_option('topsy_trackbacks_infonly', 'off'); add_option('topsy_rl_limit', '1'); add_option('topsy_rl_remaining', '1'); add_option('topsy_rl_reset', '1'); add_option('topsy_linkify_hashtag', 'on'); add_option('topsy_linkify_atuser', 'on'); add_option('topsy_ignore_list', ''); add_option('topsy_comment_type', 'trackback'); $pages = array( 'home', 'page', 'post', 'category', 'archive' ); foreach ($pages as $page) { add_option('topsy_button_' . $page . '_display', 'on'); add_option('topsy_button_' . $page . '_position', 'before'); add_option('topsy_button_' . $page . '_align', 'right'); add_option('topsy_button_' . $page . '_style', 'big'); add_option('topsy_button_' . $page . '_css', 'margin-left: 0.75em;'); } add_option('topsy_add_button_to_rss', 'off'); add_option('topsy_autonotify_enabled', 'on'); add_option('topsy_smallbutton_order', 'count,badge,retweet'); add_option('topsy_url_shortener', 'bitly'); add_option('topsy_urlshortener_username', 'topsyplugin'); add_option('topsy_urlshortener_authkey', 'R_0bb5fbe4ce1eb830679766704df41937'); topsy_update_shortener_data(); add_option('topsy_button_theme', 'blue'); add_option('topsy_button_text_tweet', 'tweet'); add_option('topsy_button_text_retweet', 'retweet'); if (get_option('topsy_retweet_username')) { $rt_username = get_option('topsy_retweet_username'); if (strrchr($rt_username, '@')) $rt_username = substr(strrchr($rt_username, '@'), 1); update_option('topsy_retweet_username', $rt_username); } add_option('topsy_suppress_categories', ''); add_option('topsy_streaming', 'off'); add_option('topsy_use_preloader', 'off'); add_option('topsy_auto_hashtags', 'none'); } function topsy_comment_exists($item) { global $post; foreach (get_comments("post_id=$post->ID") as $c) { if ($c->comment_author_url == $item->permalink_url) { return true; } if ($c->comment_author == $item->author->name && $c->comment_date == date('Y-m-d H:i:s', $item->date)) { return true; } } return false; } function topsy_deactivation() { // Placeholder, for now, but will keep error messages from happening. } function topsy_eval_braces($data) { $data = $data[1]; $num = preg_match_all("/\\$([-\w]+)/", $data, $vars); foreach ($vars[1] as $x) { global ${$x}; } global $post; if ($post) { $post_title = $post->post_title; $post_author = $post->post_author; } if (preg_match("/\)-\>/", $data)) { $data = preg_replace("/\)-\>/", ')SPLIT->', $data); list($obj, $attr) = explode('SPLIT', $data); eval("\$obj = $obj;"); eval("\$ret = \$obj$attr;"); return $ret; } if (preg_match("/^\w+$/", $data)) { $data = "get_option('$data')"; } eval("\$ret = $data;"); return $ret; } function topsy_eval_data($str) { $str = preg_replace_callback("/\[([^\]]+)\]/", "topsy_eval_braces", $str); return $str; } function topsy_filter_the_content($content) { if( is_home() && get_option('topsy_button_home_display') == 'off') { return $content; } if( is_page() && get_option('topsy_button_page_display') == 'off') { return $content; } if( is_single() && get_option('topsy_button_post_display') == 'off') { return $content; } if( is_category() && get_option('topsy_button_category_display') == 'off') { return $content; } if( is_archive() && get_option('topsy_button_archive_display') == 'off' && !is_category() ) { return $content; } if (get_option('topsy_suppress_categories')) { $suppress_cats = explode(',', get_option('topsy_suppress_categories')); foreach ($suppress_cats as $cat_name) { $cat_id = get_cat_id($cat_name); if (in_category($cat_id)) { return $content; } } } global $post; if (get_post_meta($post->ID, 'topsy_button', true) == 'suppress') { return $content; } $page = null; if ( is_home() ) { $page = 'home'; } if ( is_page() ) { $page = 'page'; } if ( is_single() ) { $page = 'post'; } if ( is_category() ) { $page = 'category'; } if ( is_archive() && !is_category() ) { $page = 'archive'; } switch ( get_option('topsy_button_' . $page . '_align') ) { case 'left': { $align_css = 'float: left;'; break; } case 'right': { $align_css = 'float: right;'; break; } default: { $align_css = ''; break; } } $button = topsy_generate_retweet_button(get_option('topsy_button_' . $page . '_style'), $align_css . get_option('topsy_button_'.$page.'_css')); switch ( get_option('topsy_button_' . $page . '_position') ) { case 'before': { $content = $button . $content; break; } case 'after': { $content = $content . $button; break; } } return "\n$content\n"; } function topsy_filter_the_content_rss($content) { if (get_option('topsy_add_button_to_rss')) { return topsy_filter_the_content($content); } return $content; } function topsy_fn_backtrace($include_self = false) { $ret = array_map(create_function('$i', 'return $i["function"];'), debug_backtrace()); if (! $include_self) { array_shift($ret); } return $ret; } function topsy_generate_retweet_button($size = 'small', $css_opts = '') { $which_class = 'data'; if (! in_array('topsy_filter_the_content', topsy_fn_backtrace())) { $which_class = 'shortcode'; } $hostname = 'button'; $url = urlencode(get_permalink()); if (preg_match("/[\?&]topsybeta=1/", $_SERVER['REQUEST_URI'], $match)) { $hostname = 'beta.button'; $url .= $match[0]; } global $post; $short_url = get_post_meta($post->ID, 'topsy_short_url', true); if ($short_url) $short_url = "\", \"shorturl\": \"$short_url"; $title = str_replace('"', '\"', $post->post_title); $hashtags = get_option('topsy_auto_hashtags'); if ($hashtags == 'post' && get_the_tags()) { foreach (get_the_tags() as $tag) { $title .= ' #' . $tag->name; } } elseif ($hashtags != 'none' && $hashtags != 'post') { foreach (preg_split("/[, ]+/", $hashtags) as $tag) { $title .= " #$tag"; } } $ret = str_replace('&', '&', "{ \"url\": \"$url$short_url\"") . ", \"style\": \"$size\", \"title\": \"$title\" }"; $coded = rawurlencode($ret); if (get_option('topsy_use_preloader') == 'on') { $ret = str_replace('\"', '\\\\\"', $ret); $inner_script = ""; } else { $inner_script = ''; } if ($css_opts) { $css_opts = trim($css_opts); if (substr($css_opts, -1) != ';') $css_opts .= ';'; $css_opts .= ' '; } $theme = topsy_simplify(get_option('topsy_button_theme')); return "
$inner_script
\n"; } function topsy_get_cache_time($url) { global $post; return intval(get_post_meta($post->ID, '_topsy_cache_timestamp', true), 10); } function topsy_get_plugin_comments($full = false) { global $wpdb; if ($full) { return $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_agent LIKE 'Topsy plugin for WordPress%'"); } else { $comments = $wpdb->get_results("SELECT comment_ID FROM $wpdb->comments WHERE comment_agent LIKE 'Topsy plugin for WordPress%'", ARRAY_N); if (! is_array($comments)) return null; $comments = array_map(create_function('$i','return $i[0];'), $comments); return $comments; } } function topsy_get_ratelimit($check = 'percent') { $limit = get_option('topsy_rl_limit'); $remaining = get_option('topsy_rl_remaining'); $reset = get_option('topsy_rl_reset'); if ($check = 'full') { return array( 'limit' => $limit, 'remaining' => $remaining, 'reset' => $reset, ); } elseif ($check == 'reset') { return $reset; } elseif ($check == 'remaining') { return $remaining; } elseif ($check == 'limit') { return $limit; } else { $percent = floor($remaining * 100 / $limit); } } function topsy_handle_single_trackback($item, $tb_url) { global $post, $wpdb; global $TOPSY_VERSION; if (topsy_comment_exists($item)) { return; } if (! isset($item->date) || $item->date < 1000000) { return; } $tweeter_username = $tweeter_realname = $item->author->name; $tweeter_url = $item->author->url; $tweet_url = $item->permalink_url; if (preg_match("/^https?:\/\/(?:[-\w]+\.)?twitter\.com\/(\w{1,15})/", $tweeter_url, $match)) { $tweeter_username = $match[1]; } if (in_array(strtolower($tweeter_username), explode('|', strtolower(get_option('topsy_ignore_list'))))) { return; } $tweeter_on_topsy = $item->author->topsy_author_url; $tweet_text = $item->content; $comment_type = get_option('topsy_comment_type'); if ($comment_type == 'comment') { $comment_type = ''; } if (get_option('topsy_linkify_atuser') == 'on') { $tweet_text = topsy_linkify_atuser($tweet_text); } if (get_option('topsy_linkify_hashtag') == 'on') { $tweet_text = topsy_linkify_hashtag($tweet_text); } $comment_data = array( 'comment_post_ID' => $post->ID, 'comment_author' => $tweeter_realname, 'comment_author_url' => $tweet_url, 'comment_date' => date('Y-m-d H:i:s', $item->date), 'comment_type' => $comment_type, 'comment_approved' => '0', 'comment_content' => $wpdb->escape("$tweet_text

Este comentario fue originalmente publicado en Twitter

"), 'comment_agent' => "Topsy plugin for WordPress v$TOPSY_VERSION", ); $comment_id = wp_insert_comment($comment_data); } function topsy_hook_delete_post($post_id) { topsy_send_autonotification($post_id, 1); } function topsy_hook_publish_post($post_id, $retry = 0) { if ($retry > 10) return; if (! $retry) { topsy_send_autonotification($post_id); } $post = get_post($post_id); $link = get_permalink($post); $title = $post->post_title; if (get_post_meta($post_id, '_topsy_long_url', true) != $link) { $short_url = topsy_shorten_url($link); if (substr($short_url, 0, 5) == 'Error') { wp_schedule_single_event(time() + 601, 'topsy_retry_shorten_url', array($post_id, $retry + 1)); } else { update_post_meta($post_id, '_topsy_long_url', $link); update_post_meta($post_id, 'topsy_short_url', $short_url); } } } function topsy_hook_unpublish_post($new_status = false, $old_status = false, $post = false) { if ($old_status == 'publish' && $new_status != 'publish' && $post) { topsy_send_autonotification($post->ID, 1); } } function topsy_http_get($url, $prev = array()) { global $topsy_conn_timeout, $TOPSY_VERSION; if (preg_match("/^http:\/\/([-\w\.]+)(:(\d+))?(\/[\S]+)?/", $url, $match)) { $hostname = $match[1]; $portno = $match[3] ? $match[3] : 80; $remote_path = count($match) == 5 ? $match[4] : '/'; } else { return array('error' => "Invalid URL!"); } $fp = @fsockopen($hostname, $portno, $errno, $errstr, $topsy_conn_timeout); if (! $fp) { return array('error' => $errstr); } fwrite($fp, "GET $remote_path HTTP/1.1\r\n"); fwrite($fp, "Host: $hostname\r\n"); fwrite($fp, "Accept: application/xml, text/*\r\n"); fwrite($fp, "Accept-Charset: us-ascii, utf-8, iso-8859-1, unicode-1-1\r\n"); fwrite($fp, "User-Agent: Topsy plugin for WordPress v$TOPSY_VERSION (PHP v" . phpversion() . ")\r\n"); fwrite($fp, "Connection: close\r\n"); fwrite($fp, "\r\n"); if ($prev == 'nowait') { fclose($fp); return; } $raw_response = ''; while (!feof($fp)) { $raw_response .= fgets($fp); } fclose($fp); list($raw_headers, $raw_body) = preg_split("/\r?\n\r?\n/", $raw_response, 2); foreach (preg_split("/\r?\n/", $raw_headers) as $hdr) { if (preg_match("/^HTTP\/\d\.\d (\d{3}) (.+)/", $hdr, $match)) { $status_code = $match[1]; $status_text = trim($match[2]); } elseif (preg_match("/^([-\w]+):\s+(.+)/", $hdr, $match)) { $headers[$match[1]] = trim($match[2]); } } if (substr($status_code, 0, 1) == '3' && array_key_exists('Location', $headers)) { if (in_array($headers['Location'], $prev)) { return array('error' => "HTTP redirects have looped back to " . $headers['Location']); } array_push($prev, $url); return topsy_http_get($headers['Location'], $prev); } $trimmed_body = preg_replace("/^[0-9a-fA-F]{4}\r?\n/", '', $raw_body); return array( 'status' => "$status_code $status_text", 'status_code' => $status_code, 'status_text' => $status_text, 'headers' => $headers, 'body' => $trimmed_body, 'raw_body' => $raw_body, 'raw_response' => $raw_response, 'error' => null, ); } function topsy_linkify_atuser($str) { return preg_replace("/@(\w{1,15})/", "@$1", $str); } function topsy_linkify_hashtag($str) { return preg_replace("/#(\w{1,20})/", "#$1", $str); } function topsy_plugin_page_settings_link( $links, $file ) { if( $file == 'topsy/topsy.php' && function_exists( "admin_url" ) ) { $settings_link = '' . __('Settings') . ''; array_unshift( $links, $settings_link ); // before other links } return $links; } function topsy_retweet_big() { print topsy_generate_retweet_button('big'); } function topsy_retweet_small() { print topsy_generate_retweet_button('small'); } function topsy_self_url() { $proto = $_SERVER['HTTPS'] ? 'https' : 'http'; return "$proto://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; } function topsy_send_autonotification($post_id, $darken = false) { if (get_option('topsy_autonotify_enabled') != 'on') return; $post = get_post($post_id); $link = get_permalink($post); $title = $post->post_title; if ($darken) { $darken = '&darken=1'; } # Don't send requests for autosaves if (preg_match("/(autosave|revision)/", $link)) return; # Also, each save triggers the save_post hook *twice*, and the first # time doesn't have the right externally-accessible permalink URL. # Silently return on that request, too. foreach (topsy_fn_backtrace() as $fn) { if (preg_match("/^_?wp_(put|save)_post_revision$/", $fn)) return; } topsy_http_get("http://otter.topsy.com/scrape?title=" . urlencode($title) . "&url=" . urlencode($link) . $darken, 'nowait'); } function topsy_settings_menu() { if (isset($_POST['topsy_save']) && $_POST['topsy_save']) { topsy_settings_save($_POST); } if (isset($_POST['topsy_clear']) && $_POST['topsy_clear']) { topsy_settings_reset(); } if (isset($_POST['topsy_delete_comments']) && $_POST['topsy_delete_comments'] == 'yes') { $failed = array(); $all_comments = topsy_get_plugin_comments(); $total = count($all_comments); if ($total) { foreach ($all_comments as $comment_id) { if (! wp_delete_comment($comment_id)) { $failed []= $comment_id; } } if (count($failed)) { $message = "Out of $total trackback comments, I was unable to delete the $failed comments with the following ID numbers: " . implode('; ', $failed); } else { $plugin_file = plugin_basename(__FILE__); $deact_link = '' . __('deactivate this plugin') . ''; $message = __("All $total trackback comments have been deleted. Note that if you don't $deact_link, the comments will return.", 'topsy'); } } else { $message = "Your blog did not have any trackback comments yet. Are you sure you had the "Enable trackback comments" feature turned on?"; } print('

'.$message.'

'); } global $TOPSY_VERSION; $topsy_nick = get_option('topsy_retweet_username'); if (! strlen($topsy_nick)) $topsy_nick = 'TopsyRT'; $all_smallbutton_orders = array( 'count', 'retweet', 'count,retweet', 'retweet,count', 'count,badge', 'badge,count', 'retweet,badge', 'badge,retweet', 'count,retweet,badge', 'count,badge,retweet', 'retweet,count,badge', 'retweet,badge,count', 'badge,count,retweet', 'badge,retweet,count', ); $cur_smallbutton_order = get_option('topsy_smallbutton_order'); if (! function_exists('json_decode') || ! class_exists('Services_JSON')) { require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'JSON.php'); } topsy_update_shortener_data(); topsy_update_theme_list(); $all_themes = get_option('topsy_button_theme_list'); if (is_string($all_themes)) $all_themes = unserialize($all_themes); $cur_theme = get_option('topsy_button_theme'); if (! function_exists('get_categories')) { function get_categories() { $tmp_ids = get_all_category_ids(); foreach ($tmp_ids as $this_id) { $this_cat = get_category($this_id); $this_name = $this_cat->cat_name; $tmp_cats[$this_name] = $this_id; } ksort($tmp_cats); foreach ($tmp_cats as $this_name => $this_id) { $this_cat = get_category($this_id); $all_cats []= $this_cat; } return $all_cats; } } $all_cats = get_categories(array('orderby' => 'name', 'order' => 'ASC')); $suppress_cats = explode(',', get_option('topsy_suppress_categories')); $all_shorteners = explode(',', get_option('topsy_urlshortener_list')); $cur_shortener = get_option('topsy_url_shortener'); foreach ($all_shorteners as $shortener) { $data = get_option('topsy_urlshortener_data_'. $shortener); if (! is_object($data)) $data = unserialize($data); $s_data[$shortener] = $data; } $topsy_auto_hashtags = get_option('topsy_auto_hashtags'); if (! $topsy_auto_hashtags) $topsy_auto_hashtags = 'none'; ?>

You are using Topsy Retweet Button v. You can check on Topsy's status at status.topsy.com. See the FAQ/user guide for answers to common support questions.

Retweet Username

Retweet buttons on your blog will create tweets that attribute posts to: RT @

URL Shortener

Topsy Widgets will automatically create short URLs for your posts. You can choose your preferred URL shortener here.

Shortening Service:

Automatic Hashtags

You can have the Topsy Widget automatically add hashtags to your retweets:


Sorry, this feature requires WordPress 2.3 or greater."; } ?>

" />

Your Topsy buttons will look like this:



Versions with Toplinks badges:

Element Selection and Order

Use this drop-down control to select which elements you want in your button (tweet count, retweet button, and Toplinks badge). You can also select the order you want the elements to appear in a small-size button.

Color Theme

Select the color theme you want to use for your retweet buttons:

Streaming

Retweet Button Text

You can adjust the wording of the Retweet button (for example, "Retwittear" for a Spanish site, or "Twittern!" for a German one). The button will say "Tweet" when nobody has tweeted about it yet, and "Retweet" afterwards.

Text for "Tweet" button:
Text for "Retweet" button:

Preload Retweet Button

You can add the Topsy button to your blog's theme with topsy_retweet_small() or topsy_retweet_big() codes — see the Topsy plugin FAQ for details. You can also include it anywhere in your posts with the tags [topsy_retweet_small] or [topsy_retweet_big].

The Retweet Button displays the total number of tweets associated with your content. The button also has a link to retweet your content. You can include it anywhere in your posts with the tags [topsy_retweet_small] or [topsy_retweet_big]. You can also add it to themes with <?php echo topsy_retweet_small() ?> or <?php echo topsy_retweet_big() ?>.

Note: Your WordPress version is only , which is a bit behind the times. If you upgrade your WordPress software to at least version 2.5, you can use shortcodes — such as [topsy_retweet_small] — to place the Retweet Button anywhere inside your posts. You'll also be much more secure against attacks if you upgrade to the latest version.

'Main Blog Page', 'page' => 'Static Content Page', 'post' => 'Individual Entry', 'category' => 'Category Listing', 'archive' => 'Archive Page', ); foreach ($pages as $page => $label) { ?>

Add Button to RSS Feeds?

1 || $all_cats[0]->cat_name != 'Uncategorized') { ?>

If a post has any of the categories selected below, the Topsy button will not be displayed on it. Note: this only affects the automatic topsy button; shortcodes in the post's content will still be displayed.


You are only using the default "Uncategorized" category. Please add another category, or rename the current one to something else, in order to enable this feature.

/>
This will add comments to your posts when people link to them in tweets.

/> [What's an "influential" person?]
/>

Comment Display Style
Some WordPress themes display trackbacks oddly. Displaying as comments may be preferable.

/>
/>

Ignore tweets by the following Twitter usernames
(One username per line. You can put your own username here to keep from having trackbacks from your own tweets. You can also use this to block spammers. Just enter the username(s), with no @ signs.)

If your trackback comments have gotten messed up somehow, you can use this button to delete all Topsy-generated trackback comments on your blog. This will only affect comments created by the Topsy plugin. Please note that if you don't disable trackback comments, the comments will be recreated again over the next day or two.

Delete Topsy-Created Trackback Comments


$value) { if (substr($key, 0, 15) == 'topsy_suppress_' && $value == 'on') { $suppress_cats []= substr($key, 15); } } update_option('topsy_suppress_categories', implode(',', $suppress_cats)); update_option('topsy_streaming', ($settings['topsy_streaming'] == 'on') ? 'on' : 'off' ); update_option('topsy_use_preloader', ($settings['topsy_use_preloader'] == 'on') ? 'on' : 'off' ); if ($settings['topsy_auto_hashtags'] == 'custom') { update_option('topsy_auto_hashtags', $settings['topsy_auto_hashtags_text']); } else { update_option('topsy_auto_hashtags', $settings['topsy_auto_hashtags']); } } function topsy_shorten_url($long_url) { $shortener = get_option('topsy_url_shortener'); $l_url = $long_url; # Need to globalize this var so topsy_eval_data/brackets can pick it up global $long_url; $long_url = $l_url; $s_data = get_option('topsy_urlshortener_data_'. $shortener); if (! is_object($s_data)) $s_data = unserialize($s_data); if ($s_data->http_method != 'get') { # We'll need to write code to perform POST requests if this ever happens. # For now, just bail out. return; } foreach ($s_data->http_reqd_vars as $r_var => $r_val) { $r_val = topsy_eval_data($r_val); $opts []= $r_var . '=' . urlencode($r_val); } foreach ($s_data->http_opt_vars as $r_var => $r_val) { $r_val = topsy_eval_data($r_val); if ($r_val) { $opts []= $r_var . '=' . urlencode($r_val); } } $all_opts = implode('&', $opts); $http_data = topsy_http_get($s_data->http_url . "?$all_opts"); if (isset($http_data['error'])) return "Error: " . $http_data['error']; switch ($s_data->decode) { case 'json': if (! function_exists('json_decode') || ! class_exists('Services_JSON')) { require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'JSON.php'); } # bit.ly sends the original URL as an object key, but the colon and # slashes make it impossible to dereference after it's been turned # into a PHP data structure. Just replace the URL with "ORIG_URL" # so we can actually access the damn thing. $preg_url = str_replace('/', '\/', preg_quote($long_url)); $http_data['body'] = preg_replace('/"' . $preg_url . '"\s*:/', '"ORIG_URL" :', $http_data['body']); $web_data = json_decode($http_data['body']); break; case 'xml': # Warning: This will only work in PHP5+ $web_data = new SimpleXMLElement($http_data['body']); break; default: # is.gd gives back HTML in "chunked" responses, as per RFC2616 section 3.6.1 # (see http://tools.ietf.org/html/rfc2616.html#section-3.6.1). # Strip the leading and trailing numbers to get the real content. $web_data = preg_replace("/^\d+\s+/", '', preg_replace("/\s+\d+$/", '', trim($http_data['body']))); } if ($s_data->shorturl_key) { $s_key = '$web_data->' . $s_data->shorturl_key; eval("\$short_url = $s_key;"); } else { $short_url = $web_data; } return $short_url; } function topsy_simplify($str) { $str = trim(strtolower($str)); $str = preg_replace("/[^ \w-]/", '', $str); $str = preg_replace("/[^a-z0-9]+/", '-', $str); return $str; } function topsy_trackback_main() { if (get_option('topsy_trackbacks_enabled') != 'on') return; if (! is_single()) return; global $topsy_cache_time_tb, $post; $my_post_id = $post->ID; $my_url = topsy_self_url(); if (time() - topsy_get_cache_time($my_url) < $topsy_cache_time_tb) { return; } if (topsy_get_ratelimit() < 5 && topsy_get_ratelimit('reset') > time()) { return; } # In WordPress versions < 2.7, get_comments() doesn't exist. Fall back # to get_approved_comments() instead. if (! function_exists('get_comments')) { function get_comments($arg) { if (substr($arg, 0, 8) == 'post_id=') { $arg = substr($arg, 8); } return get_approved_comments($arg); } } if (! function_exists('json_decode') || ! class_exists('Services_JSON')) { require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'JSON.php'); } $http_data = topsy_http_get(topsy_trackback_url()); if ($http_data['error']) { if ($topsy_debug) echo "Couldn't get trackback data; HTTP request failed with: " . $http_data['error']; return; } if ($http_data['status_code'] == 503) { # Do we need to do something special here? for now, we can just let the # default error response occur. } if ($http_data['status_code'] < 200 || $http_data['status_code'] > 299) { if ($topsy_debug) echo "Couldn't get trackback data; queried " . topsy_trackback_url() . " and got back a " . $http_data['status'] . " in response."; return; } $http_json = json_decode($http_data['body']); # There's some kind of race condition if we just loop over the # JSON->response->list on its own; the topsy_comment_exists() function # doesn't seem to pick up on the existence of the new comment fast # enough after initial creation. Instead, we'll use hash key collisions # to force uniqueness. $items = array(); foreach ($http_json->response->list as $item) { $items[$item->permalink_url] = $item; } foreach ($items as $item) { topsy_handle_single_trackback($item, $http_json->response->topsy_trackback_url); } topsy_update_ratelimit($http_data['headers']['X-RateLimit-Limit'], $http_data['headers']['X-RateLimit-Remaining'], $http_data['headers']['X-RateLimit-Reset']); topsy_update_cache(); } function topsy_trackback_url($my_url = '') { if (! $my_url) $my_url = topsy_self_url(); $tb_url = 'http://otter.topsy.com/trackbacks.json?url=' . htmlentities(urlencode($my_url)); if (get_option('topsy_trackbacks_infonly') == 'on') { $tb_url .= "&infonly=1"; } return $tb_url; } function topsy_update_cache() { global $post; $now = time(); add_post_meta($post->ID, '_topsy_cache_timestamp', $now, true) || update_post_meta($post->ID, '_topsy_cache_timestamp', $now); } function topsy_update_ratelimit($limit, $remaining, $reset) { update_option('topsy_rl_limit', $limit); update_option('topsy_rl_remaining', $remaining); update_option('topsy_rl_reset', $reset); } function topsy_update_shortener_data() { global $topsy_cache_time_udata; if (time() - intval(get_option('topsy_urlshortener_data_cachetime'), 10) < $topsy_cache_time_udata) return; $data_resource_url = 'http://cdn.topsy.com/asset/latest/widget/url_shorteners.json'; $http_data = topsy_http_get($data_resource_url); if (isset($http_data['error'])) return; if (! function_exists('json_decode') || ! class_exists('Services_JSON')) { require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'JSON.php'); } if ($http_data['status_code'] < 200 || $http_data['status_code'] > 299) { if ($topsy_debug) echo "Couldn't update URL shortener data; queried $data_resource_url and got back a " . $http_data['status'] . " in response."; return; } $http_json = json_decode($http_data['body']); foreach ($http_json as $key => $var) { $shortener_list []= $key; update_option('topsy_urlshortener_data_' . $key, serialize($var)); } update_option('topsy_urlshortener_list', implode(',', $shortener_list)); update_option('topsy_urlshortener_data_cachetime', time()); } function topsy_update_theme_list() { global $topsy_cache_time_udata; if (time() - intval(get_option('topsy_button_theme_list_cachetime'), 10) < $topsy_cache_time_udata) return; $data_resource_url = 'http://cdn.topsy.com/asset/latest/widget/themes.json'; $http_data = topsy_http_get($data_resource_url); if (isset($http_data['error'])) return; if (! function_exists('json_decode') || ! class_exists('Services_JSON')) { require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'JSON.php'); } if ($http_data['status_code'] < 200 || $http_data['status_code'] > 299) { if ($topsy_debug) echo "Couldn't update URL shortener data; queried $data_resource_url and got back a " . $http_data['status'] . " in response."; return; } $http_json = json_decode($http_data['body']); $cur_theme = get_option('topsy_button_theme'); $cur_theme_included = false; foreach ($http_json as $theme) { if (topsy_simplify($theme) == $cur_theme) { $cur_theme_included = true; break; } } if (! $cur_theme_included) { $new_theme = ucfirst(preg_replace_callback("/-([a-z0-9])/", create_function('$match', 'return " " . strtoupper($match[1]);'), $cur_theme)); array_push($http_json, $new_theme); } update_option('topsy_button_theme_list', serialize($http_json)); update_option('topsy_button_theme_list_cachetime', time()); } function topsy_wp_head() { $page = null; if ( is_home() ) { $page = 'home'; } if ( is_page() ) { $page = 'page'; } if ( is_single() ) { $page = 'post'; } if ( is_category() ) { $page = 'category'; } if ( is_archive() && !is_category() ) { $page = 'archive'; } $style = get_option('topsy_button_' . $page . '_style'); $host = ''; if (preg_match("/[\?&]topsybeta=1/", $_SERVER['REQUEST_URI'])) { $host = 'beta.static'; } else { $host = 'cdn'; } print ""; print "\n"; } add_action('admin_menu', 'topsy_action_admin_menu'); add_action('deleted_post', 'topsy_hook_delete_post'); add_action('init', 'topsy_init'); add_action('transition_post_status', 'topsy_hook_unpublish_post'); add_action('wp', 'topsy_trackback_main'); add_action('wp_head', 'topsy_wp_head'); add_action('publish_post', 'topsy_hook_publish_post'); add_action('publish_phone', 'topsy_hook_publish_post'); add_action('xmlrpc_publish_post', 'topsy_hook_publish_post'); add_action('topsy_retry_shorten_url', 'topsy_hook_publish_post'); add_filter('the_content', 'topsy_filter_the_content'); add_filter('the_content_rss', 'topsy_filter_the_content_rss'); add_filter( 'plugin_action_links', 'topsy_plugin_page_settings_link', 10, 2 ); if (function_exists('add_shortcode')) { add_shortcode('topsy_retweet_big', 'shortcode_topsy_retweet_big'); add_shortcode('topsy_retweet_small', 'shortcode_topsy_retweet_small'); } register_activation_hook( __FILE__, 'topsy_activation' ); register_deactivation_hook( __FILE__, 'topsy_deactivation' ); ?>