Passed
Push — master ( a5e8db...945884 )
by Danny
02:33
created

Menu::menuLocationAssign()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace PressCLI\WPCLI;
3
4
use PressCLI\WPCLI\CLI;
5
use PressCLI\Option\Configuration;
6
7
trait Menu
8
{
9
    /**
10
     * Gets the menu id from the menu name.
11
     *
12
     * @param  string $name The menu name.
13
     *
14
     * @return string       The menu id.
15
     */
16
    protected static function getMenuIdFromName($name)
17
    {
18
        $id = str_replace(' ', '-', $name);
19
        $id = preg_replace('/[^A-Za-z0-9-_]/um', '', $id);
20
        $id = strtolower($id);
21
22
        return $id;
23
    }
24
25
    /**
26
     * Creates all of the configuration menus.
27
     */
28
    public static function menuCreateAll()
29
    {
30
        $config = Configuration::get();
31
        $menus = isset($config['menus']) ? $config['menus'] : [];
32
        foreach ($menus as $menu) {
33
            $name = $menu['name'];
34
            $location = $menu['location'];
35
            $menu_id = self::getMenuIdFromName($name);
36
37
            // Create the menu.
38
            self::menuCreate($name);
39
40
            // Assign the menu to its location.
41
            self::menuLocationAssign($menu_id, $location);
42
        }
43
    }
44
45
    /**
46
     * Creates a menu.
47
     *
48
     * @param  string $name The menu name.
49
     */
50
    public static function menuCreate($name)
51
    {
52
        CLI::execCommand('menu', ['create', "'{$name}'"]);
53
    }
54
55
    /**
56
     * Assigns a menu to a location by menu id.
57
     *
58
     * @param  string $menu_id  The menu id to assign.
59
     * @param  string $location The menu location to assign.
60
     */
61
    public static function menuLocationAssign($menu_id, $location)
62
    {
63
        CLI::execCommand('menu', ['location', 'assign', $menu_id, $location]);
64
    }
65
}
66