shkola/wp-content/themes/dekart/inc/webmaster-recrawl.php
2026-10-08 16:59:02 +00:00

198 lines
6.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
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);
}
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 (! dekart_webmaster_enabled()) {
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
);
}
if (! ( defined('WP_CLI') && WP_CLI )) {
if (dekart_webmaster_enabled()) {
if (! wp_next_scheduled('dekart_recrawl_cron')) {
wp_schedule_event(time() + 1800, 'hourly', 'dekart_recrawl_cron');
}
} elseif (wp_next_scheduled('dekart_recrawl_cron')) {
wp_clear_scheduled_hook('dekart_recrawl_cron');
}
}