From f93b4d93e00e6af84cb50c69d9799bfa42633584 Mon Sep 17 00:00:00 2001 From: Dekart Deploy Bot Date: Sun, 4 Oct 2026 20:58:46 +0000 Subject: [PATCH] 2026-10-04 GLOBAL --- wp-content/themes/dekart/functions.php | 24 +++ .../themes/dekart/inc/webmaster-recrawl.php | 188 ++++++++++++++++++ 2 files changed, 212 insertions(+) create mode 100644 wp-content/themes/dekart/inc/webmaster-recrawl.php diff --git a/wp-content/themes/dekart/functions.php b/wp-content/themes/dekart/functions.php index 5dd5455..cfb19f0 100644 --- a/wp-content/themes/dekart/functions.php +++ b/wp-content/themes/dekart/functions.php @@ -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) diff --git a/wp-content/themes/dekart/inc/webmaster-recrawl.php b/wp-content/themes/dekart/inc/webmaster-recrawl.php new file mode 100644 index 0000000..500548d --- /dev/null +++ b/wp-content/themes/dekart/inc/webmaster-recrawl.php @@ -0,0 +1,188 @@ + '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'); +}