web/wp-content/plugins/twitter-tools/classes/aktt_account.php
changeset 194 32102edaa81b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
       
     1 <?php 
       
     2 
       
     3 class AKTT_Account {
       
     4 	var $id = null; // This is the Social Service's user ID for the specified account
       
     5 	var $social_acct = null;
       
     6 	static $settings = array();
       
     7 	
       
     8 	public static function init() {
       
     9 		// Set our default configs
       
    10 		AKTT_Account::set_default_settings();
       
    11 	}
       
    12 	
       
    13 	public static function set_default_settings() {
       
    14 		// Set default configs
       
    15 		$settings = array(
       
    16 			'enabled' => array( // author to assign to new posts
       
    17 				'label' => __('Enabled', 'twitter-tools'),
       
    18 				'label_first' => false,
       
    19 				'value' => 0,
       
    20 				'type' 	=> 'int',
       
    21 			),
       
    22 			'create_posts' => array( // author to assign to new posts
       
    23 				'label' => __('Create posts for each tweet?', 'twitter-tools'),
       
    24 				'label_first' => false,
       
    25 				'value' => 0,
       
    26 				'type' 	=> 'int',
       
    27 			),
       
    28 			'post_author' => array( // author to assign to new posts
       
    29 				'label' => __('Post Author', 'twitter-tools'),
       
    30 				'label_first' => true,
       
    31 				'value' => 0,
       
    32 				'type' 	=> 'int',
       
    33 			),
       
    34 			'post_category' => array( // cats to add to posts created from this acct
       
    35 				'label' => __('Post Category', 'twitter-tools'),
       
    36 				'label_first' => true,
       
    37 				'value' => 0,
       
    38 				'type' 	=> 'is_cat',
       
    39 			),
       
    40 			'post_tags' => array( // tags to add to posts created from this acct
       
    41 				'label' => __('Post Tags', 'twitter-tools'),
       
    42 				'label_first' => true,
       
    43 				'value' => '',
       
    44 				'type' 	=> 'tags',
       
    45 			),
       
    46 			'exclude_reply_tweets' => array( // Exclude tweets that are a reply from creating their own blog posts?
       
    47 				'label' => __('Exclude reply tweets from post creation', 'twitter-tools'),
       
    48 				'label_first' => false,
       
    49 				'value' => 1,
       
    50 				'type' 	=> 'int',
       
    51 			),
       
    52 			'exclude_retweets' => array( // Exclude RTs from creating their own blog posts?
       
    53 				'label' => __('Exclude re-tweets from post creation', 'twitter-tools'),
       
    54 				'label_first' => false,
       
    55 				'value' => 1,
       
    56 				'type' 	=> 'int',
       
    57 			),
       
    58 			'blog_post_title' => array( // Structure of the blog post Title
       
    59 				'label' => __('Blog Post Title Prefix', 'twitter-tools'),
       
    60 				'label_first' => true,
       
    61 				'value' => '',
       
    62 				'type' 	=> 'no_html',
       
    63 			),
       
    64 		);
       
    65 		AKTT_Account::$settings = apply_filters('aktt_account_default_settings', $settings);
       
    66 	}
       
    67 	
       
    68 	/**
       
    69 	 * Safe constructor.  
       
    70 	 *
       
    71 	 * @param object $acct Social Twitter Account (stdObj)
       
    72 	 * @return AKTT_Account | false
       
    73 	 */
       
    74 	public static function load($acct = null) {
       
    75 		// Make sure we have the appropriate classes, etc. for the AKTT_Account object
       
    76 		if (
       
    77 			is_null($acct) ||
       
    78 			!is_object($acct) || // Ensure we have an account object
       
    79 			!is_a($acct, 'Social_Service_Twitter_Account') // Ensure we have a Social_Twitter object
       
    80 			) {
       
    81 			return false;
       
    82 		}
       
    83 		
       
    84 		// We passed the gauntlet, return a new object
       
    85 		return new AKTT_Account($acct);
       
    86 	}
       
    87 	
       
    88 	function __construct($acct = null) {
       
    89 		// Set our ID
       
    90 		$this->id = $acct->id();
       
    91 		
       
    92 		// Set the account (Social_Service_Twitter_Account)
       
    93 		$this->social_acct = $acct;
       
    94 		
       
    95 		// For convenience, set a reference to the service which has all the account methods
       
    96 		$this->service = &Social::instance()->service('twitter');
       
    97 		
       
    98 	}
       
    99 	
       
   100 	
       
   101 	/**
       
   102 	 * Get an option from the DB, and fall back to the default setting
       
   103 	 *
       
   104 	 * @param string $key 
       
   105 	 * @return mixed
       
   106 	 */
       
   107 	function option($key) {
       
   108 		$option = get_option('aktt_v3_accounts');
       
   109 		if (
       
   110 			!empty($option) 
       
   111 			&& is_array($option) 
       
   112 			&& isset($option[$this->id])
       
   113 			&& isset($option[$this->id]['settings'])
       
   114 			&& isset($option[$this->id]['settings'][$key])
       
   115 			) {
       
   116 			$val = $option[$this->id]['settings'][$key];
       
   117 		}
       
   118 		else {
       
   119 		// Get a default
       
   120 			$val = isset(AKTT_Account::$settings[$key]) ? AKTT_Account::$settings[$key]['value'] : null;
       
   121 		}
       
   122 		return apply_filters('aktt_account_option', $val, $key);
       
   123 	}
       
   124 	
       
   125 	
       
   126 	function download_tweets() {
       
   127 		// Use Social to download tweets for this account
       
   128 		$response = $this->service->request($this->social_acct, 'statuses/user_timeline', array(
       
   129 			'count' => apply_filters('aktt_account_api_download_count', 20), // default to twitter's default 
       
   130 			'include_entities' => 1, // include explicit hashtags and mentions
       
   131 			'include_rts' => 1, // include retweets
       
   132 		));
       
   133 		if ($response !== false) {
       
   134 			$content = $response->body();
       
   135 			if ($content->result == 'success') {
       
   136 				return $content->response;
       
   137 			}
       
   138 		}
       
   139 		return false;
       
   140 	}
       
   141 	
       
   142 	
       
   143 	/**
       
   144 	 * Saves the tweets passed in.
       
   145 	 *
       
   146 	 * @param array $tweets - safe tweets (do error checking before passing to this function)
       
   147 	 * @return int - number of tweets saved
       
   148 	 */
       
   149 	function save_tweets($tweets) {
       
   150 		global $wpdb;
       
   151 // strip out any tweets we already have
       
   152 		$tweet_guids = array();
       
   153 		foreach ($tweets as $tweet) {
       
   154 			$tweet_guids[] = AKTT_Tweet::guid_from_twid($tweet->id);
       
   155 		}
       
   156 
       
   157 		$existing_guids = $wpdb->get_col("
       
   158 			SELECT guid
       
   159 			FROM $wpdb->posts
       
   160 			WHERE guid IN ('".implode("','", $tweet_guids)."')
       
   161 			AND post_type = '".AKTT::$post_type."'
       
   162 		");
       
   163 		
       
   164 		// Set the args for any blog posts created
       
   165 		$post_tweet_args = array(
       
   166 			'post_author' => $this->option('post_author'),
       
   167 			'post_category' => $this->option('post_category'),
       
   168 			'post_tags' => $this->option('post_tags'),
       
   169 			'title_prefix' => $this->option('blog_post_title'),
       
   170 		);
       
   171 		
       
   172 // Save new tweets
       
   173 		foreach ($tweets as $tweet) {
       
   174 			if (in_array(AKTT_Tweet::guid_from_twid($tweet->id), $existing_guids)) {
       
   175 				continue;
       
   176 			}
       
   177 
       
   178 			// Start up a tweet object
       
   179 			$t = new AKTT_Tweet($tweet);
       
   180 
       
   181 			if (!($result = $t->add())) {
       
   182 				AKTT::log('There was an error saving a tweet. Tweet ID: '.$t->id);
       
   183 				continue;
       
   184 			}
       
   185 
       
   186 // Now conditionially create the associated blog post
       
   187 			if (
       
   188 				// If we are set to create blog posts
       
   189 				$this->option('create_posts') == 1
       
   190 				
       
   191 				// AND NOT we aren't supposed to do reply tweets and this is a reply
       
   192 				&& !($this->option('exclude_reply_tweets') && $t->is_reply())
       
   193 				
       
   194 				// AND NOT we aren't supposed to do re-tweets and this is a RT
       
   195 				&& !($this->option('exclude_retweets') && $t->is_retweet())
       
   196 				
       
   197 				// AND this tweet hasn't created a post yet
       
   198 				&& !$t->tweet_post_exists()
       
   199 				
       
   200 				// AND the tweet didn't come from a Social broadcast (ie, was originally a blog post)
       
   201 				&& !$t->was_broadcast()
       
   202 				){
       
   203 				AKTT::log('Creating a blog post for tweet ID: '.$t->id);
       
   204 				$t->create_blog_post($post_tweet_args);
       
   205 			}
       
   206 		}
       
   207 	}
       
   208 	
       
   209 }
       
   210 add_action('init', array('AKTT_Account', 'init'));