FlushTransientsCommand   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
c 2
b 0
f 0
dl 0
loc 58
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 3 1
A __invoke() 0 5 1
A deleteTransients() 0 11 1
A deleteTransientTimeouts() 0 11 1
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