Passed
Push — master ( 057b0c...359636 )
by Evgenii
02:00 queued 48s
created

FlushTransientsCommand::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Helick\RelatedPosts\Commands;
4
5
use Helick\RelatedPosts\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 = "DELETE FROM {$wpdb->options} WHERE `option_name` REGEXP '^_transient_helick_related_posts_[0-9]+_[^_]+$'";
42
        $count = $wpdb->query($query);
43
44
        WP_CLI::success("{$count} related posts transients deleted.");
45
    }
46
47
    /**
48
     * Delete transient timeouts.
49
     *
50
     * @return void
51
     */
52
    private function deleteTransientTimeouts(): void
53
    {
54
        global $wpdb;
55
56
        $query = "DELETE FROM {$wpdb->options} WHERE `option_name` REGEXP '^_transient_timeout_helick_related_posts_[0-9]+_[^_]+$'";
57
        $count = $wpdb->query($query);
58
59
        WP_CLI::success("{$count} related posts transient timeouts deleted.");
60
    }
61
}
62