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