Commit 18c2a1b9 authored by Vadim's avatar Vadim

Version 0.6

Пакетная отправка данных из архива и WP Popular Posts на сервер
parent 8496b75f
......@@ -3,7 +3,7 @@
Plugin Name: Bg Az-Counter
Plugin URI: https://bogaiskov.ru
Description: Подсчет количества посещений страниц на базе stat.azbyka.ru
Version: 0.5
Version: 0.6
Author: VBog
Author URI: https://bogaiskov.ru
License: GPL2
......@@ -38,7 +38,7 @@ if ( !defined('ABSPATH') ) {
die( 'Sorry, you are not allowed to access this page directly.' );
}
define('BG_COUNTER_VERSION', '0.5.1');
define('BG_COUNTER_VERSION', '0.6.1');
define('BG_COUNTER_LOG', dirname(__FILE__ ).'/bg_counter.log');
define('BG_COUNTER_STAT_COUNTERS','https://stat.azbyka.ru/counters');
define('BG_COUNTER_STAT_RAITING','https://stat.azbyka.ru/rating');
......@@ -57,6 +57,7 @@ function bg_counter_deinstall() {
delete_option('bg_archive_status');
delete_option('bg_counter_period');
}
//delete_option('bg_wppp_loaded'); // Снять комментарий, чтобы сбросить флаг загрузки из WP Popular Posts
if (!isset($project)) {
$project = wp_parse_url( site_url(), PHP_URL_PATH );
......@@ -202,7 +203,8 @@ POST /set-counter/project/test/author/1/book/3
{"success": true}
******************************************************************************************/
function setCounts ($path, $counter) {
// Установить 1 счетчик
function setCount ($path, $counter) {
global $project;
$result = wp_remote_post (BG_COUNTER_STAT_SET.$project.$path, array('body' => '{"counter": '.$counter.'}'));
......@@ -224,6 +226,29 @@ function setCounts ($path, $counter) {
}
}
// Установить ВСЕ счетчики проекта
function setAllCounts ($request) {
global $project;
$result = wp_remote_post (BG_COUNTER_STAT_SET, array('body' => $request));
if( is_wp_error( $result ) ) {
echo "<br>".$result->get_error_message(); // сообщение ошибки
echo "<br>".$result->get_error_code(); // ключ ошибки
error_log( PHP_EOL .date("Y-m-d H:i:s ", time())." ВСЕ СЧЁТЧИКИ. Ошибка при получении данных с сервера: ".$result->get_error_message(), 3, BG_COUNTER_LOG ); // сообщение ошибки
error_log( " " .$result->get_error_code(), 3, BG_COUNTER_LOG ); // ключ ошибки
return false;
}
$json = wp_remote_retrieve_body($result);
$response = json_decode($json, false);
if ($response->success) return true;
else {
echo $json;
error_log( PHP_EOL .date("Y-m-d H:i:s ", time())." ВСЕ СЧЁТЧИКИ. Сервер вернул ответ неудачи: ".print_r($response, true), 3, BG_COUNTER_LOG );
return false;
}
}
/*****************************************************************************************
......@@ -289,16 +314,27 @@ function bg_counter_top_posts_shortcode( $atts ) {
******************************************************************************************/
function bg_counter_getWPPopularPosts() {
global $wpdb;
global $project;
// Получить данные из таблицы
// postid(bigint(20)), day(datetime), last_viewed(datetime), pageviews(bigint(20))
$data = $wpdb->get_results("SELECT postid,pageviews FROM wp_popularpostsdata", ARRAY_A);
// Отправить данные на сервер
$old_data = $wpdb->get_results("SELECT postid,pageviews FROM wp_popularpostsdata", ARRAY_A);
// Формируем запрос
$i = 0;
foreach ($data as $row) {
if (!setCounts ('/post/'.$row['postid'], $row['pageviews'])) return false;
$data = array();
$point = array();
foreach ($old_data as $row) {
$point['path'] = $project.'/post/'.$row['postid'];
$point['counter'] = (int)$row['pageviews'];
$data[] = $point;
$i++;
}
$json = json_encode($data, JSON_UNESCAPED_SLASHES);
echo $json."<br>";
// Отправить данные на сервер
if (!setAllCounts ($json)) return false;
return $i;
}
......@@ -339,7 +375,8 @@ function bg_counter_saveStatictics() {
if ($count) $data = array_merge($data, $response->data);
$offset += $limit;
}
$json = json_encode($data);
$json = json_encode($data, JSON_UNESCAPED_SLASHES);
if (file_put_contents ( ABSPATH.BG_COUNTER_ARCHIVE, $json ) === false) {
error_log( PHP_EOL .date("Y-m-d H:i:s ", time())." АРХИВ. Ошибка записи в файл: ".BG_COUNTER_ARCHIVE, 3, BG_COUNTER_LOG );
return false;
......@@ -352,6 +389,7 @@ function bg_counter_saveStatictics() {
******************************************************************************************/
function bg_counter_sendArchiveData() {
global $project;
// Получить данные из файла архива
$json = file_get_contents ( ABSPATH.BG_COUNTER_ARCHIVE);
......@@ -359,13 +397,22 @@ function bg_counter_sendArchiveData() {
echo "<br>" ."Ошибка чтения файла: ".BG_COUNTER_ARCHIVE;
return false;
}
$data = json_decode($json, true);
// Отправить данные на сервер
$old_data = json_decode($json, true);
// Формируем запрос
$i = 0;
foreach ($data as $row) {
if (!setCounts ('/'.$row['type'].'/'.$row['id'], $row['value'])) return false;
$data = array();
$point = array();
foreach ($old_data as $row) {
$point['path'] = $project.'/'.$row['type'].'/'.$row['id'];
$point['counter'] = $row['value'];
$data[] = $point;
$i++;
}
$json = json_encode($data, JSON_UNESCAPED_SLASHES);
echo $json."<br>";
// Отправить данные на сервер
if (!setAllCounts ($json)) return false;
return $i;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment