Changeset 569519
- Timestamp:
- 07/09/2012 05:03:50 PM (14 years ago)
- Location:
- page-tagger/trunk
- Files:
-
- 3 edited
-
README.txt (modified) (2 diffs)
-
page-tagger-class.php (modified) (10 diffs)
-
page-tagger.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
page-tagger/trunk/README.txt
r546205 r569519 5 5 Requires at least: 2.8.4 6 6 Tested up to: 3.3.2 7 Stable tag: 0. 3.87 Stable tag: 0.4 8 8 9 9 … … 23 23 24 24 == 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 25 29 26 30 = 0.3.8 (May 19, 2012) = -
page-tagger/trunk/page-tagger-class.php
r546205 r569519 21 21 /** 22 22 * The class. Handles everything. 23 */ 23 */ 24 24 class PageTagger 25 25 { 26 26 static $instance = NULL; 27 27 28 28 private $using_wordpress_3_or_above = FALSE; 29 29 30 30 static function init() 31 31 { … … 33 33 self::$instance = new PageTagger(); 34 34 } 35 35 36 36 function PageTagger() 37 37 { … … 41 41 // for WP 3 we will do the 'is editing page' check later 42 42 global $pagenow; 43 if ($this->using_wordpress_3_or_above || 43 if ($this->using_wordpress_3_or_above || 44 44 (isset($pagenow) && in_array( $pagenow, array('page.php', 'page-new.php') )) ) 45 45 { 46 46 add_action('admin_head',array(&$this, 'admin_head_hook') ); 47 } 48 47 } 48 49 49 add_action('pre_get_posts', array(&$this, 'setup_query_vars_for_posts'), 100); 50 50 add_action('init',array(&$this,'setup_tag_post_count_callback'),100); 51 51 } 52 53 54 55 52 53 54 55 56 56 /** 57 57 * Stuff to do for the 'admin_head' hook. 58 */ 58 */ 59 59 function admin_head_hook() 60 60 { … … 65 65 global $current_screen, $editing; 66 66 if (!$editing || empty($current_screen) || 'page' != $current_screen->post_type) 67 return; 68 } 69 67 return; 68 } 69 70 70 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', 72 72 array( 73 73 'tagsUsed' => __('Tags used on this page:'), … … 76 76 ); 77 77 wp_print_scripts('page-tagger'); 78 78 79 79 add_meta_box('tagsdiv-post_tag', __('Tags'), array(&$this, 'add_tags_meta_box'), 'page', 'side', 'default'); 80 80 } 81 81 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 86 86 * associated with a tag. 87 87 * @return unknown_type 88 */ 88 */ 89 89 function setup_tag_post_count_callback() 90 90 { … … 96 96 } 97 97 } 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 102 102 * from the db). 103 */ 103 */ 104 104 function setup_query_vars_for_posts(&$query) 105 105 { … … 108 108 { 109 109 $q = &$query->query_vars; 110 110 111 111 // set the post-type to 'any' so that pages are also included 112 112 $q['post_type'] = 'any'; 113 113 } 114 114 } 115 116 117 118 115 116 117 118 119 119 /** 120 120 * 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. 123 123 * 124 124 * @param object $post 125 125 */ 126 function add_tags_meta_box($post, $box) 126 function add_tags_meta_box($post, $box) 127 127 { 128 128 $css = ($this->using_wordpress_3_or_above ? 'wp3' : ''); 129 129 130 130 $tax_name = esc_attr(substr($box['id'], 8)); 131 131 $taxonomy = get_taxonomy($tax_name); … … 137 137 <p><?php _e('Add or remove tags'); ?></p> 138 138 <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 140 140 <span class="ajaxtag hide-if-no-js"> 141 141 <label class="screen-reader-text" for="new-tag-<?php echo $tax_name; ?>"><?php echo $box['title']; ?></label> … … 149 149 <?php 150 150 } 151 152 153 154 151 152 153 154 155 155 /** 156 156 * Custom version of the Wordpress taxonomy 'update term count' callback method. 157 * 158 * This will update term count pas ed on posts, pages and on custom post types.157 * 158 * This will update term count passed on posts, pages and on custom post types. 159 159 * @param array $terms List of Term taxonomy IDs 160 160 */ … … 167 167 $cptSql = ' OR ' . $cptSql; 168 168 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 ) ); 171 171 $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) ); 172 172 } 173 } 173 } 174 174 175 175 -
page-tagger/trunk/page-tagger.php
r466411 r569519 48 48 else 49 49 { 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 } 52 93 } 53 94
Note: See TracChangeset
for help on using the changeset viewer.