FlushTransientsCommand::__invoke()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Helick\RelatedPosts\Commands;
4
5
use Helick\Contracts\Bootable;
6
use WP_CLI;
7
8
final class FlushTransientsCommand implements Bootable
9
{
10
    /**
11
     * Boot the service.
12
     *
13
     * @return void
14
     */
15
    public static function boot(): void
16
    {
17
        WP_CLI::add_command('helick-related-posts flush-transients', static::class);
18
    }
19
20
    /**
21
     * Flush transient data.
22
     *
23
     * @return void
24
     */
25
    public function __invoke(): void
26
    {
27
        $this->deleteTransients();
28
29
        $this->deleteTransientTimeouts();
30
    }
31
32
    /**
33
     * Delete transients.
34
     *
35
     * @return void
36
     */
37
    private function deleteTransients(): void
38
    {
39
        global $wpdb;
40
41
        $query =
42
            "DELETE FROM {$wpdb->options} " .
43
            "WHERE `option_name` REGEXP '^_transient_helick_related_posts_[0-9]+_[^_]+$'";
44
45
        $count = $wpdb->query($query);
46
47
        WP_CLI::success("{$count} related posts transients deleted.");
48
    }
49
50
    /**
51
     * Delete transient timeouts.
52
     *
53
     * @return void
54
     */
55
    private function deleteTransientTimeouts(): void
56
    {
57
        global $wpdb;
58
59
        $query =
60
            "DELETE FROM {$wpdb->options} " .
61
            "WHERE `option_name` REGEXP '^_transient_timeout_helick_related_posts_[0-9]+_[^_]+$'";
62
63
        $count = $wpdb->query($query);
64
65
        WP_CLI::success("{$count} related posts transient timeouts deleted.");
66
    }
67
}
68