comments:%comment%
TBPB:%tbpb%
total words:%word%
days">Set up time:2006.4.1
last update:
%last%';
function wp_stat_activate() {
global $wp_stat_sample;
if ( get_option('wp-statistics') === false )
update_option('wp-statistics', $wp_stat_sample);
if ( get_option('wp-statistics-style') === false )
update_option('wp-statistics-style', 1);
}
if ( !function_exists('mb_strlen1') ):
function mb_strlen1($str) {
$num = 0;
for ( $i = 0; $i < strlen($str); $i++ ) {
$ord = ord($str{$i});
if ( ($ord & 0xC0) != 0x80 ) $num++;
}
return $num;
}
endif;
class Ystat {
var $cache; //Cache
var $cachelen; //Cache length
var $now;
var $stat_code; //Statistics code
var $style; //Statistics code style
function Ystat() {
$this->cache = wp_cache_get('ystat', 'plugins');
if ( $this->cache == false )
$this->cache = array();
$this->cachelen = count($this->cache);
$this->stat_code = get_option('wp-statistics');
$this->style = get_option('wp-statistics-style');
}
function show() {
$code = $this->stat_code;
if ( $this->style == 1 ) {
$code = '
' . nl2br($code) . '
';
} elseif ( $this->style == 2 ) {
$tmp = "\n";
foreach ( explode("\n", $code) as $line ) {
$line = trim($line);
if ( !empty($line) )
$tmp .= "- $line
\n";
}
$tmp .= '
';
$code = $tmp;
}
$code = preg_replace_callback('/%([^%]*)%/u', array(&$this, 'replace_stat'), $code);
eval(' ?>' . $code . 'get($stat);
}
function get($str) {
global $wpdb;
if ( isset($this->cache[$str]) )
return $this->cache[$str];
switch ($str) {
//Posts
case 'post':
$result = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'post' AND (post_status = 'publish' OR post_status = 'private')");
break;
//Pages
case 'page':
$result = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'page' AND post_status = 'publish'");
break;
//Categories
case 'category':
$result = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE taxonomy = 'category'");
break;
//Tags
case 'tag':
$result = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE taxonomy = 'post_tag'");
break;
//Links
case 'link':
$result = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->links WHERE link_visible = 'Y'");
break;
//Comments
case 'comment':
$result = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '1' AND comment_type != 'trackback' AND comment_type != 'pingback'");
break;
//Comments by guest
case 'comment_guest':
$result = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '1' AND comment_type != 'trackback' AND comment_type != 'pingback' AND user_id = 0");
break;
//Comments by author
case 'comment_author':
$result = $this->get('comment') - $this->get('comment_guest');
break;
//Trackback&Pingback
case 'tbpb':
$result = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '1' AND (comment_type = 'trackback' OR comment_type = 'pingback')");
break;
//Word counts
case 'word':
$words = $wpdb->get_results("SELECT post_content FROM $wpdb->posts WHERE (post_type = 'post' OR post_type = 'page') AND (post_status = 'publish' OR post_status = 'private')");
$result = 0;
$mb_strlen = function_exists('mb_strlen') ? 'mb_strlen' : 'mb_strlen1';
foreach ($words as $word)
$result += $mb_strlen(preg_replace('/\s/', '', html_entity_decode( strip_tags($word->post_content) ) ), 'UTF-8');
break;
//Last Update
case 'last':
$result = $wpdb->get_results("SELECT MAX(post_modified) AS MAX_m FROM $wpdb->posts WHERE (post_type = 'post' OR post_type = 'page') AND (post_status = 'publish' OR post_status = 'private')");
$result = date('Y-n-j g:ia', strtotime($result[0]->MAX_m));
break;
default:
break;
}
$this->cache[$str] = $result;
return $result;
}
function close() {
if ($this->cachelen <> count($this->cache))
wp_cache_set('ystat', $this->cache, 'plugins');
}
}
function ShowStatistics() {
$y = new Ystat();
$y->show();
$y->close();
}
add_action('admin_menu', 'wp_stat_add_option_page');
function wp_stat_add_option_page() {
if ( function_exists('add_options_page') ) {
add_options_page("WP 2.3 statistics Settings", 'wp-statistics', 8, __FILE__, 'wp_stat_option_page');
}
}
function wp_stat_option_page() {
global $wpdb;
if ( isset($_POST['Submit']) ) {
update_option('wp-statistics', stripslashes((string) $_POST['stat']) );
update_option('wp-statistics-style', (int) $_POST['style']);
?>
stat_code = $wp_stat_sample;
?>