|
9 | 9 |
|
10 | 10 | if (is_admin()) { |
11 | 11 |
|
12 | | - function clearBouncerCache($dislaySuccessFlashMessage = true, $noWarmup = false) |
| 12 | + function clearBouncerCacheInAdminPage() |
13 | 13 | { |
14 | 14 | try { |
15 | 15 | $bouncer = getBouncerInstance(); |
16 | 16 | $bouncer->clearCache(); |
17 | 17 | $message = __('CrowdSec cache has just been cleared.'); |
18 | 18 |
|
19 | 19 | // In stream mode, immediatelly warm the cache up. |
20 | | - if (!$noWarmup && get_option("crowdsec_stream_mode")) { |
21 | | - $bouncer->refreshBlocklistCache(); |
22 | | - $message .= __(' As the stream is enabled, the cache has just been warmed up.'); |
| 20 | + if (get_option("crowdsec_stream_mode")) { |
| 21 | + $result = $bouncer->warmBlocklistCacheUp(); |
| 22 | + $message .= __(' As the stream mode is enabled, the cache has just been warmed up, ' . ($result > 0 ? 'there are now '. $result . ' decisions' : 'there is now '. $result . ' decision') . ' in cache.'); |
23 | 23 | } |
24 | 24 |
|
25 | | - if ($dislaySuccessFlashMessage) { |
26 | | - AdminNotice::displaySuccess($message); |
27 | | - } |
| 25 | + AdminNotice::displaySuccess($message); |
28 | 26 |
|
29 | 27 | // TODO P3 i18n the whole lib https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/ |
30 | 28 | } catch (WordpressCrowdSecBouncerException $e) { |
31 | | - // TODO log error for debug mode only. |
32 | | - AdminNotice::displayError($e->getMessage()); |
| 29 | + getCrowdSecLoggerInstance()->error(null, [ |
| 30 | + 'type' => 'WP_EXCEPTION_WHILE_CLEARING_CACHE', |
| 31 | + 'messsage' => $e->getMessage(), |
| 32 | + 'code' => $e->getCode(), |
| 33 | + 'file' => $e->getFile(), |
| 34 | + 'line' => $e->getLine(), |
| 35 | + ]); |
| 36 | + AdminNotice::displayError('Technical error while clearing the cache: ' . $e->getMessage()); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + function refreshBouncerCacheInAdminPage() |
| 41 | + { |
| 42 | + try { |
| 43 | + if (!get_option("crowdsec_stream_mode")) { |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | + // In stream mode, immediatelly warm the cache up. |
| 48 | + if (get_option("crowdsec_stream_mode")) { |
| 49 | + $bouncer = getBouncerInstance(); |
| 50 | + $result = $bouncer->refreshBlocklistCache(); |
| 51 | + getCrowdSecLoggerInstance()->error(var_export($result, true)); |
| 52 | + AdminNotice::displaySuccess(__(' The cache has just been refreshed (' . ($result['new'] > 0 ? $result['new'] . ' new decisions' : $result['new'] . ' new decision') . ', '.$result['deleted'].' deleted).')); |
| 53 | + } |
| 54 | + } catch (WordpressCrowdSecBouncerException $e) { |
| 55 | + getCrowdSecLoggerInstance()->error(null, [ |
| 56 | + 'type' => 'WP_EXCEPTION_WHILE_REFRESHING_CACHE', |
| 57 | + 'messsage' => $e->getMessage(), |
| 58 | + 'code' => $e->getCode(), |
| 59 | + 'file' => $e->getFile(), |
| 60 | + 'line' => $e->getLine(), |
| 61 | + ]); |
| 62 | + AdminNotice::displayError('Technical error while refreshing the cache: ' . $e->getMessage()); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + function pruneBouncerCacheInAdminPage() |
| 67 | + { |
| 68 | + try { |
| 69 | + $bouncer = getBouncerInstance(); |
| 70 | + $bouncer->pruneCache(); |
| 71 | + |
| 72 | + AdminNotice::displaySuccess(__('CrowdSec cache has just been pruned.')); |
| 73 | + } catch (WordpressCrowdSecBouncerException $e) { |
| 74 | + getCrowdSecLoggerInstance()->error(null, [ |
| 75 | + 'type' => 'WP_EXCEPTION_WHILE_PRUNING', |
| 76 | + 'messsage' => $e->getMessage(), |
| 77 | + 'code' => $e->getCode(), |
| 78 | + 'file' => $e->getFile(), |
| 79 | + 'line' => $e->getLine(), |
| 80 | + ]); |
| 81 | + AdminNotice::displayError('Technical error while pruning the cache: ' . $e->getMessage()); |
33 | 82 | } |
34 | 83 | } |
35 | 84 |
|
36 | 85 | // ACTIONS |
| 86 | + add_action('admin_post_clear_cache', function () { |
| 87 | + clearBouncerCacheInAdminPage(); |
| 88 | + header("Location: {$_SERVER['HTTP_REFERER']}"); |
| 89 | + exit(0); |
| 90 | + }); |
37 | 91 | add_action('admin_post_refresh_cache', function () { |
38 | | - clearBouncerCache(); |
| 92 | + refreshBouncerCacheInAdminPage(); |
| 93 | + header("Location: {$_SERVER['HTTP_REFERER']}"); |
| 94 | + exit(0); |
| 95 | + }); |
| 96 | + add_action('admin_post_prune_cache', function () { |
| 97 | + pruneBouncerCacheInAdminPage(); |
39 | 98 | header("Location: {$_SERVER['HTTP_REFERER']}"); |
40 | 99 | exit(0); |
41 | 100 | }); |
|
0 commit comments