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)); |
|
|
|
|
122
|
|
|
update_metadata('post', $postId, '_elementor_data', $json); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|