Test Failed
Push — develop ( 55c79d...550055 )
by Paul
09:38
created

Migrate_8_0_0::migrateElementor()   D

Complexity

Conditions 18
Paths 9

Size

Total Lines 79
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 52
c 0
b 0
f 0
dl 0
loc 79
rs 4.8666
cc 18
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
                $assignments = [
77
                    'assigned_posts' => 'assigned_posts_custom',
78
                    'assigned_users' => 'assigned_users_custom',
79
                ];
80
                foreach ($assignments as $assignment => $custom) {
81
                    if ('custom' === ($element['settings'][$assignment] ?? '')) {
82
                        $element['settings'][$assignment] = $element['settings'][$custom] ?? '';
83
                        $do_update = true;
84
                    }
85
                    if (isset($element['settings'][$custom])) {
86
                        unset($element['settings'][$custom]);
87
                        $do_update = true;
88
                    }
89
                }
90
                $replacements = [
91
                    'hide' => [
92
                        'prefix' => 'hide-',
93
                        'values' => [],
94
                    ],
95
                    'filters' => [ // used by the Review Filters addon
96
                        'prefix' => 'filter-',
97
                        'values' => [],
98
                    ],
99
                ];
100
                foreach ($element['settings'] as $key => $value) {
101
                    foreach ($replacements as $setting => $r) {
102
                        if (str_starts_with($key, $r['prefix']) && !empty($value)) {
103
                            $replacements[$setting]['values'][] = Str::removePrefix($key, $r['prefix']);
104
                            unset($element['settings'][$key]);
105
                            break;
106
                        }
107
                    }
108
                }
109
                foreach ($replacements as $setting => $r) {
110
                    if (!empty($r['values'])) {
111
                        $element['settings'][$setting] = implode(',', $r['values']);
112
                        $do_update = true;
113
                    }
114
                }
115
                return $element;
116
            });
117
            if (!$do_update) {
118
                continue;
119
            }
120
            // We need the `wp_slash` because `update_post_meta` does `wp_unslash`
121
            $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

121
            $json = wp_slash(/** @scrutinizer ignore-type */ wp_json_encode($data));
Loading history...
122
            update_metadata('post', $postId, '_elementor_data', $json);
123
        }
124
    }
125
}
126