| 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 |
* @var UI |
| 20 |
*/ |
| 21 |
private static $instance = null; |
| 22 |
|
| 23 |
/** |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
private $slug = ''; |
| 27 |
/** |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
private $title = ''; |
| 31 |
/** |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
private $capabilities; |
| 35 |
/** |
| 36 |
* Store all Pages |
| 37 |
* |
| 38 |
* @var array<UIPage> |
| 39 |
*/ |
| 40 |
private $pages; |
| 41 |
/** |
| 42 |
* Components |
| 43 |
*/ |
| 44 |
private $components = []; |
| 45 |
/** |
| 46 |
* @var TemplateHandler |
| 47 |
*/ |
| 48 |
private TemplateHandler $templateHandler; |
| 49 |
|
| 50 |
/** |
| 51 |
* @param $slug |
| 52 |
* @param string $title |
| 53 |
* @param string $capabilities |
| 54 |
* |
| 55 |
* @return UI |
| 56 |
* @deprecated |
| 57 |
* @deprecated |
| 58 |
*/ |
| 59 |
public static function getInstance( $slug, $title = 'Dashboard', $capabilities = 'manage_options' ) { |
| 60 |
if ( null == self::$instance ) { |
| 61 |
self::$instance = new UI( $slug, $title, $capabilities ); |
| 62 |
} |
| 63 |
|
| 64 |
return self::$instance; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Add a Page to the UI |
| 69 |
* |
| 70 |
* @param $UIPage |
| 71 |
* |
| 72 |
* @return void |
| 73 |
*/ |
| 74 |
public function addPage( $UIPage ) { |
| 75 |
$this->pages[] = $UIPage; |
| 76 |
|
| 77 |
add_action( 'forge12-plugin-content-' . $this->slug, array( $UIPage, 'renderContent' ), 10, 2 ); |
| 78 |
add_action( 'forge12-plugin-sidebar-' . $this->slug, array( $UIPage, 'renderSidebar' ), 10, 2 ); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* @return array<UIPage> |
| 83 |
*/ |
| 84 |
private function getPages() { |
| 85 |
return $this->pages; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* @param $slug |
| 90 |
* |
| 91 |
* @return UIPage|null |
| 92 |
*/ |
| 93 |
private function get( $slug ) { |
| 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( TemplateHandler $templateHandler, string $slug, string $title, string $capabilities ) { |
| 109 |
$this->templateHandler = $templateHandler; |
| 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 |
* Stylesheets for the admin panel |
| 138 |
*/ |
| 139 |
wp_enqueue_style( 'f12-cf7-doubleoptin-admin', plugins_url( 'assets/admin-style.css', __FILE__ ), array(), '1.3' ); |
| 140 |
|
| 141 |
/** |
| 142 |
* Toggle Button |
| 143 |
*/ |
| 144 |
wp_enqueue_script( 'f12-cf7-doubleoptin-toggle', plugins_url( 'assets/toggle.js', __FILE__ ), array( 'jquery' ), '1.0' ); |
| 145 |
|
| 146 |
/** |
| 147 |
* Copy to clipboard |
| 148 |
*/ |
| 149 |
wp_enqueue_script( 'f12-cf7-doubleoptin-copy', plugins_url( 'assets/copy-to-clipboard.js', __FILE__ ), array( 'jquery' ), '1.0' ); |
| 150 |
|
| 151 |
/** |
| 152 |
* External vendor scripts |
| 153 |
*/ |
| 154 |
wp_enqueue_script( 'vendor-highlight', plugins_url( 'assets/vendor/highlight/highlight.min.js', __FILE__ ), array( 'jquery' ), '1.0' ); |
| 155 |
wp_enqueue_style( 'vendor-highlight', plugins_url( 'assets/vendor/highlight/default.min.css', __FILE__ ), array(), '1.0' ); |
| 156 |
} |
| 157 |
|
| 158 |
public function sortComponents( $UI, $domain ) { |
| 159 |
if ( ! empty( $this->pages ) ) { |
| 160 |
usort( $this->pages, function ( $a, $b ) { |
| 161 |
if ( $a->getPosition() < $b->getPosition() ) { |
| 162 |
return - 1; |
| 163 |
} else if ( $a->getPosition() > $b->getPosition() ) { |
| 164 |
return 1; |
| 165 |
} else { |
| 166 |
return 0; |
| 167 |
} |
| 168 |
} ); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* @param UI $UI |
| 174 |
* @param $domain |
| 175 |
* |
| 176 |
* @return void |
| 177 |
*/ |
| 178 |
public function registerComponents( $UI, $domain ) { |
| 179 |
/** |
| 180 |
* Load all registered components |
| 181 |
*/ |
| 182 |
foreach ( $this->components as $component ) { |
| 183 |
/** |
| 184 |
* Check if the path has been defined |
| 185 |
*/ |
| 186 |
if ( ! isset( $component['path'] ) ) { |
| 187 |
continue; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Check if the name has been defined |
| 192 |
*/ |
| 193 |
if ( ! isset( $component['name'] ) ) { |
| 194 |
continue; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Load the file |
| 199 |
*/ |
| 200 |
require_once( $component['path'] ); |
| 201 |
|
| 202 |
/** |
| 203 |
* Load the instance of the object |
| 204 |
*/ |
| 205 |
$UIPage = new $component['name']( $this->templateHandler, $domain ); |
| 206 |
|
| 207 |
/** |
| 208 |
* Add the page to the user interface in the backend of wordpress. |
| 209 |
*/ |
| 210 |
$UI->addPage( $UIPage ); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
private function addComponent( $name, $path ) { |
| 215 |
$this->components[] = array( |
| 216 |
'name' => $name, |
| 217 |
'path' => $path |
| 218 |
); |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Retrieves the directories where the components are located. |
| 223 |
* |
| 224 |
* This method is responsible for retrieving the directories where the components |
| 225 |
* for the current application are located. It uses the 'f12_cf7_doubleoption_ui_get_component_directories' |
| 226 |
* filter to allow customization of the directories. By default, it returns an array |
| 227 |
* containing the 'ui' directory located two levels above the current file's directory. |
| 228 |
* |
| 229 |
* @return array An array containing the directories where the components are located. |
| 230 |
*/ |
| 231 |
private function get_component_directories() { |
| 232 |
/** |
| 233 |
* Component Directories |
| 234 |
* |
| 235 |
* This filter allows developers to add custom ui directories which will be used to display ui pages. |
| 236 |
* |
| 237 |
* @param array $directories |
| 238 |
* |
| 239 |
* @since 3.0.0 |
| 240 |
*/ |
| 241 |
return apply_filters( 'f12_cf7_doubleoption_ui_get_component_directories', [ dirname( __FILE__, 2 ) . '/ui' ] ); |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Load all components from the 'ui' directory. |
| 246 |
* |
| 247 |
* @return void |
| 248 |
*/ |
| 249 |
private function loadComponents() { |
| 250 |
$directories = $this->get_component_directories(); |
| 251 |
|
| 252 |
foreach ( $directories as $directory ) { |
| 253 |
if ( is_dir( $directory ) ) { |
| 254 |
$handle = opendir( $directory ); |
| 255 |
|
| 256 |
if ( ! $handle ) { |
| 257 |
return; |
| 258 |
} |
| 259 |
|
| 260 |
while ( false !== ( $entry = readdir( $handle ) ) ) { |
| 261 |
if ( $entry != '.' && $entry != '..' ) { |
| 262 |
if ( preg_match( '!UI([a-zA-Z_0-9]+)\.class\.php!', $entry, $matches ) ) { |
| 263 |
if ( isset( $matches[1] ) ) { |
| 264 |
$this->addComponent( '\\' . __NAMESPACE__ . '\UI' . $matches[1], $directory . '/' . $entry ); |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
closedir( $handle ); |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Add the WordPress Page for the Settings to the WordPress CMS |
| 276 |
* |
| 277 |
* @private WordPress Hook |
| 278 |
*/ |
| 279 |
public function addSubmenuPagesToWordPress() { |
| 280 |
$icon_url = plugins_url( 'assets/icon-double-opt-in-20x20.png', dirname( __FILE__ ) ); |
| 281 |
add_menu_page( $this->title, $this->title, $this->capabilities, $this->slug, '', $icon_url ); |
| 282 |
|
| 283 |
foreach ( $this->getPages() as /** @var UIPage $Page */ $Page ) { |
| 284 |
if ( $Page->isDashboard() ) { |
| 285 |
$slug = $this->slug; |
| 286 |
} else { |
| 287 |
$slug = $this->slug . '_' . $Page->getSlug(); |
| 288 |
} |
| 289 |
|
| 290 |
add_submenu_page( $this->slug, $Page->getTitle(), $Page->getTitle(), $this->capabilities, $slug, function () { |
| 291 |
$this->render(); |
| 292 |
}, $Page->getPosition() ); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* This will ensure that the menu page will be removed before the rendering. |
| 298 |
* This needs to be called from add_action('admin_head', ''); to be working. |
| 299 |
* |
| 300 |
* @return void |
| 301 |
*/ |
| 302 |
public function hideSubmenuPagesToWordpress() { |
| 303 |
foreach ( $this->getPages() as /** @var UIPage $Page */ $Page ) { |
| 304 |
if ( ! $Page->hideInMenu() ) { |
| 305 |
continue; |
| 306 |
} |
| 307 |
|
| 308 |
if ( $Page->isDashboard() ) { |
| 309 |
$slug = $this->slug; |
| 310 |
} else { |
| 311 |
$slug = $this->slug . '_' . $Page->getSlug(); |
| 312 |
} |
| 313 |
|
| 314 |
remove_submenu_page( $this->slug, $slug ); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
public function render() { |
| 319 |
$page = sanitize_text_field( $_GET['page'] ); |
| 320 |
$page = substr( explode( $this->slug, $page )[1], 1 ); |
| 321 |
|
| 322 |
if ( empty( $page ) ) { |
| 323 |
$page = $this->slug; |
| 324 |
} |
| 325 |
|
| 326 |
$pages = $this->getPages(); |
| 327 |
$menuPages = array(); |
| 328 |
|
| 329 |
foreach ( $pages as $UIPage ) { |
| 330 |
if ( ! $UIPage->hideInMenu() ) { |
| 331 |
$menuPages[] = $UIPage; |
| 332 |
} |
| 333 |
} |
| 334 |
?> |
| 335 |
<div class="forge12-plugin <?php esc_attr_e( $this->slug ); ?>"> |
| 336 |
<div class="forge12-plugin-header"> |
| 337 |
<div class="forge12-plugin-header-inner"> |
| 338 |
<img src="<?php echo esc_url( plugin_dir_url( __FILE__ ) ); ?>/assets/logo-forge12.png" |
| 339 |
alt="Forge12 Interactvie GmbH" title="Forge12 Interactive GmbH"/> |
| 340 |
</div> |
| 341 |
</div> |
| 342 |
<div class="forge12-plugin-menu"> |
| 343 |
<?php do_action( 'f12_cf7_doubleoptin_admin_menu', $menuPages, $page, $this->slug ); ?> |
| 344 |
</div> |
| 345 |
<div class="forge12-plugin-content"> |
| 346 |
<div class="forge12-plugin-content-main"> |
| 347 |
<?php do_action( 'forge12-plugin-content-' . $this->slug, $this->slug, $page ) ?> |
| 348 |
</div> |
| 349 |
<div class="forge12-plugin-content-sidebar"> |
| 350 |
<?php do_action( 'forge12-plugin-sidebar-' . $this->slug, $this->slug, $page ) ?> |
| 351 |
</div> |
| 352 |
</div> |
| 353 |
<div class="forge12-plugin-footer"> |
| 354 |
<div class="forge12-plugin-footer-inner"> |
| 355 |
<img src="<?php echo esc_url( plugin_dir_url( __FILE__ ) ); ?>/assets/logo-forge12-dark.png" |
| 356 |
alt="Forge12 Interactvie GmbH" title="Forge12 Interactive GmbH"/> |
| 357 |
</div> |
| 358 |
</div> |
| 359 |
</div> |
| 360 |
<?php |
| 361 |
} |
| 362 |
} |
| 363 |
} |