159 lines
5.1 KiB
PHP
159 lines
5.1 KiB
PHP
|
|
<?php
|
|||
|
|
|
|||
|
|
// action-scheduler-functions.php (загружается только для воркера)
|
|||
|
|
add_action( 'async_post_processing_trigger', 'handle_async_post_processing', 10, 1 );
|
|||
|
|
|
|||
|
|
|
|||
|
|
function handle_async_post_processing( $post_id ) {
|
|||
|
|
|
|||
|
|
// Сохраняем оригинальное состояние кеширования
|
|||
|
|
$original_cache_addition = wp_suspend_cache_addition( true );
|
|||
|
|
$original_cache_invalidation = wp_suspend_cache_invalidation( true );
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
// Очищаем кеш поста
|
|||
|
|
clean_post_cache( $post_id );
|
|||
|
|
|
|||
|
|
// Очищаем кеш таксономий
|
|||
|
|
$taxonomies = get_object_taxonomies( get_post_type( $post_id ) );
|
|||
|
|
foreach ( $taxonomies as $taxonomy ) {
|
|||
|
|
wp_cache_delete( 'all_ids', $taxonomy );
|
|||
|
|
wp_cache_delete( 'get', $taxonomy );
|
|||
|
|
delete_option( "{$taxonomy}_children" );
|
|||
|
|
wp_cache_delete( 'last_changed', 'terms' );
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Получаем свежие данные
|
|||
|
|
$post = get_post( $post_id );
|
|||
|
|
|
|||
|
|
if ( ! $post || 'publish' !== $post->post_status ) {
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
$post_url = get_permalink( $post_id );
|
|||
|
|
$post_title = $post->post_title;
|
|||
|
|
|
|||
|
|
//google news
|
|||
|
|
as_schedule_single_action(
|
|||
|
|
time() + $delay,
|
|||
|
|
'process_sitemap_submission',
|
|||
|
|
[],
|
|||
|
|
'sitemap_generation'
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
//IndexNow
|
|||
|
|
as_schedule_single_action(
|
|||
|
|
time() + 5,
|
|||
|
|
'process_indexnow_submission',
|
|||
|
|
[ $post_id, $post_url, $post_title ],
|
|||
|
|
'post_publish_processing'
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
return true;
|
|||
|
|
|
|||
|
|
} catch ( Exception $e ) {
|
|||
|
|
// Логируем ошибку
|
|||
|
|
error_log( "failed for post {$post_id}: " . $e->getMessage() );
|
|||
|
|
log_scheduler_activity( "failed for post {$post_id}: " . $e->getMessage() );
|
|||
|
|
|
|||
|
|
// Пробрасываем исключение для перевода задачи в Failed
|
|||
|
|
throw $e;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
function log_scheduler_activity( $message, $post_id = null ) {
|
|||
|
|
|
|||
|
|
$log_file = ABSPATH . 'scheduler.log';
|
|||
|
|
|
|||
|
|
$log_entry = sprintf(
|
|||
|
|
"[%s] %s%s\n",
|
|||
|
|
current_time( 'Y-m-d H:i:s' ),
|
|||
|
|
$post_id ? "Post {$post_id}: " : "",
|
|||
|
|
$message
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
file_put_contents( $log_file, $log_entry, FILE_APPEND | LOCK_EX );
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
// generation google news
|
|||
|
|
add_action('process_sitemap_submission', 'handle_sitemap_submission');
|
|||
|
|
|
|||
|
|
function handle_sitemap_submission() {
|
|||
|
|
$generator = new AK_Sitemap_Generator();
|
|||
|
|
$result = $generator->generate_news_sitemap([ 'profile_article', 'anew', 'yellow' ]);
|
|||
|
|
|
|||
|
|
if ($result) {
|
|||
|
|
error_log('News Sitemap generated successfully');
|
|||
|
|
} else {
|
|||
|
|
error_log('News Sitemap generation failed');
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return $result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//index now
|
|||
|
|
function process_indexnow_submission( $post_id, $post_url, $post_title ) {
|
|||
|
|
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
|
|||
|
|
// Проверяем флаг отправки
|
|||
|
|
if ( get_post_meta( $post_id, '_indexnow_sent', true ) ) {
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Ваша логика отправки в IndexNow
|
|||
|
|
$api_key = 'b1a2g3d4i8f6g7h8i9juyg0k11l12';
|
|||
|
|
//$domain = parse_url( home_url(), PHP_URL_HOST );
|
|||
|
|
$domain = 'https://profile.ru';
|
|||
|
|
|
|||
|
|
|
|||
|
|
$body = array(
|
|||
|
|
'host' => $domain,
|
|||
|
|
'key' => $api_key,
|
|||
|
|
// 'keyLocation' => esc_url( home_url( '/' . $api_key . '.txt' ) ), // ❌ НЕ НУЖНО
|
|||
|
|
'urlList' => array( $post_url )
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
$response = wp_remote_post( 'https://api.indexnow.org/IndexNow', array(
|
|||
|
|
'body' => wp_json_encode( $body ),
|
|||
|
|
'headers' => array( 'Content-Type' => 'application/json; charset=utf-8' ),
|
|||
|
|
'timeout' => 30,
|
|||
|
|
));
|
|||
|
|
|
|||
|
|
// ПРАВИЛЬНАЯ ПРОВЕРКА ОТВЕТА
|
|||
|
|
if ( is_wp_error( $response ) ) {
|
|||
|
|
log_scheduler_activity( "IndexNow WP Error for post {$post_id}: " . $response->get_error_message(), $post_id );
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
$response_code = wp_remote_retrieve_response_code( $response );
|
|||
|
|
$response_body = wp_remote_retrieve_body( $response );
|
|||
|
|
|
|||
|
|
// IndexNow возвращает 200/201 при успехе
|
|||
|
|
if ( in_array( $response_code, array( 200, 201, 202 ) ) ) {
|
|||
|
|
// Помечаем как успешно отправленный
|
|||
|
|
update_post_meta( $post_id, '_indexnow_sent', current_time( 'mysql' ) );
|
|||
|
|
log_scheduler_activity( "IndexNow success {$post_url}", $post_id );
|
|||
|
|
return true;
|
|||
|
|
} else {
|
|||
|
|
log_scheduler_activity( "IndexNow failed {$post_url}. Status: {$response_code} | Response: {$response_body}", $post_id );
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
} catch ( Exception $e ) {
|
|||
|
|
// Логируем ошибку
|
|||
|
|
error_log( "IndexNow failed for post {$post_id}: " . $e->getMessage() );
|
|||
|
|
|
|||
|
|
echo $e->getMessage();
|
|||
|
|
|
|||
|
|
// Пробрасываем исключение для перевода задачи в Failed
|
|||
|
|
throw $e;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|