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