Plugin Directory

Changeset 569519


Ignore:
Timestamp:
07/09/2012 05:03:50 PM (14 years ago)
Author:
randomaniac
Message:

Simplified and improved integration for WP 3 and above - thanks Bjorn Wijers

Ensure tag counts only get updated for non-draft posts

Location:
page-tagger/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • page-tagger/trunk/README.txt

    r546205 r569519  
    55Requires at least: 2.8.4
    66Tested up to: 3.3.2
    7 Stable tag: 0.3.8
     7Stable tag: 0.4
    88
    99
     
    2323
    2424== Changelog ==
     25
     26= 0.4 (Jul 19, 2012) =
     27* Simplified and improved integration for WP 3 and above - thanks Bjorn Wijers
     28* Ensure tag counts only get updated for non-draft posts
    2529
    2630= 0.3.8 (May 19, 2012) =
  • page-tagger/trunk/page-tagger-class.php

    r546205 r569519  
    2121/**
    2222 * The class. Handles everything.
    23  */ 
     23 */
    2424class PageTagger
    2525{
    2626    static $instance = NULL;
    27    
     27
    2828    private $using_wordpress_3_or_above = FALSE;
    29    
     29
    3030    static function init()
    3131    {
     
    3333            self::$instance = new PageTagger();
    3434    }
    35    
     35
    3636    function PageTagger()
    3737    {
     
    4141        // for WP 3 we will do the 'is editing page' check later
    4242        global $pagenow;
    43         if ($this->using_wordpress_3_or_above || 
     43        if ($this->using_wordpress_3_or_above ||
    4444                (isset($pagenow) && in_array( $pagenow, array('page.php', 'page-new.php') )) )
    4545        {
    4646            add_action('admin_head',array(&$this, 'admin_head_hook') );
    47         }   
    48        
     47        }
     48
    4949        add_action('pre_get_posts', array(&$this, 'setup_query_vars_for_posts'), 100);
    5050        add_action('init',array(&$this,'setup_tag_post_count_callback'),100);
    5151    }
    52    
    53 
    54    
    55    
     52
     53
     54
     55
    5656    /**
    5757     * Stuff to do for the 'admin_head' hook.
    58      */ 
     58     */
    5959    function admin_head_hook()
    6060    {
     
    6565            global $current_screen, $editing;
    6666            if (!$editing || empty($current_screen) || 'page' != $current_screen->post_type)
    67                 return;         
    68         }
    69        
     67                return;
     68        }
     69
    7070        wp_register_script('page-tagger', WP_PLUGIN_URL.'/'.PAGE_TAGGER_PARENT_DIR.'/page-tagger.js', array('jquery','suggest'), false);
    71         wp_localize_script('page-tagger','pageTaggerL10n', 
     71        wp_localize_script('page-tagger','pageTaggerL10n',
    7272                array(
    7373                    'tagsUsed' =>  __('Tags used on this page:'),
     
    7676        );
    7777        wp_print_scripts('page-tagger');
    78        
     78
    7979        add_meta_box('tagsdiv-post_tag', __('Tags'), array(&$this, 'add_tags_meta_box'), 'page', 'side', 'default');
    8080    }
    8181
    82    
    83 
    84     /**
    85      * Setup the callback that will get used for counting the no. of posts 
     82
     83
     84    /**
     85     * Setup the callback that will get used for counting the no. of posts
    8686     * associated with a tag.
    8787     * @return unknown_type
    88      */ 
     88     */
    8989    function setup_tag_post_count_callback()
    9090    {
     
    9696        }
    9797    }
    98    
    99    
    100     /**
    101      * Set the Wordpress query variables (this determines what gets loaded 
     98
     99
     100    /**
     101     * Set the Wordpress query variables (this determines what gets loaded
    102102     * from the db).
    103      */     
     103     */
    104104    function setup_query_vars_for_posts(&$query)
    105105    {
     
    108108        {
    109109            $q = &$query->query_vars;
    110            
     110
    111111            // set the post-type to 'any' so that pages are also included
    112112            $q['post_type'] = 'any';
    113113        }
    114114    }
    115        
    116 
    117    
    118    
     115
     116
     117
     118
    119119    /**
    120120     * Display page tags form fields.
    121      * 
    122      * This is coped from wp-admin/edit-form-advanced.php. 
     121     *
     122     * This is coped from wp-admin/edit-form-advanced.php.
    123123     *
    124124     * @param object $post
    125125     */
    126     function add_tags_meta_box($post, $box) 
     126    function add_tags_meta_box($post, $box)
    127127    {
    128128        $css = ($this->using_wordpress_3_or_above ? 'wp3' : '');
    129        
     129
    130130        $tax_name = esc_attr(substr($box['id'], 8));
    131131        $taxonomy = get_taxonomy($tax_name);
     
    137137            <p><?php _e('Add or remove tags'); ?></p>
    138138            <textarea name="<?php echo "tax_input[$tax_name]"; ?>" class="the-tags" id="tax-input[<?php echo $tax_name; ?>]"><?php echo esc_attr(get_terms_to_edit( $post->ID, $tax_name )); ?></textarea></div>
    139        
     139
    140140            <span class="ajaxtag hide-if-no-js">
    141141                <label class="screen-reader-text" for="new-tag-<?php echo $tax_name; ?>"><?php echo $box['title']; ?></label>
     
    149149    <?php
    150150    }
    151    
    152 
    153    
    154    
     151
     152
     153
     154
    155155    /**
    156156     * Custom version of the Wordpress taxonomy 'update term count' callback method.
    157      * 
    158      * This will update term count pased on posts, pages and on custom post types.
     157     *
     158     * This will update term count passed on posts, pages and on custom post types.
    159159     * @param array $terms List of Term taxonomy IDs
    160160     */
     
    167167            $cptSql = ' OR ' . $cptSql;
    168168
    169         foreach ( (array) $terms as $term ) {
    170             $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND (post_type = 'post' OR post_type = 'page' $cptSql) AND term_taxonomy_id = %d", $term ) );
     169        foreach ( (array) $terms as $term ) {
     170            $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND $wpdb->posts.post_status != 'draft' AND (post_type = 'post' OR post_type = 'page' $cptSql) AND term_taxonomy_id = %d", $term ) );
    171171            $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
    172172        }
    173     }   
     173    }
    174174
    175175
  • page-tagger/trunk/page-tagger.php

    r466411 r569519  
    4848else
    4949{
    50     require_once('page-tagger-class.php');
    51     add_action('plugins_loaded',array('PageTagger','init'));
     50  // if we're at version 3 or above then we can keep it simple using WP hooks:
     51  $wp_version = floatval(get_bloginfo('version'));
     52  if (3 <= $wp_version)
     53  {
     54    // Based on code by Bjorn Wijers at https://github.com/BjornW/tag-pages
     55
     56    /**
     57     * Add the 'post_tag' taxonomy, which is the name of the existing taxonomy
     58     * used for tags to the Post type page. Normally in WordPress Pages cannot
     59     * be tagged, but this let's WordPress treat Pages just like Posts
     60     * and enables the tags metabox so you can add tags to a Page.
     61     * NB: This uses the register_taxonomy_for_object_type() function which is only
     62     * in WordPress 3 and higher!
     63     */
     64    if( ! function_exists('page_tagger_register_taxonomy') ){
     65        function page_tagger_register_taxonomy()
     66        {
     67            register_taxonomy_for_object_type('post_tag', 'page');
     68        }
     69        add_action('admin_init', 'page_tagger_register_taxonomy');
     70    }
     71
     72    /**
     73     * Display all post_types on the tags archive page. This forces WordPress to
     74     * show tagged Pages together with tagged Posts.
     75     */
     76    if( ! function_exists('page_tagger_display_tagged_pages_archive') ){
     77        function page_tagger_display_tagged_pages_archive(&$query)
     78        {
     79            if ( $query->is_archive && $query->is_tag ) {
     80                $q = &$query->query_vars;
     81                $q['post_type'] = 'any';
     82            }
     83        }
     84        add_action('pre_get_posts', 'page_tagger_display_tagged_pages_archive');
     85    }
     86  }
     87  // if we're before version 3
     88  else
     89  {
     90    require_once('page-tagger-class.php');
     91    add_action('plugins_loaded',array('PageTagger','init'));
     92  }
    5293}
    5394
Note: See TracChangeset for help on using the changeset viewer.