<?php
require_once(COMPONENTS_PATH . "icons/camera-icon.php");
require_once(COMPONENTS_PATH . "icons/video-content-icon.php");
require_once(COMPONENTS_PATH . 'line-news-list-item.php');
require_once(COMPONENTS_PATH . 'content-exist-markers.php');
function render_news_whole_post($id, $cat = NULL)
{
$single_post = get_post($id);
gt_set_post_view($id);
$author_id = $single_post->post_author;
$taxonomies = get_my_taxonomies($id);
if (!empty($taxonomies)) {
$primary_category = get_post_primary_category($id, $taxonomies);
if(!empty($primary_category['primary_category'])){
$primary_category = $primary_category['primary_category'];
$cat_link = home_url();
$cat_link .= '/';
$cat_link .= $primary_category->taxonomy;
$cat_link .= '/';
$cat_link .= $primary_category->slug;
}
}
?>
<div class="post" data-url="<?php echo get_permalink($id)?>">
<div class="post-header">
<div class="content-exists">
<div class="content">
<?php $exist_markers = render_content_exist_markers($id); ?>
<?php if ($exist_markers): ?>
<?php echo $exist_markers; ?>
<?php endif; ?>
</div>
<?php if (isset($cat_link)): ?>
<div class="tags">
<a href="<?php echo $cat_link; ?>" class="tags__link">
<span>
<?php echo $primary_category->name; ?>
</span>
</a>
</div>
<?php endif; ?>
</div>
<div class="title">
<span>
<?php //echo get_the_title($single_post->ID); ?>
<?php echo get_the_title(); ?>
</span>
</div>
<div class="share">
<div class="date">
<span>
<?php echo date("H:i", strtotime($single_post->post_date)); ?>
</span>
<span>
<?php echo date("d.m.Y", strtotime($single_post->post_date)); ?>
</span>
</div>
</div>
<?php if (function_exists('nvvc_visitors_views_shortcode'))
{
echo nvvc_visitors_views_shortcode($id);
}
else
{
echo gt_in_post_view($id);
}
?>
</div>
<?php
//$content = apply_filters('the_content', $single_post->post_content, $single_post->ID);
?>
<div class="page-content">
<?php// echo $content; ?>
<?php the_content(); ?>
<?php $is_advertising = carbon_get_post_meta($single_post->ID, 'news_is_advertising'); ?>
<?php if ($is_advertising) : ?>
<div class="adv__info">
<span class="adv_icon__box"><?php render_advertising_icon(); ?></span>
<span class="adv_text__box"><?php echo carbon_get_post_meta($single_post->ID, 'news_text_advertising'); ?></span>
</div>
<?php endif; ?>
</div>
<div class="footer-content">
<div class="author-info">
<div class="author-image">
<?php echo get_avatar($author_id); ?>
</div>
<div class="author-details">
<span class="label">Автор материалов</span>
<span class="name"><?php echo the_author_meta('display_name', $author_id); ?></span>
<a href="<?php echo get_author_posts_url($author_id); ?>" class="other-posts">Все новости автора</a>
</div>
</div>
<?php echo share_links($id); ?>
</div>
</div>
<?php
}
<?php
/*
*Plugin Name: News Views and Visitors Counter
*Plugin URI: https://example.com/
*Description: This plugin counts the number of unique visitors of each news post.
*Version: 0.4
*Author: Strah Roman
*Author URI: https://vk.com/strah_roman
*License: GPLv2 or later
*License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
register_activation_hook(__FILE__, 'nvvc_create_table');
add_action('admin_menu', 'nvvc_unique_visitors_counter');
function nvvc_unique_visitors_counter()
{
add_menu_page(
"Статистика по новостям",
"Статистика новости (+уникальные)",
"edit_others_posts",
"nvvc-unique-visitors-counter",
"nvvc_unique_visitors_counter_page"
);
}
function nvvc_create_table()
{
// Set up the database table for tracking views and visitors.
global $wpdb;
$table_name = $wpdb->prefix . 'news_stats';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id BIGINT(20) NOT NULL AUTO_INCREMENT,
post_id BIGINT(20) NOT NULL,
views BIGINT(20) DEFAULT 0,
unqviews BIGINT(20) DEFAULT 0,
ip VARCHAR(64) NOT NULL,
date DATE NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
// Define the function that will run when the post is displayed
function nvvc_count_views($id)
{
global $wpdb;
$table_name = $wpdb->prefix . 'news_stats';
$ip = $_SERVER['REMOTE_ADDR'];
$today = date('Y-m-d');
$check_ip = $wpdb->get_results($wpdb->prepare(
"SELECT * FROM $table_name WHERE post_id = %d AND ip = %s AND date = %s",
$id,
$ip,
$today
));
// Check if the IP address has already visited the post today
if (empty($check_ip)) {
// If not, insert a new row in the database to count the visitor
$wpdb->query(
$wpdb->prepare(
"INSERT INTO $table_name (post_id, views, unqviews, ip, date) VALUES (%d, %d, %d, %s, %s)",
$id,
1,
1,
$ip,
$today
)
);
} else {
// Update the view count in the database
$wpdb->query($wpdb->prepare(
"UPDATE $table_name SET views = views + 1 WHERE post_id = %d AND ip = %s AND date = %s",
$id,
$ip,
$today
));
}
}
function nvvc_unique_visitors_counter_page()
{
global $wpdb;
$table_name = $wpdb->prefix . 'news_stats';
// Check if the user has permission to access this page
if (!current_user_can("edit_others_posts")) {
wp_die("You do not have sufficient permissions to access this page.");
}
// Check if a query has been submitted
if (isset($_POST["limit_count"])
and isset($_POST["date_from"])
and isset($_POST["date_to"])
and isset($_POST["limit_count"])) {
$limit_count = $_POST["limit_count"];
$date_to = $_POST["date_to"];
$date_from = $_POST["date_from"];
if (strtotime($date_from) > strtotime($date_to)) {
echo '<div class="error"><p> Значение "Дата от" не может быть больше значания "Дата до" </p></div>';
}
$sql = $wpdb->prepare(
"SELECT post_id, SUM(views) AS total_views, SUM(unqviews) AS unique_views
FROM $table_name nvvc JOIN $wpdb->posts wpp ON nvvc.post_id = wpp.ID
WHERE wpp.post_date BETWEEN %s AND %s
GROUP BY post_id
ORDER BY unique_views DESC LIMIT %d",
$date_from . " 00:00:00",
$date_to . " 23:59:59",
$limit_count
);
$results = $wpdb->get_results($sql);
// Prepare the table for displaying the results
echo '<table>';
echo '<thead><tr><th>Post ID</th><th>Title</th><th>URL</th><th>Unique views</th><th>Total views</th></tr></thead>';
echo '<tbody>';
if ($results) {
// Iterate over the results and display them in the table
$sample_total=0;
$sample_total_unq=0;
foreach ($results as $result) {
$sample_total=$sample_total+$result->total_views;
$sample_total_unq=$sample_total_unq+$result->unique_views;
echo '<tr>';
echo '<td>' . $result->post_id . '</td>';
echo '<td>' . get_the_title($result->post_id) . '</td>';
echo '<td>' . get_permalink($result->post_id) . '</td>';
echo '<td>' . $result->unique_views . '</td>';
echo '<td>' . $result->total_views . '</td>';
echo '</tr>';
}
} else {
// Display a message if no results were found
echo '<tr><td colspan="3">No results found.</td></tr>';
}
echo '<div class="updated"><p>Общее кол-во просмотров (по выборке): ' . $sample_total . '</p></div>';
echo '<div class="updated"><p>Общее кол-во уникальных посетителей (по выборке): ' . $sample_total_unq . '</p></div>';
echo '</tbody>';
echo '</table>';
}
?>
<div class="wrap">
<h1>Посещалка сайта</h1>
<form method="post">
<label for="custom_query">Дата от</label>
</br>
<input id="date_from" type="date" name="date_from" value="<?php echo date("Y-m-d",strtotime("-1 day")); ?>">
</br>
<label for="custom_query">Дата до</label>
</br>
<input id="date_to" type="date" name="date_to" value="<?php echo date(
"Y-m-d",
strtotime("-1 day")
); ?>">
</br>
<label for="custom_query">Лимит вывода</label>
</br>
<input id="limit_count" type="number" name="limit_count" value="100" min="1" max="300">
</br>
<input type="submit" value="Enter" class="button button-primary">
</form>
</div>
<?php
}
<?php
/*
*Plugin Name: News Views and Visitors Counter
*Plugin URI: https://example.com/
*Description: This plugin counts the number of unique visitors of each news post.
*Version: 0.4
*Author: Strah Roman
*Author URI: https://vk.com/strah_roman
*License: GPLv2 or later
*License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
register_activation_hook(__FILE__, 'nvvc_create_table');
add_action('admin_menu', 'nvvc_unique_visitors_counter');
function nvvc_unique_visitors_counter()
{
add_menu_page(
"Статистика по новостям",
"Статистика новости",
"edit_others_posts",
"nvvc-unique-visitors-counter",
"nvvc_unique_visitors_counter_page"
);
}
function nvvc_create_table()
{
// Set up the database table for tracking views and visitors.
global $wpdb;
$table_name = $wpdb->prefix . 'news_stats';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id BIGINT(20) NOT NULL AUTO_INCREMENT,
post_id BIGINT(20) NOT NULL,
views BIGINT(20) DEFAULT 0,
unqviews BIGINT(20) DEFAULT 0,
ip VARCHAR(64) NOT NULL,
date DATETIME NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
// Define the function that will run when the post is displayed
function nvvc_count_views($id)
{
global $wpdb;
$table_name = $wpdb->prefix . 'news_stats';
$ip = $_SERVER['REMOTE_ADDR'];
$today = date('Y-m-d H:i:s');
$check_ip = $wpdb->prepare(
"SELECT ip FROM %s WHERE post_id = %d AND ip = %s AND date = %s DESC LIMIT 1",
$table_name,
$id,
$ip,
$today
);
$results_check_ip=$wpdb->get_results($check_ip);
// Check if the IP address has already visited the post today
if (empty($results_check_ip)) {
// If not, insert a new row in the database to count the visitor
$wpdb->query(
$wpdb->prepare(
"INSERT INTO %s (post_id, views, unqviews, ip, date) VALUES (%d, %d, %d, %s, %s)",
$table_name,
$id,
1,
1,
$ip,
$today
)
);
} else {
// Update the view count in the database
$wpdb->query($wpdb->prepare(
"UPDATE %s SET views = views+1 WHERE post_id = %d AND ip = %s AND date = %s",
$table_name,
$id,
$ip,
$today
));
}
}
function nvvc_unique_visitors_counter_page()
{
global $wpdb;
$table_name = $wpdb->prefix . 'news_stats';
// Check if the user has permission to access this page
if (!current_user_can("edit_others_posts")) {
wp_die("You do not have sufficient permissions to access this page.");
}
// Check if a query has been submitted
if (isset($_POST["limit_count"])
and isset($_POST["date_from"])
and isset($_POST["date_to"])
and isset($_POST["limit_count"])) {
$limit_count = $_POST["limit_count"];
$date_to = $_POST["date_to"];
$date_from = $_POST["date_from"];
if (strtotime($date_from) > strtotime($date_to)) {
echo '<div class="error"><p> Значение "Дата от" не может быть больше значания "Дата до" </p></div>';
}
$sql = $wpdb->prepare(
"SELECT post_id, SUM(views) AS total_views, SUM(unqviews) AS unique_views
FROM $table_name nvvc JOIN $wpdb->posts wpp ON nvvc.post_id = wpp.ID
WHERE wpp.post_date BETWEEN %s AND %s
GROUP BY post_id
ORDER BY views DESC LIMIT %d",
$date_from,
$date_to,
$limit_count
);
$results = $wpdb->get_results($sql);
// Prepare the table for displaying the results
echo '<table>';
echo '<thead><tr><th>Post ID</th><th>Total views</th><th>Unique views</th></tr></thead>';
echo '<tbody>';
if ($results) {
// Iterate over the results and display them in the table
foreach ($results as $result) {
echo '<tr>';
echo '<td>' . $result->post_id . '</td>';
echo '<td>' . $result->total_views . '</td>';
echo '<td>' . $result->unique_views . '</td>';
echo '</tr>';
}
} else {
// Display a message if no results were found
echo '<tr><td colspan="3">No results found.</td></tr>';
}
echo '</tbody>';
echo '</table>';
}
?>
<div class="wrap">
<h1>Посещалка сайта</h1>
<form method="post">
<label for="custom_query">Дата от</label>
</br>
<input id="date_from" type="date" name="date_from" value="<?php echo date("Y-m-d",strtotime("-1 day")); ?>">
</br>
<label for="custom_query">Дата до</label>
</br>
<input id="date_to" type="date" name="date_to" value="<?php echo date(
"Y-m-d",
strtotime("-1 day")
); ?>">
</br>
<label for="custom_query">Лимит вывода</label>
</br>
<input id="limit_count" type="number" name="limit_count" value="100" min="1" max="300">
</br>
<input type="submit" value="Enter" class="button button-primary">
</form>
</div>
<?php
}