<?php/** * Plugin Name: Site Rating * Plugin URI: http://www.yoursite.com * Version: 1.0 * Description: Rate websites * Author: Sean */define('STAR_IMAGE_WIDTH', 20);/** * Star rating class */class SiteRating { /** * The site's URL. * @var string */ var $siteURL; /** * The wpdb class object. * @var obj */ var $wpdb; /** * Class constructor * * @param obj $wpdb The wpdb class object * @param string $table_prefix The WP table prefix * @return void */ function SiteRating(&$wpdb, $table_prefix) { $this->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'"); return $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 = " <ul class='star-rating'> <li class='current-rating' id='$siteHash-container' style='width:{$width}px;'>Currently $siteRating/5 Stars.</li> <li><a href='$this->siteURL/index.php?rateSite=$linkHref&rating=1' onclick='return rateSite(\"$linkHref\", 1)' title='1 star out of 5' class='one-star'></a></li> <li><a href='$this->siteURL/index.php?rateSite=$linkHref&rating=2' onclick='return rateSite(\"$linkHref\", 2)' title='2 stars out of 5' class='two-stars'></a></li> <li><a href='$this->siteURL/index.php?rateSite=$linkHref&rating=3' onclick='return rateSite(\"$linkHref\", 3)' title='3 stars out of 5' class='three-stars'></a></li> <li><a href='$this->siteURL/index.php?rateSite=$linkHref&rating=4' onclick='return rateSite(\"$linkHref\", 4)' title='4 stars out of 5' class='four-stars'></a></li> <li><a href='$this->siteURL/index.php?rateSite=$linkHref&rating=5' onclick='return rateSite(\"$linkHref\", 5)' title='5 stars out of 5' class='five-stars'></a></li> </ul> "; 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() { ?> <script type="text/javascript"> var siteURL = '<?php echo $this->siteURL; ?>'; var starImageWidth = <?php echo STAR_IMAGE_WIDTH; ?>; </script> <link rel="stylesheet" type="text/css" href="<?php echo $this->siteURL; ?>/wp-content/plugins/<?php echo $this->pluginDirectory; ?>/ratingstyle.css" /> <script type="text/javascript" src="<?php echo $this->siteURL; ?>/wp-content/plugins/<?php echo $this->pluginDirectory; ?>/prototype.lite.js"></script> <script type="text/javascript" src="<?php echo $this->siteURL; ?>/wp-content/plugins/<?php echo $this->pluginDirectory; ?>/moo.ajax.js"></script> <script type="text/javascript" src="<?php echo $this->siteURL; ?>/wp-content/plugins/<?php echo $this->pluginDirectory; ?>/ratingstyle.js"></script> <?php } /** * Removes a trailing slash from a URL * * Removes a trailing slash from a URL. * * @param string $url The URL * @retrun string */ function trimURL($url) { if (substr($url, -1, 1) == '/') { $url = substr($url, 0, strlen($url) - 1); } return $url; } /** * Installs the plugin * * Method that creates the needed database tables for the plugin. Called when the * plugin is activated. * * @return void */ function install() { if($this->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'));?>