Test Failed
Push — main ( ed15c3...2f7161 )
by Paul
12:26 queued 06:03
created

Migrate_7_0_0::migrateSettings()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 21
rs 9.8333
cc 3
nc 4
nop 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Migrations;
4
5
use GeminiLabs\SiteReviews\Contracts\MigrateContract;
6
use GeminiLabs\SiteReviews\Database;
7
use GeminiLabs\SiteReviews\Database\OptionManager;
8
use GeminiLabs\SiteReviews\Database\Query;
9
use GeminiLabs\SiteReviews\Database\Tables;
10
use GeminiLabs\SiteReviews\Modules\Migrate;
11
12
class Migrate_7_0_0 implements MigrateContract
13
{
14
    public function run(): bool
15
    {
16
        delete_option(OptionManager::databaseKey(5));
17
        delete_transient(glsr()->prefix.'cloudflare_ips');
18
        $this->migrateDatabase();
19
        $this->migrateSettings();
20
        return true;
21
    }
22
23
    public function migrateDatabase(): void
24
    {
25
        if ($this->insertTableColumn('is_flagged', 'is_pinned')) {
26
            update_option(glsr()->prefix.'db_version', '1.3');
27
        }
28
    }
29
30
    public function migrateSettings(): void
31
    {
32
        $settings = get_option(OptionManager::databaseKey());
33
        // remove last_migration_run from settings
34
        if (isset($settings['last_migration_run'])) {
35
            unset($settings['last_migration_run']);
36
        }
37
        // fix notification message
38
        if (isset($settings['settings']['general']['notification_message'])) {
39
            $search = [
40
                '{review_author}  - {review_ip}',
41
                '{review_link}',
42
            ];
43
            $replace = [
44
                '{review_author} ({review_email}) - {review_ip}',
45
                '<a href="{edit_url}">Edit Review</a>',
46
            ];
47
            $notification = str_replace($search, $replace, $settings['settings']['general']['notification_message']);
48
            $settings['settings']['general']['notification_message'] = $notification;
49
        }
50
        update_option(OptionManager::databaseKey(), $settings);
51
    }
52
53
    protected function insertTableColumn(string $column, string $afterColumn): bool
54
    {
55
        if (glsr(Tables::class)->columnExists('ratings', $column)) {
56
            return true;
57
        }
58
        if (glsr(Tables::class)->isSqlite()) {
59
            $sql = glsr(Query::class)->sql("
60
                ALTER TABLE table|ratings
61
                ADD COLUMN {$column} tinyint(1) NOT NULL DEFAULT '0'
62
            ");
63
        } else {
64
            $sql = glsr(Query::class)->sql("
65
                ALTER TABLE table|ratings
66
                ADD COLUMN {$column} tinyint(1) NOT NULL DEFAULT '0'
67
                AFTER {$afterColumn}
68
            ");
69
        }
70
        if (false === glsr(Database::class)->dbQuery($sql)) {
71
            glsr_log()->error("The ratings table could not be altered, the [{$column}] column was not added.");
72
            return false;
73
        }
74
        return true;
75
    }
76
}
77