Passed
Push — master ( 18d41c...f9af5f )
by Paul
13:52 queued 07:13
created

MigrateSidebars   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Test Coverage

Coverage 63.95%

Importance

Changes 4
Bugs 2 Features 0
Metric Value
wmc 25
eloc 84
c 4
b 2
f 0
dl 0
loc 186
ccs 55
cts 86
cp 0.6395
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 6 1
A migrateSidebarWidgets() 0 6 2
A migrateUserMeta() 0 28 2
A migrateThemeModWidgets() 0 9 3
A migrateWidgetData() 0 11 4
A migrateWidgets() 0 13 3
A updateWidgetNames() 0 10 2
A mapWidgetData() 0 16 3
A __construct() 0 5 1
A queryThemeMods() 0 5 1
A widgetsExist() 0 12 3
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Migrations\Migrate_5_0_0;
4
5
use GeminiLabs\SiteReviews\Helpers\Arr;
6
use GeminiLabs\SiteReviews\Helpers\Str;
7
8
class MigrateSidebars
9
{
10
    public $db;
11
    public $limit;
12
13 14
    public function __construct()
14
    {
15 14
        global $wpdb;
16 14
        $this->db = $wpdb;
17 14
        $this->limit = 250;
18 14
    }
19
20
    /**
21
     * @return void
22
     */
23 14
    public function run()
24
    {
25 14
        $this->migrateSidebarWidgets();
26 14
        $this->migrateThemeModWidgets();
27 14
        $this->migrateUserMeta();
28 14
        $this->migrateWidgets();
29 14
    }
30
31
    /**
32
     * @return array
33
     */
34
    protected function mapWidgetData(array $data)
35
    {
36
        $mappedKeys = [
37
            'assign_to' => 'assigned_posts',
38
            'assigned_to' => 'assigned_posts',
39
            'category' => 'assigned_terms',
40
            'per_page' => 'display',
41
            'user' => 'assigned_users',
42
        ];
43
        foreach ($mappedKeys as $oldKey => $newKey) {
44
            if (array_key_exists($oldKey, $data)) {
45
                $data[$newKey] = $data[$oldKey];
46
                unset($data[$oldKey]);
47
            }
48
        }
49
        return $data;
50
    }
51
52
    /**
53
     * @return void
54
     */
55 14
    protected function migrateSidebarWidgets()
56
    {
57 14
        $sidebars = Arr::consolidate(get_option('sidebars_widgets'));
58 14
        if ($this->widgetsExist($sidebars)) {
59
            $sidebars = $this->updateWidgetNames($sidebars);
60
            update_option('sidebars_widgets', $sidebars);
61
        }
62 14
    }
63
64
    /**
65
     * @return void
66
     */
67 14
    protected function migrateThemeModWidgets()
68
    {
69 14
        $themes = $this->queryThemeMods();
70 14
        foreach ($themes as $theme) {
71
            $themeMod = get_option($theme);
72
            $sidebars = Arr::consolidate(Arr::get($themeMod, 'sidebars_widgets.data'));
73
            if ($this->widgetsExist($sidebars)) {
74
                $themeMod['sidebars_widgets']['data'] = $this->updateWidgetNames($sidebars);
75
                update_option($theme, $themeMod);
76
            }
77
        }
78 14
    }
79
80
    /**
81
     * @return void
82
     */
83 14
    protected function migrateUserMeta()
84
    {
85 14
        $postType = glsr()->post_type;
86 14
        $metaKey = 'meta-box-order_'.$postType;
87
        $metaOrder = [
88
            'side' => [
89 14
                'submitdiv',
90 14
                $postType.'-categorydiv',
91 14
                $postType.'-postsdiv',
92 14
                $postType.'-usersdiv',
93 14
                $postType.'-authordiv',
94
            ],
95
            'normal' => [
96 14
                $postType.'-responsediv',
97 14
                $postType.'-detailsdiv',
98
            ],
99
            'advanced' => [],
100
        ];
101
        array_walk($metaOrder, function (&$order) {
102 14
            $order = implode(',', $order);
103 14
        });
104 14
        $userIds = get_users([
105 14
            'fields' => 'ID',
106 14
            'meta_compare' => 'EXISTS',
107 14
            'meta_key' => $metaKey,
108
        ]);
109 14
        foreach ($userIds as $userId) {
110
            update_user_meta($userId, $metaKey, $metaOrder);
111
        }
112 14
    }
113
114
    /**
115
     * @param mixed $option
116
     * @return string|array
117
     */
118
    protected function migrateWidgetData($option)
119
    {
120
        if (!is_array($option)) {
121
            return $option;
122
        }
123
        foreach ($option as $index => $values) {
124
            if (is_array($values)) {
125
                $option[$index] = $this->mapWidgetData($values);
126
            }
127
        }
128
        return $option;
129
    }
130
131
    /**
132
     * @return void
133
     */
134 14
    protected function migrateWidgets()
135
    {
136
        $widgets = [
137 14
            'site-reviews',
138
            'site-reviews-form',
139
            'site-reviews-summary',
140
        ];
141 14
        foreach ($widgets as $widget) {
142 14
            $oldWidget = 'widget_'.glsr()->id.'_'.$widget;
143 14
            $newWidget = 'widget_'.glsr()->prefix.$widget;
144 14
            if ($option = get_option($oldWidget)) {
145
                update_option($newWidget, $this->migrateWidgetData($option));
146
                delete_option($oldWidget);
147
            }
148
        }
149 14
    }
150
151
    /**
152
     * @return array
153
     */
154 14
    protected function queryThemeMods()
155
    {
156 14
        return $this->db->get_col("
157
            SELECT option_name 
158 14
            FROM {$this->db->options} 
159
            WHERE option_name LIKE '%theme_mods_%'
160
        ");
161
    }
162
163
    /**
164
     * @param array $sidebars
165
     * @return array
166
     */
167
    protected function updateWidgetNames(array $sidebars)
168
    {
169
        array_walk($sidebars, function (&$widgets) {
170
            array_walk($widgets, function (&$widget) {
171
                if (Str::startsWith(glsr()->id.'_', $widget)) {
172
                    $widget = Str::replaceFirst(glsr()->id.'_', glsr()->prefix, $widget);
173
                }
174
            });
175
        });
176
        return $sidebars;
177
    }
178
179
    /**
180
     * @return bool
181
     */
182 14
    protected function widgetsExist(array $sidebars)
183
    {
184 14
        $sidebars = array_filter($sidebars, 'is_array');
185 14
        $sidebars = array_values($sidebars);
186 14
        $widgets = call_user_func_array('array_merge', $sidebars);
187 14
        $widgets = Arr::consolidate($widgets); // ensure this is an array in case call_user_func_array() errors
188 14
        foreach ($widgets as $widget) {
189 14
            if (Str::startsWith(glsr()->id.'_', $widget)) {
190
                return true;
191
            }
192
        }
193 14
        return false;
194
    }
195
}
196