Menu::registerPage()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 2
eloc 9
nc 2
nop 6
1
<?php
2
/**
3
 * Menu
4
 *
5
 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
6
 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
7
 *
8
 * Permission is hereby granted to use or copy this program
9
 * for any purpose, provided the above notices are retained on all copies.
10
 * Permission to modify the code and to distribute modified code is granted,
11
 * provided the above notices are retained, and a notice that the code was
12
 * modified is included with the above copyright notice.
13
 *
14
 * @category  Wp
15
 * @package   Punction
16
 * @author    Andrzej Marcinkowski <[email protected]>
17
 * @copyright 2014 Wojewódzki Szpital Zespolony, Kalisz
18
 * @license   MIT http://opensource.org/licenses/MIT
19
 * @version   1.0 $Format:%H$
20
 * @link      http://
21
 * @since     File available since Release 1.0.0
22
 * PHP Version 5
23
 */
24
namespace Hospitalplugin\WP;
25
26
/**
27
 * Menu
28
 *
29
 * @category  Wp
30
 * @package   Punction
31
 * @author    Andrzej Marcinkowski <[email protected]>
32
 * @copyright 2014 Wojewódzki Szpital Zespolony, Kalisz
33
 * @license   MIT http://opensource.org/licenses/MIT
34
 * @version   1.0  $Format:%H$
35
 * @link      http://
36
 * @since     File available since Release 1.0.0
37
 *
38
 */
39
class Menu
40
{
41
42
    private $menus;
43
44
    private $url;
45
46
    private $unregister;
47
48
    /**
49
     * init
50
     */
51
    public function init($menus, $url, $unregister = null)
52
    {
53
        $this->menus = $menus;
54
        $this->url = $url;
55
        $this->unregister = $unregister;
56
        add_action('admin_menu', array(
57
            $this,
58
            'registerPages'
59
        ));
60
    }
61
62
    /**
63
     * register menu pages
64
     */
65
    public function registerPages()
66
    {
67
        foreach ($this->menus as $menu) {
68
            $this->registerPage($menu['title'], $menu['cap'], $menu['link'], $menu['class'], $menu['callback'], $menu['type']);
69
        }
70
        $this->hideMenu();
71
    }
72
73
    /**
74
     * hide unused
75
     */
76
    public function hideMenu()
77
    {
78
        foreach ($this->unregister as $menu) {
79
            remove_submenu_page($menu['url'], $menu['page']);
80
        }
81
    }
82
83
    /**
84
     * register using add_submenu_page
85
     */
86
    private function registerPage($title, $capabilities, $url_param, $class, $callback, $type)
87
    {
88
        if ($type == 'submenu') {
89
            add_submenu_page($this->url, $title, $title, $capabilities, $url_param, array(
90
                $class,
91
                $callback
92
            ));
93
        } else {
94
            add_menu_page($title, $title, $capabilities, $url_param, array(
95
                $class,
96
                $callback
97
            ));
98
        }
99
    }
100
}
101