. Thanks to Allan Ellegaard for testing and input. */ /* # Word Counts -------------------------------------------------------------- */ load_plugin_textdomain( 'word-stats', '/wp-content/plugins/word-stats/languages/', 'word-stats/languages/' ); // Delete deprecated options if ( get_option( 'ws-counts-cache' ) ) { delete_option( 'ws-counts-cache' ); } // Classes are used to avoid function name collisions // Word Counts class class word_stats_counts { // Count words from all post types and cache the output function cache_word_counts() { $total_count = 0; $cache = ''; $author_count = array(); // Get the post types $args=array( 'public' => true, ); $post_types = get_post_types( $args ); foreach( $post_types as $post_type ) { if ( $post_type != 'attachment' && $post_type != 'nav_menu_item' && $post_type != 'revision' ) { $total_count = 0; $posts = get_posts( array( 'numberposts' => -1, 'post_type' => array( $post_type ) )); foreach( $posts as $post ) { $word_count = str_word_count( strip_tags( get_post_field( 'post_content', $post->ID ) ) ); $total_count += $word_count; // Multidimensional array, stores monthly words per author $author_count[ $post->post_author ][ substr( $post->post_date, 0, 7 ) ] += $word_count; } $num = number_format_i18n( $total_count ); // This adds the word count for each post type to the stats portion of the Right Now box $text = __( 'Words', 'word-stats' ) . ' (' . $post_type . ')'; $cache = $cache . "::opentag::{$num}::separator::{$text}::closetag::"; $total_num += $total_count; } } $text = __( 'Total words', 'word-stats' ); $total_num = number_format_i18n( $total_num ); $cache = $cache . "::totalopentag::{$total_num}::separator::{$text}::closetag::"; update_option( 'ws-total-counts-cache', $cache ); update_option( 'ws-monthly-counts-cache', $author_count ); return $cache; } // Output the cached word counts with the proper HTML tags function get_word_counts( $mode ) { if ( !get_option( 'ws-total-counts-cache' ) ) { $cached = word_stats_counts::cache_word_counts(); } else { $cached = get_option( 'ws-total-counts-cache' ); } if ( $mode == 'table' ) { $cached = str_replace( '::opentag::', '', $cached ); $cached = str_replace( '::totalopentag::', '', $cached ); $cached = str_replace( '::separator::', '', $cached ); $cached = str_replace( '::closetag::', '', $cached ); } else { $cached = str_replace( '::opentag::', '
  • ', $cached ); $cached = str_replace( '::totalopentag::', '
  • ', $cached ); $cached = str_replace( '::separator::', ' ', $cached ); $cached = str_replace( '::closetag::', '
  • ', $cached ); } return $cached; } function total_word_counts() { echo word_stats_counts::get_word_counts( 'table' ); } // Shortcode to output word counts function word_counts_sc( $atts = null, $content = null ) { return ''; } } // Widget to output word counts class widget_ws_word_counts extends WP_Widget { function widget_ws_word_counts() { // widget actual processes parent::WP_Widget(false, $name = __( 'Total Word Counts', 'word-stats' ), array('description' => __( 'Displays the word counts of all public post types', 'word-stats' ) ) ); } function form($instance) { // outputs the options form on admin $title = esc_attr( $instance[ 'title' ] ); echo '

    '; } function update( $new_instance, $old_instance ) { // processes widget options to be saved $instance = $old_instance; $instance[ 'title' ] = strip_tags( $new_instance[ 'title' ] ); return $instance; } function widget( $args, $instance ) { // outputs the content of the widget extract( $args ); $title = apply_filters( 'widget_title', $instance['title'] ); if ( !$title ) $title = __( 'Total Word Counts', 'word-stats' ); $title = esc_attr( strip_tags( $title ) ); echo '
  • ', $title, '

  • '; } } // end class // Hook the functions if ( get_option( 'word_stats_totals' ) || get_option( 'word_stats_totals' ) == '' ) { add_action( 'save_post', array( 'word_stats_counts', 'cache_word_counts' ) ); add_action( 'right_now_content_table_end', array( 'word_stats_counts', 'total_word_counts' ) ); add_action( 'widgets_init', create_function( '', 'return register_widget( "widget_ws_word_counts" );' ) ); } add_shortcode( 'wordcounts', array( 'word_stats_counts', 'word_counts_sc' ) ); /* # Live post stats -------------------------------------------------------------- */ // Display post legibility class word_stats_readability { function live_stats() { echo ' '; } /* # Static post stats -------------------------------------------------------------- */ // Calculate stats the PHP way and store them. function cache_stats( $id = null ) { if ( !$id ) { global $post; if ( !$post->ID ) { return null; } $id = $post->ID; } $thepost = get_post( $id ); // Strip tags $allWords = strip_tags( $thepost->post_content ); // Count if ( $allWords ) { $totalCharacters = strlen( $allWords ); $temp = preg_replace( '/\W/', '', $allWords ); $totalAlphanumeric = strlen( $temp ); $temp = str_replace( '!', '.', $allWords ); $temp = str_replace( '?', '.', $temp ); $temp = str_replace( ';', '.', $temp ); $temp = str_replace( ' ', ' ', $temp ); $temp = preg_replace( '/[A-Z]\./', 'A', $temp ); $sentenceArray = explode( '.', $temp ); $totalSentences = count( $sentenceArray ); if ( $sentenceArray[ $totalSentences - 1 ] == '' ) { $totalSentences = $totalSentences - 1; } $wordArray = preg_split( '/[\s\.]+/', $temp ); $totalWords = count( $wordArray ); if ( $wordArray[ $totalWords - 1 ] == '' ) { $totalWords = $totalWords - 1; } // Do the calcs if we aren't going to divide by zero if ( $totalWords > 0 && $totalSentences > 0 ) { $charsPerWord = intval( $totalAlphanumeric / $totalWords ); $charsPerSentence = intval( $totalAlphanumeric / $totalSentences ); $wordsPerSentence = intval( $totalWords / $totalSentences ); // Automated Readability Index $ARI = round( 4.71 * ( $totalAlphanumeric / $totalWords ) + 0.5 * ( $totalWords / $totalSentences ) - 21.43, 1); // Coleman-Liau Index $CLI = round( 5.88 * ( $totalAlphanumeric / $totalWords ) - 29.6 * ( $totalSentences / $totalWords ) - 15.8, 1); // LIX $LIXlongwords = 0; for ($i = 0; $i < count( $wordArray ); $i = $i + 1 ) { if ( strlen( $wordArray[ $i ] ) > 6 ) { $LIXlongwords++; } } $temp = preg_split( '/[,;\.\(\:]/', $allWords ); $LIX = round( $totalWords / count( $temp ) + ( $LIXlongwords * 100 ) / $totalWords, 1) ; } else { $ARI = '0'; $CLI = '0'; $LIX = '0'; } } else { $ARI = '0'; $CLI = '0'; $LIX = '0'; } // Create/update the post meta fields for readability update_post_meta( $id, 'readability_ARI', $ARI ); update_post_meta( $id, 'readability_CLI', $CLI ); update_post_meta( $id, 'readability_LIX', $LIX ); } /* # Add a column to the post management list -------------------------------------------------------------- */ function add_posts_list_column( $defaults ) { $defaults[ 'readability' ] = __( 'R.I.', 'word-stats' ); return $defaults; } function create_posts_list_column() { global $post; $ARI = get_post_meta( $post->ID, 'readability_ARI', true ); $CLI = get_post_meta( $post->ID, 'readability_CLI', true ); $LIX = get_post_meta( $post->ID, 'readability_LIX', true ); if ( !$ARI ) { // If there is no data or the post is blank echo '--'; } else { // Trying to aggregate the indexes in a meaningful way. $r_avg = ( floatval( $ARI ) + floatval( $CLI ) + ( ( floatval( $LIX ) - 10 ) / 2 ) ) / 3; if ( $r_avg < 8 ) { echo '', round( $r_avg, 1 ), ''; } if ( $r_avg > 7.9 && $r_avg < 12 ) { echo '', round( $r_avg, 1 ), ''; } if ( $r_avg > 11.9 && $r_avg < 16 ) { echo '', round( $r_avg, 1 ), ''; } if ( $r_avg > 15.9 && $r_avg < 20 ) { echo '', round( $r_avg, 1 ), ''; } if ( $r_avg > 19.9 ) { echo '', round( $r_avg, 1 ), ''; } } } // Load style for the column function style_column() { wp_register_style( 'word-stats-css', plugins_url() . '/word-stats/word-stats.css' ); wp_enqueue_style( 'word-stats-css' ); } } // Hook live stats. Load only when editing a post if ( $_GET[ 'action' ] == 'edit' || !strpos( $_SERVER['SCRIPT_FILENAME'], 'post-new.php' ) === false ) { add_action('admin_footer', array( 'word_stats_readability' , 'live_stats' ) ); } // Hook cached stats add_action( 'save_post', array( 'word_stats_readability', 'cache_stats' ) ); // Hook custom column if ( get_option( 'word_stats_RI_Column' ) || get_option( 'word_stats_RI_Column' ) == '' ) { add_filter( 'manage_posts_columns', array( 'word_stats_readability', 'add_posts_list_column' ) ); add_action( 'manage_posts_custom_column', array( 'word_stats_readability', 'create_posts_list_column' ) ); add_action( 'admin_init', array( 'word_stats_readability', 'style_column' ) ); } /* § Construct the options page -------------------------------------------------------------- */ // create custom plugin settings menu class word_stats_admin { function register_settings() { //register our settings register_setting( 'word-stats-settings-group', 'word_stats_RI_column' ); register_setting( 'word-stats-settings-group', 'word_stats_totals' ); register_setting( 'word-stats-settings-group', 'word_stats_replace_word_count' ); register_setting( 'word-stats-settings-group', 'word_stats_averages' ); register_setting( 'word-stats-settings-group', 'word_stats_show_keywords' ); register_setting( 'word-stats-settings-group', 'word_stats_ignore_keywords' ); } function settings_page() { // Default values $opt_RI_column = get_option( 'word_stats_RI_column' ); if ( $opt_RI_column == null ) { $opt_RI_column = 1; } $opt_totals = get_option( 'word_stats_totals' ); if ( $opt_totals == null ) { $opt_totals = 1; } $opt_replace_wc = get_option( 'word_stats_replace_word_count' ); if ( $opt_replace_wc == null ) { $opt_replace_wc = 1; } $opt_averages = get_option( 'word_stats_averages' ); if ( $opt_averages == null ) { $opt_averages = 1; } $opt_show_keywords = get_option( 'word_stats_show_keywords' ); if ( $opt_show_keywords == null ) { $opt_show_keywords = 1; } $opt_ignore_keywords = get_option( 'word_stats_ignore_keywords' ); echo '

    ' , __( 'Word Stats Options', 'word-stats' ), '

    ', settings_fields( 'word-stats-settings-group' ), '

    ', __( 'Display aggregate readability column in the manage posts list', 'word-stats' ), '

    ', __( 'Enable total word counts.', 'word-stats' ), __( 'This may slow down post saving in large blogs.', 'word-stats' ), '

    ', __( 'Replace WordPress live word count with Word Stats word count.', 'word-stats' ), '

    ', __( 'Enable live character/word/sentence averages.', 'word-stats' ), '

    ', __( 'Enable live keyword count.', 'word-stats' ), '

    ', __( 'Ignore these keywords (space separated):', 'word-stats' ), '

    '; } function graphs_page() { echo '
    '; echo '

    ' , __( 'Word Stats', 'word-stats' ), '

    '; $author_stats = get_option( 'ws-monthly-counts-cache' ); if ( $author_stats ) { foreach ( $author_stats as $id=>$author ) { $i++; $author_data = get_userdata( $id ); echo ''; foreach ( $author as $month=>$count ) { echo ''; } echo '
    ', $author_data->nickname, '
    ', $month, '', number_format_i18n( $count ), '
    '; if ( $i == 4 ) { $i = 0; echo '
    '; } } echo '
    '; } else { echo 'No data available'; } } } function word_stats_create_menu() { add_action( 'admin_init', array( 'word_stats_admin', 'register_settings' ) ); add_options_page( 'Word Stats Plugin Settings', 'Word Stats', 'manage_options', 'word-stats-options', array( 'word_stats_admin', 'settings_page' ) ); if ( get_option( 'word_stats_totals' ) || get_option( 'word_stats_totals' ) == '' ) { add_submenu_page( 'index.php', 'Word Stats Plugin Stats', 'Word Stats', 'manage_options', 'word-stats-graphs', array( 'word_stats_admin', 'graphs_page' ) ); } } add_action( 'admin_menu', 'word_stats_create_menu' ); ?>