Stats by Word Stats v'.$version.''; }else{ return "Word Stats not available."; } } function word_stats_calc(){ global $wpdb, $table_prefix; $posts = $wpdb->get_results("SELECT ID, post_title, post_content, post_status, DATE_FORMAT(post_date, '%k|%i|%s|%m|%d|%Y') AS timestamp FROM {$table_prefix}posts WHERE post_status = 'publish' OR post_status = 'static' ORDER BY post_date DESC;"); $num_posts = count($posts); $last = current($posts); $first = end($posts); //Gets time stamps for first and last post. $stsa = explode("|", $first->timestamp); $etsa = explode("|", $last->timestamp); $s_ts = mktime($stsa[0], $stsa[1], $stsa[2], $stsa[3], $stsa[4], $stsa[5]); $e_ts = mktime($etsa[0], $etsa[1], $etsa[2], $etsa[3], $etsa[4], $etsa[5]); $bas = $e_ts - $s_ts; //Blog alive, seconds $bad = $bas / 86400; //Blog alive, days reset($posts); $longest_post = 0; $longest_post_name = ''; $longest_post_id = ''; $shortest_post_name = ''; $shortest_post_id = ''; foreach($posts as $num => $entry) { //Add up the number of published words if($entry->post_status == "publish") {//For posts $wip = str_word_count(strip_tags($entry->post_content)); $post_words += $wip; if($wip > $longest_post){ $longest_post = $wip; $longest_post_name = $entry->post_title; $longest_post_id = $entry->ID; } if(!isset($shortest_post) || $wip < $shortest_post){ $shortest_post = $wip; $shortest_post_name = $entry->post_title; $shortest_post_id = $entry->ID; } } if($entry->post_status == "static") {//For static pages. $static_words += str_word_count(strip_tags($entry->post_content)); } } $blog_words = $post_words + $static_words; $wpd = round($blog_words / $bad); //Words per day $wpp = round($blog_words / $num_posts); //Words per post $word_stats = array( 'start_ts' => $s_ts, 'end_ts' => $e_ts, 'wpd' => $wpd, 'wpp' => $wpp, 'post_count' => $num_posts, 'post_words' => $post_words, 'static_words' => $static_words, 'blog_words' => $blog_words, 'longest_post' => $longest_post, 'longest_post_name' => $longest_post_name, 'longest_post_id' => $longest_post_id, 'shortest_post' => $shortest_post, 'shortest_post_name' => $shortest_post_name, 'shortest_post_id' => $shortest_post_id, 'version' => WORD_STATS_VERSION ); update_option('word_stats_data', $word_stats); } function word_stats($format_name){ $formats = get_option('word_stats_format'); if( is_array($formats) && isset($formats[$format_name]) ){ return wordstats::word_stats_string($formats[$format_name]); }elseif( !is_array($formats) || is_null($formats) || empty($formats) ){ return "No formats set"; }else{ return "Format pattern '$format_name' not found"; } } function word_stats_widget_init(){ if ( !function_exists('register_sidebar_widget') ) return; function word_stats_widget($args) { extract($args); $options = get_option('word_stats_widget_format'); extract($options); echo $before_widget . $before_title . $title . $after_title; echo wordstats::word_stats($format); echo $after_widget; } function word_stats_widget_control() { $options = get_option('word_stats_widget_format'); if ( !is_array($options) ) $options = array('title'=>'', 'format'=>''); if ( $_POST['wstats-submit'] ) { $options['title'] = strip_tags(stripslashes($_POST['wstats-title'])); $options['format'] = strip_tags(stripslashes($_POST['wstats-format'])); update_option('word_stats_widget_format', $options); } $title = htmlspecialchars($options['title'], ENT_QUOTES); $format_current = htmlspecialchars($options['format'], ENT_QUOTES); $formats = get_option('word_stats_format'); echo '

'; if( empty($formats) ){ echo '

Please create a format in Plugins->Word Stats

'; }else{ echo '

'; } echo ''; } register_sidebar_widget('Word Stats', 'word_stats_widget'); register_widget_control('Word Stats', 'word_stats_widget_control', 300, 100); } function word_stats_check(){ $stats = get_option('word_stats_data'); if(empty($stats)){ wordstats::word_stats_calc(); }else{ if($stats['version'] < WORD_STATS_VERSION){ wordstats::word_stats_calc(); } } $formats = get_option('word_stats_format'); $default = '

'; if(empty($formats)){ $defaults = array('Default' => $default); update_option('word_stats_format', $defaults); }else{ $formats['Default'] = $default; update_option('word_stats_format', $formats); } $widget_options = get_option('word_stats_widget_format'); if(empty($widget_options)){ $widget_defaults = array('title' => 'Word Stats', 'format' => 'Default'); update_option('word_stats_widget_format', $widget_defaults); } } } function word_stats_filter_callback($matches){ return wordstats::word_stats($matches[1]); } function word_stats_filter($content){ $content = preg_replace_callback('/\<\!--word_stats;([^;]*);--\>/', "word_stats_filter_callback", $content); return $content; } add_action('publish_post', array('wordstats','word_stats_calc')); add_action('edit_post', array('wordstats','word_stats_calc')); add_action('init', array('wordstats','word_stats_check')); add_action('admin_menu', array('wordstatsadmin', 'word_stats_config_add')); add_action('admin_menu', array('wordstatsadmin', 'word_stats_dashboard_add')); add_action('plugins_loaded', array('wordstats','word_stats_widget_init')); add_filter('the_content', 'word_stats_filter'); add_filter('the_excerpt', 'word_stats_filter'); add_filter('the_excerpt_rss', 'word_stats_filter'); ?>