PluginProbe
Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification / 3.0.61
Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification v3.0.61
5.6.2 5.6.3 5.6.1 5.6.0 5.5.0 5.4.0 5.3.2 5.3.1 5.1.6 5.1.5 trunk 2.1.5 2.11 2.12 2.13 2.15 3.0.0 3.0.1 3.0.2 3.0.3 3.0.5 3.0.51 3.0.60 3.0.61 3.0.62 All 38 releases
double-opt-in / core / UI.class.php

UI.class.php in Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification 3.0.61, at core/UI.class.php

365 lines 10.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 add_action('init', function(){
126 // Called after all Pages have been initialized
127 do_action( 'f12_cf7_doubleoptin_ui_after_load_pages', $this, $this->slug );
128 });
129
130 // Create the Submenus
131 add_action( 'admin_menu', array( $this, 'addSubmenuPagesToWordPress' ) );
132
133 // Hotfix to hide the submenus that should be hidden but still callable in wordpress backend
134 add_action( 'admin_head', array( $this, 'hideSubmenuPagesToWordpress' ) );
135 }
136
137 public function addAssets() {
138 /**
139 * Stylesheets for the admin panel
140 */
141 wp_enqueue_style( 'f12-cf7-doubleoptin-admin', plugins_url( 'assets/admin-style.css', __FILE__ ), array(), '1.3' );
142
143 /**
144 * Toggle Button
145 */
146 wp_enqueue_script( 'f12-cf7-doubleoptin-toggle', plugins_url( 'assets/toggle.js', __FILE__ ), array( 'jquery' ), '1.0' );
147
148 /**
149 * Copy to clipboard
150 */
151 wp_enqueue_script( 'f12-cf7-doubleoptin-copy', plugins_url( 'assets/copy-to-clipboard.js', __FILE__ ), array( 'jquery' ), '1.0' );
152
153 /**
154 * External vendor scripts
155 */
156 wp_enqueue_script( 'vendor-highlight', plugins_url( 'assets/vendor/highlight/highlight.min.js', __FILE__ ), array( 'jquery' ), '1.0' );
157 wp_enqueue_style( 'vendor-highlight', plugins_url( 'assets/vendor/highlight/default.min.css', __FILE__ ), array(), '1.0' );
158 }
159
160 public function sortComponents( $UI, $domain ) {
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 $UI
176 * @param $domain
177 *
178 * @return void
179 */
180 public function registerComponents( $UI, $domain ) {
181 /**
182 * Load all registered components
183 */
184 foreach ( $this->components as $component ) {
185 /**
186 * Check if the path has been defined
187 */
188 if ( ! isset( $component['path'] ) ) {
189 continue;
190 }
191
192 /**
193 * Check if the name has been defined
194 */
195 if ( ! isset( $component['name'] ) ) {
196 continue;
197 }
198
199 /**
200 * Load the file
201 */
202 require_once( $component['path'] );
203
204 /**
205 * Load the instance of the object
206 */
207 $UIPage = new $component['name']( $this->templateHandler, $domain );
208
209 /**
210 * Add the page to the user interface in the backend of wordpress.
211 */
212 $UI->addPage( $UIPage );
213 }
214 }
215
216 private function addComponent( $name, $path ) {
217 $this->components[] = array(
218 'name' => $name,
219 'path' => $path
220 );
221 }
222
223 /**
224 * Retrieves the directories where the components are located.
225 *
226 * This method is responsible for retrieving the directories where the components
227 * for the current application are located. It uses the 'f12_cf7_doubleoption_ui_get_component_directories'
228 * filter to allow customization of the directories. By default, it returns an array
229 * containing the 'ui' directory located two levels above the current file's directory.
230 *
231 * @return array An array containing the directories where the components are located.
232 */
233 private function get_component_directories() {
234 /**
235 * Component Directories
236 *
237 * This filter allows developers to add custom ui directories which will be used to display ui pages.
238 *
239 * @param array $directories
240 *
241 * @since 3.0.0
242 */
243 return apply_filters( 'f12_cf7_doubleoption_ui_get_component_directories', [ dirname( __FILE__, 2 ) . '/ui' ] );
244 }
245
246 /**
247 * Load all components from the 'ui' directory.
248 *
249 * @return void
250 */
251 private function loadComponents() {
252 $directories = $this->get_component_directories();
253
254 foreach ( $directories as $directory ) {
255 if ( is_dir( $directory ) ) {
256 $handle = opendir( $directory );
257
258 if ( ! $handle ) {
259 return;
260 }
261
262 while ( false !== ( $entry = readdir( $handle ) ) ) {
263 if ( $entry != '.' && $entry != '..' ) {
264 if ( preg_match( '!UI([a-zA-Z_0-9]+)\.class\.php!', $entry, $matches ) ) {
265 if ( isset( $matches[1] ) ) {
266 $this->addComponent( '\\' . __NAMESPACE__ . '\UI' . $matches[1], $directory . '/' . $entry );
267 }
268 }
269 }
270 }
271 closedir( $handle );
272 }
273 }
274 }
275
276 /**
277 * Add the WordPress Page for the Settings to the WordPress CMS
278 *
279 * @private WordPress Hook
280 */
281 public function addSubmenuPagesToWordPress() {
282 $icon_url = plugins_url( 'assets/icon-double-opt-in-20x20.png', dirname( __FILE__ ) );
283 add_menu_page( $this->title, $this->title, $this->capabilities, $this->slug, '', $icon_url );
284
285 foreach ( $this->getPages() as /** @var UIPage $Page */ $Page ) {
286 if ( $Page->isDashboard() ) {
287 $slug = $this->slug;
288 } else {
289 $slug = $this->slug . '_' . $Page->getSlug();
290 }
291
292 add_submenu_page( $this->slug, $Page->getTitle(), $Page->getTitle(), $this->capabilities, $slug, function () {
293 $this->render();
294 }, $Page->getPosition() );
295 }
296 }
297
298 /**
299 * This will ensure that the menu page will be removed before the rendering.
300 * This needs to be called from add_action('admin_head', ''); to be working.
301 *
302 * @return void
303 */
304 public function hideSubmenuPagesToWordpress() {
305 foreach ( $this->getPages() as /** @var UIPage $Page */ $Page ) {
306 if ( ! $Page->hideInMenu() ) {
307 continue;
308 }
309
310 if ( $Page->isDashboard() ) {
311 $slug = $this->slug;
312 } else {
313 $slug = $this->slug . '_' . $Page->getSlug();
314 }
315
316 remove_submenu_page( $this->slug, $slug );
317 }
318 }
319
320 public function render() {
321 $page = sanitize_text_field( $_GET['page'] );
322 $page = substr( explode( $this->slug, $page )[1], 1 );
323
324 if ( empty( $page ) ) {
325 $page = $this->slug;
326 }
327
328 $pages = $this->getPages();
329 $menuPages = array();
330
331 foreach ( $pages as $UIPage ) {
332 if ( ! $UIPage->hideInMenu() ) {
333 $menuPages[] = $UIPage;
334 }
335 }
336 ?>
337 <div class="forge12-plugin <?php esc_attr_e( $this->slug ); ?>">
338 <div class="forge12-plugin-header">
339 <div class="forge12-plugin-header-inner">
340 <img src="<?php echo esc_url( plugin_dir_url( __FILE__ ) ); ?>/assets/logo-forge12.png"
341 alt="Forge12 Interactvie GmbH" title="Forge12 Interactive GmbH"/>
342 </div>
343 </div>
344 <div class="forge12-plugin-menu">
345 <?php do_action( 'f12_cf7_doubleoptin_admin_menu', $menuPages, $page, $this->slug ); ?>
346 </div>
347 <div class="forge12-plugin-content">
348 <div class="forge12-plugin-content-main">
349 <?php do_action( 'forge12-plugin-content-' . $this->slug, $this->slug, $page ) ?>
350 </div>
351 <div class="forge12-plugin-content-sidebar">
352 <?php do_action( 'forge12-plugin-sidebar-' . $this->slug, $this->slug, $page ) ?>
353 </div>
354 </div>
355 <div class="forge12-plugin-footer">
356 <div class="forge12-plugin-footer-inner">
357 <img src="<?php echo esc_url( plugin_dir_url( __FILE__ ) ); ?>/assets/logo-forge12-dark.png"
358 alt="Forge12 Interactvie GmbH" title="Forge12 Interactive GmbH"/>
359 </div>
360 </div>
361 </div>
362 <?php
363 }
364 }
365 }