Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Taxonomy View Count
- Plugin URI: http://www.anhtuannd.com
- Description: Get Taxonomy View Count
- Author: anhtuannd, mitcho (Michael Yoshitaka Erlewine), sirzooro
- Author URI:http://www.anhtuannd.com
- Version: 1.0.0
- */
- class Taxonomy_Metadata {
- function __construct() {
- add_action( 'init', array($this, 'wpdbfix') );
- }
- /*
- * Quick touchup to wpdb
- */
- function wpdbfix() {
- global $wpdb;
- $wpdb->taxonomymeta = "{$wpdb->prefix}taxonomymeta";
- }
- /*
- * TABLE MANAGEMENT
- */
- function activate() {
- global $wpdb;
- $this->setup_blog();
- }
- function setup_blog() {
- global $wpdb;
- $charset_collate = '';
- if ( ! empty($wpdb->charset) )
- $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
- if ( ! empty($wpdb->collate) )
- $charset_collate .= " COLLATE $wpdb->collate";
- $tables = $wpdb->get_results("show tables like '{$wpdb->prefix}taxonomymeta'");
- if (!count($tables))
- $wpdb->query("CREATE TABLE {$wpdb->prefix}taxonomymeta (
- meta_id bigint(20) unsigned NOT NULL auto_increment,
- taxonomy_id bigint(20) unsigned NOT NULL default '0',
- meta_key varchar(255) default NULL,
- meta_value longtext,
- PRIMARY KEY (meta_id),
- KEY taxonomy_id (taxonomy_id),
- KEY meta_key (meta_key)
- ) $charset_collate;");
- }
- }
- $taxonomy_metadata = new Taxonomy_Metadata;
- register_activation_hook( __FILE__, array($taxonomy_metadata, 'activate') );
- function get_term_id() {
- $term = get_queried_object();
- if ( !$term )
- return 0;
- if ( empty( $term->term_id ) )
- return 0;
- return $term->term_id;
- }
- // Template tag functions
- // Display view count of current term
- function display_term_view() {
- $term_id = get_term_id();
- $count = get_metadata('taxonomy', $term_id, 'views', true);
- if ($count === false) {
- add_metadata('taxonomy', $term_id, 'views', 1, false);
- if (!is_user_logged_in() && !is_bot()) return 1;
- else return 0;
- }
- else {
- if (!is_user_logged_in() && !is_bot()) {
- $count++;
- update_metadata('taxonomy', $term_id, 'views', $count, '');
- return $count;
- }
- else return $count;
- }
- }
- function hidden_term_view() {
- display_term_view();
- }
- function is_bot () {
- $botlist = array("Googlebot", "MSNBOT");
- $bottest = strtolower($_SERVER['HTTP_USER_AGENT']);
- foreach($botlist as $bot) {
- if(strpos($bottest,$bot)!==false) {
- $thebot = $bot;
- break;
- }
- }
- return ( $thebot ? $thebot : false );
- }
- add_filter('manage_edit-category_columns', 'add_view_column', 10, 2);
- add_filter('manage_edit-post_tag_columns', 'add_view_column', 10, 2);
- add_action( 'init', 'add_custom_taxonomy_view', 1000 );
- function add_custom_taxonomy_view() {
- $args=array(
- '_builtin' => false
- );
- $output = 'names'; // or objects
- $taxonomies = get_taxonomies($args,$output);
- //print_r($taxonomies);
- if ($taxonomies) {
- foreach ($taxonomies as $taxonomy ) {
- add_filter('manage_edit-'.$taxonomy.'_columns', 'add_view_column', 10, 2);
- add_filter('manage_'.$taxonomy.'_custom_column' , 'add_taxonomy_view_column', 10, 3 );
- add_filter( 'manage_edit-'.$taxonomy.'_sortable_columns', 'add_view_sort_column' );
- }
- }
- }
- function add_view_column($columns)
- {
- $columns['views'] = 'View';
- return $columns;
- }
- function add_view_sort_column($columns)
- {
- $columns['views'] = 'view';
- return $columns;
- }
- add_filter( 'manage_category_custom_column' , 'add_taxonomy_view_column', 10, 3 );
- add_filter( 'manage_edit-category_sortable_columns', 'add_view_sort_column' );
- add_filter( 'manage_post_tag_custom_column' , 'add_taxonomy_view_column', 10, 3 );
- add_filter( 'manage_edit-post_tag_sortable_columns', 'add_view_sort_column' );
- function add_taxonomy_view_column($blank, $column, $term_id ) {
- return getViewFromTerm($term_id);
- }
- function getViewFromTerm($term_id) {
- $count = get_metadata('taxonomy', $term_id, 'views', true);
- if (($count === false) || !isset($count)||(trim($count)=='')) {
- add_metadata('taxonomy', $term_id, 'views', 0, false);
- return 0;
- }
- return $count;
- }
- function add_term_meta_query( $terms, $taxonomies, $args) {
- if(isset($args['orderby'])&&($args['orderby']=='view')) {
- $data = array();
- $index = array();
- foreach($terms as $term) {
- $data[] = $term;
- $index[] = getViewFromTerm($term->term_id);
- }
- if (trim($args['order']=='asc')) $sort = SORT_ASC;
- else if (trim($args['order']=='desc')) $sort = SORT_DESC;
- else $sort = SORT_ASC;
- array_multisort($index, $sort, $data);
- return $data;
- }
- else return $terms;
- }
- add_filter( 'get_terms', 'add_term_meta_query', 10, 3);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement