columns.php
11 years ago
counter.php
11 years ago
cron.php
11 years ago
frontend.php
11 years ago
functions.php
11 years ago
query.php
11 years ago
settings.php
11 years ago
update.php
11 years ago
widgets.php
11 years ago
columns.php
209 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) |
| 3 | exit; |
| 4 | |
| 5 | new Post_Views_Counter_Columns(); |
| 6 | |
| 7 | class Post_Views_Counter_Columns { |
| 8 | |
| 9 | public function __construct() { |
| 10 | // actions |
| 11 | add_action( 'current_screen', array( &$this, 'register_new_column' ) ); |
| 12 | add_action( 'post_submitbox_misc_actions', array( &$this, 'submitbox_views' ) ); |
| 13 | add_action( 'save_post', array( &$this, 'save_post' ), 10, 2 ); |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Output post views for single post. |
| 18 | */ |
| 19 | public function submitbox_views() { |
| 20 | global $post; |
| 21 | |
| 22 | $post_types = Post_Views_Counter()->get_attribute( 'options', 'general', 'post_types_count' ); |
| 23 | |
| 24 | if ( ! in_array( $post->post_type, (array) $post_types ) ) |
| 25 | return; |
| 26 | |
| 27 | global $wpdb; |
| 28 | |
| 29 | // get total post views |
| 30 | $views = $wpdb->get_var( |
| 31 | $wpdb->prepare( " |
| 32 | SELECT count |
| 33 | FROM " . $wpdb->prefix . "post_views |
| 34 | WHERE id = %d AND type = 4", absint( $post->ID ) |
| 35 | ) |
| 36 | ); |
| 37 | ?> |
| 38 | |
| 39 | <div class="misc-pub-section" id="post-views"> |
| 40 | |
| 41 | <?php wp_nonce_field( 'post_views_count', 'pvc_nonce' ); ?> |
| 42 | |
| 43 | <span id="post-views-display"> |
| 44 | |
| 45 | <?php echo __( 'Post Views', 'post-views-counter' ) . ': <b>' . number_format_i18n( (int) $views ) . '</b>'; ?> |
| 46 | |
| 47 | </span> |
| 48 | |
| 49 | <a href="#post-views" class="edit-post-views hide-if-no-js"><?php _e( 'Edit', 'post-views-counter' ); ?></a> |
| 50 | |
| 51 | <div id="post-views-input-container" class="hide-if-js"> |
| 52 | |
| 53 | <p><?php _e( 'Adjust the views count for this post.', 'post-views-counter' ); ?></p> |
| 54 | <input type="hidden" name="current_post_views" id="post-views-current" value="<?php echo (int) $views; ?>" /> |
| 55 | <input type="text" name="post_views" id="post-views-input" value="<?php echo (int) $views; ?>"/><br /> |
| 56 | <p> |
| 57 | <a href="#post-views" class="save-post-views hide-if-no-js button"><?php _e( 'OK', 'post-views-counter' ); ?></a> |
| 58 | <a href="#post-views" class="cancel-post-views hide-if-no-js"><?php _e( 'Cancel', 'post-views-counter' ); ?></a> |
| 59 | </p> |
| 60 | |
| 61 | </div> |
| 62 | |
| 63 | </div> |
| 64 | <?php |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Save post views data |
| 69 | */ |
| 70 | public function save_post( $post_id, $post ) { |
| 71 | |
| 72 | // break if doing autosave |
| 73 | if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) |
| 74 | return $post_id; |
| 75 | |
| 76 | // break if current user can't edit this post |
| 77 | if ( ! current_user_can( 'edit_post', $post_id ) ) |
| 78 | return $post_id; |
| 79 | |
| 80 | // is post views set |
| 81 | if ( ! isset( $_POST['post_views'] ) ) |
| 82 | return $post_id; |
| 83 | |
| 84 | // break if post views in not one of the selected |
| 85 | $post_types = Post_Views_Counter()->get_attribute( 'options', 'general', 'post_types_count' ); |
| 86 | |
| 87 | if ( ! in_array( $post->post_type, (array) $post_types ) ) |
| 88 | return $post_id; |
| 89 | |
| 90 | // validate data |
| 91 | if ( ! isset( $_POST['pvc_nonce'] ) || ! wp_verify_nonce( $_POST['pvc_nonce'], 'post_views_count' ) ) |
| 92 | return $post_id; |
| 93 | |
| 94 | global $wpdb; |
| 95 | |
| 96 | $count = apply_filters( 'pvc_update_post_views_count', absint( $_POST['post_views'] ), $post_id ); |
| 97 | |
| 98 | // insert or update db post views count |
| 99 | $wpdb->query( |
| 100 | $wpdb->prepare( " |
| 101 | INSERT INTO " . $wpdb->prefix . "post_views (id, type, period, count) |
| 102 | VALUES (%d, %d, %s, %d) |
| 103 | ON DUPLICATE KEY UPDATE count = %d", $post_id, 4, 'total', $count, $count |
| 104 | ) |
| 105 | ); |
| 106 | |
| 107 | do_action( 'pvc_after_update_post_views_count', $post_id ); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Register post views column for specific post types |
| 112 | */ |
| 113 | public function register_new_column() { |
| 114 | $screen = get_current_screen(); |
| 115 | |
| 116 | if ( Post_Views_Counter()->get_attribute( 'options', 'general', 'post_views_column' ) && ($screen->base == 'edit' && in_array( $screen->post_type, Post_Views_Counter()->get_attribute( 'options', 'general', 'post_types_count' ) )) ) { |
| 117 | |
| 118 | foreach ( Post_Views_Counter()->get_attribute( 'options', 'general', 'post_types_count' ) as $post_type ) { |
| 119 | |
| 120 | if ( $post_type === 'page' && $screen->post_type === 'page' ) { |
| 121 | // actions |
| 122 | add_action( 'manage_pages_custom_column', array( &$this, 'add_new_column_content' ), 10, 2 ); |
| 123 | |
| 124 | // filters |
| 125 | add_filter( 'manage_pages_columns', array( &$this, 'add_new_column' ) ); |
| 126 | add_filter( 'manage_edit-page_sortable_columns', array( &$this, 'register_sortable_custom_column' ) ); |
| 127 | } elseif ( $post_type === 'post' && $screen->post_type === 'post' ) { |
| 128 | // actions |
| 129 | add_action( 'manage_posts_custom_column', array( &$this, 'add_new_column_content' ), 10, 2 ); |
| 130 | |
| 131 | // filters |
| 132 | add_filter( 'manage_posts_columns', array( &$this, 'add_new_column' ) ); |
| 133 | add_filter( 'manage_edit-post_sortable_columns', array( &$this, 'register_sortable_custom_column' ) ); |
| 134 | } elseif ( $screen->post_type === $post_type ) { |
| 135 | // actions |
| 136 | add_action( 'manage_' . $post_type . '_posts_custom_column', array( &$this, 'add_new_column_content' ), 10, 2 ); |
| 137 | |
| 138 | // filters |
| 139 | add_filter( 'manage_' . $post_type . '_posts_columns', array( &$this, 'add_new_column' ) ); |
| 140 | add_filter( 'manage_edit-' . $post_type . '_sortable_columns', array( &$this, 'register_sortable_custom_column' ) ); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Register sortable post views column |
| 148 | */ |
| 149 | public function register_sortable_custom_column( $columns ) { |
| 150 | // add new sortable column |
| 151 | $columns['post_views'] = 'post_views'; |
| 152 | |
| 153 | return $columns; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Add post views column |
| 158 | */ |
| 159 | public function add_new_column( $columns ) { |
| 160 | $offset = 0; |
| 161 | |
| 162 | if ( isset( $columns['date'] ) ) |
| 163 | $offset ++; |
| 164 | |
| 165 | if ( isset( $columns['comments'] ) ) |
| 166 | $offset ++; |
| 167 | |
| 168 | if ( $offset > 0 ) { |
| 169 | $date = array_slice( $columns, -$offset, $offset, true ); |
| 170 | |
| 171 | foreach ( $date as $column => $name ) { |
| 172 | unset( $columns[$column] ); |
| 173 | } |
| 174 | |
| 175 | $columns['post_views'] = __( 'Post Views', 'post-views-counter' ); |
| 176 | |
| 177 | foreach ( $date as $column => $name ) { |
| 178 | $columns[$column] = $name; |
| 179 | } |
| 180 | } else |
| 181 | $columns['post_views'] = __( 'Post Views', 'post-views-counter' ); |
| 182 | |
| 183 | return $columns; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Add post views column content |
| 188 | */ |
| 189 | public function add_new_column_content( $column_name, $id ) { |
| 190 | |
| 191 | if ( $column_name === 'post_views' ) { |
| 192 | |
| 193 | global $wpdb; |
| 194 | |
| 195 | // get total post views |
| 196 | $views = $wpdb->get_var( |
| 197 | $wpdb->prepare( " |
| 198 | SELECT count |
| 199 | FROM " . $wpdb->prefix . "post_views |
| 200 | WHERE id = %d AND type = 4", $id |
| 201 | ) |
| 202 | ); |
| 203 | |
| 204 | echo number_format_i18n( (int) $views ); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | } |
| 209 |