diff --git a/adminpages/options.php b/adminpages/options.php new file mode 100644 index 0000000..865054e --- /dev/null +++ b/adminpages/options.php @@ -0,0 +1,132 @@ +query($sql)) + { + $msg = true; + $msgt = "Account added successfully."; + } + else + { + $msg = -1; + $msgt = "Error adding account."; + } + } + elseif($tname) + { + $msg = -1; + $msgt = "Please enter the Twitter Account Number as well."; + } + elseif($taccount) + { + $msg = -1; + $msgt = "Please enter the Twitter Username as well."; + } + + //are we deleting? + $delete = $_REQUEST['delete']; + if($delete) + { + //remove all tweets + $sql = "DELETE FROM $wp_grouptwitter WHERE account_id = '$delete'"; + $wpdb->query($sql); + + //remove the account + $sql = "DELETE FROM $wp_grouptwitter_accounts WHERE id = '$delete' LIMIT 1"; + if($wpdb->query($sql)) + { + $msg = true; + $msgt = "Account deleted successfully."; + } + else + { + $msg = -1; + $msgt = "Error deleting account #$delete."; + } + } + + //get the accounts + $gtaccounts = $wpdb->get_results("SELECT * FROM $wp_grouptwitter_accounts ORDER BY last_update"); + + //updating? + $update = $_REQUEST['update']; + if($update) + { + foreach($gtaccounts as $gta) + { + echo $gta->name . "["; + $Twitter = new Twitter($gta->id); + $n = $Twitter->rebuild_archive('America/New_York'); + if($n !== FALSE) + { + $wpdb->query("UPDATE $wp_grouptwitter_accounts SET last_update = now()"); + echo $n; + } + else + echo "X"; + echo "] "; + } + echo "
=$msgt?>
Use this form to add and manage the Twitter accounts you would like to include in the database.
+ + + +There are =$ntweets?> tweet(s) in the database now. Click here to refresh the tweet database.
+ +Add this script to a cronjob (run no more than every 15 minutes) to update your cache automatically:
+/usr/bin/curl -d mypp_cmd=status =get_bloginfo("home")?>/wp-content/plugins/grouptwitter/services/updatecache.php?key==$gt_secretkey?>
++ + +
+ id = (int)$twitter_id; + $this->profile_image_url = $wpdb->get_var("SELECT profile_image_url FROM $wp_grouptwitter_accounts WHERE id = '$this->id' LIMIT 1"); + } + + public function user_timeline($page, $count = '50', $since_id = '') { + $url = 'http://twitter.com/statuses/user_timeline/' . $this->id . '.xml?count=' . $count . '&page=' . $page; + if ($since_id && $since_id != '') { + $url .= '&since_id=' . $since_id; + } + $c = curl_init(); + curl_setopt($c, CURLOPT_URL, $url); + curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 3); + curl_setopt($c, CURLOPT_TIMEOUT, 5); + $response = curl_exec($c); + $responseInfo = curl_getinfo($c); + curl_close($c); + if ($response != '' && intval($responseInfo['http_code']) == 200) { + if (class_exists('SimpleXMLElement')) { + return new SimpleXMLElement($response); + } else { + return $response; + } + } else { + return false; + } + } + + public function rebuild_archive($your_timezone) { + global $wpdb, $wp_grouptwitter, $wp_grouptwitter_accounts; + $orig_tz = date_default_timezone_get(); + date_default_timezone_set('GMT'); + $tz = new DateTimeZone($your_timezone); + $sql = "SELECT id FROM $wp_grouptwitter WHERE account_id = '$this->id' ORDER BY id DESC LIMIT 1"; + $since_id = $wpdb->get_var($sql); + $tweet_count = 0; + for ($page = 1; $page <= 1; ++$page) { + if ($twitter_xml = $this->user_timeline($page, '50', $since_id)) { + //check the profile image on the first page + if($page == 1) + { + if($twitter_xml->status[0]->user->profile_image_url && $twitter_xml->status[0]->user->profile_image_url != $this->profile_image_url) + { + $wpdb->query("UPDATE $wp_grouptwitter_accounts SET profile_image_url = '" . addslashes($twitter_xml->status[0]->user->profile_image_url) . "' WHERE id = '$this->id' LIMIT 1"); + } + } + + foreach ($twitter_xml->status as $key => $status) { + $datetime = new DateTime($status->created_at); + $datetime->setTimezone($tz); + $created_at = $datetime->format('Y-m-d H:i:s'); + $sql = "INSERT IGNORE INTO $wp_grouptwitter + (id, account_id, created_at, source, in_reply_to_screen_name, text) + VALUES ( + '" . $status->id . "', + '" . $this->id . "', + '" . $created_at . "', + '" . addslashes((string)$status->source) . "', + '" . addslashes((string)$status->in_reply_to_screen_name) . "', + '" . addslashes((string)$status->text) . "' + )"; + $wpdb->query($sql); + ++$tweet_count; + } + } else { + break; + } + } + //$sql = "ALTER TABLE $wp_grouptwitter ORDER BY `id`"; + //$wpdb->query($sql); + date_default_timezone_set($orig_tz); + return $tweet_count; + } +} +?> diff --git a/css/admin.css b/css/admin.css new file mode 100644 index 0000000..048bed3 --- /dev/null +++ b/css/admin.css @@ -0,0 +1,4 @@ +form.gt_newaccount div {width: 150px; float: left;} +form.gt_newaccount input.text {width: 140px; height: 2em;} +form.gt_newaccount input {height: 2em;} +form.gt_newaccount div label {display: block; width: 150px; color: #999; font-size: .8em;} \ No newline at end of file diff --git a/css/frontend.css b/css/frontend.css new file mode 100644 index 0000000..aa1a14b --- /dev/null +++ b/css/frontend.css @@ -0,0 +1,6 @@ +.twitterfeed {font-size: 1em; } +.twitterfeed li {margin-bottom: 2em; } +.gt_summary {font-size: 1.2em; } +.gt_summary span {color: #144989; font-weight: bold;} + +.navigation {margin: 1.5em 0; } \ No newline at end of file diff --git a/grouptwitter.php b/grouptwitter.php new file mode 100644 index 0000000..cb8b6de --- /dev/null +++ b/grouptwitter.php @@ -0,0 +1,219 @@ +prefix ."grouptwitter"; + $wp_grouptwitter_accounts = $wpdb->prefix ."grouptwitter_accounts"; + + require_once(dirname(__FILE__) . "/classes/class.twitter.php"); + require_once(dirname(__FILE__) . "/classes/class.grouptwitter_widget.php"); + + function gt_install() + { + global $wpdb; + global $gt_db_version; + global $wp_grouptwitter; + global $wp_grouptwitter_accounts; + + $table_name = $wp_grouptwitter; + $table2_name = $wp_grouptwitter_accounts; + if($wpdb->get_var("show tables like '$table_name'") != $table_name) + { + //our table + $sql = "CREATE TABLE " . $table_name . " ( + `id` BIGINT( 10 ) UNSIGNED NOT NULL , + `account_id` VARCHAR( 255 ) NOT NULL , + `created_at` DATETIME NOT NULL , + `source` VARCHAR( 255 ) NOT NULL , + `in_reply_to_screen_name` VARCHAR( 255 ) NOT NULL , + `text` VARCHAR( 255 ) NOT NULL , + PRIMARY KEY `id` ( `id` ), + KEY `account_id` (`account_id`) + ) ENGINE = MYISAM DEFAULT CHARSET = utf8"; + + $sql2 = "CREATE TABLE " . $table2_name . " ( + `id` BIGINT(10) NOT NULL , + `name` VARCHAR( 255 ) NOT NULL , + `profile_image_url` VARCHAR( 255 ) NOT NULL , + `last_update` datetime NOT NULL, + PRIMARY KEY ( `id` ) + ) ENGINE = MYISAM"; + + //need this to run the dbDelta to create table + require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); + dbDelta($sql); + dbDelta($sql2); + + //incase we upgrade DB in the future + add_option("gt_db_version", $gt_db_version); + } + } + + function gt_menu() + { + add_options_page('Group Twitter', 'Group Twitter', 8, 'grouptwitter', 'gt_options_page'); + } + + function gt_options_page() + { + require_once(dirname(__FILE__) . "/adminpages/options.php"); + } + + function gt_addAdminHeaderCode() + { + echo '' . "\n"; + } + + function gt_addFrontendHeaderCode() + { + echo '' . "\n"; + } + + function gt_linkify( $text ) { + $text = preg_replace( '/(?!<\S)(\w+:\/\/[^<>\s]+\w)(?!\S)/i', '$1', $text ); + $text = preg_replace( '/(?!<\S)#(\w+\w)(?!\S)/i', '#$1', $text ); + $text = preg_replace( '/(?!<\S)@(\w+\w)(?!\S)/i', '@$1', $text ); + return $text; + } + + function gt_getTweets($n = 5, $a = "all", $s = "", $p = 1, $details = false) + { + global $wpdb, $wp_grouptwitter, $wp_grouptwitter_accounts; + + $end = $p * $n; + $start = $end - $n; + + $sqlQuery = "SELECT SQL_CALC_FOUND_ROWS t.*, UNIX_TIMESTAMP(t.created_at) as created_at, a.name, a.profile_image_url FROM $wp_grouptwitter t LEFT JOIN $wp_grouptwitter_accounts a ON t.account_id = a.id WHERE 1=1 "; + if($a && $a != "all") + $sqlQuery .= "AND a.name = '$a' "; + if($s) + { + $terms = split(" ", $s); + foreach($terms as $term) + { + $term = trim($term); + $sqlQuery .= "AND t.text LIKE '%$term%' "; + } + } + + $sqlQuery .= " ORDER BY t.id DESC LIMIT $start, $n "; + + $tweets = $wpdb->get_results($sqlQuery); + + $totalrows = $wpdb->get_var("SELECT FOUND_ROWS() as found_rows"); + $end = min($end, $totalrows); + + //linkify + for($i = 0; $i < count($tweets); $i++) + { + $tweets[$i]->text = gt_linkify($tweets[$i]->text); + } + + if($details) + { + $temp->tweets = $tweets; + $temp->start = $start; + $temp->last = $end; + $temp->totalrows = $totalrows; + + return $temp; + } + else + return $tweets; + } + + function gt_displayDateTime($dt) + { + if(date("Y") == date("Y", $dt)) + return str_replace(" ", " ", date("g:iA M jS", $dt)); + else + return str_replace(" ", " ", date("g:iA M j, Y", $dt)); + } + + function gt_showTweets($n = 5, $a = "all", $s = "", $p = 1, $shownav = false, $class = "twitterfeed", $avatar = false) + { + if($shownav) + { + $results = gt_getTweets($n, $a, $s, $p, true); + $tweets = $results->tweets; + if($results->last) + { + ?> +Showing tweets =$results->start+1?> to =$results->last?> of =$results->totalrows?>.
+ +No tweets found.
+ +