Skip to content

Commit e621ac7

Browse files
Merge pull request #428 from JLG-WOCFR-DEV/codex/review-code-and-report-errors
Fix image scan cleanup and preserve null http status
2 parents dc45553 + e12b42b commit e621ac7

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

liens-morts-detector-jlg/includes/blc-scanner.php

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2510,22 +2510,35 @@ private function persistBrokenImage(
25102510
$checked_at_gmt = current_time('mysql', true);
25112511
$row_type = $is_remote_upload_candidate ? 'remote-image' : 'image';
25122512

2513+
$insert_data = [
2514+
'url' => $url_for_storage,
2515+
'anchor' => $anchor_for_storage,
2516+
'post_id' => $post->ID,
2517+
'post_title' => $post_title_for_storage,
2518+
'type' => $row_type,
2519+
'url_host' => $metadata['host'],
2520+
'is_internal' => $metadata['is_internal'],
2521+
'http_status' => null,
2522+
'last_checked_at' => $checked_at_gmt,
2523+
];
2524+
25132525
$inserted = $wpdb->insert(
25142526
$table_name,
2515-
[
2516-
'url' => $url_for_storage,
2517-
'anchor' => $anchor_for_storage,
2518-
'post_id' => $post->ID,
2519-
'post_title' => $post_title_for_storage,
2520-
'type' => $row_type,
2521-
'url_host' => $metadata['host'],
2522-
'is_internal' => $metadata['is_internal'],
2523-
'http_status' => null,
2524-
'last_checked_at' => $checked_at_gmt,
2525-
],
2527+
$insert_data,
25262528
['%s', '%s', '%d', '%s', '%s', '%s', '%d', '%d', '%s']
25272529
);
25282530
if ($inserted) {
2531+
$insert_id = isset($wpdb->insert_id) ? (int) $wpdb->insert_id : 0;
2532+
if ($insert_id > 0 && array_key_exists('http_status', $insert_data) && $insert_data['http_status'] === null) {
2533+
$sanitized_table = '`' . esc_sql($table_name) . '`';
2534+
$wpdb->query(
2535+
$wpdb->prepare(
2536+
"UPDATE {$sanitized_table} SET http_status = NULL WHERE id = %d",
2537+
$insert_id
2538+
)
2539+
);
2540+
}
2541+
25292542
$checked_local_paths[$file_path]['reported_posts'][$post->ID] = true;
25302543
$register_pending_image_insert($post->ID, $row_bytes);
25312544
blc_adjust_dataset_storage_footprint('image', $row_bytes);
@@ -2541,11 +2554,12 @@ private function cleanupOnFailure($pending_image_inserts, $should_cleanup_pendin
25412554
$row_id = isset($entry['id']) ? (int) $entry['id'] : 0;
25422555
$bytes = isset($entry['bytes']) ? (int) $entry['bytes'] : 0;
25432556

2557+
$deleted_rows = 0;
25442558
if ($row_id > 0) {
2545-
$wpdb->delete($table_name, ['id' => $row_id], ['%d']);
2559+
$deleted_rows = (int) $wpdb->delete($table_name, ['id' => $row_id], ['%d']);
25462560
}
25472561

2548-
if ($bytes !== 0) {
2562+
if ($deleted_rows > 0 && $bytes !== 0) {
25492563
blc_adjust_dataset_storage_footprint('image', -$bytes);
25502564
}
25512565
}

0 commit comments

Comments
 (0)