Last active
August 29, 2015 14:06
-
-
Save javorszky/1748e164f830c9900e00 to your computer and use it in GitHub Desktop.
This is the entire php code of the simpleviews plugin. I'm posting it here, because I need it repaired for a project, and the plugin doesn't have a github repository. Plugin url: http://wordpress.org/plugins/simple-post-views-counter/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Simple Post Views Counter | |
Plugin URI: http://wordpress.org/extend/plugins/simple-post-views-counter/ | |
Description: This plugin will enable you to display how many times a post has been viewed. The hits/views are displayed in the posts entry meta. Please refer to the included readme file for proper install instructions and use. | |
Version: 1.2 | |
Author: RS Publishing | |
License: GPLv2 | |
*/ | |
/* | |
Copyright 2012 Rynaldo Stoltz (email : [email protected]) | |
This program is free software; you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation; either version 2 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program; if not, write to the Free Software | |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
*/ | |
class SPVCO { | |
/** | |
* Creates the table on activation, if table doesn't exist yet. | |
* @return void creates a table. | |
*/ | |
public static function install() { | |
global $wpdb; | |
$table = $wpdb->prefix . "simpleviews"; | |
if ($wpdb->get_var( "SHOW TABLES LIKE '$table'" ) != $table ) { | |
$sql_array = array( | |
"CREATE TABLE " . $table, | |
"( UNIQUE KEY id (post_id),", | |
"post_id int(10) NOT NULL,", | |
"view int(10) default 0,", | |
"view_datetime datetime NOT NULL default '0000-00-00 00:00:00');" | |
); | |
$sql = join( ' ', $sql_array ); | |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); | |
dbDelta($sql); | |
} | |
} | |
/** | |
* On uninstall, drops the table. | |
* @return void drops the table | |
*/ | |
public static function drop() { | |
global $wpdb; | |
$table = $wpdb->prefix . "simpleviews"; | |
if ($wpdb->get_var( "SHOW TABLES LIKE '$table'" ) != $table ) { | |
$sql = "DROP TABLE " . $table; | |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); | |
dbDelta($sql); | |
} | |
} | |
/** | |
* Inserting views. Called from update views function. It only fires if there's no row for the | |
* post_id. In that case it inserts the view value of 1. | |
* @param integer $post_id The ID of the post in question | |
* @return integer the ID for the auto increment columns | |
*/ | |
public static function insert_views( $post_id ) { | |
global $wpdb; | |
$table = $wpdb->prefix . "simpleviews"; | |
$result = $wpdb->insert( $table, array( 'post_id' => $post_id, 'view' => 1 ) ); | |
return 1; | |
} | |
/** | |
* Increments the view count by 1 | |
* @param integer $post_id the ID of the post | |
* @return integer the auto increment | |
*/ | |
public static function update_views($post_id) { | |
global $wpdb; | |
$table = $wpdb->prefix . "simpleviews"; | |
if( !self::get_views( $post_id ) ) { | |
return self::insert_views( $post_id ); | |
} | |
$sql = $wpdb->prepare( "UPDATE `$table` SET view = view + 1 WHERE post_id = %d", $post_id ); | |
$wpdb->query($sql); | |
$sql = $wpdb->prepare( "SELECT view from `$table` WHERE post_id = %d", $post_id ); | |
$view = $wpdb->get_results( $sql, ARRAY_A ); | |
// This is the bit that echo_views gets. | |
return $view[0]['view']; | |
} | |
/** | |
* Gets the view count for a given post_id | |
* @param integer $post_id the ID of the post in question | |
* @return array either an empty or a one-dimensional array | |
*/ | |
public static function get_views($post_id) { | |
global $wpdb; | |
$table = $wpdb->prefix . "simpleviews"; | |
return $wpdb->get_col( $wpdb->prepare( "SELECT view FROM `$table` WHERE post_id = %d", $post_id ) ); | |
} | |
} | |
/** | |
* For backwards compatibility | |
*/ | |
if(!function_exists('echo_views')) { | |
function echo_views ( $post_id = 0 ) { | |
echo number_format_i18n( SPVCO::update_views( $post_id ) ); | |
} | |
} | |
register_activation_hook( __FILE__, array( 'SPVCO', 'install' ) ); | |
register_uninstall_hook( __FILE__, array( 'SPVCO', 'drop' ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment