|
<?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');
|