<?php
/*
Plugin Name: Impress
Plugin URI: http://immike.net
Description: Impress your readers with statistics on the number of users, posts, comments, pages, categories, words in posts, words in comments, and words in pages you have.
Author: Mike Malone
Version: 0.01 (beta)
Author URI: http://immike.net
*/
define('ENABLE_MEMCACHE', false);
define('MEMCACHE_HOST', '');
define('MEMCACHE_PORT', 11211);
define('MEMCACHE_TIME', 600);
function impress( $format=':post_wordcount' ) {
echo preg_replace_callback('/:([a-zA-Z_]+)/', 'impress_callback', $format);
}
function impress_callback( $matches ) {
if($return = impress_cache_get($matches[1])) {
return $return;
}
$function = 'impress_'.$matches[1];
if(function_exists($function)) {
$return = call_user_func($function);
impress_cache_store($matches[1], $return);
return $return;
}
return $matches[0];
}
function impress_cache_get( $key ) {
if(!ENABLE_MEMCACHE)
return false;
$m = new Memcache();
if($m->connect(MEMCACHE_HOST, MEMCACHE_PORT))
{
$memcache_id = 'impress_'.$key;
$m->get($memcache_id);
}
}
function impress_cache_store( $key, $val ) {
if(!ENABLE_MEMCACHE)
return false;
$m = new Memcache();
if($m->connect(MEMCACHE_HOST, MEMCACHE_PORT))
{
$memcache_id = 'impress_'.$key;
$m->set($memcache_id, $val, false, MEMCACHE_TIME);
}
}
function impress_wpdb_query($statement) {
global $wpdb;
$results = $wpdb->get_col($statement);
return number_format($results[0]);
}
function impress_users() {
global $wpdb;
return impress_wpdb_query("SELECT COUNT(ID) as counter FROM $wpdb->users");
}
function impress_posts() {
global $wpdb;
return impress_wpdb_query("SELECT COUNT(ID) as counter FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post'");
}
function impress_pages() {
global $wpdb;
return impress_wpdb_query("SELECT COUNT(ID) as counter FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'page'");
}
function impress_comments() {
global $wpdb;
return impress_wpdb_query("SELECT COUNT(comment_ID) as counter FROM $wpdb->comments WHERE comment_approved = '1'");
}
function impress_categories() {
global $wpdb;
return impress_wpdb_query("SELECT COUNT(cat_ID) as counter FROM $wpdb->categories");
}
function impress_post_wordcount() {
global $wpdb;
$now = gmdate('Y-m-d H:i:s', time());
$words = $wpdb->get_results("SELECT post_content FROM $wpdb->posts WHERE post_status='publish' AND post_type='post' AND post_date < '$now'");
$count = 0;
foreach($words as $word) {
$count += impress_html_wordcount($word->post_content);
}
return number_format( $count );
}
function impress_page_wordcount() {
global $wpdb;
$now = gmdate('Y-m-d H:i:s', time());
$words = $wpdb->get_results("SELECT post_content FROM $wpdb->posts WHERE post_status='publish' AND post_type='page' AND post_date < '$now'");
$count = 0;
foreach($words as $word) {
$count += impress_html_wordcount($word->post_content);
}
return number_format( $count );
}
function impress_comment_wordcount() {
global $wpdb;
$words = $wpdb->get_results("SELECT comment_content FROM $wpdb->comments WHERE comment_approved = '1'");
$count = 0;
foreach($words as $word) {
$count += impress_html_wordcount($word->comment_content);
}
return number_format($count);
}
function impress_html_wordcount( $string )
{
// str_word_count turns out to be quite a bit slower than this regex
return count(preg_split('/\W+/', strip_tags($string)));
}
You are viewing a mobilized version of this site...
View original page here