| 1 |
<?php |
| 2 |
|
| 3 |
namespace forge12\contactform7\CF7DoubleOptIn { |
| 4 |
if (!defined('ABSPATH')) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
/** |
| 8 |
* dependencies |
| 9 |
*/ |
| 10 |
require_once('UIMenu.class.php'); |
| 11 |
|
| 12 |
/** |
| 13 |
* Class UI |
| 14 |
* |
| 15 |
* @package forge12\ui |
| 16 |
*/ |
| 17 |
class UI |
| 18 |
{ |
| 19 |
/** |
| 20 |
* @var UI |
| 21 |
*/ |
| 22 |
private static $instance = null; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
private $slug = ''; |
| 28 |
/** |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
private $title = ''; |
| 32 |
/** |
| 33 |
* @var string |
| 34 |
*/ |
| 35 |
private $capabilities; |
| 36 |
/** |
| 37 |
* Store all Pages |
| 38 |
* |
| 39 |
* @var array<UIPage> |
| 40 |
*/ |
| 41 |
private $pages; |
| 42 |
/** |
| 43 |
* Components |
| 44 |
*/ |
| 45 |
private $components = array(); |
| 46 |
|
| 47 |
/** |
| 48 |
* @param $slug |
| 49 |
* @param string $title |
| 50 |
* @param string $capabilities |
| 51 |
* |
| 52 |
* @return UI |
| 53 |
* @deprecated |
| 54 |
* @deprecated |
| 55 |
*/ |
| 56 |
public static function getInstance($slug, $title = 'Dashboard', $capabilities = 'manage_options') |
| 57 |
{ |
| 58 |
if (null == self::$instance) { |
| 59 |
self::$instance = new UI($slug, $title, $capabilities); |
| 60 |
} |
| 61 |
return self::$instance; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Add a Page to the UI |
| 66 |
* |
| 67 |
* @param $UIPage |
| 68 |
* |
| 69 |
* @return void |
| 70 |
*/ |
| 71 |
public function addPage($UIPage) |
| 72 |
{ |
| 73 |
$this->pages[] = $UIPage; |
| 74 |
|
| 75 |
add_action('forge12-plugin-content-' . $this->slug, array($UIPage, 'renderContent'), 10, 2); |
| 76 |
add_action('forge12-plugin-sidebar-' . $this->slug, array($UIPage, 'renderSidebar'), 10, 2); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* @return array<UIPage> |
| 81 |
*/ |
| 82 |
private function getPages() |
| 83 |
{ |
| 84 |
return $this->pages; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* @param $slug |
| 89 |
* |
| 90 |
* @return UIPage|null |
| 91 |
*/ |
| 92 |
private function get($slug) |
| 93 |
{ |
| 94 |
foreach ($this->pages as $UIPage) { |
| 95 |
if ($UIPage->getSlug() == $slug) { |
| 96 |
return $UIPage; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
return null; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* UI constructor. |
| 105 |
* |
| 106 |
* @param $slug |
| 107 |
*/ |
| 108 |
public function __construct($slug, $title, $capabilities) |
| 109 |
{ |
| 110 |
$this->slug = $slug; |
| 111 |
$this->title = $title; |
| 112 |
$this->capabilities = $capabilities; |
| 113 |
|
| 114 |
$this->loadComponents(); |
| 115 |
|
| 116 |
// Add Assets |
| 117 |
add_action('admin_enqueue_scripts', array($this, 'addAssets')); |
| 118 |
|
| 119 |
// Load Components after the Pages have been initialized |
| 120 |
add_action('f12_cf7_doubleoptin_ui_after_load_pages', array($this, 'registerComponents'), 999999990, 2); |
| 121 |
|
| 122 |
// Sort Pages after intialize |
| 123 |
add_action('f12_cf7_doubleoptin_ui_after_load_pages', array($this, 'sortComponents'), 999999999, 2); |
| 124 |
|
| 125 |
// Called after all Pages have been initialized |
| 126 |
do_action('f12_cf7_doubleoptin_ui_after_load_pages', $this, $this->slug); |
| 127 |
|
| 128 |
// Create the Submenus |
| 129 |
add_action('admin_menu', array($this, 'addSubmenuPagesToWordPress')); |
| 130 |
|
| 131 |
// Hotfix to hide the submenus that should be hidden but still callable in wordpress backend |
| 132 |
add_action('admin_head', array($this, 'hideSubmenuPagesToWordpress')); |
| 133 |
} |
| 134 |
|
| 135 |
public function addAssets() |
| 136 |
{ |
| 137 |
/** |
| 138 |
* Stylesheets for the admin panel |
| 139 |
*/ |
| 140 |
wp_enqueue_style('f12-cf7-doubleoptin-admin', plugins_url('assets/admin-style.css', __FILE__), array(), '1.3'); |
| 141 |
|
| 142 |
/** |
| 143 |
* Toggle Button |
| 144 |
*/ |
| 145 |
wp_enqueue_script('f12-cf7-doubleoptin-toggle', plugins_url('assets/toggle.js', __FILE__), array('jquery'), '1.0'); |
| 146 |
|
| 147 |
/** |
| 148 |
* Copy to clipboard |
| 149 |
*/ |
| 150 |
wp_enqueue_script('f12-cf7-doubleoptin-copy', plugins_url('assets/copy-to-clipboard.js', __FILE__), array('jquery'), '1.0'); |
| 151 |
|
| 152 |
/** |
| 153 |
* External vendor scripts |
| 154 |
*/ |
| 155 |
wp_enqueue_script('vendor-highlight', plugins_url('assets/vendor/highlight/highlight.min.js', __FILE__), array('jquery'), '1.0'); |
| 156 |
wp_enqueue_style('vendor-highlight', plugins_url('assets/vendor/highlight/default.min.css', __FILE__), array(), '1.0'); |
| 157 |
} |
| 158 |
|
| 159 |
public function sortComponents($UI, $domain) |
| 160 |
{ |
| 161 |
if (!empty($this->pages)) { |
| 162 |
usort($this->pages, function ($a, $b) { |
| 163 |
if ($a->getPosition() < $b->getPosition()) { |
| 164 |
return -1; |
| 165 |
} else if ($a->getPosition() > $b->getPosition()) { |
| 166 |
return 1; |
| 167 |
} else { |
| 168 |
return 0; |
| 169 |
} |
| 170 |
}); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* @param $UI |
| 176 |
* @param $domain |
| 177 |
* |
| 178 |
* @return void |
| 179 |
*/ |
| 180 |
public function registerComponents($UI, $domain) |
| 181 |
{ |
| 182 |
foreach ($this->components as $component) { |
| 183 |
if (isset($component['path']) && isset($component['name'])) { |
| 184 |
require_once($component['path']); |
| 185 |
$UIPage = new $component['name']($domain); |
| 186 |
$UI->addPage($UIPage); |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
private function addComponent($name, $path) |
| 192 |
{ |
| 193 |
$this->components[] = array( |
| 194 |
'name' => $name, |
| 195 |
'path' => $path |
| 196 |
); |
| 197 |
} |
| 198 |
|
| 199 |
private function loadComponents() |
| 200 |
{ |
| 201 |
$directory = dirname(dirname(__FILE__)) . '/ui'; |
| 202 |
|
| 203 |
if (is_dir($directory)) { |
| 204 |
$handle = opendir($directory); |
| 205 |
|
| 206 |
if (!$handle) { |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
while (false !== ($entry = readdir($handle))) { |
| 211 |
if ($entry != '.' && $entry != '..') { |
| 212 |
if (preg_match('!UI([a-zA-Z_0-9]+)\.class\.php!', $entry, $matches)) { |
| 213 |
if (isset($matches[1])) { |
| 214 |
$this->addComponent('\\' . __NAMESPACE__ . '\UI' . $matches[1], $directory . '/' . $entry); |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Add the WordPress Page for the Settings to the WordPress CMS |
| 224 |
* |
| 225 |
* @private WordPress Hook |
| 226 |
*/ |
| 227 |
public function addSubmenuPagesToWordPress() |
| 228 |
{ |
| 229 |
add_menu_page($this->title, $this->title, $this->capabilities, $this->slug, '', 'dashicons-email'); |
| 230 |
|
| 231 |
foreach ($this->getPages() as /** @var UIPage $Page */ $Page) { |
| 232 |
if ($Page->isDashboard()) { |
| 233 |
$slug = $this->slug; |
| 234 |
} else { |
| 235 |
$slug = $this->slug . '_' . $Page->getSlug(); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
add_submenu_page($this->slug, $Page->getTitle(), $Page->getTitle(), $this->capabilities, $slug, function () { |
| 240 |
$this->render(); |
| 241 |
}, $Page->getPosition()); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* This will ensure that the menu page will be removed before the rendering. |
| 247 |
* This needs to be called from add_action('admin_head', ''); to be working. |
| 248 |
* |
| 249 |
* @return void |
| 250 |
*/ |
| 251 |
public function hideSubmenuPagesToWordpress() |
| 252 |
{ |
| 253 |
foreach ($this->getPages() as /** @var UIPage $Page */ $Page) { |
| 254 |
if (!$Page->hideInMenu()) { |
| 255 |
continue; |
| 256 |
} |
| 257 |
|
| 258 |
if ($Page->isDashboard()) { |
| 259 |
$slug = $this->slug; |
| 260 |
} else { |
| 261 |
$slug = $this->slug . '_' . $Page->getSlug(); |
| 262 |
} |
| 263 |
|
| 264 |
remove_submenu_page($this->slug, $slug); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
public function render() |
| 269 |
{ |
| 270 |
$page = sanitize_text_field($_GET['page']); |
| 271 |
$page = substr(explode($this->slug, $page)[1], 1); |
| 272 |
|
| 273 |
if (empty($page)) { |
| 274 |
$page = $this->slug; |
| 275 |
} |
| 276 |
|
| 277 |
$pages = $this->getPages(); |
| 278 |
$menuPages = array(); |
| 279 |
|
| 280 |
foreach ($pages as $UIPage) { |
| 281 |
if (!$UIPage->hideInMenu()) { |
| 282 |
$menuPages[] = $UIPage; |
| 283 |
} |
| 284 |
} |
| 285 |
?> |
| 286 |
<div class="forge12-plugin <?php esc_attr_e($this->slug);?>"> |
| 287 |
<div class="forge12-plugin-header"> |
| 288 |
<div class="forge12-plugin-header-inner"> |
| 289 |
<img src="<?php echo esc_url(plugin_dir_url(__FILE__)); ?>/assets/logo-forge12.png" |
| 290 |
alt="Forge12 Interactvie GmbH" title="Forge12 Interactive GmbH"/> |
| 291 |
</div> |
| 292 |
</div> |
| 293 |
<div class="forge12-plugin-menu"> |
| 294 |
<?php do_action('f12_cf7_doubleoptin_admin_menu', $menuPages, $page, $this->slug); ?> |
| 295 |
</div> |
| 296 |
<div class="forge12-plugin-content"> |
| 297 |
<div class="forge12-plugin-content-main"> |
| 298 |
<?php do_action('forge12-plugin-content-' . $this->slug, $this->slug, $page) ?> |
| 299 |
</div> |
| 300 |
<div class="forge12-plugin-content-sidebar"> |
| 301 |
<?php do_action('forge12-plugin-sidebar-' . $this->slug, $this->slug, $page) ?> |
| 302 |
</div> |
| 303 |
</div> |
| 304 |
<div class="forge12-plugin-footer"> |
| 305 |
<div class="forge12-plugin-footer-inner"> |
| 306 |
<img src="<?php echo esc_url(plugin_dir_url(__FILE__)); ?>/assets/logo-forge12-dark.png" |
| 307 |
alt="Forge12 Interactvie GmbH" title="Forge12 Interactive GmbH"/> |
| 308 |
</div> |
| 309 |
</div> |
| 310 |
</div> |
| 311 |
<?php |
| 312 |
} |
| 313 |
} |
| 314 |
} |