diff -r 2f6f6f7551ca -r 32102edaa81b web/wp-content/plugins/twitter-tools/classes/aktt_tweet.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/wp-content/plugins/twitter-tools/classes/aktt_tweet.php Mon Nov 19 18:26:13 2012 +0100 @@ -0,0 +1,638 @@ +populate_from_twitter_obj($data); + } + else { + // Assign the tweet ID to this object + $this->id = $data; + + // Flag to populate the object from the DB on construct + if ($from_db == true) { + $this->populate_from_db(); + } + } + } + + + /** + * Allows the object's properties to be populated from a post object in the DB + * + * @return void + */ + function populate_from_db() { + $post = $this->get_post(AKTT::$post_type); + + if (is_wp_error($post) || empty($post)) { + return false; + } + + $this->post = $post; + $this->raw_data = get_post_meta($this->post->ID, '_aktt_tweet_raw_data', true); + $this->data = json_decode($this->raw_data); + } + + + /** + * Populates the object from a twitter API response object + * + * @param object $tweet_obj + * @return void + */ + function populate_from_twitter_obj($tweet_obj) { + $this->data = $tweet_obj; + $this->raw_data = json_encode($tweet_obj); + $this->id = $tweet_obj->id_str; + } + + /** + * Accessor function for tweet id + * + * @return string|null + */ + public function id() { + return (isset($this->data) ? $this->data->id_str : null); + } + + /** + * Accessor function for tweet text shortened for post title + * + * @return string + */ + public function title() { + if (isset($this->data)) { + $title = trim(AKTT::substr($this->data->text, 0, 50)); + if (AKTT::strlen($this->data->text) > 50) { + $title = $title.'...'; + } + } + else { + $title = null; + } + return $title; + } + + /** + * Accessor function for tweet text + * + * @return string + */ + public function content() { + if (isset($this->data) && isset($this->data->text)) { + return $this->data->text; + } + if (isset($this->post) && isset($this->post->post_content)) { + return $this->post->post_content; + } + return null; + } + + /** + * Accessor function for tweet date/time + * + * @return string + */ + public function date() { + return (isset($this->data) ? $this->data->created_at : null); + } + + /** + * Accessor function for tweet author's username + * + * @return string + */ + public function username() { + return (isset($this->data) ? $this->data->user->screen_name : null); + } + + /** + * Accessor function for tweet reply-to username + * + * @return string + */ + public function reply_screen_name() { + return (isset($this->data) ? $this->data->in_reply_to_screen_name : null); + } + + /** + * Accessor function for tweet reply-to tweet id + * + * @return string + */ + public function reply_id() { + return (isset($this->data) ? $this->data->in_reply_to_status_id_str : null); + } + + /** + * Accessor function for tweet's hashtags + * + * @return string + */ + public function hashtags() { + return (isset($this->data) && isset($this->data->entities) ? $this->data->entities->hashtags : array()); + } + + /** + * Accessor function for tweet's mentions + * + * @return string + */ + public function mentions() { + return (isset($this->data) && isset($this->data->entities) ? $this->data->entities->user_mentions : array()); + } + + /** + * Accessor function for tweet's URLS + * + * @return string + */ + public function urls() { + return (isset($this->data) && isset($this->data->entities) ? $this->data->entities->urls : array()); + } + + /** + * Accessor function for tweet's status URL on Twitter + * + * @return string + */ + public function status_url() { + if ($username = $this->username() && $id = $this->id()) { + return AKTT::status_url($username, $id); + } + return null; + } + + /** + * Takes the twitter date format and gets a timestamp from it + * + * @param string $date - "Fri Aug 05 20:33:38 +0000 2011" + * @return int - timestamp + */ + static function twdate_to_time($date) { + $parts = explode(' ', $date); + return strtotime($parts[1].' '.$parts[2].', '.$parts[5].' '.$parts[3]); + } + + + /** + * See if the tweet ID matches any tweet ID post meta value + * + * @return bool + */ + function exists() { + $test = $this->get_post(AKTT::$post_type); + return (bool) (count($test) > 0); + } + + function exists_by_guid() { + global $wpdb; + $guid = $this->guid(); + if (empty($guid)) { + return false; + } + $count = $wpdb->get_var($wpdb->prepare(" + SELECT COUNT(ID) + FROM $wpdb->posts + WHERE guid = %s + ", $guid)); + return (bool) $count; + } + + /** + * Checks the posts to see if this tweet has been attached to + * any of them. + * + * @return bool + */ + function tweet_post_exists() { + $posts = $this->get_post('post'); + return (bool) (count($posts) > 0); + } + + + /** + * Generate a GUID for WP post based on tweet ID. + * + * @return mixed + */ + static function guid_from_twid($tweet_id = null) { + return (empty($tweet_id) ? false : 'http://twitter-'.$tweet_id); + } + + + /** + * Generate a GUID for WP post based on this objects' tweet ID. + * + * @uses guid_from_twid + * + * @return mixed + */ + function guid() { + return $this->guid_from_twid($this->id()); + } + + + /** + * Grabs the post from the DB + * + * @uses get_posts + * + * @return obj|false + */ + function get_post($post_type = null) { + if (isset($this->post)) { + $this->post_id = $this->post->ID; + return $this->post; + } + if (is_null($post_type)) { + $post_type = AKTT::$post_type; + } +// TODO (future) - search by GUID instead? + $posts = get_posts(array( + 'post_type' => $post_type, + 'meta_key' => '_aktt_tweet_id', + 'meta_value' => $this->id, + )); + if (!is_array($posts)) { + return false; + } + else { + $this->post = array_shift($posts); + $this->post_id = $this->post->ID; + return $this->post; + } + } + + + /** + * Twitter data changed - users still expect anything starting with @ is a reply + * + * @return bool + */ + function is_reply() { + return (bool) (AKTT::substr($this->content(), 0, 1) == '@' || !empty($this->data->in_reply_to_screen_name)); + } + + + /** + * Is this a retweet? + * + * @return bool + */ + function is_retweet() { + return (bool) (AKTT::substr($this->content(), 0, 2) == 'RT' || !empty($this->data->retweeted_status)); + } + + + /** + * Look up whether this tweet came from a broadcast + * + * @return bool + */ + function was_broadcast() { + $was_broadcast = false; + if (isset($this->data) && !empty($this->data->source)) { + $was_broadcast = (bool) (strpos($this->data->source, 'sopresto.mailchimp.com') !== false); + } + else { + $was_broadcast = (bool) (strpos($this->content(), home_url()) !== false); + } + return $was_broadcast; + } + + function link_entities($defer_to_anywhere = true) { + $entities = array(); +// mentions + $anywhere = Social::option('twitter_anywhere_api_key'); + if (!$defer_to_anywhere || empty($anywhere) || is_feed()) { + foreach ($this->mentions() as $entity) { + $entities['start_'.str_pad($entity->indices[0], 5, '0', STR_PAD_LEFT)] = array( + 'find' => $entity->screen_name, + 'replace' => AKTT::profile_link($entity->screen_name), + 'start' => $entity->indices[0], + 'end' => $entity->indices[1], + ); + } + } +// hashtags + foreach ($this->hashtags() as $entity) { + $entities['start_'.str_pad($entity->indices[0], 5, '0', STR_PAD_LEFT)] = array( + 'find' => $entity->text, + 'replace' => AKTT::hashtag_link($entity->text), + 'start' => $entity->indices[0], + 'end' => $entity->indices[1], + ); + } +// URLs + foreach ($this->urls() as $entity) { + $entities['start_'.str_pad($entity->indices[0], 5, '0', STR_PAD_LEFT)] = array( + 'find' => $entity->url, + 'replace' => ''.esc_html($entity->display_url).'', + 'start' => $entity->indices[0], + 'end' => $entity->indices[1], + ); + } + ksort($entities); + $str = $this->content(); + $diff = 0; + foreach ($entities as $entity) { + $start = $entity['start'] + $diff; + $end = $entity['end'] + $diff; +// $log = array(); +// $log[] = 'diff: '.$diff; +// $log[] = 'entity start: '.$entity['start']; +// $log[] = 'entity start chars: '.AKTT::substr($this->content(), $entity['start'], 3); +// $log[] = 'diff start: '.$start; +// $log[] = 'diff start chars: '.AKTT::substr($str, $start, 3); +// $log[] = 'entity end: '.$entity['end']; +// $log[] = 'diff end: '.$end; +// $log[] = 'find len: '.AKTT::strlen($entity['find']); +// $log[] = 'find: '.htmlspecialchars($entity['find']); +// $log[] = 'replace len: '.AKTT::strlen($entity['replace']); +// $log[] = 'replace: '.htmlspecialchars($entity['replace']); +// echo '
'.implode('
', $log).'