get_results("SELECT ID, post_content, post_status, MID(post_date, 1, 7) AS post_date, post_title, post_author, post_type FROM $table_prefix"."posts WHERE post_status <> 'inherit' AND post_status <> 'auto-draft' AND (post_type = 'page' OR post_type = 'post') ORDER BY post_date DESC;"); foreach ($bjl_word_count_items as $items => $item) { // CALCULATE AND STORE MAIN STATS $words = word_counter($item->post_content); $arr_bjl_author_word_count[$item->post_author] = $arr_bjl_author_word_count[$item->post_author] + $words; $arr_bjl_month_word_count[$item->post_date] = $arr_bjl_month_word_count[$item->post_date] + $words; if ($item->post_type == "post") { if ($item->post_status == "publish") { $words_posts_publish += $words; $count_posts_publish++; $arr_bjl_author_posts_publish[$item->post_author] = $arr_bjl_author_posts_publish[$item->post_author] + 1; $arr_bjl_author_posts_publish_word_count[$item->post_author] = $arr_bjl_author_posts_publish_word_count[$item->post_author] + $words; $arr_bjl_month_posts_publish[$item->post_date] = $arr_bjl_month_posts_publish[$item->post_date] + 1; $arr_bjl_month_posts_publish_word_count[$item->post_date] = $arr_bjl_month_posts_publish_word_count[$item->post_date] + $words; } elseif ($item->post_status == "draft") { $words_posts_draft += $words; $count_posts_draft++; $arr_bjl_author_posts_draft[$item->post_author] = $arr_bjl_author_posts_draft[$item->post_author] + 1; $arr_bjl_author_posts_draft_word_count[$item->post_author] = $arr_bjl_author_posts_draft_word_count[$item->post_author] + $words; $arr_bjl_month_posts_draft[$item->post_date] = $arr_bjl_month_posts_draft[$item->post_date] + 1; $arr_bjl_month_posts_draft_word_count[$item->post_date] = $arr_bjl_month_posts_draft_word_count[$item->post_date] + $words; } } elseif ($item->post_type == "page") { if ($item->post_status == "publish") { $words_pages_publish += $words; $count_pages_publish++; $arr_bjl_author_pages_publish[$item->post_author] = $arr_bjl_author_pages_publish[$item->post_author] + 1; $arr_bjl_author_pages_publish_word_count[$item->post_author] = $arr_bjl_author_pages_publish_word_count[$item->post_author] + $words; $arr_bjl_month_pages_publish[$item->post_date] = $arr_bjl_month_pages_publish[$item->post_date] + 1; $arr_bjl_month_pages_publish_word_count[$item->post_date] = $arr_bjl_month_pages_publish_word_count[$item->post_date] + $words; } elseif ($item->post_status == "draft") { $words_pages_draft += $words; $count_pages_draft++; $arr_bjl_author_pages_draft[$item->post_author] = $arr_bjl_author_pages_draft[$item->post_author] + 1; $arr_bjl_author_pages_draft_word_count[$item->post_author] = $arr_bjl_author_pages_draft_word_count[$item->post_author] + $words; $arr_bjl_month_pages_draft[$item->post_date] = $arr_bjl_month_pages_draft[$item->post_date] + 1; $arr_bjl_month_pages_draft_word_count[$item->post_date] = $arr_bjl_month_pages_draft_word_count[$item->post_date] + $words; } } // ITEM STATS $arr_bjl_content_word_count[$item->ID] = $words; $arr_bjl_content_post_title[$item->ID] = $item->post_title; $arr_bjl_content_post_type[$item->ID] = $item->post_type; $arr_bjl_content_post_status[$item->ID] = $item->post_status; $arr_bjl_content_post_author[$item->ID] = $item->post_author; } // WRITE MAIN STATS TO OPTIONS TABLE $arr_bjl_word_count_main = array ( 'words_posts_publish' => $words_posts_publish, 'words_posts_draft' => $words_posts_draft, 'words_pages_publish' => $words_pages_publish, 'words_pages_draft' => $words_pages_draft, 'count_posts_publish' => $count_posts_publish, 'count_posts_draft' => $count_posts_draft, 'count_pages_publish' => $count_pages_publish, 'count_pages_draft' => $count_pages_draft ); update_option('bjl_word_count_main', $arr_bjl_word_count_main); // WRITE CACHED ITEMS TO OPTIONS TABLE $arr_bjl_word_count_cache = array ( 'bjl_content_word_count' => $arr_bjl_content_word_count, 'bjl_content_post_title' => $arr_bjl_content_post_title, 'bjl_content_post_type' => $arr_bjl_content_post_type, 'bjl_content_post_status' => $arr_bjl_content_post_status, 'bjl_content_post_author' => $arr_bjl_content_post_author ); update_option('bjl_word_count_cache', $arr_bjl_word_count_cache); // WRITE AUTHOR STATS TO OPTIONS TABLE $arr_bjl_word_count_author = array ( 'bjl_author_word_count' => $arr_bjl_author_word_count, 'bjl_author_posts_publish' => $arr_bjl_author_posts_publish, 'bjl_author_posts_publish_word_count' => $arr_bjl_author_posts_publish_word_count, 'bjl_author_posts_draft' => $arr_bjl_author_posts_draft, 'bjl_author_posts_draft_word_count' => $arr_bjl_author_posts_draft_word_count, 'bjl_author_pages_publish' => $arr_bjl_author_pages_publish, 'bjl_author_pages_publish_word_count' => $arr_bjl_author_pages_publish_word_count, 'bjl_author_pages_draft' => $arr_bjl_author_pages_draft, 'bjl_author_pages_draft_word_count' => $arr_bjl_author_pages_draft_word_count ); update_option('bjl_word_count_author', $arr_bjl_word_count_author); // WRITE MONTH STATS TO OPTIONS TABLE $arr_bjl_word_count_month = array ( 'bjl_month_word_count' => $arr_bjl_month_word_count, 'bjl_month_posts_publish' => $arr_bjl_month_posts_publish, 'bjl_month_posts_publish_word_count' => $arr_bjl_month_posts_publish_word_count, 'bjl_month_posts_draft' => $arr_bjl_month_posts_draft, 'bjl_month_posts_draft_word_count' => $arr_bjl_month_posts_draft_word_count, 'bjl_month_pages_publish' => $arr_bjl_month_pages_publish, 'bjl_month_pages_publish_word_count' => $arr_bjl_month_pages_publish_word_count, 'bjl_month_pages_draft' => $arr_bjl_month_pages_draft, 'bjl_month_pages_draft_word_count' => $arr_bjl_month_pages_draft_word_count ); update_option('bjl_word_count_month', $arr_bjl_word_count_month); } function word_counter($content) { //http://www.zhounaiming.com/entry/111 $str = strip_tags($content); $str = preg_replace('/[\x80-\xff]{1,3}/', ' ', $str,-1,$n); $n += str_word_count($str); return $n; } function bjl_word_count_admin() { if ( $_GET['ac'] == 'recount' || !get_option('bjl_word_count_main') ) { echo '
'; echo __("Counting... Please wait a few minutes and re-open this page.", 'asianwordcount'); echo '
'; echo __("Configuration Saved.", 'asianwordcount'); echo '
| '.__("Words", 'asianwordcount').' | '; echo ''.__("Title", 'asianwordcount').' | '; echo ''.__("Type", 'asianwordcount').' | '; echo ''.__("Status", 'asianwordcount').' | '; echo ''.__("Author", 'asianwordcount').' | '; echo '
|---|---|---|---|---|
| '.number_format($value).' | '; echo ''.$bjl_content_post_title[$key].' | '; echo ''.ucwords($bjl_content_post_type[$key]).' | '; echo ''.ucwords($bjl_content_post_status[$key]).' | '; echo ''.$user->user_login.' | '; echo '
| '.__("Author", 'asianwordcount').' | '; echo ''.__("Words", 'asianwordcount').' | '; echo ''.__("Published Posts", 'asianwordcount').' | '; echo ''.__("Published Pages", 'asianwordcount').' | '; echo ''.__("Published Total", 'asianwordcount').' | '; echo ''.__("Draft Posts", 'asianwordcount').' | '; echo ''.__("Draft Pages", 'asianwordcount').' | '; echo ''.__("Draft Total", 'asianwordcount').' | '; echo '
|---|---|---|---|---|---|---|---|
| '.$user->user_login.' | '; echo ''.number_format($value).' | '; echo ''.number_format($bjl_author_posts_publish_word_count[$key]).' '.__("Words", 'asianwordcount').' '.@number_format($bjl_author_posts_publish_word_count[$key] / $bjl_author_posts_publish[$key]).' '.__("Word Avg.", 'asianwordcount').' | ';
echo ''.number_format($bjl_author_pages_publish_word_count[$key]).' '.__("Words", 'asianwordcount').' '.@number_format($bjl_author_pages_publish_word_count[$key] / $bjl_author_pages_publish[$key]).' '.__("Word Avg.", 'asianwordcount').' | ';
echo ''.number_format($bjl_author_posts_publish_word_count[$key] + $bjl_author_pages_publish_word_count[$key]).' '.__("Words", 'asianwordcount').' '.@number_format(($bjl_author_posts_publish_word_count[$key] + $bjl_author_pages_publish_word_count[$key]) / ($bjl_author_posts_publish[$key] + $bjl_author_pages_publish[$key])).' '.__("Word Avg.", 'asianwordcount').' | ';
echo ''.number_format($bjl_author_posts_draft_word_count[$key]).' '.__("Words", 'asianwordcount').' '.@number_format($bjl_author_posts_draft_word_count[$key] / $bjl_author_posts_draft[$key]).' '.__("Word Avg.", 'asianwordcount').' | ';
echo ''.number_format($bjl_author_pages_draft_word_count[$key]).' '.__("Words", 'asianwordcount').' '.@number_format($bjl_author_pages_draft_word_count[$key] / $bjl_author_pages_draft[$key]).' '.__("Word Avg.", 'asianwordcount').' | ';
echo ''.number_format($bjl_author_posts_draft_word_count[$key] + $bjl_author_pages_draft_word_count[$key]).' '.__("Words", 'asianwordcount').' '.@number_format(($bjl_author_posts_draft_word_count[$key] + $bjl_author_pages_draft_word_count[$key]) / ($bjl_author_posts_draft[$key] + $bjl_author_pages_draft[$key])).' '.__("Word Avg.", 'asianwordcount').' | ';
echo '
| '.__("Month", 'asianwordcount').' | '; echo ''.__("Words", 'asianwordcount').' | '; echo ''.__("Published Posts", 'asianwordcount').' | '; echo ''.__("Published Pages", 'asianwordcount').' | '; echo ''.__("Published Total", 'asianwordcount').' | '; echo ''.__("Draft Posts", 'asianwordcount').' | '; echo ''.__("Draft Pages", 'asianwordcount').' | '; echo ''.__("Draft Total", 'asianwordcount').' | '; echo '
|---|---|---|---|---|---|---|---|
| '.$key.' | '; echo ''.number_format($value).' | '; echo ''.number_format($bjl_month_posts_publish_word_count[$key]).' '.__("Words", 'asianwordcount').' '.@number_format($bjl_month_posts_publish_word_count[$key] / $bjl_month_posts_publish[$key]).' '.__("Word Avg.", 'asianwordcount').' | ';
echo ''.number_format($bjl_month_pages_publish_word_count[$key]).' '.__("Words", 'asianwordcount').' '.@number_format($bjl_month_pages_publish_word_count[$key] / $bjl_month_pages_publish[$key]).' '.__("Word Avg.", 'asianwordcount').' | ';
echo ''.number_format($bjl_month_posts_publish_word_count[$key] + $bjl_month_pages_publish_word_count[$key]).' '.__("Words", 'asianwordcount').' '.@number_format(($bjl_month_posts_publish_word_count[$key] + $bjl_month_pages_publish_word_count[$key]) / ($bjl_month_posts_publish[$key] + $bjl_month_pages_publish[$key])).' '.__("Word Avg.", 'asianwordcount').' | ';
echo ''.number_format($bjl_month_posts_draft_word_count[$key]).' '.__("Words", 'asianwordcount').' '.@number_format($bjl_month_posts_draft_word_count[$key] / $bjl_month_posts_draft[$key]).' '.__("Word Avg.", 'asianwordcount').' | ';
echo ''.number_format($bjl_month_pages_draft_word_count[$key]).' '.__("Words", 'asianwordcount').' '.@number_format($bjl_month_pages_draft_word_count[$key] / $bjl_month_pages_draft[$key]).' '.__("Word Avg.", 'asianwordcount').' | ';
echo ''.number_format($bjl_month_posts_draft_word_count[$key] + $bjl_month_pages_draft_word_count[$key]).' '.__("Words", 'asianwordcount').' '.@number_format(($bjl_month_posts_draft_word_count[$key] + $bjl_month_pages_draft_word_count[$key]) / ($bjl_month_posts_draft[$key] + $bjl_month_pages_draft[$key])).' '.__("Word Avg.", 'asianwordcount').' | ';
echo '
';
if ($show_total) echo ''.__("Total").': '.number_format($words_posts_publish + $words_pages_publish).' '.__("Words").'
';
if ($show_posts) echo ''.__("Posts").': '.number_format($words_posts_publish).' '.__("Words").' ('.@number_format($words_posts_publish / $count_posts_publish).' '.__("Avg.").')
';
if ($show_pages) echo ''.__("Pages").': '.number_format($words_pages_publish).' '.__("Words").' ('.@number_format($words_pages_publish / $count_pages_publish).' '.__("Avg.").')
';
if ($show_total || $show_posts || $show_pages) echo '
Powered by WP Word Count
'; echo $after_widget; } function update($new_instance, $old_instance) { $instance = $old_instance; $instance['title'] = strip_tags($new_instance['title']); $instance['show_total'] = $new_instance['show_total']; $instance['show_posts'] = $new_instance['show_posts']; $instance['show_pages'] = $new_instance['show_pages']; $instance['show_link'] = $new_instance['show_link']; return $instance; } function form($instance) { $defaults = array('title' => 'Word Count Statistics', 'show_total' => true, 'show_posts' => true, 'show_pages' => true, 'show_link' => true); $instance = wp_parse_args((array) $instance, $defaults); ?>
id="get_field_id('show_total'); ?>" name="get_field_name('show_total'); ?>" />
id="get_field_id('show_posts'); ?>" name="get_field_name('show_posts'); ?>" />
id="get_field_id('show_pages'); ?>" name="get_field_name('show_pages'); ?>" />
id="get_field_id('show_link'); ?>" name="get_field_name('show_link'); ?>" />