wpdb = & $wpdb; $this->wpdb->site_ratings = $table_prefix . 'site_ratings'; $this->siteURL = $this->trimURL(get_bloginfo('url')); $this->pluginDirectory = dirname(plugin_basename(__FILE__)); add_action('wp_head', array(&$this, 'wpHead')); add_action('activate_' . $this->pluginDirectory . '/site_rating.php', array(&$this, 'install')); add_action('init', array(&$this, 'init')); } /** * Init method * * Method that catches votes being passed in the URL, either normally, or * passed by the Ajax methods. * * @return void */ function init() { if (!empty($_GET['rateSite']) && !empty($_GET['rating'])) { $siteHash = $_GET['rateSite']; $rating = $_GET['rating']; if ($siteHash == '0') { return; } $siteHash = addslashes($siteHash); $rating = (int) $rating; $this->castVote($siteHash, $rating); return; } } /** * Cast a vote for a site * * Takes the site hash, and the rating, and if the user hasn't already voted for * the site, cast the vote for the site. * * @param string $siteHash The site's hash * @param int $rating The rating being cast * @return void */ function castVote($siteHash, $rating) { if ($this->hasVoted($siteHash)) { return false; } $ipAddress = $_SERVER['REMOTE_ADDR']; return $this->wpdb->query("INSERT INTO {$this->wpdb->site_ratings} VALUES(null, NOW(), '$ipAddress', '$siteHash', $rating)"); } /** * Shows the stars for a site * * Echos the number of stars for a website. That will also include the onClick events for * the Ajax methods. * * @param string $siteURL The site's URL * @return void */ function showRating($siteURL) { $siteHash = md5($this->cleanURL($siteURL)); $siteRating = $this->getSiteRating($siteHash); if (empty($siteRating)) { $siteRating = 0; } $starHTML = $this->getStars($siteRating, $siteHash); echo $starHTML; } function showNumVotes($siteURL) { $siteHash = md5($this->cleanURL($siteURL)); $numVotes = $this->wpdb->get_var("SELECT COUNT(id) FROM {$this->wpdb->site_ratings} WHERE site_hash = '$siteHash'"); echo $numVotes; } /** * Returns star HTML for a site * * Returns the star HTML for a site. * * @param string $siteRating The rating the site has * @param string $siteHash The site's hash * @return string */ function getStars($siteRating, $siteHash) { $linkHref = $siteHash; if ($this->hasVoted($siteHash)) { $linkHref = '0'; } $siteRating = $this->roundVote($siteRating); $width = $siteRating * STAR_IMAGE_WIDTH; $starHTML = " "; return $starHTML; } /** * Returns whether a user has voted already * * Returns FALSE if the user hasn't already voted for the site, or TRUE if * the user has. * * @param string $siteHash The site's hash * @return bool */ function hasVoted($siteHash) { $ipAddress = $_SERVER['REMOTE_ADDR']; return $this->wpdb->get_var("SELECT id FROM {$this->wpdb->site_ratings} WHERE site_hash = '$siteHash' AND ip = '$ipAddress' LIMIT 1"); } /** * Returns the rating for a site * * Returns the rating for a site. * * @return float */ function getSiteRating($siteHash) { $rating = $this->wpdb->get_var("SELECT AVG(rating) FROM {$this->wpdb->site_ratings} WHERE site_hash = '$siteHash'"); if (empty($rating)) { $rating = 0; } return $this->roundVote($rating); } /** * Rounds a float to the nearest .5 * * Rounds a float to the nearest .5 * * @return float */ function roundVote($vote) { return round(($vote * 2), 0) / 2; } /** * Cleans up a site URL * * Method removes everything except for the the host from a URL. This is * to give a little breathing room in how a user enteres a URL. * * @param string $siteURL The URL * @return string */ function cleanURL($siteURL) { if (stristr($siteURL, 'http') === false) { $siteURL = 'http://' . $siteURL; } $parts = parse_url($siteURL); if (empty($parts['host'])) { if (empty($parts['path'])) { return $siteURL; } else { return $parts['path']; } } return $parts['host']; } /** * Inserts HTML into the document's head * * Inserts the SCRIPT and LINK tags needed by the plugin, into the blogs * head. * * @return void */ function wpHead() { ?> wpdb->get_var("SHOW TABLES LIKE '{$this->wpdb->site_ratings}'") != $this->wpdb->site_ratings) { $sql = "CREATE TABLE `{$this->wpdb->site_ratings}` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `time` DATETIME NOT NULL , `ip` CHAR( 15 ) NOT NULL , `site_hash` VARCHAR( 32 ) NOT NULL , `rating` FLOAT NOT NULL , INDEX ( `ip` , `site_hash` ) ) TYPE = MYISAM ;"; require_once(ABSPATH . 'wp-admin/upgrade-functions.php'); dbDelta($sql); } } } $siteRating = & new SiteRating($wpdb, $table_prefix); add_action('site_rating', array(&$siteRating, 'showRating')); add_action('show_num_votes', array(&$siteRating, 'showNumVotes')); ?>