Passed
Push — master ( ccb079...7906b4 )
by Paul
04:39
created

Migrate_4_6_0::migrateSidebarWidgets()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.2559

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 6
ccs 3
cts 5
cp 0.6
crap 2.2559
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Migrations;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Helpers\Arr;
7
use GeminiLabs\SiteReviews\Helpers\Str;
8
9
class Migrate_4_6_0
10
{
11
    /**
12
     * @return void
13
     */
14 1
    public function migrateSidebarWidgets()
15
    {
16 1
        $sidebars = get_option('sidebars_widgets');
17 1
        if ($this->widgetsExist($sidebars)) {
0 ignored issues
show
Bug introduced by
It seems like $sidebars can also be of type false; however, parameter $sidebars of GeminiLabs\SiteReviews\M...e_4_6_0::widgetsExist() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

17
        if ($this->widgetsExist(/** @scrutinizer ignore-type */ $sidebars)) {
Loading history...
18
            $sidebars = $this->updateWidgetNames($sidebars);
1 ignored issue
show
Bug introduced by
It seems like $sidebars can also be of type false; however, parameter $sidebars of GeminiLabs\SiteReviews\M..._0::updateWidgetNames() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

18
            $sidebars = $this->updateWidgetNames(/** @scrutinizer ignore-type */ $sidebars);
Loading history...
19
            update_option('sidebars_widgets', $sidebars);
20
        }
21 1
    }
22
23
    /**
24
     * @return void
25
     */
26 1
    public function migrateThemeModWidgets()
27
    {
28 1
        $themes = $this->queryThemeMods();
29 1
        foreach ($themes as $theme) {
30
            $themeMod = get_option($theme);
31
            $sidebars = Arr::get($themeMod, 'sidebars_widgets.data');
32
            if ($this->widgetsExist($sidebars)) {
0 ignored issues
show
Bug introduced by
It seems like $sidebars can also be of type string; however, parameter $sidebars of GeminiLabs\SiteReviews\M...e_4_6_0::widgetsExist() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

32
            if ($this->widgetsExist(/** @scrutinizer ignore-type */ $sidebars)) {
Loading history...
33
                $themeMod['sidebars_widgets']['data'] = $this->updateWidgetNames($sidebars);
1 ignored issue
show
Bug introduced by
It seems like $sidebars can also be of type string; however, parameter $sidebars of GeminiLabs\SiteReviews\M..._0::updateWidgetNames() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

33
                $themeMod['sidebars_widgets']['data'] = $this->updateWidgetNames(/** @scrutinizer ignore-type */ $sidebars);
Loading history...
34
                update_option($theme, $themeMod);
35
            }
36
        }
37 1
    }
38
39
    /**
40
     * @return void
41
     */
42 1
    public function run()
43
    {
44 1
        $this->migrateSidebarWidgets();
45 1
        $this->migrateThemeModWidgets();
46 1
    }
47
48
    /**
49
     * @return array
50
     */
51 1
    protected function queryThemeMods()
52
    {
53 1
        global $wpdb;
54 1
        return $wpdb->get_col("
55
            SELECT option_name 
56 1
            FROM {$wpdb->options} 
57
            WHERE option_name LIKE '%theme_mods_%'
58
        ");
59
    }
60
61
    /**
62
     * @param array $sidebars
63
     * @return array
64
     */
65
    protected function updateWidgetNames(array $sidebars)
66
    {
67
        array_walk($sidebars, function(&$widgets) {
68
            array_walk($widgets, function(&$widget) {
69
                if (Str::startsWith(Application::ID.'_', $widget)) {
70
                    $widget = Str::replaceFirst(Application::ID.'_', Application::PREFIX, $widget);
71
                }
72
            });
73
        });
74
        return $sidebars;
75
    }
76
77
    /**
78
     * @param array $sidebars
79
     * @return bool
80
     */
81 1
    protected function widgetsExist($sidebars)
82
    {
83 1
        $widgets = call_user_func_array('array_merge', array_filter(Arr::consolidate($sidebars), 'is_array'));
84 1
        foreach ($widgets as $widget) {
85 1
            if (Str::startsWith(Application::ID.'_', $widget)) {
86 1
                return true;
87
            }
88
        }
89 1
        return false;
90
    }
91
}
92