<?php
/**
* Plugin Name: Статистика по новостям
* Description: nope
* Version: 0.2
* Author: Strah Roman
*/
// Add a menu item to the admin menu
add_action('admin_menu', 'statistic_news_page');
function statistic_news_page() {
add_menu_page(
'Статистика по новостям',
'Статистика новости',
'manage_options',
'statistic-news',
'statistic_news_page'
);
}
// Display the custom query page
function statistic_news_page() {
// Check if the user has permission to access this page
if (!current_user_can('manage_options')) {
wp_die('You do not have sufficient permissions to access this page.');
}
// Check if a query has been submitted
if (isset($_POST['post_id_from']) and isset($_POST['post_id_to'])and isset($_POST['post_id_to'])) {
global $wpdb;
$post_id_from = $_POST['post_id_from'];
$post_id_to = $_POST['post_id_to'];
$limit_count= $_POST['limit_count'];
$date_to= $_POST['date_to'];
$date_from= $_POST['date_from'];
if ($post_id_from > $post_id_to)
{
echo '<div class="error"><p> Значение "id поста от" не может быть больше значания "id поста до" </p></div>';
}
else
{
//$results = $wpdb->get_results("SELECT `post_id`, `meta_value` FROM `wp_postmeta` WHERE `meta_key` LIKE 'post_views_count' and `post_id` BETWEEN " . (int)$post_id_from . " and " . (int)$post_id_to . " ORDER BY CAST(`meta_value` AS DECIMAL) DESC LIMIT " . (int)$limit_count . "");
$results = $wpdb->get_results("SELECT `post_id`, `meta_value` FROM `wp_postmeta` WHERE `meta_key` LIKE 'post_views_count' INNER JOIN `wp_posts` ON `post_date` BETWEEN %s AND %s ORDER BY CAST(`meta_value` AS DECIMAL) DESC LIMIT " . (int)$limit_count . "",
array(
date("Y-m-d", strtotime($_POST['date_from'])),
date("Y-m-d", strtotime($_POST['date_to']))
)
);
if ($results) {
echo '<div class="updated"><p>Query executed successfully.</p></div>';
echo '<table class="widefat"><thead><tr>';
echo '<th>post_id</th>';
echo '<th>Title</th>';
echo '<th>URL post</th>';
echo '<th>Count views</th>';
echo '</tr></thead><tbody>';
foreach ($results as $row) {
echo '<tr>';
$is_id=false;
foreach ($row as $key => $value) {
if ($is_id==false)
{
echo '<td>' . $value . '</td>';
$is_id=true;
}
else
{
echo '<td>' . get_the_title($value) . '</td>';
echo '<td>' . get_permalink($value) . '</td>';
$is_id=false;
}
}
echo '</tr>';
}
echo '</tbody></table>';
} else {
echo '<div class="error"><p>Query failed.</p></div>';
}
}
}
// Display the query form
?>
<div class="wrap">
<h1>Посещалка сайта</h1>
<form method="post">
<label for="post_id_from">Введите id поста от</label>
<input id="post_id_from" type="post_id_from" name="post_id_from" value="">
<label for="post_id_to">Введите id поста до</label>
<input id="post_id_to" type="number" name="post_id_to" value="">
<label for="limit_count">Лимит вывода</label>
<input id="limit_count" type="number" name="limit_count" value="50">
</br>
<label for="date_from">Дата от</label>
<input id="date_from" type="date" name="date_from" value="">
<label for="date_to">Дата до</label>
<input id="date_to" type="date" name="date_to" value="">
</br>
<input type="submit" value="submit" class="button button-primary">
</form>
</div>
<?php
}