Skip to content

Commit f7e3a87

Browse files
Merge pull request #215 from JLG-WOCFR-DEV/codex/update-dashboard-cron-scheduling-logic
Trigger WordPress cron after manual scan scheduling
2 parents 508722b + 53f0563 commit f7e3a87

File tree

4 files changed

+189
-0
lines changed

4 files changed

+189
-0
lines changed

liens-morts-detector-jlg/includes/blc-admin-pages.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,45 @@ function blc_dashboard_links_page() {
116116
esc_html__("La vérification des liens n'a pas pu être programmée. Veuillez réessayer.", 'liens-morts-detector-jlg')
117117
);
118118
} else {
119+
$manual_trigger_failed = false;
120+
121+
if (!defined('DISABLE_WP_CRON') || !DISABLE_WP_CRON) {
122+
$manual_trigger_result = null;
123+
124+
if (function_exists('spawn_cron')) {
125+
$manual_trigger_result = spawn_cron();
126+
} elseif (function_exists('wp_cron')) {
127+
$manual_trigger_result = wp_cron();
128+
}
129+
130+
if (null !== $manual_trigger_result) {
131+
$is_error = (false === $manual_trigger_result);
132+
133+
if (function_exists('is_wp_error') && is_wp_error($manual_trigger_result)) {
134+
$is_error = true;
135+
}
136+
137+
if ($is_error) {
138+
$manual_trigger_failed = true;
139+
error_log('BLC: Manual cron trigger failed for link check.');
140+
}
141+
}
142+
}
143+
119144
printf(
120145
'<div class="notice notice-success is-dismissible"><p>%s</p></div>',
121146
esc_html__("La vérification des liens a été programmée et s'exécute en arrière-plan.", 'liens-morts-detector-jlg')
122147
);
148+
149+
if ($manual_trigger_failed) {
150+
printf(
151+
'<div class="notice notice-error is-dismissible"><p>%s</p></div>',
152+
esc_html__(
153+
"Le déclenchement immédiat du cron a échoué. Le système WordPress essaiera de l'exécuter automatiquement.",
154+
'liens-morts-detector-jlg'
155+
)
156+
);
157+
}
123158
}
124159
}
125160

@@ -223,10 +258,45 @@ function blc_dashboard_images_page() {
223258
esc_html__("La vérification des images n'a pas pu être programmée. Veuillez réessayer.", 'liens-morts-detector-jlg')
224259
);
225260
} else {
261+
$manual_trigger_failed = false;
262+
263+
if (!defined('DISABLE_WP_CRON') || !DISABLE_WP_CRON) {
264+
$manual_trigger_result = null;
265+
266+
if (function_exists('spawn_cron')) {
267+
$manual_trigger_result = spawn_cron();
268+
} elseif (function_exists('wp_cron')) {
269+
$manual_trigger_result = wp_cron();
270+
}
271+
272+
if (null !== $manual_trigger_result) {
273+
$is_error = (false === $manual_trigger_result);
274+
275+
if (function_exists('is_wp_error') && is_wp_error($manual_trigger_result)) {
276+
$is_error = true;
277+
}
278+
279+
if ($is_error) {
280+
$manual_trigger_failed = true;
281+
error_log('BLC: Manual cron trigger failed for image check.');
282+
}
283+
}
284+
}
285+
226286
printf(
227287
'<div class="notice notice-success is-dismissible"><p>%s</p></div>',
228288
esc_html__("La vérification des images a été programmée et s'exécute en arrière-plan.", 'liens-morts-detector-jlg')
229289
);
290+
291+
if ($manual_trigger_failed) {
292+
printf(
293+
'<div class="notice notice-error is-dismissible"><p>%s</p></div>',
294+
esc_html__(
295+
"Le déclenchement immédiat du cron a échoué. Le système WordPress essaiera de l'exécuter automatiquement.",
296+
'liens-morts-detector-jlg'
297+
)
298+
);
299+
}
230300
}
231301
}
232302

tests/BlcDashboardImagesPageTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace {
44
require_once __DIR__ . '/translation-stubs.php';
5+
require_once __DIR__ . '/stubs/cron-stubs.php';
56
}
67

78
namespace Tests {
@@ -106,6 +107,7 @@ protected function tearDown(): void
106107
}
107108

108109
$_POST = [];
110+
unset($GLOBALS['__blc_spawn_cron_callback'], $GLOBALS['__blc_wp_cron_callback']);
109111
}
110112

111113
public function test_manual_image_check_shows_error_notice_when_schedule_fails(): void
@@ -130,6 +132,51 @@ public function test_manual_image_check_shows_error_notice_when_schedule_fails()
130132
$this->assertStringNotContainsString("La vérification des images a été programmée", $output);
131133
}
132134

135+
public function test_manual_image_check_triggers_spawn_cron_when_schedule_succeeds(): void
136+
{
137+
$_POST['blc_manual_image_check'] = '1';
138+
139+
$calls = 0;
140+
$GLOBALS['__blc_spawn_cron_callback'] = static function () use (&$calls) {
141+
$calls++;
142+
143+
return true;
144+
};
145+
146+
ob_start();
147+
blc_dashboard_images_page();
148+
$output = (string) ob_get_clean();
149+
150+
$this->assertSame(1, $calls);
151+
$this->assertStringContainsString("La vérification des images a été programmée", $output);
152+
$this->assertStringNotContainsString("Le déclenchement immédiat du cron a échoué", $output);
153+
}
154+
155+
public function test_manual_image_check_shows_error_when_manual_trigger_fails(): void
156+
{
157+
$_POST['blc_manual_image_check'] = '1';
158+
159+
$calls = 0;
160+
$GLOBALS['__blc_spawn_cron_callback'] = static function () use (&$calls) {
161+
$calls++;
162+
163+
return false;
164+
};
165+
166+
Functions\expect('error_log')->once()->withArgs(static function ($message) {
167+
return is_string($message)
168+
&& str_contains($message, 'Manual cron trigger failed for image check');
169+
});
170+
171+
ob_start();
172+
blc_dashboard_images_page();
173+
$output = (string) ob_get_clean();
174+
175+
$this->assertSame(1, $calls);
176+
$this->assertStringContainsString("La vérification des images a été programmée", $output);
177+
$this->assertStringContainsString("Le déclenchement immédiat du cron a échoué", $output);
178+
}
179+
133180
/**
134181
* @param string $name
135182
* @param mixed $default

tests/BlcDashboardLinksPageTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace {
44
require_once __DIR__ . '/translation-stubs.php';
5+
require_once __DIR__ . '/stubs/cron-stubs.php';
56
}
67

78
namespace Tests {
@@ -123,6 +124,7 @@ protected function tearDown(): void
123124
}
124125

125126
$_POST = [];
127+
unset($GLOBALS['__blc_spawn_cron_callback'], $GLOBALS['__blc_wp_cron_callback']);
126128
}
127129

128130
public function test_last_check_time_uses_site_timezone(): void
@@ -175,6 +177,51 @@ public function test_manual_check_shows_error_notice_when_schedule_fails(): void
175177
$this->assertStringNotContainsString("La vérification des liens a été programmée", $output);
176178
}
177179

180+
public function test_manual_check_triggers_spawn_cron_when_schedule_succeeds(): void
181+
{
182+
$_POST['blc_manual_check'] = '1';
183+
184+
$calls = 0;
185+
$GLOBALS['__blc_spawn_cron_callback'] = static function () use (&$calls) {
186+
$calls++;
187+
188+
return true;
189+
};
190+
191+
ob_start();
192+
blc_dashboard_links_page();
193+
$output = (string) ob_get_clean();
194+
195+
$this->assertSame(1, $calls);
196+
$this->assertStringContainsString("La vérification des liens a été programmée", $output);
197+
$this->assertStringNotContainsString("Le déclenchement immédiat du cron a échoué", $output);
198+
}
199+
200+
public function test_manual_check_shows_error_when_manual_trigger_fails(): void
201+
{
202+
$_POST['blc_manual_check'] = '1';
203+
204+
$calls = 0;
205+
$GLOBALS['__blc_spawn_cron_callback'] = static function () use (&$calls) {
206+
$calls++;
207+
208+
return false;
209+
};
210+
211+
Functions\expect('error_log')->once()->withArgs(static function ($message) {
212+
return is_string($message)
213+
&& str_contains($message, 'Manual cron trigger failed for link check');
214+
});
215+
216+
ob_start();
217+
blc_dashboard_links_page();
218+
$output = (string) ob_get_clean();
219+
220+
$this->assertSame(1, $calls);
221+
$this->assertStringContainsString("La vérification des liens a été programmée", $output);
222+
$this->assertStringContainsString("Le déclenchement immédiat du cron a échoué", $output);
223+
}
224+
178225
/**
179226
* @param string $name
180227
* @param mixed $default

tests/stubs/cron-stubs.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace {
4+
if (!function_exists('spawn_cron')) {
5+
function spawn_cron()
6+
{
7+
if (isset($GLOBALS['__blc_spawn_cron_callback']) && is_callable($GLOBALS['__blc_spawn_cron_callback'])) {
8+
return call_user_func($GLOBALS['__blc_spawn_cron_callback']);
9+
}
10+
11+
return null;
12+
}
13+
}
14+
15+
if (!function_exists('wp_cron')) {
16+
function wp_cron()
17+
{
18+
if (isset($GLOBALS['__blc_wp_cron_callback']) && is_callable($GLOBALS['__blc_wp_cron_callback'])) {
19+
return call_user_func($GLOBALS['__blc_wp_cron_callback']);
20+
}
21+
22+
return null;
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)