Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 198 lines (173 sloc) 5.721 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
<?php

class AKTT_Account {
var $id = null; // This is the Social Service's user ID for the specified account
var $social_acct = null;
static $settings = array();

public static function init() {
// Set our default configs
AKTT_Account::set_default_settings();
}

public static function set_default_settings() {
// Set default configs
AKTT_Account::$settings = array(
'enabled' => array( // author to assign to new posts
'label' => __('Enabled', 'twitter-tools'),
'label_first' => false,
'value' => 0,
'type' => 'int',
),
'create_posts' => array( // author to assign to new posts
'label' => __('Create posts for each tweet?', 'twitter-tools'),
'label_first' => false,
'value' => 0,
'type' => 'int',
),
'post_author' => array( // author to assign to new posts
'label' => __('Post Author', 'twitter-tools'),
'label_first' => true,
'value' => 0,
'type' => 'int',
),
'post_category' => array( // cats to add to posts created from this acct
'label' => __('Post Category', 'twitter-tools'),
'label_first' => true,
'value' => 0,
'type' => 'is_cat',
),
'post_tags' => array( // tags to add to posts created from this acct
'label' => __('Post Tags', 'twitter-tools'),
'label_first' => true,
'value' => '',
'type' => 'tags',
),
'exclude_reply_tweets' => array( // Exclude tweets that are a reply from creating their own blog posts?
'label' => __('Exclude reply tweets from post creation', 'twitter-tools'),
'label_first' => false,
'value' => 1,
'type' => 'int',
),
'blog_post_title' => array( // Structure of the blog post Title
'label' => __('Blog Post Title Prefix', 'twitter-tools'),
'label_first' => true,
'value' => '',
'type' => 'no_html',
),
);
}

/**
* Safe constructor.
*
* @param object $acct Social Twitter Account (stdObj)
* @return AKTT_Account | false
*/
public static function load($acct = null) {
// Make sure we have the appropriate classes, etc. for the AKTT_Account object
if (
is_null($acct)
|| !is_object($acct) // Ensure we have an account object
|| !is_a($acct, 'Social_Service_Twitter_Account') // Ensure we have a Social_Twitter object
) {
return false;
}

// We passed the gauntlet, return a new object
return new AKTT_Account($acct);
}

function __construct($acct = null) {
// Set our ID
$this->id = $acct->id();

// Set the account (Social_Service_Twitter_Account)
$this->social_acct = $acct;

// For convenience, set a reference to the service which has all the account methods
$this->service = &Social::instance()->service('twitter');

}


/**
* Get an option from the DB, and fall back to the default setting
*
* @param string $key
* @return mixed
*/
function option($key) {
$option = get_option('aktt_v3_accounts');
if (
!empty($option)
&& is_array($option)
&& isset($option[$this->id])
&& isset($option[$this->id]['settings'])
&& isset($option[$this->id]['settings'][$key])
) {
$val = $option[$this->id]['settings'][$key];
}
else {
// Get a default
$val = isset(AKTT_Account::$settings[$key]) ? AKTT_Account::$settings[$key]['value'] : null;
}
return apply_filters('aktt_account_option', $val, $key);
}


function download_tweets() {
// Use Social to download tweets for this account
$response = $this->service->request($this->social_acct, 'statuses/user_timeline', array(
'count' => apply_filters('aktt_account_api_download_count', 20), // default to twitter's default
'include_entities' => 1, // include explicit hashtags and mentions
'include_rts' => 1, // include retweets
));
$content = $response->body();
if ($content->result == 'success') {
return $content->response;
}
return false;
}


/**
* Saves the tweets passed in.
*
* @param array $tweets - safe tweets (do error checking before passing to this function)
* @return int - number of tweets saved
*/
function save_tweets($tweets) {
global $wpdb;
// strip out any tweets we already have
$tweet_guids = array();
foreach ($tweets as $tweet) {
$tweet_guids[] = AKTT_Tweet::guid_from_twid($tweet->id);
}

$existing_guids = $wpdb->get_col("
SELECT guid
FROM $wpdb->posts
WHERE guid IN ('".implode("','", $tweet_guids)."')
AND post_type = '".AKTT::$post_type."'
");

// Set the args for any blog posts created
$post_tweet_args = array(
'post_author' => $this->option('post_author'),
'post_category' => $this->option('post_category'),
'post_tags' => $this->option('post_tags'),
'title_prefix' => $this->option('blog_post_title_prefix'),
);

// Save new tweets
foreach ($tweets as $tweet) {
if (in_array(AKTT_Tweet::guid_from_twid($tweet->id), $existing_guids)) {
continue;
}

// Start up a tweet object
$t = new AKTT_Tweet($tweet);

if (!($result = $t->add())) {
AKTT::log('There was an error saving a tweet. Tweet ID: '.$t->id);
continue;
}

// Now conditionially create the associated blog post
if (
// If we are set to create blog posts
$this->option('create_posts') == 1

// AND NOT we aren't supposed to do reply tweets and this is a reply
&& !($this->option('exclude_reply_tweets') && $t->is_reply())

// AND this tweet hasn't created a post yet
&& !$t->tweet_post_exists()

// AND the tweet didn't come from a Social broadcast (ie, was originally a blog post)
&& !$t->was_broadcast()
){
AKTT::log('Creating a blog post for tweet ID: '.$t->id);
$t->create_blog_post($post_tweet_args);
}
}
}

}
add_action('init', array('AKTT_Account', 'init'));
Something went wrong with that request. Please try again.