Skip to content

Instantly share code, notes, and snippets.

@tareq1988
Created March 7, 2013 19:46
Show Gist options
  • Save tareq1988/5111149 to your computer and use it in GitHub Desktop.
Save tareq1988/5111149 to your computer and use it in GitHub Desktop.
<?php
/**
* Entry Views - for counting single post views.
*
* Borrowed from Hybrid Core framework and adapted for weDevs Framework
* @package WeDevs Framework
*/
class WeDevs_Entry_Views {
public function __construct() {
$this->actions();
$this->add_shortcodes();
}
private function actions() {
/* Registers the entry views extension scripts if we're on the correct page. */
add_action( 'template_redirect', array( $this, 'load_views' ) );
/* Add the entry views AJAX actions to the appropriate hooks. */
add_action( 'wp_ajax_entry_views', array( $this, 'update_ajax' ) );
add_action( 'wp_ajax_nopriv_entry_views', array( $this, 'update_ajax' ) );
add_filter( 'manage_posts_columns', array( $this, 'admin_table_columns' ) );
add_filter( 'manage_posts_custom_column', array( $this, 'admin_column_value' ), 10, 2 );
}
function add_shortcodes() {
/* Add the [entry-views] shortcode. */
add_shortcode( 'entry-views', array( $this, 'get_views' ) );
}
/**
* Displays a small script that sends an AJAX request for the page. It passes the $post_id to the AJAX
* callback function for updating the meta.
*/
function load_scripts() {
global $entry_views;
/* Create a nonce for the AJAX request. */
$nonce = wp_create_nonce( 'entry_views_ajax' );
/* Display the JavaScript needed. */
echo '<script type="text/javascript">/* <![CDATA[ */ jQuery(document).ready( function() { jQuery.post( "' . admin_url( 'admin-ajax.php' ) . '", { action : "entry_views", _ajax_nonce : "' . $nonce . '", post_id : ' . get_the_ID() . ' } ); } ); /* ]]> */</script>' . "\n";
}
/**
* Checks if we're on a singular post view and if the current post type supports the 'entry-views'
* extension. If so, set the $post_id variable and load the needed JavaScript.
*/
function load_views() {
/* Check if we're on a singular post view. */
if ( is_singular() ) {
/* Enqueue the jQuery library. */
wp_enqueue_script( 'jquery' );
/* Load the entry views JavaScript in the footer. */
add_action( 'wp_footer', array( $this, 'load_scripts' ) );
}
}
/**
* Updates the number of views when on a singular view of a post. This function uses post meta to store
* the number of views per post. By default, the meta key is 'Views', but you can filter this with the
* 'entry_views_meta_key' hook.
*/
function update_view( $post_id = '' ) {
/* If we're on a singular view of a post, calculate the number of views. */
if ( !empty( $post_id ) ) {
/* Allow devs to override the meta key used. By default, this is 'Views'. */
$meta_key = apply_filters( 'entry_views_meta_key', 'Views' );
/* Get the number of views the post currently has. */
$old_views = get_post_meta( $post_id, $meta_key, true );
/* Add +1 to the number of current views. */
$new_views = absint( $old_views ) + 1;
/* Update the view count with the new view count. */
update_post_meta( $post_id, $meta_key, $new_views, $old_views );
}
}
/**
* Gets the number of views a specific post has. It also doubles as a shortcode, which is called with the
* [entry-views] format.
*
* @param array $attr Attributes for use in the shortcode.
*/
function get_views( $attr = '' ) {
/* Merge the defaults and the given attributes. */
$attr = shortcode_atts( array('before' => '', 'after' => '', 'post_id' => get_the_ID()), $attr );
/* Allow devs to override the meta key used. */
$meta_key = apply_filters( 'entry_views_meta_key', 'Views' );
/* Get the number of views the post has. */
$views = intval( get_post_meta( $attr['post_id'], $meta_key, true ) );
/* Returns the formatted number of views. */
return $attr['before'] . number_format_i18n( $views ) . $attr['after'];
}
/**
* Callback function hooked to 'wp_ajax_entry_views' and 'wp_ajax_nopriv_entry_views'. It checks the
* AJAX nonce and passes the given $post_id to the entry views update function.
*/
function update_ajax() {
/* Check the AJAX nonce to make sure this is a valid request. */
check_ajax_referer( 'entry_views_ajax' );
/* If the post ID is set, set it to the $post_id variable and make sure it's an integer. */
if ( isset( $_POST['post_id'] ) ) {
$post_id = absint( $_POST['post_id'] );
}
/* If $post_id isn't empty, pass it to the entry_views_update() function to update the view count. */
if ( !empty( $post_id ) ) {
$this->update_view( $post_id );
}
exit;
}
/**
* Ads additional columns to admin user table
*
* @param array $columns
* @return array
*/
function admin_table_columns( $columns ) {
$columns['views'] = __( 'Views' );
return $columns;
}
/**
* Adds column value to custom admin user table columns
*
* @param string $column
* @param int $post_id
*/
function admin_column_value( $column, $post_id ) {
switch ($column) {
case 'views':
echo number_format_i18n( (int) get_post_meta( $post_id, 'Views', true) );
break;
}
}
}
$entry_views = new WeDevs_Entry_Views();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment