ControllerTabNavigationTrait.php
3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/**
* ControllerTabNavigationTrait.php
*
* The ControllerTabNavigationTrait class file.
*
* PHP versions 5
*
* @author Alexander Schneider <alexanderschneider85@gmail.com>
* @copyright 2008-2017 Alexander Schneider
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
* @version SVN: $id$
* @link http://wordpress.org/extend/plugins/user-access-manager/
*/
declare(strict_types=1);
namespace UserAccessManager\Controller\Backend;
/**
* Trait ControllerTabNavigationTrait
*
* @package UserAccessManager\Controller
*/
trait ControllerTabNavigationTrait
{
/**
* Returns the current request url.
* @return string
*/
abstract public function getRequestUrl(): string;
/**
* Returns the request parameter.
* @param string $name
* @param mixed $default
* @return mixed
*/
abstract public function getRequestParameter(string $name, $default = null);
/**
* Translates the given group by the group key.
* @param string $key
* @return string
*/
abstract public function getGroupText(string $key): string;
/**
* Translates the given group section by the group key.
* @param string $key
* @return string
*/
abstract public function getGroupSectionText(string $key): string;
/**
* Returns the tab groups.
* @return array
*/
abstract public function getTabGroups(): array;
/**
* Returns the current tab group.
* @return string
*/
public function getCurrentTabGroup(): string
{
$groups = $this->getTabGroups();
$keys = array_keys($groups);
return (string) $this->getRequestParameter('tab_group', reset($keys));
}
/**
* Returns the tab group sections.
* @return array
*/
public function getSections(): array
{
$groups = $this->getTabGroups();
$group = $this->getCurrentTabGroup();
return isset($groups[$group]) === true ? $groups[$group] : [];
}
/**
* Returns the current tab group section.
* @return string
*/
public function getCurrentTabGroupSection(): string
{
$groups = $this->getTabGroups();
$group = $this->getCurrentTabGroup();
if (isset($groups[$group]) === true) {
$default = reset($groups[$group]);
} else {
$firstGroup = reset($groups);
$default = reset($firstGroup);
}
return (string) $this->getRequestParameter('tab_group_section', $default);
}
/**
* Returns the settings group link by the given group key.
* @param string $groupKey
* @return string
*/
public function getTabGroupLink(string $groupKey): string
{
$rawUrl = $this->getRequestUrl();
$url = preg_replace('/&tab_group[^&]*/i', '', $rawUrl);
return $url . '&tab_group=' . $groupKey;
}
/**
* Returns the settings section link by the given group and section key.
* @param string $groupKey
* @param string $sectionKey
* @return string
*/
public function getTabGroupSectionLink(string $groupKey, string $sectionKey): string
{
$rawUrl = $this->getTabGroupLink($groupKey);
$url = preg_replace('/&tab_group_section[^&]*/i', '', $rawUrl);
return $url . '&tab_group_section=' . $sectionKey;
}
}