Skip to content

Instantly share code, notes, and snippets.

@anthonycole
Last active December 15, 2015 16:19
Show Gist options
  • Save anthonycole/5288644 to your computer and use it in GitHub Desktop.
Save anthonycole/5288644 to your computer and use it in GitHub Desktop.
A simple "tracker" for seeing what WordPress posts a user looks at.
<?php
/*
Plugin Name: Maybelline
Author: Anthony Cole
Author URI: http://anthonycole.me/
*/
/**
* WP_Maybelline
*
* @package default
* @author
**/
Class WP_Maybelline
{
public static function install()
{
$sql = "CREATE TABLE `wp_maybelline` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`post_id` int(11) DEFAULT NULL,
`timestamp` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;";
}
/**
* Add a "view"
*
* @return void
* @author
**/
public static function add_view($user_id, $post_id, $time = '') {
if( '' == $time )
$time = date("Y-m-d H:i:s");
$query = $wpdb->prepare("INSERT INTO wp_maybelline (name, user_id, post_id) VALUES(%s, %s, %s)", $user_id, $post_id, $time);
$wpdb->query($query);
do_action('wp_maybelline_add_view');
}
/**
* Get all of the posts a user has looked at.
*
* @return void
* @author
**/
public static function get_views_by_user($user_id, $limit = 5) {
$query = $wpdb->prepare("SELECT DISTINCT * FROM wp_maybelline WHERE user_id = %d LIMIT", $user_id, $limit );
$wpdb->get_results($query);
do_action('wp_maybelline_get_user_views', $user_id);
}
/**
* Get all of the users that have looked at a specified post.
*
* @return void
* @author
**/
public static function get_views_by_post($post_id) {
$query = $wpdb->prepare("SELECT DISTINCT * FROM wp_maybelline WHERE post_id = %d LIMIT", $post_id, $limit );
$wpdb->get_results($query);
do_action('wp_maybelline_get_post_views', $post_id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment