This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

code cleanup

  • Loading branch information...
benbalter committed Jan 29, 2012
1 parent 80b7fed commit 9a929f36786a13dc4420e5e5955b3ac51e29dbd8
Showing with 958 additions and 196 deletions.
  1. +32 −19 includes/admin.php
  2. +70 −57 includes/calls.php
  3. +694 −0 license.html
  4. +162 −120 twitter-mentions-as-comments.php
View
@@ -1,34 +1,41 @@
<?php
/**
* Functions for admin UI
+ * @author Benjamin J. Balter <ben@balter.com>
* @package Twitter_Mentions_As_Comments
*/
class Twitter_Mentions_As_Comments_Admin {
private $parent;
-
+
+ /**
+ * Register Hooks with WordPress API
+ * @param class $parent (reference) the parent class
+ */
function __construct( &$parent ) {
-
+
if ( !is_admin() )
return;
$this->parent = &$parent;
-
+
add_action( 'admin_menu', array( &$this, 'options_menu_init' ) );
-
+
add_action( 'tmac_enqueue_init', array( &$this, 'enqueue_init' ) );
-
+
add_filter( 'tmac_options_validate', array( &$this, 'options_validate' ) );
-
+
}
-
+
+
/**
* Register enqueue data
*/
function enqueue_init() {
$this->parent->enqueue->data = array ( 'hide_manual_cron_details' => !( $this->parent->options->manual_cron ) );
}
-
+
+
/**
* Creates the options sub-panel
* @since .1a
@@ -37,33 +44,39 @@ function options() {
$mentions = false;
$options = &$this->parent->options;
-
+
if ( isset( $_GET['force_refresh'] ) && $_GET['force_refresh'] == true )
- $mentions = $this->parent->mentions_check();
-
+ $mentions = $this->parent->mentions_check();
+
$this->parent->template->options( compact( 'mentions', 'options' ) );
-
+
}
-
+
+
/**
* Sanitize options
+ * @param unknown $options
+ * @return unknown
*/
function options_validate( $options ) {
-
+
$options['posts_per_check'] = (int) $options['posts_per_check'];
-
+
$bools = array( 'hide-donate', 'RTs', 'manual_cron' );
foreach ( $bools as $bool )
$options[ $bool ] = ( isset( $options[ $bool ] ) ) ? (bool) $options[ $bool ]: false;
-
+
return $options;
-
+
}
-
+
+
/**
* Register menu
*/
function options_menu_init() {
- add_options_page( 'Twitter Mentions as Comments Options', 'Twitter -> Comments', 'manage_options', 'tmac_options', array( &$this, 'options' ) );
+ add_options_page( 'Twitter Mentions as Comments Options', 'Twitter -> Comments', 'manage_options', 'tmac_options', array( &$this, 'options' ) );
}
+
+
}
View
@@ -1,161 +1,174 @@
<?php
/**
* Makes calls to the Twitter API (or pulls from local cache)
+ * @author Benjamin J. Balter <ben@balter.com>
* @package Twitter_Mentions_As_Comments
*/
class Twitter_Mentions_As_Comments_Calls {
-
+
private $parent;
public $count;
-
+
+ /**
+ * Store parent on construct
+ * @param class $parent (reference) the parent class
+ */
function __construct( &$parent ) {
$this->parent = &$parent;
-
+
}
-
+
+
/**
* Calls the public twitter API and retrieves information on a given user
+ * @since .1
* @param string $handle handle of twitter user
* @returns array assoc. array of info returned
- * @since .1
*/
function query_twitter( $handle ) {
-
+
//increment API counter
$this->count++;
-
+
//if we are over the limit, kick
if ( $this->count > $this->options->api_call_limit ) {
-
+
//if we already sent an e-mail this go around, don't send again
global $tmac_api_limit_msg_sent;
- if ( $tmac_api_limit_msg_sent )
- return false;
-
+ if ( $tmac_api_limit_msg_sent )
+ return false;
+
$tmac_api_limit_msg_sent = true;
-
+
//e-mail the admin to tell them we've hit the API limit
- wp_mail(
+ wp_mail(
get_settings('admin_email'),
- 'Twitter Mentions as Comments API Limit Reached',
+ 'Twitter Mentions as Comments API Limit Reached',
'The WordPress Twitter Mentions as Comments Plugin has reached its API limit. You may want to consider checking less frequently.'
);
-
+
return false;
-
+
}
-
+
//build the URL
$url = 'http://api.twitter.com/1/users/show/'. $handle .'.json';
-
+
$this->parent->api->apply_filters( 'query_url', $url, $handle );
-
+
//make the call
$data = json_decode( wp_remote_retrieve_body( wp_remote_get( $url ) ) );
-
+
return $data;
-
+
}
-
+
+
/**
* Checks for previous tweet-commments by the author and tries to retrieve their cached real name
+ * @since .2
* @param string $twitterID handle of twitter user
* @returns string their real name
- * @since .2
*/
function get_author_name( $twitterID ) {
-
+
global $wpdb;
-
+
if ( $name = $this->parent->cache->get( $twitterID . '_name' ) )
return $name;
-
+
//Check to see if twitter user has previously commented, if so just grab their name
$name = $wpdb->get_var( $wpdb->prepare( "SELECT comment_author FROM $wpdb->comments WHERE comment_author_email = %s and comment_approved = '1' LIMIT 1", $twitterID . '@twitter.com' ) );
-
+
//if they do not previosly have a comment, or that comment doesn't have a real name, call the Twitter API
if ( empty( $name ) || substr( $name, 0, 1 ) == '@' ) {
-
+
//Query the API
$data = $this->query_twitter( $twitterID );
-
+
//If we hit the API limit, kick
if ( !$data )
return false;
-
+
$name = $data->name;
-
+
}
-
+
//Because our query will return the name in the form of REAL NAME (@Handle), split the string at "(@"
$name = substr( $name, strrpos( $name, '(@' ) );
-
+
$name = $this->parent->api->apply_filters( 'author_name', $name, $twitterID );
-
+
$this->parent->cache->set( $twitterID . '_name', $name );
-
+
return $name;
-
+
}
+
/**
* Retrieves twitter profile image given a twitter username, stores in comment meta
* @param string twitterID twitter handle to lookup
* @param int comment_id ID of comment to store meta on (for caching)
* @since .1a
+ * @param unknown $twitterID
+ * @param unknown $comment_id
* @returns string url of profile image
*/
function get_profile_image( $twitterID, $comment_id) {
-
+
if ( $image = $this->parent->cache->get( $twitterID . '_profile_image' ) )
return $image;
-
+
//Check to see if we already have the image stored in comment meta
$image = get_comment_meta($comment_id, 'tmac_image', true);
-
+
//If we don't already have the immage, call the twitter API
if (!$image) {
-
+
$data = $this->query_twitter( $twitterID );
$image = $data->profile_image_url;
-
+
//Cache the image URL
add_comment_meta($comment_id, 'tmac_image', $image, true);
-
+
}
-
+
$image = $this->parent->api->apply_filters( 'user_image', $image, $twitterID, $comment_id );
-
+
$this->parent->cache->set( $twitterID . '_profile_image', $image );
-
+
return $image;
}
-
+
+
/**
* Calls the Twitter API and gets mentions for a given post
* @parama int postID ID of current post
- * @returns array array of tweets mentioning current page
* @since .1a
* @todo multiple calls for multiple pages of results (e.g., > 100)
+ * @param unknown $postID
+ * @returns array array of tweets mentioning current page
*/
function get_mentions( $postID ) {
-
+
//Retrive last ID checked for on this post so we don't re-add a comment already added
$lastID = $this->parent->get_lastID( $postID );
-
+
//Build URL, verify that $lastID is a string and not scientific notation, see http://jetlogs.org/2008/02/05/php-problems-with-big-integers-and-scientific-notation/
- $url = 'http://search.twitter.com/search.json?rpp=100&since_id=' . $lastID . '&q=' . urlencode( get_permalink( $postID ) ) . '%20OR%20' . urlencode( get_bloginfo( 'wpurl' ) . '/?p=' . $postID );
-
+ $url = 'http://search.twitter.com/search.json?rpp=100&since_id=' . $lastID . '&q=' . urlencode( get_permalink( $postID ) ) . '%20OR%20' . urlencode( get_bloginfo( 'wpurl' ) . '/?p=' . $postID );
+
$url = $this->parent->api->apply_filters( 'query_url', $url, $postID );
-
+
//make the API call and pass it back
$data = json_decode( $response = wp_remote_retrieve_body( wp_remote_get( $url ) ) );
return $this->parent->api->apply_filters( 'query_response', $data, $postID );
-
+
}
-
+
+
/**
* Resets internal API counter every hour
*
@@ -167,12 +180,12 @@ function get_mentions( $postID ) {
* @todo query the API for our actual limit
*/
function reset_count() {
-
+
$this->count = 0;
$this->parent->options->api_call_counter = 0;
$this->parent->api->do_action( 'api_counter_reset' );
-
+
}
-
+
}
Oops, something went wrong.

0 comments on commit 9a929f3

Please sign in to comment.