PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.9.0
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.9.0
2.13.0 2.13.1 2.12.0 2.11.1 2.11.0 2.10.0 2.9.0 2.7.4 2.7.5 2.7.6 2.7.7 2.8.0 2.8.1 2.9.1 trunk 1.0 1.0-beta1 1.0-beta2 1.0-beta3 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 All 80 releases
ablocks / includes / blocks / frontend-dashboard / block.php

block.php in aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder 2.9.0, at includes/blocks/frontend-dashboard/block.php

485 lines 16.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace ABlocks\Blocks\FrontendDashboard;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit;
6 }
7
8 use ABlocks\Classes\BlockBaseAbstract;
9 use ABlocks\Classes\CssGenerator;
10 use ABlocks\Helper;
11 use ABlocks\Controls\Typography;
12 use ABlocks\Controls\TextShadow;
13 use ABlocks\Controls\TextStroke;
14 use ABlocks\Controls\Background;
15 use ABlocks\Controls\Border;
16 use ABlocks\Controls\Dimensions;
17 use ABlocks\Controls\Range;
18 use ABlocks\Controls\Color;
19
20
21 class Block extends BlockBaseAbstract {
22 protected $block_name = 'frontend-dashboard';
23
24 public function __construct() {
25 parent::__construct();
26
27 }
28
29 public function build_css( $attributes ) {
30
31 $css_generator = new CssGenerator( $attributes, $this->block_name );
32
33 $get_gaps_css = $this->get_wrapper_gap_css( $attributes );
34 $css_generator->add_class_styles(
35 '{{WRAPPER}}.ablocks-block--frontend-dashboard',
36 $get_gaps_css,
37 $this->get_wrapper_gap_css( $attributes, 'Tablet' ),
38 $this->get_wrapper_gap_css( $attributes, 'Mobile' )
39 );
40
41 $get_setting_css = $this->get_setting_css( $attributes );
42
43 $css_generator->add_class_styles(
44 '{{WRAPPER}} .ablocks-frontend-dashboard-sidebar',
45 $get_setting_css,
46 $this->get_setting_css( $attributes, 'Tablet' ),
47 $this->get_setting_css( $attributes, 'Mobile' )
48 );
49
50 $get_user_setting_css = $this->get_user_setting_css( $attributes );
51
52 $css_generator->add_class_styles(
53 '{{WRAPPER}} .ablocks-frontend-dashboard-user',
54 $get_user_setting_css,
55 $this->get_user_setting_css( $attributes, 'Tablet' ),
56 $this->get_user_setting_css( $attributes, 'Mobile' )
57 );
58
59 $get_menu_list_css = $this->get_menu_list_css( $attributes );
60
61 $css_generator->add_class_styles(
62 '{{WRAPPER}} .ablocks-frontend-dashboard-menu__item a',
63 $get_menu_list_css,
64 $this->get_menu_list_css( $attributes, 'Tablet' ),
65 $this->get_menu_list_css( $attributes, 'Mobile' )
66 );
67
68 $get_menu_hover_list_css = $this->get_menu_hover_list_css( $attributes );
69
70 $css_generator->add_class_styles(
71 '{{WRAPPER}} .ablocks-frontend-dashboard-menu__item a:hover',
72 $get_menu_hover_list_css,
73 $this->get_menu_hover_list_css( $attributes, 'Tablet' ),
74 $this->get_menu_hover_list_css( $attributes, 'Mobile' )
75 );
76
77 $get_menu_active_list_css = $this->get_menu_active_list_css( $attributes );
78
79 $css_generator->add_class_styles(
80 '{{WRAPPER}} .ablocks-frontend-dashboard-menu__item--current > a',
81 $get_menu_active_list_css,
82 $this->get_menu_active_list_css( $attributes, 'Tablet' ),
83 $this->get_menu_active_list_css( $attributes, 'Mobile' )
84 );
85
86 $get_breadcrumb_css = $this->get_breadcrumb_css( $attributes );
87
88 $css_generator->add_class_styles(
89 '{{WRAPPER}} .ablocks-frontend-dashboard-breadcrumb li',
90 $get_breadcrumb_css,
91 $this->get_breadcrumb_css( $attributes, 'Tablet' ),
92 $this->get_breadcrumb_css( $attributes, 'Mobile' )
93 );
94
95 $get_breadcrumb_css = $this->get_breadcrumb_css( $attributes );
96
97 $css_generator->add_class_styles(
98 '{{WRAPPER}} .ablocks-frontend-dashboard-breadcrumb li + li::before',
99 $get_breadcrumb_css,
100 $this->get_breadcrumb_css( $attributes, 'Tablet' ),
101 $this->get_breadcrumb_css( $attributes, 'Mobile' )
102 );
103
104 $get_content_css = $this->get_content_css( $attributes );
105
106 $css_generator->add_class_styles(
107 '{{WRAPPER}} .ablocks-frontend-dashboard-content',
108 $get_content_css,
109 $this->get_content_css( $attributes, 'Tablet' ),
110 $this->get_content_css( $attributes, 'Mobile' )
111 );
112
113 $get_content_hover_css = $this->get_content_hover_css( $attributes );
114
115 $css_generator->add_class_styles(
116 '{{WRAPPER}} .ablocks-frontend-dashboard-content:hover',
117 $get_content_hover_css,
118 $this->get_content_hover_css( $attributes, 'Tablet' ),
119 $this->get_content_hover_css( $attributes, 'Mobile' )
120 );
121
122 return $css_generator->generate_css();
123 }
124
125
126 public function get_setting_css( $attributes, $device = '' ) {
127 $css = [];
128
129 if ( ! empty( $attributes['sidebarBackground'] ) ) {
130 $css['background'] = Color::get_css(
131 isset( $attributes['sidebarBackground'] ) ? $attributes['sidebarBackground'] : '');
132 }
133
134 return array_merge(
135 $css,
136 Border::get_css( $attributes['sidebarBorder'] ?? [], '', $device )
137 );
138 }
139
140 public function get_user_setting_css( $attributes, $device = '' ) {
141 $css = [];
142
143 if ( ! empty( $attributes['userTextColor'] ) ) {
144 $css['color'] = Color::get_css(
145 isset( $attributes['userTextColor'] ) ? $attributes['userTextColor'] : '');
146 }
147 if ( ! empty( $attributes['sidebarUserBackground'] ) ) {
148 $css['background'] = Color::get_css(
149 isset( $attributes['sidebarUserBackground'] ) ? $attributes['sidebarUserBackground'] : '');
150 }
151 $userTypographyGlobal = ! empty( $attributes['userTypographyGlobal'] ) ? $attributes['userTypographyGlobal'] : '';
152 return array_merge(
153 $css,
154 Border::get_css( $attributes['userSidebarBorder'] ?? [], '', $device ),
155 Typography::get_css( $attributes['userTypography'], '', $device, $userTypographyGlobal ),
156 );
157 }
158
159 public function get_menu_list_css( $attributes, $device = '' ) {
160 $css = [];
161
162 if ( ! empty( $attributes['menuListTextColor'] ) ) {
163 $css['color'] = Color::get_css(
164 isset( $attributes['menuListTextColor'] ) ? $attributes['menuListTextColor'] : '');
165 }
166 if ( ! empty( $attributes['menuListBackground'] ) ) {
167 $css['background'] = Color::get_css(
168 isset( $attributes['menuListBackground'] ) ? $attributes['menuListBackground'] : '');
169 }
170
171 $userTypographyGlobal = ! empty( $attributes['userTypographyGlobal'] ) ? $attributes['userTypographyGlobal'] : '';
172
173 return array_merge(
174 $css,
175 Border::get_css( $attributes['menuListBorder'] ?? [], '', $device ),
176 Dimensions::get_css( $attributes['menuListPadding'] ?? [], 'padding', $device ),
177 Typography::get_css( $attributes['menuListTypography'] ?? [], '', $device, $userTypographyGlobal )
178 );
179 }
180
181 public function get_menu_hover_list_css( $attributes, $device = '' ) {
182 $css = [];
183
184 if ( ! empty( $attributes['menuListHoverTextColor'] ) ) {
185 $css['color'] = Color::get_css(
186 isset( $attributes['menuListHoverTextColor'] ) ? $attributes['menuListHoverTextColor'] : '');
187 }
188 if ( ! empty( $attributes['menuListHoverBackground'] ) ) {
189 $css['background'] = Color::get_css(
190 isset( $attributes['menuListHoverBackground'] ) ? $attributes['menuListHoverBackground'] : '');
191 }
192
193 return array_merge(
194 $css,
195 Border::get_hover_css( $attributes['menuListBorder'] ?? [], '', $device )
196 );
197 }
198
199 public function get_menu_active_list_css( $attributes, $device = '' ) {
200 $css = [];
201
202 if ( ! empty( $attributes['menuListActiveTextColor'] ) ) {
203 $css['color'] = Color::get_css(
204 isset( $attributes['menuListActiveTextColor'] ) ? $attributes['menuListActiveTextColor'] : '');
205 }
206 if ( ! empty( $attributes['menuListActiveBackground'] ) ) {
207 $css['background'] = Color::get_css(
208 isset( $attributes['menuListActiveBackground'] ) ? $attributes['menuListActiveBackground'] : '');
209 }
210
211 return $css;
212 }
213 public function get_content_css( $attributes, $device = '' ) {
214 $css = [];
215
216 if ( ! empty( $attributes['contentBackground'] ) ) {
217 $css['background'] = Color::get_css(
218 isset( $attributes['contentBackground'] ) ? $attributes['contentBackground'] : '');
219 }
220
221 return array_merge(
222 $css,
223 Border::get_css( $attributes['contentBorder'] ?? [], '', $device ),
224 Dimensions::get_css( $attributes['contentPadding'] ?? [], 'padding', $device ),
225 );
226 }
227 public function get_content_hover_css( $attributes, $device = '' ) {
228
229 return array_merge(
230 Border::get_hover_css( $attributes['contentBorder'] ?? [], '', $device ),
231 );
232 }
233
234 public function get_breadcrumb_css( $attributes, $device = '' ) {
235 $css = [];
236
237 if ( ! empty( $attributes['breadcrumbColor'] ) ) {
238 $css['color'] = Color::get_css(
239 isset( $attributes['breadcrumbColor'] ) ? $attributes['breadcrumbColor'] : '');
240 }
241 $breadcrumbtTypographyGlobal = ! empty( $attributes['breadcrumbtTypographyGlobal'] ) ? $attributes['breadcrumbtTypographyGlobal'] : '';
242
243 return array_merge(
244 $css,
245 Typography::get_css( $attributes['breadcrumbtTypography'] ?? [], '', $device, $breadcrumbtTypographyGlobal )
246 );
247 }
248 public function get_wrapper_gap_css( $attributes, $device = '' ) {
249 $css = [];
250
251 $css['display'] = 'flex';
252 if ( $device === 'Tablet' ) {
253 $css['flex-direction'] = 'column';
254 } elseif ( $device === 'Mobile' ) {
255 $css['flex-direction'] = 'column';
256 } else {
257 $css['flex-direction'] = 'row';
258 }
259
260 if ( ! empty( $attributes['bothGap'] ) ) {
261 $css['gap'] = $attributes['bothGap'] . 'px';
262 }
263 return array_merge(
264 $css
265 );
266 }
267
268 // * Render the block content.
269 public function render_block_content( $attributes, $content, $block_instance ) {
270
271 global $wp;
272 global $wpdb;
273
274 $dashboard_page = isset( $attributes['dashboard_page'] ) && ! empty( $attributes['dashboard_page'] ) ? $attributes['dashboard_page'] : 'root';
275
276 $dashboard_pages = json_decode( get_option( ABLOCKS_FRONTEND_DASHBOARD_SUB_PAGES_SETTINGS_NAME, '{}' ), true );
277 // print_r($dashboard_pages);
278 $breadcrumbs = [];
279 $breadcrumbs[] = [
280 'label' => __( 'Dashboard', 'ablocks' ),
281 'url' => $this->get_frontend_dashboard_endpoint_url( '' ),
282 ];
283
284 $current_page_slug = get_query_var( 'ablocks_dashboard_page', 'root' );
285 $current_sub_page_slug = get_query_var( 'ablocks_dashboard_sub_page' );
286
287 if ( $current_page_slug !== 'root' ) {
288 $current_slug = empty( $current_sub_page_slug ) ? $current_page_slug : $current_sub_page_slug;
289 // var_dump($current_slug);
290 // Find the current page in the dashboard_pages array
291 foreach ( $dashboard_pages as $page ) {
292 if ( isset( $page['slug'] ) && $page['slug'] === $current_slug ) {
293 $breadcrumbs[] = [
294 'label' => $page['label'],
295 'url' => isset( $page['permalink'] ) ? $page['permalink'] : $this->get_frontend_dashboard_endpoint_url( $page['slug'] ),
296 ];
297 break;
298 }
299
300 if ( is_array( $page['children'] ?? '' ) ) {
301 if (
302 isset( $page['slug'] ) &&
303 $page['slug'] === $current_page_slug
304 ) {
305 $breadcrumbs[] = [
306 'label' => $page['label'],
307 'url' => isset( $page['permalink'] ) ? $page['permalink'] : $this->get_frontend_dashboard_endpoint_url( $page['slug'] ),
308 ];
309 }
310
311 foreach ( $page['children'] as $sub_page ) {
312 // print_r([$sub_page['slug'], $current_slug]);
313 if ( isset( $sub_page['slug'] ) && $sub_page['slug'] === $current_slug ) {
314 $breadcrumbs[] = [
315 'label' => $sub_page['label'],
316 'url' => isset( $sub_page['permalink'] ) ? $sub_page['permalink'] : $this->get_frontend_dashboard_endpoint_url( $sub_page['slug'] ),
317 ];
318 break;
319 }
320 }
321 }//end if
322 }//end foreach
323 }//end if
324 ?>
325 <div class="ablocks-frontend-dashboard-sidebar">
326 <div class="ablocks-frontend-dashboard-user">
327 <div id="ablocks-frontend-dashboard-user-avatar">
328 <?php echo get_avatar( get_current_user_id(), 96, '', wp_get_current_user()->display_name, [ 'loading' => 'lazy' ] ); ?>
329 <span class="ablocks-frontend-dashboard-user__label"><?php echo esc_html( wp_get_current_user()->display_name ); ?></span>
330 </div>
331 </div>
332 <ul class="ablocks-frontend-dashboard-menu">
333 <?php
334 foreach ( $dashboard_pages as $menu ) :
335 // print_r($menu);
336 $endpoint = $menu['slug'] ?? $menu['link'];
337 // print_r($endpoint);
338 $menu['permalink'] = $menu['permalink'] ?? $menu['link'] ?? null;
339 if ( $endpoint === 'root' ) {
340 $menu['label'] = __( 'Dashboard', 'ablocks' );
341 $menu['slug'] = '';
342 $menu['priority'] = 0;
343 } ?>
344 <?php
345 $is_parent_current = ( get_query_var( 'ablocks_dashboard_page' ) === $endpoint || ( get_query_var( 'ablocks_dashboard_page' ) === '' && 'index' === $endpoint ) ) && empty( get_query_var( 'ablocks_dashboard_sub_page' ) );
346 ?>
347 <li class="ablocks-frontend-dashboard-menu__item ablocks-frontend-dashboard-menu__item--<?php echo esc_attr( $endpoint ); ?> <?php echo esc_attr( $menu['class_name'] ?? '' ); ?> <?php echo $is_parent_current ? 'ablocks-frontend-dashboard-menu__item--current' : ''; ?>">
348 <a href="<?php echo esc_url( isset( $menu['permalink'] ) ? $menu['permalink'] : $this->get_frontend_dashboard_endpoint_url( $endpoint ) ); ?>">
349 <i class="<?php if ( ! empty( $menu['icon'] ) ) {
350 echo esc_html( $menu['icon'] );} ?>"></i>
351 <span class="ablocks-frontend-dashboard-menu__item-label"><?php echo esc_html( $menu['label'] ); ?></span>
352 </a>
353
354 <?php if ( ! empty( $menu['children'] ) ) : ?>
355 <ul class="ablocks-frontend-dashboard-menu__submenu">
356 <?php foreach ( $menu['children'] as $child ) : ?>
357 <?php
358 $child['permalink'] = $child['permalink'] ?? $child['link'] ?? null;
359 $is_current_child = get_query_var( 'ablocks_dashboard_sub_page' ) === ( $child['slug'] ?? null );
360 ?>
361 <li class="<?php echo esc_attr( $child['class_name'] ?? '' ); ?> <?php echo $is_current_child ? 'ablocks-frontend-dashboard-menu__item--current' : ''; ?>"><a href="<?php echo esc_url( isset( $child['permalink'] ) ? $child['permalink'] : $this->get_frontend_dashboard_endpoint_url( $endpoint . '/' . ( $child['slug'] ?? '' ) ) ); ?>">
362 <i class="<?php if ( ! empty( $child['icon'] ) ) {
363 echo esc_html( $child['icon'] );} ?>"></i>
364 <?php echo esc_html( $child['label'] ); ?>
365 </a></li>
366 <?php endforeach; ?>
367 </ul>
368 <?php endif; ?>
369 </li>
370 <?php endforeach; ?>
371 </ul>
372
373 </div>
374 <div class="ablocks-frontend-dashboard-content">
375 <!-- Breadcrumb -->
376 <ul class="ablocks-frontend-dashboard-breadcrumb">
377 <?php foreach ( $breadcrumbs as $crumb ) :
378 ?>
379 <li>
380 <?php echo esc_html( $crumb['label'] ); ?>
381 </li>
382 <?php endforeach; ?>
383 </ul>
384 <?php
385 if ( Helper::is_gutenberg_editor() ) {
386 self::render_dashboard_page_by_slug( $dashboard_page );
387 // Show Notice for empty pages
388 if ( ! is_array( $dashboard_pages ) || ! count( $dashboard_pages ) ) {
389 echo 'Dashboard Page is empty. You haven\'t created any dashboard pages.';
390 }
391 } else {
392 $is_resolved = false;
393 if ( ! empty( $wp->query_vars ) ) {
394 if ( isset( $wp->query_vars['ablocks_dashboard_page'] ) &&
395 isset( $wp->query_vars['ablocks_dashboard_sub_page'] )
396 ) {
397 self::render_dashboard_page_by_slug(
398 get_query_var(
399 'ablocks_dashboard_sub_page'
400 )
401 );
402 } else {
403 self::render_dashboard_page_by_slug(
404 get_query_var(
405 'ablocks_dashboard_page'
406 )
407 );
408
409 }
410 $is_resolved = true;
411 }//end if
412 if ( ! $is_resolved ) {
413 // Render root
414 self::render_dashboard_page_by_slug( 'root' );
415 }
416 }//end if
417 ?>
418 </div>
419 <?php
420 }
421
422 public static function get_endpoint_url( $endpoint, $value = '', $permalink = '' ) {
423 global $wp_query;
424 if ( ! $permalink ) {
425 $permalink = get_permalink();
426 }
427
428 // Map endpoint to options.
429 $query_vars = $wp_query->query_vars;
430 $endpoint = ! empty( $query_vars[ $endpoint ] ) ? $query_vars[ $endpoint ] : $endpoint;
431
432 if ( get_option( 'permalink_structure' ) ) {
433 if ( strstr( $permalink, '?' ) ) {
434 $query_string = '?' . wp_parse_url( $permalink, PHP_URL_QUERY );
435 $permalink = current( explode( '?', $permalink ) );
436 } else {
437 $query_string = '';
438 }
439 $url = trailingslashit( $permalink );
440
441 if ( $value ) {
442 $url .= trailingslashit( $endpoint ) . user_trailingslashit( $value );
443 } else {
444 $url .= user_trailingslashit( $endpoint );
445 }
446
447 $url .= $query_string;
448 } else {
449 $url = add_query_arg( $endpoint, $value, $permalink );
450 }
451
452 return apply_filters( 'ablocks/get_endpoint_url', $url, $endpoint, $value, $permalink );
453 }
454
455 public static function render_dashboard_page_by_slug( $slug ) {
456 global $wpdb;
457 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
458 $page = $wpdb->get_row( $wpdb->prepare(
459 "SELECT post_content FROM $wpdb->posts WHERE post_name = %s AND post_type = 'page'",
460 basename( $slug )
461 ) );
462
463 if ( $page ) {
464 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
465 echo apply_filters( 'the_content', $page->post_content );
466 return;
467 }
468 }
469
470 public static function get_frontend_dashboard_endpoint_url( $endpoint ) {
471 if ( 'index' === $endpoint ) {
472 return Helper::get_page_permalink( 'frontend_dashboard_page' );
473 }
474
475 if ( 'logout' === $endpoint ) {
476 return Helper::get_logout_url();
477 }
478
479 return self::get_endpoint_url( $endpoint, '', Helper::get_page_permalink( 'frontend_dashboard_page' ) );
480 }
481
482
483
484 }
485