Advertisement
pjeaje

Count View Custom Terms

Mar 23rd, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.74 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Taxonomy View Count
  4. Plugin URI: http://www.anhtuannd.com
  5. Description: Get Taxonomy View Count
  6. Author: anhtuannd, mitcho (Michael Yoshitaka Erlewine), sirzooro
  7. Author URI:http://www.anhtuannd.com
  8. Version: 1.0.0
  9. */
  10.  
  11. class Taxonomy_Metadata {
  12.     function __construct() {
  13.         add_action( 'init', array($this, 'wpdbfix') );
  14.     }
  15.  
  16.     /*
  17.      * Quick touchup to wpdb
  18.      */
  19.     function wpdbfix() {
  20.         global $wpdb;
  21.         $wpdb->taxonomymeta = "{$wpdb->prefix}taxonomymeta";
  22.     }
  23.    
  24.     /*
  25.      * TABLE MANAGEMENT
  26.      */
  27.  
  28.     function activate() {
  29.         global $wpdb;
  30.  
  31.         $this->setup_blog();
  32.     }
  33.    
  34.     function setup_blog() {
  35.         global $wpdb;
  36.  
  37.         $charset_collate = '';  
  38.         if ( ! empty($wpdb->charset) )
  39.             $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
  40.         if ( ! empty($wpdb->collate) )
  41.             $charset_collate .= " COLLATE $wpdb->collate";
  42.  
  43.         $tables = $wpdb->get_results("show tables like '{$wpdb->prefix}taxonomymeta'");
  44.         if (!count($tables))
  45.             $wpdb->query("CREATE TABLE {$wpdb->prefix}taxonomymeta (
  46.                 meta_id bigint(20) unsigned NOT NULL auto_increment,
  47.                 taxonomy_id bigint(20) unsigned NOT NULL default '0',
  48.                 meta_key varchar(255) default NULL,
  49.                 meta_value longtext,
  50.                 PRIMARY KEY (meta_id),
  51.                 KEY taxonomy_id (taxonomy_id),
  52.                 KEY meta_key (meta_key)
  53.                 ) $charset_collate;");
  54.     }
  55. }
  56. $taxonomy_metadata = new Taxonomy_Metadata;
  57. register_activation_hook( __FILE__, array($taxonomy_metadata, 'activate') );
  58.  
  59. function get_term_id() {
  60.     $term = get_queried_object();
  61.  
  62.     if ( !$term )
  63.         return 0;
  64.  
  65.     if ( empty( $term->term_id ) )
  66.         return 0;
  67.  
  68.     return $term->term_id;
  69. }
  70.  
  71. // Template tag functions
  72. // Display view count of current term
  73. function display_term_view() {
  74.  
  75.     $term_id = get_term_id();
  76.  
  77.     $count = get_metadata('taxonomy', $term_id, 'views', true);
  78.     if ($count === false) {
  79.         add_metadata('taxonomy', $term_id, 'views', 1, false);
  80.         if (!is_user_logged_in() && !is_bot()) return 1;
  81.         else return 0;
  82.     }
  83.     else {
  84.         if (!is_user_logged_in() && !is_bot()) {
  85.             $count++;
  86.             update_metadata('taxonomy', $term_id, 'views', $count, '');
  87.             return $count;
  88.         }
  89.         else return $count;
  90.     }
  91. }
  92.  
  93. function hidden_term_view() {
  94.     display_term_view();
  95. }
  96.  
  97. function is_bot () {
  98.     $botlist = array("Googlebot", "MSNBOT");
  99.     $bottest = strtolower($_SERVER['HTTP_USER_AGENT']);
  100.  
  101.     foreach($botlist as $bot) {
  102.         if(strpos($bottest,$bot)!==false) {
  103.             $thebot = $bot;
  104.             break;
  105.         }
  106.     }
  107.     return ( $thebot ? $thebot : false );
  108. }
  109.  
  110. add_filter('manage_edit-category_columns', 'add_view_column', 10, 2);
  111. add_filter('manage_edit-post_tag_columns', 'add_view_column', 10, 2);
  112. add_action( 'init', 'add_custom_taxonomy_view', 1000 );
  113.  
  114. function add_custom_taxonomy_view() {
  115.     $args=array(
  116.         '_builtin' => false
  117.         );
  118.  
  119.     $output = 'names'; // or objects
  120.     $taxonomies = get_taxonomies($args,$output);
  121.     //print_r($taxonomies);
  122.     if  ($taxonomies) {
  123.         foreach ($taxonomies  as $taxonomy ) {
  124.             add_filter('manage_edit-'.$taxonomy.'_columns', 'add_view_column', 10, 2);
  125.             add_filter('manage_'.$taxonomy.'_custom_column' , 'add_taxonomy_view_column', 10, 3 );
  126.             add_filter( 'manage_edit-'.$taxonomy.'_sortable_columns', 'add_view_sort_column' );
  127.  
  128.         }
  129.     }
  130. }
  131.  
  132. function add_view_column($columns)
  133. {
  134.     $columns['views'] = 'View';
  135.     return $columns;
  136. }
  137.  
  138. function add_view_sort_column($columns)
  139. {
  140.     $columns['views'] = 'view';
  141.     return $columns;
  142. }
  143.  
  144.  
  145. add_filter( 'manage_category_custom_column' , 'add_taxonomy_view_column', 10, 3 );
  146. add_filter( 'manage_edit-category_sortable_columns', 'add_view_sort_column' );
  147.  
  148. add_filter( 'manage_post_tag_custom_column' , 'add_taxonomy_view_column', 10, 3 );
  149. add_filter( 'manage_edit-post_tag_sortable_columns', 'add_view_sort_column' );
  150.  
  151.  
  152. function add_taxonomy_view_column($blank, $column, $term_id ) {
  153.     return getViewFromTerm($term_id);
  154. }
  155.  
  156. function getViewFromTerm($term_id) {
  157.     $count = get_metadata('taxonomy', $term_id, 'views', true);
  158.     if (($count === false) || !isset($count)||(trim($count)=='')) {
  159.         add_metadata('taxonomy', $term_id, 'views', 0, false);
  160.         return 0;
  161.     }
  162.     return $count;
  163. }
  164.  
  165. function add_term_meta_query( $terms, $taxonomies, $args) {
  166.     if(isset($args['orderby'])&&($args['orderby']=='view')) {
  167.         $data = array();
  168.         $index = array();
  169.         foreach($terms as $term) {
  170.             $data[] = $term;
  171.             $index[] = getViewFromTerm($term->term_id);
  172.            
  173.         }
  174.         if (trim($args['order']=='asc')) $sort = SORT_ASC;
  175.         else if (trim($args['order']=='desc')) $sort = SORT_DESC;
  176.         else $sort = SORT_ASC;
  177.         array_multisort($index, $sort, $data);
  178.         return $data;
  179.     }
  180.     else return $terms;
  181. }
  182. add_filter( 'get_terms', 'add_term_meta_query', 10, 3);
  183.  
  184. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement