Plugin Directory

Changeset 382286


Ignore:
Timestamp:
05/08/2011 12:16:31 AM (15 years ago)
Author:
isharis
Message:

Made twitter count to use number_format to separate number with commas. Fixed a potential MySQL error.

Location:
twounter/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • twounter/trunk/changelog.txt

    r114121 r382286  
    33== Version 1.0.1 ==
    44
    5 1.  Twitter API is limited to 100 calls per hour. Twounter now caches the number of followers for
    6     5 minutes.
     51.  Twitter API is limited to 100 calls per hour. Twounter now caches the number of followers for 5 minutes.
     6
     7== Version 1.0.2 ==
     8
     91.  Added number_format to separate the number with commas.
     102.  Fixed a potential mysql bug.
  • twounter/trunk/readme.txt

    r114273 r382286  
    22Contributors: isharis
    33Tags: twitter, sidebar, plugin
    4 Requires at least: 2.7.1
    5 Tested up to: 2.7.1
    6 Stable tag: 1.0.1
     4Tested up to: 3.1.2
     5Stable tag: 1.0.2
    76
    87Twounter returns the number of followers of a twitter user in simple text format.
  • twounter/trunk/twounter.php

    r114273 r382286  
    22/*
    33 * Plugin Name: Twounter
    4  * Plugin URI: http://themesphere.com/twitter-counter-wordpress-plugin.html
     4 * Plugin URI: http://mharis.net/twitter-counter-wordpress-plugin/
    55 * Description: Twounter returns the number of followers of a twitter user in simple text format.
    6  * Version: 1.0.1
    7  * Author: Muhammad Haris
    8  * Author URI: http://twitter.com/mharis
     6 * Version: 1.0.2
     7 * Author: Muhammad Haris - <a href='http://twitter.com/mharis'>@mharis</a> on twitter
     8 * Author URI: http://mharis.net
    99 */
    1010
     
    1717
    1818function logger($log) {
    19   global $wpdb;
    20  
    21   $wpdb->query('INSERT into logger(log) VALUES("' . $log . '")');
     19     global $wpdb;
     20     $wpdb->query('INSERT into logger(log) VALUES("' . $log . '")');
    2221}
    2322
     
    2625 */
    2726function twounter_install() {
    28   global $wpdb;
    29  
    30   $table = $wpdb->prefix . 'twounter';
    31 
    32   $sql =
    33   'CREATE TABLE IF NOT EXISTS ' . $table . ' (
    34     `ID` bigint NOT NULL AUTO_INCREMENT PRIMARY KEY,
    35     `twounter_twitter_id` varchar(250) NOT NULL,
    36     `twounter_followers` bigint NOT NULL,
    37     `twounter_time` datetime NOT NULL default "0000-00-00 00:00:00",
    38     UNIQUE key (id),
    39     UNIQUE key (twounter_twitter_id)
    40   );';
    41  
    42   $wpdb->query($sql);
     27      global $wpdb;
     28     
     29      $table = $wpdb->prefix . 'twounter';
     30   
     31      $sql =
     32      'CREATE TABLE IF NOT EXISTS ' . $table . ' (
     33        `ID` bigint NOT NULL AUTO_INCREMENT PRIMARY KEY,
     34        `twounter_twitter_id` varchar(250) NOT NULL,
     35        `twounter_followers` bigint NOT NULL,
     36        `twounter_time` datetime NOT NULL default "0000-00-00 00:00:00",
     37        UNIQUE key (id),
     38        UNIQUE key (twounter_twitter_id)
     39      );';
     40     
     41      $wpdb->query($sql);
    4342}
    4443
     
    4746 */
    4847function twounter_uninstall() {
    49   global $wpdb;
    50  
    51   $table = $wpdb->prefix . 'twounter';
    52   $sql = 'DROP TABLE ' . $table;
    53   $wpdb->query($sql);
     48      global $wpdb;
     49     
     50      $table = $wpdb->prefix . 'twounter';
     51      $sql = 'DROP TABLE ' . $table;
     52      $wpdb->query($sql);
    5453}
    5554
     
    6160 */
    6261function twounter_followers($username) {
    63   global $wpdb;
    64  
    65   $sql = 'SELECT *
    66           FROM ' . $wpdb->prefix . 'twounter
    67           WHERE twounter_twitter_id = "' . $username . '"';
    68   $followers = $wpdb->get_results($sql);
    69   $now = strtotime(date('Y-m-d H:i:s'));
    70   $api_call = strtotime($followers[0]->twounter_time);
    71   $difference = $now - $api_call;
    72   $api_time_seconds = 18000;
    73  
    74   if(!$followers || $difference >= $api_time_seconds) {
    75     $api_page = 'http://twitter.com/users/show/' . $username;
    76     $xml = file_get_contents($api_page);
    77    
    78     $profile = new SimpleXMLElement($xml);
    79    
    80     $sql = 'INSERT into ' . $wpdb->prefix . 'twounter
    81             (twounter_twitter_id, twounter_followers, twounter_time)
    82             VALUES("' . $username . '", ' . $profile->followers_count . ', "' . date('Y-m-d H:i:s') . '")';
    83     $query = $wpdb->query($sql);
    84    
    85     if(!$query) {
    86       $sql = 'UPDATE ' . $wpdb->prefix . 'twounter
    87               SET twounter_followers = ' . $profile->followers_count . ',
    88               twounter_time = "' . date('Y-m-d H:i:s') . '"
    89               WHERE twounter_twitter_id = "' . $username . '"';
    90       $query = $wpdb->query($sql);
    91     }
    92   } else {
    93     $profile->followers_count = $followers[0]->twounter_followers;
    94   }
    95   return $profile->followers_count;
     62      global $wpdb;
     63     
     64      $sql = 'SELECT *
     65              FROM ' . $wpdb->prefix . 'twounter
     66              WHERE twounter_twitter_id = "' . $username . '"';
     67      $followers = $wpdb->get_results($sql);
     68      $now = strtotime(date('Y-m-d H:i:s'));
     69      $api_call = strtotime($followers[0]->twounter_time);
     70      $difference = $now - $api_call;
     71      $api_time_seconds = 18000;
     72     
     73      if(!$followers || $difference >= $api_time_seconds) {
     74        $api_page = 'http://twitter.com/users/show/' . $username;
     75        $xml = file_get_contents($api_page);
     76       
     77        $profile = new SimpleXMLElement($xml);
     78       
     79        $sql = 'INSERT into ' . $wpdb->prefix . 'twounter
     80                (twounter_twitter_id, twounter_followers, twounter_time)
     81                VALUES("' . $username . '", ' . $profile->followers_count . ', "' . date('Y-m-d H:i:s') . '")';
     82        @$query = $wpdb->query($sql);
     83       
     84        if(!$query) {
     85          $sql = 'UPDATE ' . $wpdb->prefix . 'twounter
     86                  SET twounter_followers = ' . $profile->followers_count . ',
     87                  twounter_time = "' . date('Y-m-d H:i:s') . '"
     88                  WHERE twounter_twitter_id = "' . $username . '"';
     89          $query = $wpdb->query($sql);
     90        }
     91      } else {
     92        $profile->followers_count = $followers[0]->twounter_followers;
     93      }
     94     
     95      return number_format($profile->followers_count);
    9696}
    9797
     
    103103 */
    104104function twounter($username) {
    105   $followers = twounter_followers($username);
    106  
    107   return $followers;
     105      $followers = twounter_followers($username);
     106     
     107      return $followers;
    108108}
    109109
     
    115115 */
    116116function twounter_formatting($content) {
    117   $find_twounter = preg_match_all('/\[twounter\](.*)\[\/twounter\]/i', $content, $matches);
    118  
    119   if(!empty($matches[1])) {
    120     $usernames = $matches[1];
    121     foreach($usernames as $username) {
    122       $patterns[] = '/\[twounter\]' . $username . '\[\/twounter\]/i';
    123       $replacements[] = twounter($username);
    124     }
    125    
    126     $content = preg_replace($patterns, $replacements, $content);
    127   }
    128  
    129   return $content;
     117      $find_twounter = preg_match_all('/\[twounter\](.*)\[\/twounter\]/i', $content, $matches);
     118     
     119      if(!empty($matches[1])) {
     120        $usernames = $matches[1];
     121        foreach($usernames as $username) {
     122          $patterns[] = '/\[twounter\]' . $username . '\[\/twounter\]/i';
     123          $replacements[] = twounter($username);
     124        }
     125       
     126        $content = preg_replace($patterns, $replacements, $content);
     127      }
     128     
     129      return $content;
    130130}
Note: See TracChangeset for help on using the changeset viewer.