Commit a0940c14 authored by root's avatar root Committed by lechuck
Browse files

aggiunto identi.ca tools

parent bda08664
<?php
/*
Plugin Name: Identi.ca Tools
Plugin URI: http://vikingisaverb.com/
Description: A complete integration between your WordPress blog and <a href="http://identi.ca/">Identi.ca</a>. Bring your dents into your blog and pass your blog posts to Identi.ca. <a href="options-general.php?page=identica-tools.php">Configure your settings here</a>.
Version: 1.0
Author: Matt White
Author URI: http://vikingisaverb.com/
*/
// Original twitter-tools URL: http://alexking.org/projects/wordpress
// Original twitter-wordpress headers by the original author:
//
// Copyright (c) 2007 Alex King. All rights reserved.
//
// Released under the GPL license
// http://www.opensource.org/licenses/gpl-license.php
//
// This is an add-on for WordPress
// http://wordpress.org/
//
// Thanks to John Ford ( http://www.aldenta.com ) for his contributions.
// Thanks to Dougal Campbell ( http://dougal.gunters.org ) for his contributions.
//
// **********************************************************************
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// **********************************************************************
//
// New identica-wordpress note:
//
// I did very little work, basically replacing a few strings. For the most part it was a simple search-and-replace
// (twitter.com to identi.ca/api), but some URLs for identi.ca are slightly different from twitter. As you can clearly see
// looking through the code, I left most the variables alone and only changed what comes out in html.
// All thanks and kudos for the highly useful original twitter-wordpress plugin should go to the original author, Alex King.
// I'm just a guy who's handy with sed, likes identi.ca, and wanted to take advantage of the GPL'd nature of twitter-tools.
// - Matt White (matt@cleversitename.com)
load_plugin_textdomain('identica-tools');
if (!function_exists('is_admin_page')) {
function is_admin_page() {
if (function_exists('is_admin')) {
return is_admin();
}
if (function_exists('check_admin_referer')) {
return true;
}
else {
return false;
}
}
}
class twitter_tools {
function twitter_tools() {
$this->options = array(
'twitter_username'
, 'twitter_password'
, 'create_blog_posts'
, 'create_digest'
, 'digest_title'
, 'blog_post_author'
, 'blog_post_category'
, 'notify_twitter'
, 'sidebar_tweet_count'
, 'tweet_from_sidebar'
, 'give_tt_credit'
, 'last_tweet_download'
, 'doing_tweet_download'
, 'doing_digest_post'
);
$this->twitter_username = '';
$this->twitter_password = '';
$this->create_blog_posts = '0';
$this->create_digest = '0';
$this->digest_title = __("Identi.ca Updates for %s", 'identica-tools');
$this->blog_post_author = '1';
$this->blog_post_category = '1';
$this->notify_twitter = '0';
$this->sidebar_tweet_count = '3';
$this->tweet_from_sidebar = '1';
$this->give_tt_credit = '1';
// not included in options
$this->update_hash = '';
$this->tweet_prefix = 'New blog post';
$this->tweet_format = $this->tweet_prefix.': %s %s';
$this->last_digest_post = '';
$this->last_tweet_download = '';
$this->doing_tweet_download = '0';
$this->doing_digest_post = '0';
$this->version = '1.0';
}
function install() {
global $wpdb;
$charset_collate = '';
if ( version_compare(mysql_get_server_info(), '4.1.0', '>=') ) {
if (!empty($wpdb->charset)) {
$charset_collate .= " DEFAULT CHARACTER SET $wpdb->charset";
}
if (!empty($wpdb->collate)) {
$charset_collate .= " COLLATE $wpdb->collate";
}
}
$result = $wpdb->query("
CREATE TABLE `$wpdb->aktt` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`tw_id` VARCHAR( 255 ) NOT NULL ,
`tw_text` VARCHAR( 255 ) NOT NULL ,
`tw_created_at` DATETIME NOT NULL ,
`modified` DATETIME NOT NULL ,
INDEX ( `tw_id` )
) $charset_collate
");
foreach ($this->options as $option) {
add_option('aktt_'.$option, $this->$option);
}
add_option('aktt_update_hash', '');
}
function get_settings() {
foreach ($this->options as $option) {
$this->$option = get_option('aktt_'.$option);
}
}
function update_settings() {
if (current_user_can('manage_options')) {
if (get_option('aktt_create_digest') != '1' && $this->create_digest == 1) {
$this->initiate_digests();
}
$this->sidebar_tweet_count = intval($this->sidebar_tweet_count);
if ($this->sidebar_tweet_count == 0) {
$this->sidebar_tweet_count = '3';
}
foreach ($this->options as $option) {
update_option('aktt_'.$option, $this->$option);
}
}
}
function populate_settings() {
foreach ($this->options as $option) {
if (isset($_POST['aktt_'.$option])) {
$this->$option = stripslashes($_POST['aktt_'.$option]);
}
}
}
function initiate_digests() {
$this->last_digest_post = date('Y-m-d 00:00:00', strtotime('-1 day'));
if (get_option('aktt_last_digest_post') == '') {
add_option('aktt_last_digest_post', $this->last_digest_post);
}
else {
update_option('aktt_last_digest_post', $this->last_digest_post);
}
}
function tweet_download_interval() {
return 1800;
}
function do_tweet($tweet = '') {
if (empty($this->twitter_username)
|| empty($this->twitter_password)
|| empty($tweet)
|| empty($tweet->tw_text)
) {
return;
}
require_once(ABSPATH.WPINC.'/class-snoopy.php');
$snoop = new Snoopy;
$snoop->agent = 'Identi.ca Tools http://vikingisaverb.com/ddl/identica-tools.zip';
$snoop->rawheaders = array(
'X-Twitter-Client' => 'Identi.ca Tools'
, 'X-Twitter-Client-Version' => $this->version
, 'X-Twitter-Client-URL' => 'http://vikingisaverb.com/ddl/identica-tools.xml'
);
$snoop->user = $this->twitter_username;
$snoop->pass = $this->twitter_password;
$snoop->submit(
'http://identi.ca/api/statuses/update.json'
, array(
'status' => $tweet->tw_text
, 'source' => 'identicatools'
)
);
if (strpos($snoop->response_code, '200')) {
update_option('aktt_last_tweet_download', strtotime('-28 minutes'));
return true;
}
return false;
}
function do_blog_post_tweet($post_id = 0) {
if ($this->notify_twitter == '0'
|| $post_id == 0
|| get_post_meta($post_id, 'aktt_tweeted', true) == '1'
) {
return;
}
$post = get_post($post_id);
$tweet = new aktt_tweet;
$tweet->tw_text = sprintf(__($this->tweet_format, 'identica-tools'), $post->post_title, get_permalink($post_id));
$this->do_tweet($tweet);
add_post_meta($post_id, 'aktt_tweeted', '1', true);
}
function do_tweet_post($tweet) {
global $wpdb;
remove_action('publish_post', 'aktt_notify_twitter');
$data = array(
'post_content' => $wpdb->escape($tweet->tw_text)
, 'post_title' => $wpdb->escape(trim_add_elipsis($tweet->tw_text, 30))
, 'post_date' => get_date_from_gmt(date('Y-m-d H:i:s', $tweet->tw_created_at))
, 'post_category' => array($this->blog_post_category)
, 'post_status' => 'publish'
, 'post_author' => $wpdb->escape($this->blog_post_author)
);
$post_id = wp_insert_post($data);
add_post_meta($post_id, 'aktt_twitter_id', $tweet->tw_id, true);
add_action('publish_post', 'aktt_notify_twitter');
}
function do_digest_post() {
global $wpdb;
if ($this->create_digest != '1' || get_option('aktt_doing_digest_post') == '1') {
return;
}
update_option('aktt_doing_digest_post', '1');
remove_action('publish_post', 'aktt_notify_twitter');
$now = ak_gmmktime();
$yesterday = strtotime('-1 day', $now);
$last_post = get_option('aktt_last_digest_post');
if ($last_post != date('Y-m-d 00:00:00', $yesterday)) {
$days = ceil((strtotime(date('Y-m-d 00:00:00', $yesterday)) - strtotime($last_post)) / (3600 * 24));
}
else {
$days = 1;
}
for ($i = 0; $i < $days; $i++) {
$n = $days - $i;
$digest_day = strtotime('-'.$n.' days', $now);
$tweets = $wpdb->get_results("
SELECT *
FROM $wpdb->aktt
WHERE tw_created_at >= '".date('Y-m-d 00:00:00', $digest_day)."'
AND tw_created_at <= '".date('Y-m-d 23:59:59', $digest_day)."'
GROUP BY tw_id
ORDER BY tw_created_at
");
if (count($tweets) > 0) {
$tweets_to_post = array();
foreach ($tweets as $data) {
$tweet = new aktt_tweet;
$tweet->tw_text = $data->tw_text;
if (!$tweet->tweet_is_post_notification()) {
$tweets_to_post[] = $data;
}
}
if (count($tweets_to_post) > 0) {
$content = '<ul class="aktt_tweet_digest">'."\n";
foreach ($tweets_to_post as $tweet) {
$content .= ' <li>'.make_clickable($tweet->tw_text).' <a href="http://identi.ca/notice/'.$tweet->tw_id.'">#</a></li>'."\n";
}
$content .= '</ul>'."\n";
if ($this->give_tt_credit == '1') {
$content .= '<p class="aktt_credit">Powered by modified <a href="http://alexking.org/projects/wordpress">Twitter Tools</a>.</p>';
}
$data = array(
'post_content' => $wpdb->escape($content)
, 'post_title' => $wpdb->escape(sprintf($this->digest_title, date('Y-m-d', $digest_day)))
, 'post_date' => date('Y-m-d 23:59:59', $digest_day)
, 'post_category' => array($this->blog_post_category)
, 'post_status' => 'publish'
, 'post_author' => $wpdb->escape($this->blog_post_author)
);
$post_id = wp_insert_post($data);
add_post_meta($post_id, 'aktt_tweeted', '1', true);
}
}
}
$this->last_digest_post = date('Y-m-d 00:00:00', $now);
update_option('aktt_last_digest_post', $this->last_digest_post);
add_action('publish_post', 'aktt_notify_twitter');
update_option('aktt_doing_digest_post', '0');
}
}
class aktt_tweet {
function aktt_tweet(
$tw_id = ''
, $tw_text = ''
, $tw_created_at = ''
) {
$this->id = '';
$this->modified = '';
$this->tw_created_at = $tw_created_at;
$this->tw_text = $tw_text;
$this->tw_id = $tw_id;
}
function twdate_to_time($date) {
$parts = explode(' ', $date);
$date = strtotime($parts[1].' '.$parts[2].', '.$parts[5].' '.$parts[3]);
return $date;
}
function tweet_post_exists() {
global $wpdb;
$test = $wpdb->get_results("
SELECT *
FROM $wpdb->postmeta
WHERE meta_key = 'aktt_twitter_id'
AND meta_value = '$this->tw_id'
");
if (count($test) > 0) {
return true;
}
return false;
}
function tweet_is_post_notification() {
global $aktt;
if (substr($this->tw_text, 0, strlen($aktt->tweet_prefix)) == $aktt->tweet_prefix) {
return true;
}
return false;
}
function add() {
global $wpdb, $aktt;
$wpdb->query("
INSERT
INTO $wpdb->aktt
( tw_id
, tw_text
, tw_created_at
, modified
)
VALUES
( '".$wpdb->escape($this->tw_id)."'
, '".$wpdb->escape($this->tw_text)."'
, '".date('Y-m-d H:i:s', $this->tw_created_at)."'
, NOW()
)
");
do_action('aktt_add_tweet', $this);
if ($aktt->create_blog_posts == '1' && !$this->tweet_post_exists() && !$this->tweet_is_post_notification()) {
$aktt->do_tweet_post($this);
}
}
}
function aktt_login_test($username, $password) {
require_once(ABSPATH.WPINC.'/class-snoopy.php');
$snoop = new Snoopy;
$snoop->agent = 'Identi.ca Tools http://cleversitename.com/matt';
$snoop->user = $username;
$snoop->pass = $password;
$snoop->fetch('http://identi.ca/api/statuses/user_timeline.json');
if (strpos($snoop->response_code, '200')) {
return true;
}
else {
return false;
}
}
function aktt_update_tweets() {
// let the last update run for 10 minutes
if (time() - intval(get_option('aktt_doing_tweet_download')) < 600) {
return;
}
update_option('aktt_doing_tweet_download', time());
global $wpdb, $aktt;
if (empty($aktt->twitter_username) || empty($aktt->twitter_password)) {
update_option('aktt_doing_tweet_download', '0');
die();
}
require_once(ABSPATH.WPINC.'/class-snoopy.php');
$snoop = new Snoopy;
$snoop->agent = 'Identi.ca Tools http://cleversitename.com/matt';
$snoop->user = $aktt->twitter_username;
$snoop->pass = $aktt->twitter_password;
$snoop->fetch('http://identi.ca/api/statuses/user_timeline.json');
if (!strpos($snoop->response_code, '200')) {
update_option('aktt_doing_tweet_download', '0');
return;
}
$data = $snoop->results;
$hash = md5($data);
if ($hash == get_option('aktt_update_hash')) {
update_option('aktt_doing_tweet_download', '0');
return;
}
$json = new Services_JSON();
$tweets = $json->decode($data);
if (is_array($tweets) && count($tweets) > 0) {
$tweet_ids = array();
foreach ($tweets as $tweet) {
$tweet_ids[] = $wpdb->escape($tweet->id);
}
$existing_ids = $wpdb->get_col("
SELECT tw_id
FROM $wpdb->aktt
WHERE tw_id
IN ('".implode("', '", $tweet_ids)."')
");
$new_tweets = array();
foreach ($tweets as $tw_data) {
if (!$existing_ids || !in_array($tw_data->id, $existing_ids)) {
$tweet = new aktt_tweet(
$tw_data->id
, $tw_data->text
);
$tweet->tw_created_at = $tweet->twdate_to_time($tw_data->created_at);
$new_tweets[] = $tweet;
}
}
foreach ($new_tweets as $tweet) {
$tweet->add();
}
}
update_option('aktt_update_hash', $hash);
update_option('aktt_last_tweet_download', time());
update_option('aktt_doing_tweet_download', '0');
if ($aktt->create_digest == '1' && strtotime(get_option('aktt_last_digest_post')) < strtotime(date('Y-m-d 00:00:00', ak_gmmktime()))) {
$aktt->do_digest_post();
}
}
function aktt_notify_twitter($post_id) {
global $aktt;
$aktt->do_blog_post_tweet($post_id);
}
add_action('publish_post', 'aktt_notify_twitter');
function aktt_sidebar_tweets() {
global $wpdb, $aktt;
$tweets = $wpdb->get_results("
SELECT *
FROM $wpdb->aktt
GROUP BY tw_id
ORDER BY tw_created_at DESC
LIMIT $aktt->sidebar_tweet_count
");
$output = '<div class="aktt_tweets">'."\n"
.' <ul>'."\n";
if (count($tweets) > 0) {
foreach ($tweets as $tweet) {
$output .= ' <li>'.aktt_make_clickable(wp_specialchars($tweet->tw_text)).' <a href="http://identi.ca/notice/'.$tweet->tw_id.'">'.aktt_relativeTime($tweet->tw_created_at, 3).'</a></li>'."\n";
}
}
else {
$output .= ' <li>'.__('No dents available at the moment.', 'identica-tools').'</li>'."\n";
}
$output .= ' <li class="aktt_more_updates"><a href="http://identi.ca/'.$aktt->twitter_username.'">More updates...</a></li>'."\n"
.'</ul>';
if ($aktt->tweet_from_sidebar == '1') {
$output .= aktt_tweet_form('input', 'onsubmit="akttPostTweet(); return false;"');
$output .= ' <p id="aktt_tweet_posted_msg">'.__('Posting tweet...', 'identica-tools').'</p>';
}
if ($aktt->give_tt_credit == '1') {
$output .= '<p class="aktt_credit">Powered by modified <a href="http://alexking.org/projects/wordpress">Twitter Tools</a>.</p>';
}
$output .= '</div>';
print($output);
}
function aktt_latest_tweet() {
global $wpdb, $aktt;
$tweets = $wpdb->get_results("
SELECT *
FROM $wpdb->aktt
GROUP BY tw_id
ORDER BY tw_created_at DESC
LIMIT 1
");
if (count($tweets) == 1) {
foreach ($tweets as $tweet) {
$output = aktt_make_clickable(wp_specialchars($tweet->tw_text)).' <a href="http://identi.ca/notice/'.$tweet->tw_id.'">'.aktt_relativeTime($tweet->tw_created_at, 3).'</a>';
}
}
else {
$output = __('No tweets available at the moment.', 'identica-tools');
}
print($output);
}
function aktt_make_clickable($tweet) {
if (substr($tweet, 0, 1) == '@' && substr($tweet, 1, 1) != ' ') {
$space = strpos($tweet, ' ');
$username = substr($tweet, 1, $space - 1);
$tweet = '<a href="http://identi.ca/'.$username.'">@'.$username.'</a>'.substr($tweet, $space);
}
if (function_exists('make_chunky')) {
return make_chunky($tweet);
}
else {
return make_clickable($tweet);
}
}
function aktt_tweet_form($type = 'input', $extra = '') {
$output = '';
if (current_user_can('publish_posts')) {
$output .= '
<form action="'.get_bloginfo('wpurl').'/index.php" method="post" id="aktt_tweet_form" '.$extra.'>
<fieldset>
';
switch ($type) {
case 'input':
$output .= '
<p><input type="text" size="20" maxlength="140" id="aktt_tweet_text" name="aktt_tweet_text" onkeyup="akttCharCount();" /></p>
<input type="hidden" name="ak_action" value="aktt_post_tweet_sidebar" />
<script type="text/javascript">
//<![CDATA[
function akttCharCount() {
var count = document.getElementById("aktt_tweet_text").value.length;
if (count > 0) {
document.getElementById("aktt_char_count").innerHTML = 140 - count;
}
else {
document.getElementById("aktt_char_count").innerHTML = "";
}
}
setTimeout("akttCharCount();", 500);
document.getElementById("aktt_tweet_form").setAttribute("autocomplete", "off");
//]]>
</script>
';
break;
case 'textarea':
$output .= '
<p><textarea type="text" cols="60" rows="5" maxlength="140" id="aktt_tweet_text" name="aktt_tweet_text" onkeyup="akttCharCount();"></textarea></p>
<input type="hidden" name="ak_action" value="aktt_post_tweet_admin" />
<script type="text/javascript">
//<![CDATA[
function akttCharCount() {
var count = document.getElementById("aktt_tweet_text").value.length;
if (count > 0) {
document.getElementById("aktt_char_count").innerHTML = (140 - count) + "'.__(' characters remaining', 'identica-tools').'";
}
else {
document.getElementById("aktt_char_count").innerHTML = "";
}
}
setTimeout("akttCharCount();", 500);
document.getElementById("aktt_tweet_form").setAttribute("autocomplete", "off");
//]]>
</script>
';
break;
}
$output .= '
<p>
<input type="submit" id="aktt_tweet_submit" name="aktt_tweet_submit" value="'.__('Post Tweet!', 'identica-tools').'" />
<span id="aktt_char_count"></span>
</p>
<div class="clear"></div>
</fieldset>
</form>
';
}
return $output;
}
function aktt_widget_init() {
if (!function_exists('register_sidebar_widget')) {
return;
}
function aktt_widget($args) {
extract($args);
$options = get_option('aktt_widget');
$title = $options['title'];
if (empty($title)) {
}
echo $before_widget . $before_title . $title . $after_title;
aktt_sidebar_tweets();
echo $after_widget;
}
register_sidebar_widget(array(__('Identi.ca Tools', 'identica-tools'), 'widgets'), 'aktt_widget');
function aktt_widget_control() {
$options = get_option('aktt_widget');
if (!is_array($options)) {