// 管理画面にバックグラウンド開始フォームを追加
function fanza_auto_import_settings_page() {
echo '
FANZA商品API 自動取得設定
';
echo '';
if (isset($_POST['fanza_bg_run_now']) && check_admin_referer('fanza_bg_run')) {
$st = sanitize_text_field($_POST['fanza_bg_openstt']);
$ed = sanitize_text_field($_POST['fanza_bg_openend']);
update_option('fanza_runner_state', array(
'openstt' => $st,
'openend' => $ed,
'offset' => 1,
'added' => 0,
'updated' => 0,
'skipped' => 0,
'fetched' => 0,
'done' => 0,
'last' => current_time('mysql'),
), false);
delete_option('fanza_import_bookmark');
wp_schedule_single_event(time() + 5, 'fanza_background_runner');
echo '';
}
// 進捗表示
$state = get_option('fanza_runner_state', array());
if (!empty($state)) {
echo 'バックグラウンド進捗
';
echo '' . esc_html(print_r($state, true)) . '
';
}
// ログ表示
echo '自動取得ログ
';
$logs = get_option('fanza_auto_import_logs', array());
if (!empty($logs)) {
echo '';
foreach (array_reverse($logs) as $log) {
echo '
[' . esc_html($log['date']) . '] ' . esc_html($log['message']) . '
';
}
echo '
';
echo 'ログをクリア
';
}
if (isset($_GET['clear_logs']) && $_GET['clear_logs'] == '1') {
delete_option('fanza_auto_import_logs');
echo '';
}
}
add_action('fanza_background_runner', function () {
if (get_transient('fanza_bg_lock')) return;
set_transient('fanza_bg_lock', 1, 2 * MINUTE_IN_SECONDS);
$state = get_option('fanza_runner_state', array());
if (empty($state) || empty($state['openstt']) || empty($state['openend'])) {
delete_transient('fanza_bg_lock');
return;
}
$msg = fanza_fetch_and_insert_posts($state['openstt'], $state['openend']);
$state['last'] = current_time('mysql');
update_option('fanza_runner_state', $state, false);
$bm = get_option('fanza_import_bookmark', array());
if (!empty($bm['openstt']) && !empty($bm['openend'])) {
wp_schedule_single_event(time() + 5, 'fanza_background_runner');
fanza_add_import_log('BG: 次バッチをスケジュール (offset=' . intval($bm['offset'] ?? 0) . ')');
} else {
$state['done'] = 1;
update_option('fanza_runner_state', $state, false);
fanza_add_import_log('BG: 完走しました。');
}
delete_transient('fanza_bg_lock');
});
function fanza_add_import_log($message) {
$logs = get_option('fanza_auto_import_logs', array());
$logs[] = array(
'date' => current_time('Y-m-d H:i:s'),
'message' => $message
);
if (count($logs) > 500) {
$logs = array_slice($logs, -500);
}
update_option('fanza_auto_import_logs', $logs);
}
if (defined('WP_CLI') && WP_CLI) {
class FANZA_CLI_Commands {
public function import($args, $assoc_args) {
$from = $assoc_args['date-from'] ?? current_time('Ymd');
$to = $assoc_args['date-to'] ?? current_time('Ymd');
$msg = fanza_fetch_and_insert_posts($from, $to);
WP_CLI::success($msg);
}
public function status($args, $assoc_args) {
$enabled = get_option('fanza_auto_import_enabled', false);
$time = get_option('fanza_auto_import_time', '03:00');
$status = get_option('fanza_auto_import_status', 'draft');
WP_CLI::log("自動取得: " . ($enabled ? '有効' : '無効'));
WP_CLI::log("時刻: {$time}, ステータス: {$status}");
}
public function stats($args, $assoc_args) {
$count = wp_count_posts('post');
WP_CLI::log("投稿数: " . $count->publish);
}
}
WP_CLI::add_command('fanza', 'FANZA_CLI_Commands');
}
Warning: Cannot modify header information - headers already sent by (output started at /home/realine/realine.xsrv.jp/public_html/wp-content/plugins/fanza-products/fanza-products.php:1697) in /home/realine/realine.xsrv.jp/public_html/wp-includes/pluggable.php on line 1450
Warning: Cannot modify header information - headers already sent by (output started at /home/realine/realine.xsrv.jp/public_html/wp-content/plugins/fanza-products/fanza-products.php:1697) in /home/realine/realine.xsrv.jp/public_html/wp-includes/pluggable.php on line 1453