PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.1
GiveWP – Donation Plugin and Fundraising Platform v4.16.1
4.17.0 4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 All 256 releases
give / src / DonorDashboards / Tabs / TabsRegister.php

TabsRegister.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.1, at src/DonorDashboards/Tabs/TabsRegister.php

130 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\DonorDashboards\Tabs;
4
5 use Give\DonorDashboards\Exceptions\DuplicateTabException;
6 use Give\DonorDashboards\Exceptions\MissingTabException;
7 use Give\DonorDashboards\Tabs\Contracts\Tab;
8
9 /**
10 * @since 2.10.0
11 */
12 class TabsRegister
13 {
14 /**
15 * FQCN of Tab classes
16 *
17 * @since 2.10.0
18 *
19 * @var string[]
20 */
21 private $tabs = [];
22
23 /**
24 * Returns all of the registered tabs
25 *
26 * @since 2.10.0
27 *
28 * @return string[]
29 */
30 public function getTabs()
31 {
32 return $this->tabs;
33 }
34
35 /**
36 * Checks to see if a tab is registered with the given ID
37 *
38 * @since 2.10.0
39 *
40 * @param string $id
41 *
42 * @return bool
43 */
44 public function hasTab($id)
45 {
46 return isset($this->tabs[$id]);
47 }
48
49 /**
50 * Returns a tab with the given ID
51 *
52 * @since 2.10.0
53 *
54 * @param string $id
55 *
56 * @return string
57 */
58 public function getTab($id)
59 {
60 if ( ! $this->hasTab($id)) {
61 throw new MissingTabException($id);
62 }
63
64 return $this->tabs[$id];
65 }
66
67 /**
68 * Returns all of the registered tab ids
69 *
70 * @since 2.10.0
71 *
72 * @return string[]
73 */
74 public function getRegisteredIds()
75 {
76 return array_keys($this->tabs);
77 }
78
79 /**
80 * Add a tab to the list of tabs
81 *
82 * @since 2.10.0
83 *
84 * @param string $tabClass FQCN of the Tab Class
85 */
86 public function addTab($tabClass)
87 {
88 if ( ! is_subclass_of($tabClass, Tab::class)) {
89 throw new \InvalidArgumentException('Class must extend the ' . Tab::class . ' class');
90 }
91
92 $tabId = $tabClass::id();
93
94 if ($this->hasTab($tabId)) {
95 throw new DuplicateTabException();
96 }
97
98 $this->tabs[$tabId] = $tabClass;
99 }
100
101 /**
102 * Helper for adding a bunch of tabs at once
103 *
104 * @since 2.10.0
105 *
106 * @param string[] $tabClasses
107 */
108 public function addTabs(array $tabClasses)
109 {
110 foreach ($tabClasses as $tabClass) {
111 $this->addTab($tabClass);
112 }
113 }
114
115 public function registerTabRoutes()
116 {
117 foreach (give()->donorDashboardTabs->tabs as $tabClass) {
118 $tab = new $tabClass;
119 $tab->registerRoutes();
120 }
121 }
122
123 public function enqueueTabAssets()
124 {
125 foreach (give()->donorDashboardTabs->tabs as $tabClass) {
126 (new $tabClass)->enqueueAssets();
127 }
128 }
129 }
130