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 ""; } } 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 "
\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_textEste 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.'