.
*/
/*
Count views based in plugin WP-post-view of Towards Technology http://answer2me.com/
*/
?>
prefix . "postview";
if ($wpdb->get_var("SHOW TABLES LIKE '$table'") != $table) {
$sql = "CREATE TABLE " . $table .
" ( UNIQUE KEY id (post_id), post_id int(10) NOT NULL,
view int(10),
view_datetime datetime NOT NULL default '0000-00-00 00:00:00'
)";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
}
/**
* Se borra el plugin, se borra la tabla
* @global $wpdb $wpdb
*/
function wpp_destroy() {
global $wpdb;
$table = $wpdb->prefix . "postview";
if ($wpdb->get_var("SHOW TABLES LIKE '$table'") != $table) {
$sql = "DROP TABLE " . $table;
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
}
/**
* Estilos panel de adminsitración
*/
function post_view_style() {
echo
'';
}
/**
* Se crea una columna en el listado de post del panel de administración
*
* @param array $columns
* @return
*/
function show_post_header_views($columns) {
$columns['views'] = __('Views');
return $columns;
}
/**
*
* Se muestran el número de visitas en cada post en el panel de administración
*
* @param $column_name
* @param $post_id
* @return
*/
function show_post_row_views($column_name, $post_id) {
if ($column_name != 'views')
return;
echo wp_get_post_views($post_id);
}
/**
*
* Función que muestra el número de visitas a cada post
*
*/
if (!function_exists('echo_post_views')) {
/**
* Echo, print or display the views of the post.
* @param $post_id
*/
function echo_post_views($post_id) {
if (wp_update_post_views($post_id) == 1) {
$views = wp_get_post_views($post_id);
number_format_i18n($views);
} else {
0;
}
}
}
/**
* Returns 1 if successfully updated post views.
*
* @global $wpdb $wpdb
* @param $views
* @param $post_id
* @return
*/
function wp_insert_post_views($views, $post_id) {
global $wpdb;
$table = $wpdb->prefix . "postview";
$result = $wpdb->query("INSERT INTO $table VALUES($post_id,$views,NOW())");
return ($result);
}
/**
* Returns 1 if successfully updated post views.
*
* @global $wpdb
* @param $post_id
* @return
*/
function wp_update_post_views($post_id) {
global $wpdb;
$table = $wpdb->prefix . "postview";
$views = wp_get_post_views($post_id) + 1;
if ($wpdb->query("SELECT view FROM $table WHERE post_id = '$post_id'") != 1)
wp_insert_post_views($views, $post_id);
$result = $wpdb->query("UPDATE $table SET view=$views, view_datetime=NOW() WHERE post_id = '$post_id'");
return ($result);
}
/**
* Get the post views amount.
* @global $wpdb $wpdb
* @param $post_id
* @return
*/
function wp_get_post_views($post_id) {
global $wpdb;
$table = $wpdb->prefix . "postview";
$result = $wpdb->get_results("SELECT view FROM $table WHERE post_id = '$post_id'", ARRAY_A);
if (!is_array($result) || empty($result)) {
return "0";
} else {
return $result[0]['view'];
}
}
/**
*
* añado las funciones dentro del single.php
*
*/
function anp_post_vistos($content) {
if(is_singular()){
$content.=echo_post_views(get_the_ID());
return $content;
}else{
return $content;
}
}
add_action('the_content', anp_post_vistos);
/*
*
* registramos jscolor
*
*/
function anp_regis_jscolor() {
wp_register_script('anp_jscolor', plugins_url('/js/jscolor/jscolor.js', __FILE__),"", "", true);
wp_enqueue_script('anp_jscolor', 1);
wp_register_script('anp_script', plugins_url('/js/script.js', __FILE__));
wp_enqueue_script('anp_script');
}
add_action('admin_print_scripts-widgets.php', 'anp_regis_jscolor');
/*
*
* Activamos multilenguaje
*
*/
function traduce_anp_popular_post(){
load_plugin_textdomain('anp_text_popular_post', false, basename( dirname( __FILE__ ) ) . '/lang' );
}
add_action('init', 'traduce_anp_popular_post');
/**
*
* Creamos un widget
*
**/
class anp_popular_post_widget extends WP_Widget {
public function __construct() {
parent::__construct(
'anp_post_popular_post', // Base ID
'anp Popular Post', // Name
array( 'description' => __( 'Displays a list of most commented post', 'anp_text_popular_post' ), ) // Args
);
}
/**
* Front-end widget.
*
*/
public function widget( $args, $instance ) {
extract( $args );
$title = apply_filters( 'widget_title', $instance['title'] );
$anp_num_post= $instance['anp_num_post'];
$anp_colores_post = $instance['anp_colores_post'];
$anp_autor = $instance['anp_autor'];
$anp_color_back_num = $instance['anp_color_back_num'];
$anp_color_lett = $instance['anp_color_lett'];
$anp_color_text_num = $instance['anp_color_text_num'];
$anp_comment_view = $instance['anp_comment_view'];
$anp_num_com_time = $instance['anp_num_com_time'];
update_option('anp_color_back_num', $anp_color_back_num);
update_option('anp_color_lett', $anp_color_lett);
update_option('anp_color_text_num', $anp_color_text_num);
global $post, $wpdb, $count;
echo $before_widget;
if ( ! empty( $title ) )
echo $before_title . $title . $after_title;
?>
get_results("
SELECT comment_count,ID,post_title
FROM $wpdb->posts
ORDER BY comment_count DESC LIMIT 0 , $anp_num_post");
break;
case 2:
$result = $wpdb->get_results("
SELECT comment_post_ID, COUNT( comment_post_ID ) AS 'comment_count' , ID, post_title
FROM $wpdb->posts, $wpdb->comments
WHERE comment_approved =1
AND $wpdb->comments.comment_post_ID = $wpdb->posts.ID
AND comment_date > DATE_SUB( NOW( ) , INTERVAL 1 DAY )
GROUP BY comment_post_ID
ORDER BY comment_date ASC LIMIT 0 , $anp_num_post");
break;
case 3:
$result = $wpdb->get_results("
SELECT comment_post_ID, COUNT( comment_post_ID ) AS 'comment_count' , ID, post_title
FROM $wpdb->posts, $wpdb->comments
WHERE comment_approved =1
AND $wpdb->comments.comment_post_ID = $wpdb->posts.ID
AND comment_date > DATE_SUB( NOW( ) , INTERVAL 1 WEEK )
GROUP BY comment_post_ID
ORDER BY comment_date ASC LIMIT 0 , $anp_num_post");
break;
case 4:
$result = $wpdb->get_results("
SELECT comment_post_ID, COUNT( comment_post_ID ) AS 'comment_count' , ID, post_title
FROM $wpdb->posts, $wpdb->comments
WHERE comment_approved =1
AND $wpdb->comments.comment_post_ID = $wpdb->posts.ID
AND comment_date > DATE_SUB( NOW( ) , INTERVAL 15 DAY )
GROUP BY comment_post_ID
ORDER BY comment_date ASC LIMIT 0 , $anp_num_post");
break;
case 5:
$result = $wpdb->get_results("
SELECT comment_post_ID, COUNT( comment_post_ID ) AS 'comment_count' , ID, post_title
FROM $wpdb->posts, $wpdb->comments
WHERE comment_approved =1
AND $wpdb->comments.comment_post_ID = $wpdb->posts.ID
AND comment_date > DATE_SUB( NOW( ) , INTERVAL 30 DAY )
GROUP BY comment_post_ID
ORDER BY comment_date ASC LIMIT 0 , $anp_num_post");
break;
}
/**
*
* extraemos el número mayor dentro del array
* de los números de comentarios
*
*/
foreach ($result as $post) {
$anp_mayor = $post->comment_count;
break;
}
$post_nu_clas=1; //añadimos valores a la clase css de cada post
/**
*
* recorremos el query y mostramos todos los resultados
* según se eliga por comentarios o por visitas
*
*/
if ($anp_comment_view == 1){ // si es 1 mostramos los post más comentados
foreach ($result as $post) {
setup_postdata($post);
$postid = $post->ID;
$title = $post->post_title;
$commentcount = $post->comment_count;
if ($commentcount != 0) { ?>
prefix . "postview";
$result = $wpdb->get_results("
SELECT view_datetime, view, post_ID, ID, post_title
FROM $table, $wpdb->posts
WHERE $table.post_ID = $wpdb->posts.ID
ORDER BY view DESC LIMIT 0 , $anp_num_post");
break;
case 2:
$table = $wpdb->prefix . "postview";
$result = $wpdb->get_results("
SELECT view_datetime, view, post_ID, ID, post_title,
FROM $table, $wpdb->posts
WHERE $table.post_ID = $wpdb->posts.ID
AND view_datetime > DATE_SUB( NOW( ) , INTERVAL 1 DAY )
ORDER BY view DESC LIMIT 0 , $anp_num_post");
break;
case 3:
$table = $wpdb->prefix . "postview";
$result = $wpdb->get_results("
SELECT view_datetime, view, post_ID, ID, post_title
FROM $table, $wpdb->posts
WHERE $table.post_ID = $wpdb->posts.ID
AND view_datetime > DATE_SUB( NOW( ) , INTERVAL 7 DAY )
ORDER BY view DESC LIMIT 0 , $anp_num_post");
break;
case 3:
$table = $wpdb->prefix . "postview";
$result = $wpdb->get_results("
SELECT view_datetime, view, post_ID, ID, post_title
FROM $table, $wpdb->posts
WHERE $table.post_ID = $wpdb->posts.ID
AND view_datetime > DATE_SUB( NOW( ) , INTERVAL 15 DAY )
ORDER BY view DESC LIMIT 0 , $anp_num_post");
break;
case 3:
$table = $wpdb->prefix . "postview";
$result = $wpdb->get_results("
SELECT view_datetime, view, post_ID, ID, post_title
FROM $table, $wpdb->posts
WHERE $table.post_ID = $wpdb->posts.ID
AND view_datetime > DATE_SUB( NOW( ) , INTERVAL 30 DAY )
ORDER BY view DESC LIMIT 0 , $anp_num_post");
break;
}
/**
*
* extraemos el número mayor dentro del array
* de los números de comentarios
*
*/
foreach ($result as $post) {
$anp_mayor = $post->view;
break;
}
foreach ($result as $post) {
setup_postdata($post);
$postid = $post->ID;
$title = $post->post_title;
$commentcount = $post->view; //{
if ($commentcount != 0) { ?>