54 lines
1.5 KiB
PHP
54 lines
1.5 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
/**
|
||
|
|
* ТЕСТИРОВАНИЕ ACTION SCHEDULER
|
||
|
|
* Код для проверки работы фоновых задач
|
||
|
|
*/
|
||
|
|
|
||
|
|
// Регистрируем обработчик
|
||
|
|
add_action( 'test_action_scheduler_task', 'test_action_scheduler_log_to_file', 10, 2 );
|
||
|
|
|
||
|
|
function test_action_scheduler_log_to_file( $post_id, $post_title ) {
|
||
|
|
$log_file = ABSPATH . 'action-scheduler-test.log';
|
||
|
|
|
||
|
|
$log_entry = sprintf(
|
||
|
|
"[%s] Задача выполнена | Post ID: %d | Title: %s\n",
|
||
|
|
current_time( 'Y-m-d H:i:s' ),
|
||
|
|
$post_id,
|
||
|
|
$post_title
|
||
|
|
);
|
||
|
|
|
||
|
|
file_put_contents( $log_file, $log_entry, FILE_APPEND | LOCK_EX );
|
||
|
|
//error_log( 'Action Scheduler Test: ' . trim( $log_entry ) );
|
||
|
|
}
|
||
|
|
|
||
|
|
function test_schedule_action_on_save_post( $post_id, $post, $update ) {
|
||
|
|
// Проверки
|
||
|
|
if ( wp_is_post_revision( $post_id ) ||
|
||
|
|
wp_is_post_autosave( $post_id ) ||
|
||
|
|
defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if ( ! in_array( $post->post_type, array( 'profile_article','anew','yellow' ), true ) ) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if ( 'publish' !== $post->post_status ) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Планируем задачу
|
||
|
|
as_schedule_single_action(
|
||
|
|
time() + 60,
|
||
|
|
'test_action_scheduler_task',
|
||
|
|
array( $post_id, $post->post_title ),
|
||
|
|
'test'
|
||
|
|
);
|
||
|
|
|
||
|
|
//error_log( "Задача запланирована для поста ID: {$post_id}" );
|
||
|
|
}
|
||
|
|
|
||
|
|
add_action( 'save_post', 'test_schedule_action_on_save_post', 10, 3 );
|
||
|
|
|