Test Failed
Push — develop ( a853f2...65fdf6 )
by Paul
10:01
created

Migrate_8_0_0::migrateElementor()   C

Complexity

Conditions 15
Paths 9

Size

Total Lines 65
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 42
c 0
b 0
f 0
dl 0
loc 65
rs 5.9166
cc 15
nc 9
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Query;
8
use GeminiLabs\SiteReviews\Database\Tables\TableStats;
9
use GeminiLabs\SiteReviews\Helpers\Str;
10
11
class Migrate_8_0_0 implements MigrateContract
12
{
13
    public function run(): bool
14
    {
15
        $this->migrateElementor();
16
        return $this->migrateDatabase();
17
    }
18
19
    public function migrateDatabase(): bool
20
    {
21
        $isDirty = false;
22
        $indexes = glsr(Database::class)->dbGetResults(
23
            glsr(Query::class)->sql("SHOW INDEXES FROM table|ratings")
24
        );
25
        $keyNames = wp_list_pluck($indexes, 'Key_name');
26
        if (!in_array('glsr_ratings_ip_address_index', $keyNames)) {
27
            $sql = glsr(Query::class)->sql("
28
                ALTER TABLE table|ratings ADD INDEX glsr_ratings_ip_address_index (ip_address)
29
            ");
30
            if (false === glsr(Database::class)->dbQuery($sql)) {
31
                glsr_log()->error("The ratings table could not be altered, the [ip_address_index] index was not added.");
32
                $isDirty = true;
33
            }
34
        }
35
        glsr(TableStats::class)->create();
36
        glsr(TableStats::class)->addForeignConstraints();
37
        if (!glsr(TableStats::class)->exists() || $isDirty) {
38
            return false;
39
        }
40
        update_option(glsr()->prefix.'db_version', '1.5');
41
        return true;
42
    }
43
44
    public function migrateElementor(): void
45
    {
46
        if (!class_exists('Elementor\Plugin')) {
47
            return;
48
        }
49
        $sql = glsr(Query::class)->sql("
50
            SELECT post_id
51
            FROM table|postmeta 
52
            WHERE meta_key = '_elementor_data'
53
            AND meta_value LIKE %s
54
        ", '%"widgetType":"site_review%');
55
        $postIds = glsr(Database::class)->dbGetCol($sql);
56
        if (empty($postIds)) {
57
            return;
58
        }
59
        foreach ($postIds as $postId) {
60
            $do_update = false;
61
            $document = \Elementor\Plugin::$instance->documents->get($postId);
62
            if ($document) {
63
                $data = $document->get_elements_data(); // @phpstan-ignore-line
64
            }
65
            if (empty($data)) {
66
                continue;
67
            }
68
            $data = \Elementor\Plugin::$instance->db->iterate_data($data, function ($element) use (&$do_update) {
69
                $widget = $element['widgetType'] ?? '';
70
                if (!str_starts_with($widget, 'site_review')) {
71
                    return $element;
72
                }
73
                if (empty($element['settings'])) {
74
                    return $element;
75
                }
76
                $replacements = [
77
                    'hide' => [
78
                        'prefix' => 'hide-',
79
                        'values' => [],
80
                    ],
81
                    'filters' => [ // used by the Review Filters addon
82
                        'prefix' => 'filter-',
83
                        'values' => [],
84
                    ],
85
                ];
86
                foreach ($element['settings'] as $key => $value) {
87
                    foreach ($replacements as $setting => $r) {
88
                        if (str_starts_with($key, $r['prefix']) && !empty($value)) {
89
                            $replacements[$setting]['values'][] = Str::removePrefix($key, $r['prefix']);
90
                            unset($element['settings'][$key]);
91
                            break;
92
                        }
93
                    }
94
                }
95
                foreach ($replacements as $setting => $r) {
96
                    if (!empty($r['values'])) {
97
                        $element['settings'][$setting] = implode(',', $r['values']);
98
                        $do_update = true;
99
                    }
100
                }
101
                return $element;
102
            });
103
            if (!$do_update) {
104
                continue;
105
            }
106
            // We need the `wp_slash` because `update_post_meta` does `wp_unslash`
107
            $json = wp_slash(wp_json_encode($data));
0 ignored issues
show
Bug introduced by
It seems like wp_json_encode($data) can also be of type false; however, parameter $value of wp_slash() does only seem to accept array|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
            $json = wp_slash(/** @scrutinizer ignore-type */ wp_json_encode($data));
Loading history...
108
            update_metadata('post', $postId, '_elementor_data', $json);
109
        }
110
    }
111
}
112