feat(webmaster): daily metrics collector + staleness alert

- tables wp_webmaster_query_urls and wp_collector_runs
- daily cron collects query-analytics/list, hourly staleness check
- alert option + admin notice + error_log when metric_date older than 3 days
- wp dekart webmaster-collect / webmaster-status
- internal links: last-resort list of landing pages by template
This commit is contained in:
Dekart Deploy Bot 2026-10-23 23:13:48 +00:00
parent bb6e6cf041
commit e1d0fc81f1
2 changed files with 283 additions and 0 deletions

View File

@ -4748,6 +4748,31 @@ function dekart_internal_links(): array
}
$items[] = array( 'url' => $url, 'label' => $d['label'], 'text' => $d['text'] );
}
if (empty($items)) {
// последний резерв: лендинги, найденные по шаблону
$landing_tpls = function_exists('dekart_landing_templates') ? (array) dekart_landing_templates() : array();
$pages = get_pages(array(
'post_status' => 'publish',
'number' => 12,
'exclude' => array( (int) get_the_ID() ),
));
foreach ($pages as $page) {
$tpl = (string) get_page_template_slug($page->ID);
if ('' === $tpl || ( ! empty($landing_tpls) && ! in_array($tpl, $landing_tpls, true) )) {
continue;
}
$items[] = array(
'url' => (string) get_permalink($page),
'label' => wp_strip_all_tags(get_the_title($page)),
'text' => '',
);
if (count($items) >= 6) {
break;
}
}
}
return $items;
}
@ -7339,6 +7364,7 @@ function dekart_flush_cache_page()
require_once __DIR__ . '/inc/schema.php';
require_once __DIR__ . '/inc/webmaster-recrawl.php';
require_once __DIR__ . '/inc/webmaster-collector.php';
require_once __DIR__ . '/inc/js-snippet-comments.php';
require_once __DIR__ . '/inc/cli-city-normalizer.php';
require_once __DIR__ . '/inc/cli-cf-import.php';

View File

@ -0,0 +1,257 @@
<?php
// Сборщик метрик Вебмастера: таблицы, cron, алерт устаревания
if (! defined('ABSPATH')) {
exit;
}
const DEKART_WM_TABLE_METRICS = 'webmaster_query_urls';
const DEKART_WM_TABLE_RUNS = 'collector_runs';
const DEKART_WM_STALE_DAYS = 3;
function dekart_wm_table(string $suffix): string
{
global $wpdb;
return $wpdb->prefix . $suffix;
}
function dekart_wm_install_tables(): void
{
global $wpdb;
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
$metrics = dekart_wm_table(DEKART_WM_TABLE_METRICS);
$runs = dekart_wm_table(DEKART_WM_TABLE_RUNS);
$charset = $wpdb->get_charset_collate();
dbDelta("CREATE TABLE {$metrics} (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
query_id varchar(64) NOT NULL DEFAULT '',
query_text text NULL,
url varchar(500) NOT NULL DEFAULT '',
shows int(11) NOT NULL DEFAULT 0,
clicks int(11) NOT NULL DEFAULT 0,
position decimal(6,2) NOT NULL DEFAULT 0,
metric_date date NOT NULL,
collected_at datetime NOT NULL,
PRIMARY KEY (id),
KEY metric_date (metric_date),
KEY query_id (query_id)
) {$charset};");
dbDelta("CREATE TABLE {$runs} (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
started_at datetime NOT NULL,
finished_at datetime NULL,
rows_imported int(11) NOT NULL DEFAULT 0,
status varchar(20) NOT NULL DEFAULT '',
message text NULL,
PRIMARY KEY (id),
KEY started_at (started_at)
) {$charset};");
update_option('dekart_wm_tables_version', '1', false);
}
add_action('init', function () {
if (get_option('dekart_wm_tables_version') !== '1') {
dekart_wm_install_tables();
}
}, 5);
function dekart_wm_log_run(string $status, int $rows, string $message, string $started_at): void
{
global $wpdb;
$wpdb->insert(dekart_wm_table(DEKART_WM_TABLE_RUNS), array(
'started_at' => $started_at,
'finished_at' => current_time('mysql'),
'rows_imported' => $rows,
'status' => $status,
'message' => mb_substr($message, 0, 1000),
));
}
function dekart_wm_latest_metric_date(): string
{
global $wpdb;
$table = dekart_wm_table(DEKART_WM_TABLE_METRICS);
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$date = $wpdb->get_var("SELECT MAX(metric_date) FROM {$table}");
return is_string($date) ? $date : '';
}
function dekart_wm_is_stale(): bool
{
$latest = dekart_wm_latest_metric_date();
if ('' === $latest) {
return true;
}
$ts = strtotime($latest . ' 00:00:00');
$limit = strtotime('-' . DEKART_WM_STALE_DAYS . ' days', (int) current_time('timestamp'));
return $ts < $limit;
}
function dekart_wm_collect(): array
{
$started = current_time('mysql');
if (! function_exists('dekart_webmaster_request')) {
dekart_wm_log_run('skipped', 0, 'webmaster API helper missing', $started);
return array( 'status' => 'skipped', 'rows' => 0, 'message' => 'helper missing' );
}
if ('' === dekart_webmaster_token()) {
dekart_wm_log_run('skipped', 0, 'no Webmaster token', $started);
return array( 'status' => 'skipped', 'rows' => 0, 'message' => 'no token' );
}
list( $user_id, $host_id ) = dekart_webmaster_ids();
if ('' === $user_id || '' === $host_id ) {
dekart_wm_log_run('error', 0, 'cannot resolve user_id/host_id', $started);
return array( 'status' => 'error', 'rows' => 0, 'message' => 'ids' );
}
$path = '/user/' . rawurlencode($user_id) . '/hosts/' . rawurlencode($host_id) . '/query-analytics/list';
$res = dekart_webmaster_request('POST', $path, array(
'body' => wp_json_encode(array(
'offset' => 0,
'limit' => 500,
'device_type_indicator' => 'ALL',
'text_indicator' => 'QUERY',
)),
));
if (is_wp_error($res)) {
dekart_wm_log_run('error', 0, $res->get_error_message(), $started);
return array( 'status' => 'error', 'rows' => 0, 'message' => $res->get_error_message() );
}
$code = (int) wp_remote_retrieve_response_code($res);
$data = json_decode((string) wp_remote_retrieve_body($res), true);
if (200 !== $code || ! is_array($data)) {
dekart_wm_log_run('error', 0, 'HTTP ' . $code, $started);
return array( 'status' => 'error', 'rows' => 0, 'message' => 'HTTP ' . $code );
}
global $wpdb;
$table = dekart_wm_table(DEKART_WM_TABLE_METRICS);
$today = (string) current_time('Y-m-d');
$rows = 0;
$items = array();
if (isset($data['queries']) && is_array($data['queries'])) {
$items = $data['queries'];
} elseif (isset($data['text_indicator_to_statistics']) && is_array($data['text_indicator_to_statistics'])) {
$items = $data['text_indicator_to_statistics'];
}
foreach ($items as $item) {
$query_text = '';
if (isset($item['text'])) {
$query_text = (string) $item['text'];
} elseif (isset($item['query_text'])) {
$query_text = (string) $item['query_text'];
}
$indicators = isset($item['indicators']) && is_array($item['indicators']) ? $item['indicators'] : array();
$shows = (int) ( $indicators['TOTAL_SHOWS'] ?? 0 );
$clicks = (int) ( $indicators['TOTAL_CLICKS'] ?? 0 );
$position = (float) ( $indicators['AVG_SHOW_POSITION'] ?? 0 );
$query_id = (string) ( $item['query_id'] ?? '' );
$url = (string) ( $item['url'] ?? '' );
if ('' === $query_text && '' === $query_id) {
continue;
}
$wpdb->insert($table, array(
'query_id' => $query_id,
'query_text' => $query_text,
'url' => mb_substr($url, 0, 500),
'shows' => $shows,
'clicks' => $clicks,
'position' => $position,
'metric_date' => $today,
'collected_at' => current_time('mysql'),
));
$rows++;
}
// чистим историю старше 180 дней
$wpdb->query($wpdb->prepare(
"DELETE FROM {$table} WHERE metric_date < %s",
gmdate('Y-m-d', strtotime('-180 days', (int) current_time('timestamp')))
));
dekart_wm_log_run('ok', $rows, 'collected', $started);
update_option('dekart_wm_last_success', current_time('mysql'), false);
return array( 'status' => 'ok', 'rows' => $rows, 'message' => '' );
}
function dekart_wm_check_staleness(): bool
{
$stale = dekart_wm_is_stale();
$latest = dekart_wm_latest_metric_date();
update_option('dekart_wm_stale', $stale ? '1' : '0', false);
update_option('dekart_wm_latest_metric_date', $latest, false);
if ($stale) {
$msg = 'Webmaster metrics are stale: latest metric_date = ' . ( '' !== $latest ? $latest : 'none' )
. ' (limit ' . DEKART_WM_STALE_DAYS . ' days)';
update_option('dekart_wm_alert', $msg . ' @ ' . current_time('mysql'), false);
error_log('[dekart] ' . $msg);
} else {
delete_option('dekart_wm_alert');
}
return $stale;
}
add_filter('cron_schedules', function ($schedules) {
if (! isset($schedules['dekart_hourly'])) {
$schedules['dekart_hourly'] = array( 'interval' => HOUR_IN_SECONDS, 'display' => 'Every hour (dekart)' );
}
return $schedules;
});
add_action('init', function () {
if (! wp_next_scheduled('dekart_wm_collect_daily')) {
wp_schedule_event(strtotime('tomorrow 03:20'), 'daily', 'dekart_wm_collect_daily');
}
if (! wp_next_scheduled('dekart_wm_staleness_check')) {
wp_schedule_event(time() + 300, 'dekart_hourly', 'dekart_wm_staleness_check');
}
}, 20);
add_action('dekart_wm_collect_daily', function () {
dekart_wm_collect();
dekart_wm_check_staleness();
});
add_action('dekart_wm_staleness_check', 'dekart_wm_check_staleness');
add_action('admin_notices', function () {
$alert = (string) get_option('dekart_wm_alert', '');
if ('' === $alert || ! current_user_can('manage_options')) {
return;
}
echo '<div class="notice notice-warning"><p><strong>Вебмастер:</strong> ' . esc_html($alert) . '</p></div>';
});
if (defined('WP_CLI') && WP_CLI) {
WP_CLI::add_command('dekart webmaster-collect', function () {
$res = dekart_wm_collect();
$stale = dekart_wm_check_staleness();
WP_CLI::log('status=' . $res['status'] . ' rows=' . $res['rows'] . ' ' . $res['message']);
WP_CLI::log('latest_metric_date=' . dekart_wm_latest_metric_date() . ' stale=' . ( $stale ? 'yes' : 'no' ));
});
WP_CLI::add_command('dekart webmaster-status', function () {
$stale = dekart_wm_check_staleness();
WP_CLI::log('latest_metric_date=' . dekart_wm_latest_metric_date());
WP_CLI::log('stale=' . ( $stale ? 'yes' : 'no' ));
WP_CLI::log('alert=' . (string) get_option('dekart_wm_alert', ''));
WP_CLI::log('last_success=' . (string) get_option('dekart_wm_last_success', ''));
});
}