PluginProbe
The GDPR Framework By Data443 / 2.1.0
The GDPR Framework By Data443 v2.1.0
2.5.0 2.4.0 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.3 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.39 1.0.4 1.0.40 1.0.41 1.0.42 1.0.43 1.0.44 1.0.45 1.0.46 All 41 releases
gdpr-framework / src / Admin / WordpressAdminPage.php

WordpressAdminPage.php in The GDPR Framework By Data443 2.1.0, at src/Admin/WordpressAdminPage.php

139 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Codelight\GDPR\Admin;
4
5 /**
6 * Handle registering and rendering the GDPR admin page contents
7 *
8 * Class WordpressAdminPage
9 *
10 * @package Codelight\GDPR\Admin
11 */
12 class WordpressAdminPage
13 {
14 protected $slug = 'gdpr';
15
16 protected $tabs = [];
17
18 public function __construct()
19 {
20 $this->setup();
21 }
22
23 protected function setup()
24 {
25 // Register the tabs
26 add_action('admin_init', [$this, 'registerTabs']);
27
28 // Initialize the active tab
29 add_action('admin_init', [$this, 'initActiveTab']);
30 }
31
32 /**
33 * Render the main GDPR options page
34 */
35 public function renderPage()
36 {
37 $tabs = $this->getNavigationData();
38 $currentTabContents = $this->getActiveTab()->renderContents();
39 $signature = apply_filters('gdpr/admin/show_signature', true);
40 echo gdpr('view')->render('admin/settings-page', compact('tabs', 'currentTabContents', 'signature'));
41 }
42
43 /**
44 * Allow modules to add tabs
45 */
46 public function registerTabs()
47 {
48 $this->tabs = apply_filters('gdpr/admin/tabs', []);
49 }
50
51 /**
52 * Get the active tab or the first tab if none are active
53 *
54 * @return AdminTabInterface
55 */
56 public function getActiveTab()
57 {
58 foreach ($this->tabs as $tab) {
59 if (isset($_GET['gdpr-tab']) && $_GET['gdpr-tab'] === $tab->getSlug()) {
60 return $tab;
61 }
62 }
63
64 return reset($this->tabs);
65 }
66
67 /**
68 * Check if the given tab is active
69 *
70 * @param $slug
71 * @return bool
72 */
73 public function isTabActive($slug)
74 {
75 $activeTab = $this->getActiveTab();
76 if ($activeTab->getSlug() === $slug) {
77 return true;
78 }
79
80 // Hacky: if no tab set, the first tab is active
81 if (!isset($_GET['gdpr-tab'])) {
82 $firstTab = reset($this->tabs);
83 if ($firstTab->getSlug() === $slug) {
84 return true;
85 }
86 }
87
88 return false;
89 }
90
91 /**
92 * Initialize the active tab
93 */
94 public function initActiveTab()
95 {
96 $activeTab = $this->getActiveTab();
97 $activeTab->setup();
98 }
99
100 /**
101 * Get the tabbed navigation for GDPR options page
102 *
103 * @return array
104 */
105 public function getNavigationData()
106 {
107 if (!count($this->tabs)) {
108 return [];
109 }
110
111 $navigation = [];
112
113 foreach ($this->tabs as $tab) {
114 /* @var $tab AdminTabInterface */
115 $navigation[$tab->getSlug()] = [
116 'slug' => $tab->getSlug(),
117 'url' => $this->getTabUrl($tab->getSlug()),
118 'title' => $tab->getTitle(),
119 'active' => false,
120 ];
121
122 if ($this->isTabActive($tab->getSlug())) {
123 $navigation[$tab->getSlug()]['active'] = true;
124 }
125 }
126
127 return $navigation;
128 }
129
130 /**
131 * @param $slug
132 * @return string
133 */
134 public function getTabUrl($slug)
135 {
136 return admin_url('tools.php?page=privacy&gdpr-tab=' . $slug);
137 }
138 }
139