Skip to content

Commit

Permalink
First commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Apr 29, 2012
0 parents commit f486133
Show file tree
Hide file tree
Showing 9 changed files with 612 additions and 0 deletions.
132 changes: 132 additions & 0 deletions adminpages/options.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php
set_time_limit(360);

global $wpdb, $wp_grouptwitter, $wp_grouptwitter_accounts, $gt_secretkey;

//are we adding an account?
$tname = trim($_REQUEST['tname']);
$taccount = trim($_REQUEST['taccount']);
if($tname && $taccount)
{
$sql = "INSERT INTO $wp_grouptwitter_accounts (id, name) VALUES('$taccount', '$tname')";
if($wpdb->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 "<hr />";
}

if($msg)
{
?>
<div id="message" class="<?php if($msg > 0) echo "updated fade"; else echo "error"; ?>"><p><?=$msgt?></p></div>
<?php
}
?>
<div class="wrap nosub">
<div id="icon-options-general" class="icon32"></div>
<h2>Group Twitter Options</h2>
<p>Use this form to add and manage the Twitter accounts you would like to include in the database.</p>
<form class="gt_newaccount" action="options-general.php?page=grouptwitter" method="post">
<div>
<label>Twitter Username</label>
<input class="text" type="text" id="tname" name="tname" value="" />
</div>
<div>
<label>Account Number</label>
<input class="text" type="text" id="taccount" name="taccount" onfocus="if(this.value=='') getTwitterAccountNumber($F('tname'));" value="" />
</div>
<div>
<label>&nbsp;</label>
<input type="submit" value="Add Account" />
</div>
</form>
<span id="accountnumber" style="display: none;"></span>

<div class="clear"></div><br />
<h3>Accounts</h3>
<ul>
<?php
//get the accounts
$gtaccounts = $wpdb->get_results("SELECT * FROM $wp_grouptwitter_accounts ORDER BY name");

foreach($gtaccounts as $gta)
{
?>
<li><a target="_blank" href="http://twitter.com/<?=$gta->name?>"><?=$gta->name?></a> [<a href="javascript:askfirst('Are you sure you would like to remove the <?=$gta->name?> account from the DB?', 'options-general.php?page=grouptwitter&delete=<?=$gta->id?>');">X</a>]</li>
<?php
}
?>
</ul>

<div class="clear"></div><br />
<h3>Tweets</h3>
<?php
$ntweets = $wpdb->get_var("SELECT COUNT(*) FROM $wp_grouptwitter");
?>
<p>There are <strong><?=$ntweets?></strong> tweet(s) in the database now. Click here to <a href="options-general.php?page=grouptwitter&update=1">refresh the tweet database</a>.</p>

<p>Add this script to a cronjob (run no more than every 15 minutes) to update your cache automatically:</p>
<code>/usr/bin/curl -d mypp_cmd=status <?=get_bloginfo("home")?>/wp-content/plugins/grouptwitter/services/updatecache.php?key=<?=$gt_secretkey?></code>
</div>
81 changes: 81 additions & 0 deletions classes/class.grouptwitter_widget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* Adds Foo_Widget widget.
*/
class GroupTwitter_Widget extends WP_Widget {

/**
* Register widget with WordPress.
*/
public function __construct() {
parent::__construct(
'grouptwitter_widget', // Base ID
'GroupTwitter_Widget', // Name
array( 'description' => __( 'GroupTwitter', 'text_domain' ), ) // Args
);
}

/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget( $args, $instance ) {
extract( $args );
$title = apply_filters( 'widget_title', $instance['title'] );

echo $before_widget;
if ( ! empty( $title ) )
echo $before_title . $title . $after_title;

echo gt_showTweets();

echo $after_widget;
}

/**
* Sanitize widget form values as they are saved.
*
* @see WP_Widget::update()
*
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
*
* @return array Updated safe values to be saved.
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = strip_tags( $new_instance['title'] );

return $instance;
}

/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'New title', 'text_domain' );
}
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<?php
}

} // class Foo_Widget

// register Foo_Widget widget
add_action( 'widgets_init', create_function( '', 'register_widget( "grouptwitter_widget" );' ) );
79 changes: 79 additions & 0 deletions classes/class.twitter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
class Twitter {
public function __construct($twitter_id) {
global $wpdb, $wp_grouptwitter_accounts;
$this->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;
}
}
?>
4 changes: 4 additions & 0 deletions css/admin.css
Original file line number Diff line number Diff line change
@@ -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;}
6 changes: 6 additions & 0 deletions css/frontend.css
Original file line number Diff line number Diff line change
@@ -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; }
Loading

0 comments on commit f486133

Please sign in to comment.