Skip to content

Commit f1a011e

Browse files
Merge pull request #254 from JLG-WOCFR-DEV/codex/add-recheck-interval-option-in-settings
Add configurable broken link recheck interval
2 parents 7f2051e + 5b572b0 commit f1a011e

File tree

3 files changed

+179
-4
lines changed

3 files changed

+179
-4
lines changed

liens-morts-detector-jlg/includes/blc-settings-fields.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ function blc_register_settings() {
2121
$timeout_constraints = blc_get_request_timeout_constraints();
2222
$head_timeout_limits = isset($timeout_constraints['head']) ? $timeout_constraints['head'] : array('default' => 5);
2323
$get_timeout_limits = isset($timeout_constraints['get']) ? $timeout_constraints['get'] : array('default' => 10);
24+
$recheck_constraints = blc_get_recheck_interval_days_constraints();
2425

2526
register_setting(
2627
$option_group,
@@ -72,6 +73,16 @@ function blc_register_settings() {
7273
)
7374
);
7475

76+
register_setting(
77+
$option_group,
78+
'blc_recheck_interval_days',
79+
array(
80+
'type' => 'integer',
81+
'sanitize_callback' => 'blc_sanitize_recheck_interval_days_option',
82+
'default' => isset($recheck_constraints['default']) ? $recheck_constraints['default'] : 7,
83+
)
84+
);
85+
7586
register_setting(
7687
$option_group,
7788
'blc_link_delay',
@@ -262,6 +273,17 @@ function blc_register_settings_sections() {
262273
)
263274
);
264275

276+
add_settings_field(
277+
'blc_recheck_interval_days',
278+
__('🔁 Délai avant re-contrôle', 'liens-morts-detector-jlg'),
279+
'blc_render_recheck_interval_field',
280+
$page,
281+
'blc_planification_section',
282+
array(
283+
'label_for' => 'blc_recheck_interval_days',
284+
)
285+
);
286+
265287
add_settings_section(
266288
'blc_performance_section',
267289
__('Performance', 'liens-morts-detector-jlg'),
@@ -462,6 +484,21 @@ function blc_get_timezone_label() {
462484
return $timezone_label;
463485
}
464486

487+
/**
488+
* Retourne les contraintes associées au délai de re-contrôle des liens.
489+
*
490+
* @since 1.1.0
491+
*
492+
* @return array<string, int>
493+
*/
494+
function blc_get_recheck_interval_days_constraints() {
495+
return array(
496+
'min' => 1,
497+
'max' => 30,
498+
'default' => 7,
499+
);
500+
}
501+
465502
/**
466503
* Retourne la liste des fréquences prédéfinies disponibles dans l'interface.
467504
*
@@ -692,6 +729,64 @@ function blc_render_frequency_field() {
692729
<?php
693730
}
694731

732+
/**
733+
* Affiche le champ de délai avant re-contrôle des liens.
734+
*
735+
* @return void
736+
*/
737+
function blc_render_recheck_interval_field() {
738+
$constraints = blc_get_recheck_interval_days_constraints();
739+
$min_days = isset($constraints['min']) ? (int) $constraints['min'] : 1;
740+
$max_days = isset($constraints['max']) ? (int) $constraints['max'] : 30;
741+
$default = isset($constraints['default']) ? (int) $constraints['default'] : $min_days;
742+
743+
$stored_value = get_option('blc_recheck_interval_days', $default);
744+
$value = is_numeric($stored_value) ? (int) $stored_value : $default;
745+
746+
if ($value < $min_days) {
747+
$value = $min_days;
748+
} elseif ($value > $max_days) {
749+
$value = $max_days;
750+
}
751+
752+
?>
753+
<div class="blc-recheck-interval-field">
754+
<label class="screen-reader-text" for="blc_recheck_interval_days"><?php esc_html_e('Nombre de jours avant re-vérification automatique', 'liens-morts-detector-jlg'); ?></label>
755+
<input
756+
type="range"
757+
name="blc_recheck_interval_days"
758+
id="blc_recheck_interval_days"
759+
value="<?php echo esc_attr($value); ?>"
760+
min="<?php echo esc_attr($min_days); ?>"
761+
max="<?php echo esc_attr($max_days); ?>"
762+
step="1"
763+
>
764+
<output id="blc_recheck_interval_days_output" for="blc_recheck_interval_days"><?php echo esc_html($value); ?></output>
765+
<?php esc_html_e('jours', 'liens-morts-detector-jlg'); ?>
766+
<p class="description">
767+
<?php esc_html_e('Détermine après combien de jours un lien non vérifié est de nouveau signalé comme à recontrôler dans la liste.', 'liens-morts-detector-jlg'); ?>
768+
</p>
769+
</div>
770+
<script>
771+
(function() {
772+
var slider = document.getElementById('blc_recheck_interval_days');
773+
var output = document.getElementById('blc_recheck_interval_days_output');
774+
775+
if (!slider || !output) {
776+
return;
777+
}
778+
779+
var update = function() {
780+
output.textContent = slider.value;
781+
};
782+
783+
slider.addEventListener('input', update);
784+
update();
785+
})();
786+
</script>
787+
<?php
788+
}
789+
695790
/**
696791
* Affiche les champs de plage horaire de repos.
697792
*
@@ -1245,6 +1340,31 @@ function blc_sanitize_rest_end_hour_option($value) {
12451340
return blc_sanitize_rest_hour_option($value, 'blc_rest_end_hour', '20');
12461341
}
12471342

1343+
/**
1344+
* Sanitize le délai avant re-contrôle automatique.
1345+
*
1346+
* @param mixed $value Valeur brute.
1347+
*
1348+
* @return int
1349+
*/
1350+
function blc_sanitize_recheck_interval_days_option($value) {
1351+
$constraints = blc_get_recheck_interval_days_constraints();
1352+
1353+
$min = isset($constraints['min']) ? (int) $constraints['min'] : 1;
1354+
$max = isset($constraints['max']) ? (int) $constraints['max'] : 30;
1355+
$default = isset($constraints['default']) ? (int) $constraints['default'] : $min;
1356+
1357+
$sanitized = is_scalar($value) ? (int) $value : $default;
1358+
1359+
if ($sanitized < $min) {
1360+
$sanitized = $min;
1361+
} elseif ($sanitized > $max) {
1362+
$sanitized = $max;
1363+
}
1364+
1365+
return $sanitized;
1366+
}
1367+
12481368
/**
12491369
* Sanitize le délai entre deux liens.
12501370
*

liens-morts-detector-jlg/includes/class-blc-links-list-table.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,13 +1416,40 @@ private function get_recheck_threshold_gmt() {
14161416
}
14171417

14181418
private function get_recheck_interval_seconds() {
1419-
if (defined('WEEK_IN_SECONDS')) {
1420-
return (int) WEEK_IN_SECONDS;
1419+
$default_days = 7;
1420+
$min_days = 1;
1421+
$max_days = null;
1422+
1423+
if (function_exists('blc_get_recheck_interval_days_constraints')) {
1424+
$constraints = blc_get_recheck_interval_days_constraints();
1425+
1426+
if (isset($constraints['default']) && is_numeric($constraints['default'])) {
1427+
$default_days = (int) $constraints['default'];
1428+
}
1429+
1430+
if (isset($constraints['min']) && is_numeric($constraints['min'])) {
1431+
$min_days = (int) $constraints['min'];
1432+
}
1433+
1434+
if (isset($constraints['max']) && is_numeric($constraints['max'])) {
1435+
$max_days = (int) $constraints['max'];
1436+
}
1437+
}
1438+
1439+
$stored_days = get_option('blc_recheck_interval_days', $default_days);
1440+
$days = is_numeric($stored_days) ? (int) $stored_days : $default_days;
1441+
1442+
if ($days < $min_days) {
1443+
$days = $min_days;
1444+
}
1445+
1446+
if (null !== $max_days && $days > $max_days) {
1447+
$days = $max_days;
14211448
}
14221449

14231450
$day_in_seconds = defined('DAY_IN_SECONDS') ? (int) DAY_IN_SECONDS : 86400;
14241451

1425-
return 7 * $day_in_seconds;
1452+
return max($day_in_seconds, $days * $day_in_seconds);
14261453
}
14271454

14281455
private function get_current_time_gmt() {

tests/BlcDashboardLinksPageTest.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ protected function setUp(): void
7373
'blc_frequency' => 'weekly',
7474
'blc_frequency_custom_hours' => 24,
7575
'blc_frequency_custom_time' => '02:00',
76+
'blc_recheck_interval_days' => 7,
7677
];
7778

7879
$this->previous_wpdb = $GLOBALS['wpdb'] ?? null;
@@ -530,7 +531,8 @@ public function test_prepare_items_applies_needs_recheck_filter(): void
530531
unset($_GET['link_type']);
531532

532533
$needs_recheck_snippet = 'last_checked_at IS NULL OR last_checked_at = %s OR last_checked_at <= %s';
533-
$expected_threshold = gmdate('Y-m-d H:i:s', 1700000000 - (int) WEEK_IN_SECONDS);
534+
$recheck_days = (int) $this->getStoredOption('blc_recheck_interval_days', 7);
535+
$expected_threshold = gmdate('Y-m-d H:i:s', 1700000000 - ($recheck_days * DAY_IN_SECONDS));
534536

535537
$matching_calls = array_filter(
536538
$GLOBALS['wpdb']->prepared_calls,
@@ -546,6 +548,32 @@ public function test_prepare_items_applies_needs_recheck_filter(): void
546548
}
547549
}
548550

551+
public function test_prepare_items_respects_custom_recheck_interval(): void
552+
{
553+
$_GET['link_type'] = 'needs_recheck';
554+
$this->setStoredOption('blc_recheck_interval_days', 3);
555+
556+
$list_table = new \BLC_Links_List_Table();
557+
558+
$list_table->prepare_items();
559+
560+
unset($_GET['link_type']);
561+
562+
$needs_recheck_snippet = 'last_checked_at IS NULL OR last_checked_at = %s OR last_checked_at <= %s';
563+
$expected_threshold = gmdate('Y-m-d H:i:s', 1700000000 - (3 * DAY_IN_SECONDS));
564+
565+
$matching_calls = array_filter(
566+
$GLOBALS['wpdb']->prepared_calls,
567+
static fn(array $call): bool => isset($call['query']) && is_string($call['query']) && str_contains($call['query'], $needs_recheck_snippet)
568+
);
569+
570+
$this->assertNotEmpty($matching_calls);
571+
572+
foreach ($matching_calls as $call) {
573+
$this->assertContains($expected_threshold, $call['params']);
574+
}
575+
}
576+
549577
/**
550578
* @param string $name
551579
* @param mixed $default

0 commit comments

Comments
 (0)