public
Last active — forked from kastner/README.md

  • Download Gist
README.md
Markdown

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.

high_scores.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
<?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
Author: Brian Woolley and Erik Kastner
Author URI: http://metaatem.net
*/
 
function highscores_filter($content) {
// global $wpdb; var_dump($wpdb->get_results("SELECT * FROM speedmode"));
 
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["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",
"for" => "NOW()");
$o2 = hs_default_options();
return $o + $o2;
}
 
function hs_scores_for($mode, $period, $limit=10, $for="NOW()", $opts="") {
global $wpdb;
$output = "";
if ($for != "NOW()") { $for = '"' . $for . '"'; }
if (!is_array($opts)) { $opts = hs_default_options(); }
$table = "";
switch ($mode) {
case "speed": $table = "speedmode"; $order = "score_time ASC"; break;
case "challenge": $table = "challengemode"; $order = "score DESC"; break;
}
$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 * FROM $table WHERE $where ORDER BY $order LIMIT " . $limit;
// echo $query;
foreach($wpdb->get_results($query) as $score) {
$output .= $opts["before_line"];
$output .= $opts["before_name"] . $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 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']);
?>
<?php echo $before_widget ?>
<?php if (!empty($title)): ?>
<?php echo $before_title . $title . $after_title ?>
<?php endif ?>
<ul 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"]));
$options[$w_number] = compact('title', 'period', 'hs_mode', 'limit');
}
update_option('widget_highscores', $options);
 
// echo "TheNumber is $number";
if ($number == -1) {
$title = "High Scores";
$period = "week";
$hs_mode = "speed";
$limit = 10;
$number = '%i%';
}
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"]);
}
?>
<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>
<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');
plugin_helper.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
<!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>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.