2026-10-04 GLOBAL
This commit is contained in:
parent
dcac71ac4c
commit
f93b4d93e0
@ -3711,6 +3711,29 @@ function dekart_output_counters_footer()
|
||||
}
|
||||
}
|
||||
|
||||
// Не-прод стенд (кроме CLI): страховка от переноса опций деплоем
|
||||
function dekart_nonprod_stand()
|
||||
{
|
||||
if (defined('WP_CLI') && WP_CLI) {
|
||||
return false;
|
||||
}
|
||||
return ! dekart_is_prod_host();
|
||||
}
|
||||
|
||||
// Счётчики пусты + blog_public 0 (noindex) на не-проде
|
||||
add_filter('pre_option_blog_public', 'dekart_nonprod_blog_public');
|
||||
function dekart_nonprod_blog_public($pre)
|
||||
{
|
||||
return dekart_nonprod_stand() ? '0' : $pre;
|
||||
}
|
||||
|
||||
foreach (array( 'dekart_counter_yandex', 'dekart_counter_gtm_head', 'dekart_counter_gtm_body', 'dekart_counter_footer' ) as $dekart_nonprod_counter) {
|
||||
add_filter('pre_option_' . $dekart_nonprod_counter, function ($pre) {
|
||||
return dekart_nonprod_stand() ? '' : $pre;
|
||||
});
|
||||
}
|
||||
unset($dekart_nonprod_counter);
|
||||
|
||||
// ─────────────────────────────────────────────────────────────
|
||||
// 🎨 Тёмная тема для Carbon Fields + Настройки филиала
|
||||
// ─────────────────────────────────────────────────────────────
|
||||
@ -4588,6 +4611,7 @@ function dekart_flush_cache_page()
|
||||
* Единый @graph в wp_head. Только Carbon Fields данные.
|
||||
*/
|
||||
require_once __DIR__ . '/inc/schema.php';
|
||||
require_once __DIR__ . '/inc/webmaster-recrawl.php';
|
||||
|
||||
/**
|
||||
* Фильтр: замена H1 для страницы "Подготовка к школе" (ID 2)
|
||||
|
||||
188
wp-content/themes/dekart/inc/webmaster-recrawl.php
Normal file
188
wp-content/themes/dekart/inc/webmaster-recrawl.php
Normal file
@ -0,0 +1,188 @@
|
||||
<?php
|
||||
// Яндекс.Вебмастер — отправка URL на переобход
|
||||
|
||||
if (! defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
// Токен: константа или опция филиала
|
||||
function dekart_webmaster_token()
|
||||
{
|
||||
if (defined('DEKART_YANDEX_WEBMASTER_TOKEN') && DEKART_YANDEX_WEBMASTER_TOKEN) {
|
||||
return (string) DEKART_YANDEX_WEBMASTER_TOKEN;
|
||||
}
|
||||
return trim((string) get_option('branch_yandex_webmaster_token', ''));
|
||||
}
|
||||
|
||||
// Включено только на проде и при заданном токене
|
||||
function dekart_webmaster_enabled()
|
||||
{
|
||||
return dekart_is_prod_host() && '' !== dekart_webmaster_token();
|
||||
}
|
||||
|
||||
// Обёртка запроса к API v4
|
||||
function dekart_webmaster_request($method, $path, $args = array())
|
||||
{
|
||||
$token = dekart_webmaster_token();
|
||||
if ('' === $token) {
|
||||
return new WP_Error('dekart_wm_no_token', 'Нет токена Вебмастера');
|
||||
}
|
||||
$args['method'] = $method;
|
||||
$args['timeout'] = 15;
|
||||
$args['headers'] = array_merge(
|
||||
array(
|
||||
'Authorization' => 'OAuth ' . $token,
|
||||
'Content-Type' => 'application/json; charset=utf-8',
|
||||
),
|
||||
isset($args['headers']) ? (array) $args['headers'] : array()
|
||||
);
|
||||
return wp_remote_request('https://api.webmaster.yandex.net/v4' . $path, $args);
|
||||
}
|
||||
|
||||
// user_id + host_id (с кешем в опциях)
|
||||
function dekart_webmaster_ids()
|
||||
{
|
||||
$user_id = (string) get_option('dekart_webmaster_user_id', '');
|
||||
$host_id = (string) get_option('dekart_webmaster_host_id', '');
|
||||
if ('' !== $user_id && '' !== $host_id) {
|
||||
return array( $user_id, $host_id );
|
||||
}
|
||||
|
||||
if ('' === $user_id) {
|
||||
$res = dekart_webmaster_request('GET', '/user/');
|
||||
if (is_wp_error($res)) {
|
||||
return array( '', '' );
|
||||
}
|
||||
$data = json_decode(wp_remote_retrieve_body($res), true);
|
||||
$user_id = isset($data['user_id']) ? (string) $data['user_id'] : '';
|
||||
if ('' !== $user_id) {
|
||||
update_option('dekart_webmaster_user_id', $user_id, false);
|
||||
}
|
||||
}
|
||||
|
||||
if ('' !== $user_id && '' === $host_id) {
|
||||
$res = dekart_webmaster_request('GET', '/user/' . rawurlencode($user_id) . '/hosts/');
|
||||
if (is_wp_error($res)) {
|
||||
return array( $user_id, '' );
|
||||
}
|
||||
$data = json_decode(wp_remote_retrieve_body($res), true);
|
||||
$host = strtolower((string) wp_parse_url(home_url(), PHP_URL_HOST));
|
||||
$list = ( isset($data['hosts']) && is_array($data['hosts']) ) ? $data['hosts'] : array();
|
||||
foreach ($list as $h) {
|
||||
$ascii = strtolower((string) ( $h['ascii_host_url'] ?? '' ));
|
||||
$uni = strtolower((string) ( $h['unicode_host_url'] ?? '' ));
|
||||
if ('' !== $host && ( false !== strpos($ascii, $host) || false !== strpos($uni, $host) )) {
|
||||
$host_id = (string) ( $h['host_id'] ?? '' );
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ('' === $host_id && ! empty($list[0]['host_id'])) {
|
||||
$host_id = (string) $list[0]['host_id'];
|
||||
}
|
||||
if ('' !== $host_id) {
|
||||
update_option('dekart_webmaster_host_id', $host_id, false);
|
||||
}
|
||||
}
|
||||
|
||||
return array( $user_id, $host_id );
|
||||
}
|
||||
|
||||
// В очередь URL
|
||||
function dekart_recrawl_enqueue($url)
|
||||
{
|
||||
$url = esc_url_raw($url);
|
||||
if ('' === $url) {
|
||||
return;
|
||||
}
|
||||
$queue = (array) get_option('dekart_recrawl_queue', array());
|
||||
if (in_array($url, $queue, true)) {
|
||||
return;
|
||||
}
|
||||
$queue[] = $url;
|
||||
if (count($queue) > 1000) {
|
||||
$queue = array_slice($queue, -1000);
|
||||
}
|
||||
update_option('dekart_recrawl_queue', array_values($queue), false);
|
||||
}
|
||||
|
||||
// Ставим URL при публикации/обновлении
|
||||
add_action('save_post', 'dekart_recrawl_on_save', 20, 3);
|
||||
function dekart_recrawl_on_save($post_id, $post, $update)
|
||||
{
|
||||
if (wp_is_post_revision($post_id) || wp_is_post_autosave($post_id)) {
|
||||
return;
|
||||
}
|
||||
if (! $post || 'publish' !== $post->post_status || 'attachment' === $post->post_type) {
|
||||
return;
|
||||
}
|
||||
if (function_exists('is_post_type_viewable') && ! is_post_type_viewable($post->post_type)) {
|
||||
return;
|
||||
}
|
||||
dekart_recrawl_enqueue(get_permalink($post_id));
|
||||
}
|
||||
|
||||
// Отправка из очереди (cron)
|
||||
add_action('dekart_recrawl_cron', 'dekart_recrawl_run');
|
||||
function dekart_recrawl_run()
|
||||
{
|
||||
if (! dekart_webmaster_enabled()) {
|
||||
return;
|
||||
}
|
||||
list( $user_id, $host_id ) = dekart_webmaster_ids();
|
||||
if ('' === $user_id || '' === $host_id) {
|
||||
return;
|
||||
}
|
||||
|
||||
$queue = (array) get_option('dekart_recrawl_queue', array());
|
||||
if (empty($queue)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Квота переобхода
|
||||
$remainder = null;
|
||||
$res = dekart_webmaster_request('GET', '/user/' . rawurlencode($user_id) . '/hosts/' . rawurlencode($host_id) . '/recrawl/quota/');
|
||||
if (! is_wp_error($res) && 200 === (int) wp_remote_retrieve_response_code($res)) {
|
||||
$q = json_decode(wp_remote_retrieve_body($res), true);
|
||||
if (isset($q['quota_remainder'])) {
|
||||
$remainder = (int) $q['quota_remainder'];
|
||||
}
|
||||
}
|
||||
|
||||
$limit = ( null === $remainder ) ? 10 : max(0, min(100, $remainder));
|
||||
$sent = 0;
|
||||
$failed = 0;
|
||||
$left = array_values($queue);
|
||||
|
||||
foreach ($queue as $url) {
|
||||
if ($sent >= $limit) {
|
||||
break;
|
||||
}
|
||||
$r = dekart_webmaster_request('POST', '/user/' . rawurlencode($user_id) . '/hosts/' . rawurlencode($host_id) . '/recrawl/queue', array(
|
||||
'body' => wp_json_encode(array( 'url' => $url )),
|
||||
));
|
||||
if (is_wp_error($r) || wp_remote_retrieve_response_code($r) >= 400) {
|
||||
$failed++;
|
||||
break; // ошибка/квота — стоп, остаток очереди сохраняем
|
||||
}
|
||||
$sent++;
|
||||
array_shift($left);
|
||||
}
|
||||
|
||||
update_option('dekart_recrawl_queue', $left, false);
|
||||
update_option(
|
||||
'dekart_recrawl_last',
|
||||
array(
|
||||
'ts' => time(),
|
||||
'sent' => $sent,
|
||||
'failed' => $failed,
|
||||
'remainder' => $remainder,
|
||||
'pending' => count($left),
|
||||
),
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
// Cron — ежечасно
|
||||
if (! wp_next_scheduled('dekart_recrawl_cron')) {
|
||||
wp_schedule_event(time() + 1800, 'hourly', 'dekart_recrawl_cron');
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user