Conditions | 18 |
Paths | 9 |
Total Lines | 79 |
Code Lines | 52 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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 | } |
||
126 |