Plugin Directory

Changeset 585191


Ignore:
Timestamp:
08/14/2012 04:11:16 AM (14 years ago)
Author:
JonBishop
Message:

Optimized word count generation and caching

Location:
post-admin-word-count
Files:
5 added
3 edited

Legend:

Unmodified
Added
Removed
  • post-admin-word-count/trunk

    • Property svn:ignore set to
      *.db
  • post-admin-word-count/trunk/post-admin-word-count.php

    r584354 r585191  
    11<?php
     2
    23/*
    34  Plugin Name: Post Admin Word Count
    45  Plugin URI: http://www.jonbishop.com/downloads/wordpress-plugins/post-word-count/
    56  Description: Adds a sortable column to the admin's post manager, displaying the word count for each post.
    6   Version: 1.1
     7  Version: 1.2
    78  Author: Jon Bishop
    89  Author URI: http://www.jonbishop.com
    910  License: GPL2
    1011 */
    11 class PostAdminWordCount{
    12     function init() {
    13         if(is_admin()){
    14             add_filter('manage_edit-post_sortable_columns', array(&$this, 'pwc_column_register_sortable'));
    15             add_filter('posts_orderby', array(&$this, 'pwc_column_orderby'), 10, 2);
    16             add_filter("manage_posts_columns", array(&$this, "pwc_columns"));
    17             add_action("manage_posts_custom_column", array(&$this, "pwc_column"));
     12
     13class PostAdminWordCount {
     14
     15    function init() {
     16        if (is_admin()) {
     17            add_filter('manage_edit-post_sortable_columns', array(&$this, 'pwc_column_register_sortable'));
     18            add_filter('posts_orderby', array(&$this, 'pwc_column_orderby'), 10, 2);
     19            add_filter("manage_posts_columns", array(&$this, "pwc_columns"));
     20            add_action("manage_posts_custom_column", array(&$this, "pwc_column"));
     21            add_action("admin_footer-edit.php",array(&$this, "pwc_update_date"));
     22            add_action("admin_head-edit.php",array(&$this, "pwc_get_date"));
     23           
     24        }
     25    }
     26
     27    //=============================================
     28    // Add new columns to action post type
     29    //=============================================
     30    function pwc_columns($columns) {
     31        $columns["post_word_count"] = "Word Count";
     32        return $columns;
     33    }
     34
     35    //=============================================
     36    // Add data to new columns of action post type
     37    //=============================================
     38    function pwc_column($column) {
     39        global $post, $pwc_last;
     40        if ("post_word_count" == $column) {
     41            // Grab a fresh word count
     42            $word_count = str_word_count($post->post_content);
     43            echo $word_count;
     44        }
     45    }
     46
     47    //=============================================
     48    // Queries to run when sorting
     49    // new columns of action post type
     50    //=============================================
     51    function pwc_column_orderby($orderby, $wp_query) {
     52        global $wpdb;
     53
     54        if ('post_word_count' == @$wp_query->query['orderby'])
     55            $orderby = "(SELECT CAST(meta_value as decimal) FROM $wpdb->postmeta WHERE post_id = $wpdb->posts.ID AND meta_key = '_post_word_count') " . $wp_query->get('order');
     56
     57        return $orderby;
     58    }
     59
     60    //=============================================
     61    // Make new columns to action post type sortable
     62    //=============================================
     63    function pwc_column_register_sortable($columns) {
     64        $columns['post_word_count'] = 'post_word_count';
     65        return $columns;
     66    }
     67   
     68    function pwc_get_date(){
     69        global $post, $pwc_last;
     70        // Check last updated
     71        $pwc_last = get_option('pwc_last_checked');
     72
     73        // Check to make sure we have a post and post type
     74        if ( $post && $post->post_type ){
     75           
     76            // Grab all posts with post type
     77            $args = array(
     78                'post_type' => $post->post_type,
     79                'posts_per_page' => -1
     80                );
     81
     82            // Grab the posts
     83            $post_list = new WP_Query($args);
     84            if ( $post_list->have_posts() ) : while ( $post_list->have_posts() ) : $post_list->the_post();
     85               
     86                // Grab a fresh word count
     87                $word_count = str_word_count($post->post_content);
     88
     89                // If post has been updated since last check
     90                if ($post->post_modified > $pwc_last || $pwc_last == "") {
     91                    // Grab word count from post meta
     92                    $saved_word_count = get_post_meta($post->ID, '_post_word_count', true);
     93                    // Check if new wordcount is different than old word count
     94                    if ($saved_word_count != $word_count || $saved_word_count == "") {
     95                        // Update word count in post meta
     96                        update_post_meta($post->ID, '_post_word_count', $word_count, $saved_word_count);
     97                    }
     98                }
     99            endwhile;
     100            endif;
     101
     102            // Let WordPress do it's thing
     103            wp_reset_query();
    18104        }
    19     }
     105    }
     106   
     107    function pwc_update_date(){
     108        // Save the last time this page was generated
     109        $current_date = current_time('mysql');
     110        update_option('pwc_last_checked', $current_date);
     111    }
    20112
    21     //=============================================
    22     // Add new columns to action post type
    23     //=============================================
    24     function pwc_columns($columns) {
    25         $columns["post_word_count"] = "Word Count";
    26         return $columns;
    27     }
    28    
    29     //=============================================
    30     // Add data to new columns of action post type
    31     //=============================================
    32     function pwc_column($column){
    33         global $post;
    34         if ("post_word_count" == $column) {
    35             $saved_word_count = get_post_meta($post->ID, '_post_word_count', true);
    36             $word_count = str_word_count($post->post_content);
    37             if($saved_word_count != $word_count){
    38                 update_post_meta($post->ID, '_post_word_count', $word_count, $saved_word_count);
    39             }
    40             echo $word_count;
    41         }
    42     }
    43    
    44     //=============================================
    45     // Queries to run when sorting
    46     // new columns of action post type
    47     //=============================================
    48     function pwc_column_orderby($orderby, $wp_query) {
    49         global $wpdb;
    50      
    51         $wp_query->query = wp_parse_args($wp_query->query);
     113}
    52114
    53         if ( 'post_word_count' == @$wp_query->query['orderby'] )
    54             $orderby = "(SELECT CAST(meta_value as decimal) FROM $wpdb->postmeta WHERE post_id = $wpdb->posts.ID AND meta_key = '_post_word_count') " . $wp_query->get('order');
    55 
    56         return $orderby;
    57                
    58     }
    59    
    60     //=============================================
    61     // Make new columns to action post type sortable
    62     //=============================================
    63     function pwc_column_register_sortable($columns) {
    64         $columns['post_word_count'] = 'post_word_count';
    65         return $columns;
    66     }
    67 }
    68115$postAdminWordCount = new PostAdminWordCount();
    69116$postAdminWordCount->init();
  • post-admin-word-count/trunk/readme.txt

    r584354 r585191  
    55Requires at least: 3.0
    66Tested up to: 3.4.1
    7 Stable tag: 1.1
     7Stable tag: 1.2
    88
    99Adds a sortable column to the admin's post manager, displaying the word count for each post.
     
    1313Post Word Count adds a sortable column to the admin's post manager, displaying the word count for each post.
    1414
     15I often create new drafts in WordPress every time I get an idea to blog. The problem was I had no easy way to see which drafts I had already started working on. This plugin allows me to sort all of my posts, including my drafts, by the posts word count.
     16
    1517== Installation ==
    1618
     
    18201. Activate the plugin through the 'Plugins' menu in WordPresss
    1921
     22== Screenshots ==
     23
     241. Word count on post list
     25
    2026== Changelog ==
     27
     28= 1.2 =
     29* Process post meta before display
     30* Upload screenshot
    2131
    2232= 1.1 =
Note: See TracChangeset for help on using the changeset viewer.