Passed
Push — master ( 2da942...d633d7 )
by Paul
15:03 queued 05:40
created

MigrateSidebars   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Test Coverage

Coverage 63.1%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 25
eloc 82
dl 0
loc 184
ccs 53
cts 84
cp 0.631
rs 10
c 2
b 1
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 6 1
A migrateSidebarWidgets() 0 6 2
A widgetsExist() 0 10 3
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
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 7
    public function __construct()
14
    {
15 7
        global $wpdb;
16 7
        $this->db = $wpdb;
17 7
        $this->limit = 250;
18 7
    }
19
20
    /**
21
     * @return void
22
     */
23 7
    public function run()
24
    {
25 7
        $this->migrateSidebarWidgets();
26 7
        $this->migrateThemeModWidgets();
27 7
        $this->migrateUserMeta();
28 7
        $this->migrateWidgets();
29 7
    }
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 7
    protected function migrateSidebarWidgets()
56
    {
57 7
        $sidebars = Arr::consolidate(get_option('sidebars_widgets'));
58 7
        if ($this->widgetsExist($sidebars)) {
59
            $sidebars = $this->updateWidgetNames($sidebars);
60
            update_option('sidebars_widgets', $sidebars);
61
        }
62 7
    }
63
64
    /**
65
     * @return void
66
     */
67 7
    protected function migrateThemeModWidgets()
68
    {
69 7
        $themes = $this->queryThemeMods();
70 7
        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 7
    }
79
80
    /**
81
     * @return void
82
     */
83 7
    protected function migrateUserMeta()
84
    {
85 7
        $postType = glsr()->post_type;
86 7
        $metaKey = 'meta-box-order_'.$postType;
87
        $metaOrder = [
88
            'side' => [
89 7
                'submitdiv',
90 7
                $postType.'-categorydiv',
91 7
                $postType.'-postsdiv',
92 7
                $postType.'-usersdiv',
93 7
                $postType.'-authordiv',
94
            ],
95
            'normal' => [
96 7
                $postType.'-responsediv',
97 7
                $postType.'-detailsdiv',
98
            ],
99
            'advanced' => [],
100
        ];
101
        array_walk($metaOrder, function (&$order) {
102 7
            $order = implode(',', $order);
103 7
        });
104 7
        $userIds = get_users([
105 7
            'fields' => 'ID',
106 7
            'meta_compare' => 'EXISTS',
107 7
            'meta_key' => $metaKey,
108
        ]);
109 7
        foreach ($userIds as $userId) {
110
            update_user_meta($userId, $metaKey, $metaOrder);
111
        }
112 7
    }
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 7
    protected function migrateWidgets()
135
    {
136
        $widgets = [
137 7
            'site-reviews',
138
            'site-reviews-form',
139
            'site-reviews-summary',
140
        ];
141 7
        foreach ($widgets as $widget) {
142 7
            $oldWidget = 'widget_'.glsr()->id.'_'.$widget;
143 7
            $newWidget = 'widget_'.glsr()->prefix.$widget;
144 7
            if ($option = get_option($oldWidget)) {
145
                update_option($newWidget, $this->migrateWidgetData($option));
146
                delete_option($oldWidget);
147
            }
148
        }
149 7
    }
150
151
    /**
152
     * @return array
153
     */
154 7
    protected function queryThemeMods()
155
    {
156 7
        return $this->db->get_col("
157
            SELECT option_name 
158 7
            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 7
    protected function widgetsExist(array $sidebars)
183
    {
184 7
        $sidebars = array_filter($sidebars, 'is_array');
185 7
        $widgets = call_user_func_array('array_merge', array_values($sidebars));
186 7
        foreach ($widgets as $widget) {
187 7
            if (Str::startsWith(glsr()->id.'_', $widget)) {
188
                return true;
189
            }
190
        }
191 7
        return false;
192
    }
193
}
194