Skip to content

@kastner /README.md
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

High Scores Wordpress Plugin

This is a plugin for the soon-to-be-released iPhone game "Binary Game".

Usage

Install as a normal wordpress plugin and activate. This plugin provides both a classic plugin and a widget for your sidebar.

Customizing

Widget

The Widget customization is done in your Widget admin screen. You are giving choice of period (year/month/week/day), mode (challenge/speed) and # of scores to show. Here's a screenshot:

admin panel

Plugin

To use the plugin, you include the magic comment <!--highscores --> in any page or post. That uses the defaults of:

  • 10 scores
  • Speedmode
  • This week
  • <li>'s wrapping each line
  • <strong class="name"> wrapping the name
  • <span class="score"> wrapping the score

All of those are customizable via the magic comment. Here's a more complex example:

<!--highscores for|2009-02-13 period|day mode|speed before_line|&lt;tr+class="score-row"&gt; after_line|&lt;/tr&gt; before_score|&lt;td+class="score"&gt; after_score|&lt;/td&gt; before_name|&lt;td+class="name"&gt; after_name|&lt;/td&gt;-->

(in the magic comment, <'s must be written as &lt; and spaces replaced with +)

You can use the enclosed plugin_helper.php to generate the magic comment for you.

<?php
/*
Plugin Name: highscores plugin and wiget
Plugin Script: highscores.php
Plugin URI: http://b1narygame.com/highscores
Description: Plugin to display high scores for the b1nary game
Version: 0.2.3
Author: Brian Woolley and Erik Kastner
Author URI: http://metaatem.net
*/
function highscores_filter($content) {
if (preg_match_all("/<!--\s*highscores\s+(.+?)-->/", $content, $tag, PREG_SET_ORDER)) {
foreach($tag as $t) {
$options = hs_defaults();
preg_match_all("/([^\s\|]+)\|(\S+)/", $t[1], $attrs, PREG_SET_ORDER);
if (is_array($attrs)) {
foreach ($attrs as $opts) {
$key = $opts[1];
$value = $opts[2];
$value = html_entity_decode($value);
$value = str_replace("+", " ", $value);
$options[$key] = $value;
}
}
$out = hs_scores_for($options["mode"], $options["period"], $options["limit"], $options["unique"], $options["for"], $options);
$content = str_replace($t[0], $out, $content);
}
}
return $content;
}
function hs_default_options() {
return array("before_line" => "<li class='high-score'>",
"after_line" => "</li>",
"before_name" => "<strong class='name'>",
"after_name" => "</strong>",
"before_score" => "<span class='score'>",
"after_score" => "</span>");
}
function hs_defaults() {
$o = array("limit" => 10,
"period" => "week",
"mode" => "speed",
"unique" => "true",
"for" => "NOW()");
$o2 = hs_default_options();
return $o + $o2;
}
function hs_scores_for($mode, $period, $limit=10, $unique="true", $for="NOW()", $opts="") {
global $wpdb;
$output = "";
if ($for != "NOW()") { $for = '"' . $for . '"'; }
if (!is_array($opts)) { $opts = hs_default_options(); }
$table = "";
$select = "*";
switch ($mode) {
case "speed":
$table = "speedmode";
$order = "score_time ASC";
$select = ($unique == "true") ? "name, MIN(score_time) score_time" : "*";
break;
case "challenge":
$table = "challengemode";
$order = "score DESC";
$select = ($unique == "true") ? "name, MAX(score) score" : "*";
break;
}
$group = ($unique == "true") ? "GROUP BY name" : "";
$where = "1=1";
switch(strtolower($period)) {
case "day":
$where .= " AND DAY($for) = DAY(added_at)";
case "week":
$where .= " AND WEEK($for) = WEEK(added_at)";
case "month":
$where .= " AND MONTH($for) = MONTH(added_at)";
case "year":
$where .= " AND YEAR($for) = YEAR(added_at)";
}
$query = "SELECT $select FROM $table WHERE $where $group
ORDER BY $order LIMIT " . $limit;
// echo $query . "<br/>";
foreach($wpdb->get_results($query) as $score) {
$output .= $opts["before_line"];
$output .= $opts["before_name"] . make_twitter_link($score->name) . $opts["after_name"];
$output .= $opts["before_score"];
$output .= (($mode == "speed") ? $score->score_time : $score->score);
$output .= $opts["after_score"];
$output .= $opts["after_line"];
}
return $output;
}
function make_twitter_link($name) {
$pattern = '/^@(.+)/';
if(preg_match($pattern, $name, $matches)) {
$name = "<a href='http://twitter.com/$matches[1]'>$name</a>";
return $name;
}
else {
return $name;
}
}
function widget_highscores_init() {
if (!function_exists('register_sidebar_widget')) { return; }
// delete_option("widget_highscores");
function widget_highscores($args, $w_args = -1) {
extract($args, EXTR_SKIP);
if (is_numeric($w_args)) { $w_args = array('number' => $w_args); }
$w_args = wp_parse_args($w_args, array('number' => 1));
extract($w_args, EXTR_SKIP);
$options = get_option('widget_highscores');
if (!isset($options[$number])) { return; }
$title = apply_filters('widget_title', $options[$number]['title']);
$period = apply_filters('widget_title', $options[$number]['period']);
$hs_mode = apply_filters('widget_title', $options[$number]['hs_mode']);
$limit = apply_filters('widget_title', $options[$number]['limit']);
$id = apply_filters('widget_id', $options[$number]['id']);
?>
<?php echo $before_widget ?>
<?php if (!empty($title)): ?>
<?php echo $before_title . $title . $after_title ?>
<?php endif ?>
<ul <?php if ($id) { echo 'id="'.$id.'"'; } ?>class="highscore-widget">
<?php echo hs_scores_for($hs_mode, $period, $limit) ?>
</ul>
<?php echo $after_widget ?>
<?php
}
function widget_highscores_control($w_args) {
// delete_option("widget_highscores");
if (is_numeric($w_args)) { $w_args = array('number' => $w_args); }
$w_args = wp_parse_args($w_args, array('number' => -1));
extract($w_args, EXTR_SKIP);
if (!$options = get_option('widget_highscores')) {
$options = array(1 => array('title'=>'High Scores', 'period'=>'week',
'hs_mode' => 'speed', 'limit' => 10));
}
// echo "options at the outset:"; var_dump($options);
foreach($_POST["highscores"] as $w_number => $w_opts) {
if (!isset($w_opts["period"]) && isset($options[$w_number])) {
// clicked cancel on an existing HS widget
contine;
}
$title = strip_tags(stripslashes($w_opts["title"]));
$period = strip_tags(stripslashes($w_opts["period"]));
$hs_mode = strip_tags(stripslashes($w_opts["hs_mode"]));
$limit = strip_tags(stripslashes($w_opts["limit"]));
$id = strip_tags(stripslashes($w_opts["id"]));
$options[$w_number] = compact('title','period','hs_mode','limit','id');
}
update_option('widget_highscores', $options);
// echo "TheNumber is $number";
if ($number == -1) {
$title = "High Scores";
$period = "week";
$hs_mode = "speed";
$limit = 10;
$number = '%i%';
$id = '';
}
else {
$title = attribute_escape($options[$number]["title"]);
$period = attribute_escape($options[$number]["period"]);
$hs_mode = attribute_escape($options[$number]["hs_mode"]);
$limit = attribute_escape($options[$number]["limit"]);
$id = attribute_escape($options[$number]["id"]);
}
?>
<p>
<label for='highscores-<?php echo $number ?>-title'>Title:</label><br/>
<input style="width: 150px" name="highscores[<?php echo $number ?>][title]" id="highscores-<?php echo $number ?>-title" value="<?php echo $title ?>"/>
</p>
<hr/>
<p>
<label>Period:</label><br/>
<?php foreach (array('day','week','month','year') as $p): ?>
<input type='radio' name='highscores[<?php echo $number ?>][period]' value="<?php echo $p ?>" <?php if ($p==$period) { echo "checked"; } ?>/> High Scores Per <?php echo $p ?><br/>
<?php endforeach ?>
</p>
<hr/>
<p>
<label>Mode:</label><br/>
<?php foreach (array('speed', 'challenge') as $m): ?>
<input type='radio' name='highscores[<?php echo $number ?>][hs_mode]' value="<?php echo $m ?>" <?php if ($m==$hs_mode) { echo "checked"; } ?>/> <?php echo $m ?> mode<br/>
<?php endforeach ?>
</p>
<hr/>
<p>
<label for="highscores-<?php echo $number ?>-limit"># to Show:</label>
<br/>
<input type="text" style="width: 150px" name="highscores[<?php echo $number ?>][limit]" id="highscores-<?php echo $number ?>-limit" value="<?php echo $limit ?>"/>
</p>
<hr/>
<p>
<label for='highscores-<?php echo $number ?>-title'>Widget ID:</label><br/>
<input style="width: 150px" name="highscores[<?php echo $number ?>][id]" id="highscores-<?php echo $number ?>-id" value="<?php echo $id ?>"/>
</p>
<input type="hidden" name="highscores[<?php echo $number ?>][submit]" value="1"/>
<?php
}
if (!$options = get_option('widget_highscores')) { $options = array(); }
$w_opts = array('classname' => 'widget_highscores',
'description' => __('High Scores Widget'));
$c_opts = array('id_base' => 'highscores');
$name = __('High Scores');
$id = false;
foreach ((array) array_keys($options) as $o) {
$id = "highscores-$o";
wp_register_sidebar_widget($id, $name, 'widget_highscores', $w_opts, array('number' => $o));
wp_register_widget_control($id, $name, 'widget_highscores_control', $c_opts, array('number' => $o));
}
if (!$id) {
echo "First time register";
wp_register_sidebar_widget('highscores-1', $name, 'widget_highscores', $w_opts, array('number' => -1));
wp_register_widget_control('highscores-1', $name, 'widget_highscores_control', $c_opts, array('number' => -1));
}
}
add_action('widgets_init', 'widget_highscores_init');
add_filter('the_content', 'highscores_filter');
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>plugin_helper</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.5.3/jquery-ui.min.js" type="text/javascript" charset="utf-8"></script>
<style type="text/css" media="screen">
body {
font: 75% "Lucida Grande", "Trebuchet MS", Verdana, sans-serif;
width: 90em;
}
label { display: block; }
textarea { width: 50em; height: 10em; float: left; margin-top: 15em;}
form div {
margin-bottom: 2em;
}
form {
float: left;
width: 20em;
}
</style>
</head>
<body id="plugin_helper">
<div>
<form>
<div>
<label>Mode:</label>
<label>
<input type="radio" name="mode" value="speed"/> Speed
</label>
<label>
<input type="radio" name="mode" value="challenge"/> Challenge
</label>
</div>
<div>
<label for="for">For: (format: 2009-10-10, default: today)</label>
<input type="text" name="for"/>
</div>
<div>
<label for="limit">Limit: (# of scores to show)</label>
<input type="text" name="limit"/>
</div>
<div>
<label>Period:</label>
<label>
<input type="radio" name="period" value="year"/> Year
</label>
<label>
<input type="radio" name="period" value="month"/> Month
</label>
<label>
<input type="radio" name="period" value="week"/> Week
</label>
<label>
<input type="radio" name="period" value="day"/> Day
</label>
</div>
<div>
<label for="before_line">Before Each Line: (default: &lt;li class="high-score"&gt;)</label>
<input type="text" name="before_line"/>
</div>
<div>
<label for="after_line">After Each Line: (default: &lt;/li&gt;)</label>
<input type="text" name="after_line"/>
</div>
<div>
<label for="before_name">Before Each Name: (default: &lt;strong class='name'&gt;)</label>
<input type="text" name="before_name"/>
</div>
<div>
<label for="after_name">After Each Name: (default: &lt;/strong&gt;)</label>
<input type="text" name="after_name"/>
</div>
<div>
<label for="before_score">Before Each Score: (default: &lt;span class='score'&gt;)</label>
<input type="text" name="before_score"/>
</div>
<div>
<label for="after_score">After Each Score: (default: &lt;/span&gt;)</label>
<input type="text" name="after_score"/>
</div>
</form>
<textarea id="output"></textarea>
</div>
<script type="text/javascript" charset="utf-8">
function build_string() {
f = $("form").serialize();
m = f.split("&")
a = $.map(m, function(e) {
if (e.match(/=$/)) { return; }
return unescape(e).replace(/=/, "|").replace("<", encodeURI("&amp;lt;")).replace(">", "&amp;gt;");
}).join(" ");
$("#output").html("<--highscores " + a + "-->");
}
$(function() {
$("form input").blur(function() { build_string(); });
$("form input").change(function() { build_string(); });
setTimeout("build_string()", 200);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.