| Conditions | 15 |
| Paths | 9 |
| Total Lines | 65 |
| Code Lines | 42 |
| 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 | $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)); |
||
|
|
|||
| 108 | update_metadata('post', $postId, '_elementor_data', $json); |
||
| 109 | } |
||
| 112 |