PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.8.0
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.8.0
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 1.2.0 1.2.1 All 78 releases
ablocks / includes / ajax / settings.php

settings.php in aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder 2.8.0, at includes/ajax/settings.php

693 lines 23.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ABlocks\Ajax;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit;
7 }
8
9 use ABlocks\Classes\AbstractAjaxHandler;
10 use ABlocks\Classes\Sanitizer;
11 use ABlocks\Helper;
12 use ABlocks\Admin\Settings\Base as BaseSettings;
13
14 class Settings extends AbstractAjaxHandler {
15 public function __construct() {
16 $this->actions = array(
17 'get_blocks_visibility' => array(
18 'callback' => array( $this, 'get_blocks_visibility' ),
19 'capability' => 'manage_options'
20 ),
21 'save_block_visibility' => array(
22 'callback' => array( $this, 'save_block_visibility' ),
23 'capability' => 'manage_options',
24 'fields' => array(
25 'block_name' => 'string',
26 'status' => 'boolean',
27 )
28 ),
29 'save_bulk_block_visibility' => array(
30 'callback' => array( $this, 'save_bulk_block_visibility' ),
31 'capability' => 'manage_options',
32 'fields' => array(
33 'blocks' => 'json',
34 )
35 ),
36 'get_settings' => array(
37 'callback' => array( $this, 'get_settings' ),
38 'capability' => 'manage_options',
39 ),
40 'save_settings' => array(
41 'callback' => array( $this, 'save_settings' ),
42 'capability' => 'manage_options',
43 'fields' => array(
44 'default_container_width' => 'integer',
45 'container_padding' => 'integer',
46 'container_element_gap' => 'integer',
47 'enabled_assets_file_generation' => 'boolean',
48 'enabled_block_copy_paste_style' => 'boolean',
49 'enabled_load_google_font_locally' => 'boolean',
50 'enabled_only_selected_fonts' => 'boolean',
51 'enabled_coming_soon_page' => 'boolean',
52 'coming_soon_page' => 'integer',
53 'enabled_maintenance_page' => 'boolean',
54 'maintenance_page' => 'integer',
55 'login_page' => 'integer',
56 'registration_page' => 'integer',
57 'forget_password_page' => 'integer',
58 'frontend_dashboard_page' => 'string',
59 'mailchimp_api_key' => 'string',
60 'drip_api_key' => 'string',
61 'getresponse_api_key' => 'string',
62 'convertkit_api_key' => 'string',
63 'mailerlite_api_key' => 'string',
64 // global color & typography
65 'global_color' => 'json',
66 'global_typography' => 'json',
67 'global_font_family_fallback' => 'string',
68 'global_body_text_color' => 'string',
69 'global_body_typography' => 'json',
70 'global_body_paragraph_space' => 'json',
71 'global_link_color' => 'string',
72 'global_link_hover_color' => 'string',
73 'global_link_typography' => 'json',
74 'global_link_hover_typography' => 'json',
75 'global_h1_color' => 'string',
76 'global_h1_typography' => 'json',
77 'global_h2_color' => 'string',
78 'global_h2_typography' => 'json',
79 'global_h3_color' => 'string',
80 'global_h3_typography' => 'json',
81 'global_h4_color' => 'string',
82 'global_h4_typography' => 'json',
83 'global_h5_color' => 'string',
84 'global_h5_typography' => 'json',
85 'global_h6_color' => 'string',
86 'global_h6_typography' => 'json',
87 )
88 ),
89 'fetch_posts' => array(
90 'callback' => array( $this, 'fetch_posts' ),
91 'capability' => 'manage_options',
92 'fields' => array(
93 'postId' => 'integer',
94 'postType' => 'string',
95 'keyword' => 'string',
96 )
97 ),
98 'get_fronted_dashboard_pages' => array(
99 'callback' => array( $this, 'get_fronted_dashboard_pages' ),
100 'capability' => 'manage_options',
101 ),
102 'create_fronted_dashboard_page' => array(
103 'callback' => array( $this, 'create_fronted_dashboard_page' ),
104 'capability' => 'manage_options',
105 'fields' => [
106 'label' => 'string',
107 'slug' => 'string',
108 'icon' => 'string',
109 'class_name' => 'string',
110 'priority' => 'integer',
111 'parent_id' => 'integer',
112 ]
113 ),
114 'create_fronted_dashboard_link' => array(
115 'callback' => array( $this, 'create_fronted_dashboard_link' ),
116 'capability' => 'manage_options',
117 'fields' => [
118 'label' => 'string',
119 'link' => 'string',
120 'icon' => 'string',
121 'class_name' => 'string',
122 'priority' => 'integer',
123 'parent_id' => 'string',
124 ]
125 ),
126 'edit_fronted_dashboard_link' => array(
127 'callback' => array( $this, 'edit_fronted_dashboard_link' ),
128 'capability' => 'manage_options',
129 'fields' => [
130 'label' => 'string',
131 'link' => 'string',
132 'icon' => 'string',
133 'class_name' => 'string',
134 'priority' => 'integer',
135 'page_id' => 'string',
136 ]
137 ),
138 'delete_fronted_dashboard_link' => array(
139 'callback' => array( $this, 'delete_fronted_dashboard_link' ),
140 'capability' => 'manage_options',
141 'fields' => array(
142 'page_id' => 'string',
143 )
144 ),
145 'edit_fronted_dashboard_page' => array(
146 'callback' => array( $this, 'edit_fronted_dashboard_page' ),
147 'capability' => 'manage_options',
148 'fields' => [
149 'label' => 'string',
150 'slug' => 'string',
151 'icon' => 'string',
152 'class_name' => 'string',
153 'priority' => 'integer',
154 'parent_id' => 'integer',
155 'page_id' => 'integer',
156 ]
157 ),
158 'move_fronted_dashboard_page' => array(
159 'callback' => array( $this, 'move_fronted_dashboard_page' ),
160 'capability' => 'manage_options',
161 'fields' => [
162 'reordered_items' => 'string',
163 ]
164 ),
165 'delete_fronted_dashboard_page' => array(
166 'callback' => array( $this, 'delete_fronted_dashboard_page' ),
167 'capability' => 'manage_options',
168 'fields' => array(
169 'slug' => 'string',
170 'page_id' => 'integer',
171 )
172 ),
173 );
174 }
175
176 public function get_blocks_visibility() {
177 global $ablocks_blocks;
178 wp_send_json_success( $ablocks_blocks );
179 }
180
181 public function save_block_visibility( $payload ) {
182 $block_name = $payload['block_name'];
183 $status = $payload['status'];
184
185 $json_payload = Sanitizer::sanitize_payload([
186 'required_plugin' => 'json',
187 ], $payload);
188
189 $required_plugin = $json_payload['required_plugin'];
190
191 if ( empty( $block_name ) ) {
192 wp_send_json_error( __( 'Block Name missing', 'ablocks' ) );
193 }
194
195 if ( $status && $required_plugin ) {
196 if ( $required_plugin && is_array( $required_plugin ) ) {
197 foreach ( $required_plugin as $plugin ) {
198 if ( ! Helper::is_plugin_active( sanitize_text_field( $plugin['plugin_dir_path'] ) ) ) {
199 $error_message = sprintf( '%s Plugin is required to activate %s block.', $plugin['plugin_name'], $block_name );
200 wp_send_json_error( $error_message );
201 }
202 }
203 }
204 }
205
206 // Saved Data
207 $saved_blocks = (array) json_decode( get_option( ABLOCKS_BLOCKS_VISIBILITY_SETTINGS_NAME ), true );
208 $saved_blocks[ $block_name ] = $status;
209 update_option( ABLOCKS_BLOCKS_VISIBILITY_SETTINGS_NAME, wp_json_encode( $saved_blocks ) );
210 // Fire Addon Action
211 if ( $status ) {
212 do_action( "ablocks/block/activated_{$block_name}", $status );
213 } else {
214 do_action( "ablocks/block/deactivated_{$block_name}", $status );
215 }
216 wp_send_json_success( $saved_blocks );
217 }
218 public function save_bulk_block_visibility( $payload ) {
219 $blocks = wp_list_pluck( \json_decode( $payload['blocks'], true ), 'status', 'block_name' );
220 $saved_blocks = (array) json_decode( get_option( ABLOCKS_BLOCKS_VISIBILITY_SETTINGS_NAME ), true );
221 $updated_settings = wp_parse_args( $blocks, $saved_blocks );
222 update_option( ABLOCKS_BLOCKS_VISIBILITY_SETTINGS_NAME, wp_json_encode( $updated_settings ) );
223 wp_send_json_success( $updated_settings );
224 }
225 public function get_settings() {
226 $settings = BaseSettings::get_saved_data();
227 wp_send_json_success( $settings );
228 }
229
230 public function save_settings( $payload ) {
231 // phpcs:ignore WordPress.Security.NonceVerification.Missing
232 do_action( 'ablocks/before_save_settings', $payload, 'base' );
233 $json_payload = Sanitizer::sanitize_payload([
234 'selected_fonts' => 'json',
235 'global_color' => 'json',
236 'global_typography' => 'json',
237 'global_body_typography' => 'json',
238 'global_body_paragraph_space' => 'json',
239 'global_link_typography' => 'json',
240 'global_link_hover_typography' => 'json',
241 'global_h1_typography' => 'json',
242 'global_h2_typography' => 'json',
243 'global_h3_typography' => 'json',
244 'global_h4_typography' => 'json',
245 'global_h5_typography' => 'json',
246 'global_h6_typography' => 'json',
247 ], $_POST ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
248
249 $default = BaseSettings::get_default_data();
250 $is_update = BaseSettings::save_settings( [
251 'default_container_width' => $payload['default_container_width'] ?? $default['default_container_width'],
252 'container_padding' => $payload['container_padding'] ?? $default['container_padding'],
253 'container_element_gap' => $payload['container_element_gap'] ?? $default['container_element_gap'],
254 'enabled_assets_file_generation' => $payload['enabled_assets_file_generation'] ?? $default['enabled_assets_file_generation'],
255 'enabled_block_copy_paste_style' => $payload['enabled_block_copy_paste_style'] ?? $default['enabled_block_copy_paste_style'],
256 'enabled_load_google_font_locally' => $payload['enabled_load_google_font_locally'] ?? $default['enabled_load_google_font_locally'],
257 'enabled_only_selected_fonts' => $payload['enabled_only_selected_fonts'] ?? $default['enabled_only_selected_fonts'],
258 'selected_fonts' => $json_payload['selected_fonts'] ?? $default['selected_fonts'],
259 'enabled_coming_soon_page' => $payload['enabled_coming_soon_page'] ?? $default['enabled_coming_soon_page'],
260 'coming_soon_page' => $payload['coming_soon_page'] ?? $default['coming_soon_page'],
261 'enabled_maintenance_page' => $payload['enabled_maintenance_page'] ?? $default['enabled_maintenance_page'],
262 'maintenance_page' => $payload['maintenance_page'] ?? $default['maintenance_page'],
263 'frontend_dashboard_page' => $payload['frontend_dashboard_page'] ?? $default['frontend_dashboard_page'],
264 'login_page' => $payload['login_page'] ?? $default['login_page'],
265 'registration_page' => $payload['registration_page'] ?? $default['registration_page'],
266 'forget_password_page' => $payload['forget_password_page'] ?? $default['forget_password_page'],
267
268 'mailchimp_api_key' => $payload['mailchimp_api_key'] ?? '',
269 'drip_api_key' => $payload['drip_api_key'] ?? '',
270 'getresponse_api_key' => $payload['getresponse_api_key'] ?? '',
271 'convertkit_api_key' => $payload['convertkit_api_key'] ?? '',
272 'mailerlite_api_key' => $payload['mailerlite_api_key'] ?? '',
273 // Global Color & Typography
274 'global_color' => $json_payload['global_color'] ?? [],
275 'global_typography' => $json_payload['global_typography'] ?? [],
276 'global_font_family_fallback' => $payload['global_font_family_fallback'] ?? '',
277 'global_body_typography' => $json_payload['global_body_typography'] ?? [],
278 'global_body_paragraph_space' => $json_payload['global_body_paragraph_space'] ?? [],
279 'global_link_typography' => $json_payload['global_link_typography'] ?? [],
280 'global_link_hover_typography' => $json_payload['global_link_hover_typography'] ?? [],
281 'global_h1_typography' => $json_payload['global_h1_typography'] ?? [],
282 'global_h2_typography' => $json_payload['global_h2_typography'] ?? [],
283 'global_h3_typography' => $json_payload['global_h3_typography'] ?? [],
284 'global_h4_typography' => $json_payload['global_h4_typography'] ?? [],
285 'global_h5_typography' => $json_payload['global_h5_typography'] ?? [],
286 'global_h6_typography' => $json_payload['global_h6_typography'] ?? [],
287 'global_body_text_color' => $payload['global_body_text_color'] ?? '',
288 'global_link_color' => $payload['global_link_color'] ?? '',
289 'global_link_hover_color' => $payload['global_link_hover_color'] ?? '',
290 'global_h1_color' => $payload['global_h1_color'] ?? '',
291 'global_h2_color' => $payload['global_h2_color'] ?? '',
292 'global_h3_color' => $payload['global_h3_color'] ?? '',
293 'global_h4_color' => $payload['global_h4_color'] ?? '',
294 'global_h5_color' => $payload['global_h5_color'] ?? '',
295 'global_h6_color' => $payload['global_h6_color'] ?? '',
296 ]);
297 // phpcs:ignore WordPress.Security.NonceVerification.Missing
298 do_action( 'ablocks/after_save_settings', $is_update, 'base', $payload );
299 wp_send_json_success( $is_update );
300 }
301
302 public function fetch_posts( $payload ) {
303 $post_type = ( isset( $payload['postType'] ) ? $payload['postType'] : 'page' );
304 $postId = ( isset( $payload['postId'] ) ? $payload['postId'] : 0 );
305 $keyword = ( isset( $payload['keyword'] ) ? $payload['keyword'] : '' );
306
307 if ( $postId ) {
308 $args = array(
309 'post_type' => $post_type,
310 'p' => $postId,
311 );
312 } else {
313 $args = array(
314 'post_type' => $post_type,
315 'posts_per_page' => 10,
316 );
317 if ( ! empty( $keyword ) ) {
318 $args['s'] = $keyword;
319 }
320 if ( ! current_user_can( 'manage_options' ) ) {
321 $args['author'] = get_current_user_id();
322 }
323 }
324 $results = array();
325 $posts = get_posts( $args );
326 if ( is_array( $posts ) ) {
327 foreach ( $posts as $post ) {
328 $results[] = array(
329 'label' => $post->post_title,
330 'value' => $post->ID,
331 );
332 }
333 }
334 wp_send_json_success( $results );
335 }
336
337 public function get_fronted_dashboard_pages() {
338 $settings = json_decode( get_option( ABLOCKS_FRONTEND_DASHBOARD_SUB_PAGES_SETTINGS_NAME, '{}' ), true );
339 foreach ( $settings as &$item ) {
340 if ( isset( $item['parent_id'] ) ) {
341 $item['parent_id'] = 'null' === $item['parent_id'] ? null : $item['parent_id'];
342 }
343
344 $item['edit_link'] = (
345 'link' !== ( $item['type'] ?? '' ) &&
346 is_numeric( $item['page_id'] )
347 )
348 ? str_replace( '&amp;', '&', get_edit_post_link( $item['page_id'] ) )
349 : null;
350
351 if ( is_array( $item['children'] ?? null ) ) {
352 foreach ( $item['children'] as &$child_item ) {
353 if ( isset( $child_item['parent_id'] ) ) {
354 $child_item['parent_id'] = 'null' === $child_item['parent_id'] ? null : $child_item['parent_id'];
355
356 }
357
358 $child_item['edit_link'] = (
359 'link' !== ( $child_item['type'] ?? '' ) &&
360 is_numeric( $child_item['page_id'] )
361 )
362 ? str_replace( '&amp;', '&', get_edit_post_link( $child_item['page_id'] ) )
363 : null;
364 }
365 }
366 }//end foreach
367 wp_send_json_success( $settings );
368 }
369 public function create_fronted_dashboard_page( $payload ) {
370 global $wpdb;
371 // Step 1: Get Frontend Dashboard Page ID
372 $frontend_dashboard_id = (int) Helper::get_settings( 'frontend_dashboard_page' );
373 $settings = json_decode( get_option( ABLOCKS_FRONTEND_DASHBOARD_SUB_PAGES_SETTINGS_NAME, '{}' ), true );
374 $root_page_id = 0;
375 // Step 2: Check if "root" page exists under frontend dashboard
376 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
377 $root_page = $wpdb->get_row( $wpdb->prepare(
378 "SELECT ID, post_parent FROM $wpdb->posts WHERE post_parent=%d AND post_name = %s AND post_type = 'page'",
379 $frontend_dashboard_id,
380 'root'
381 ) );
382
383 $post_parent = ! empty( $payload['parent_id'] ) ? $payload['parent_id'] : $frontend_dashboard_id;
384
385 if ( ! $root_page || (int) $root_page->post_parent !== $frontend_dashboard_id ) {
386 // Step 3: Create "root" page as child of frontend dashboard page
387 $root_page_id = wp_insert_post( [
388 'post_title' => 'Root',
389 'post_name' => 'root',
390 'post_status' => 'publish',
391 'post_type' => 'page',
392 'post_content' => '',
393 'post_parent' => $frontend_dashboard_id,
394 ] );
395
396 $settings[] = [
397 'page_id' => $root_page_id,
398 'label' => 'Root',
399 'slug' => 'root',
400 'icon' => '',
401 'class_name' => '',
402 'priority' => 0,
403 ];
404 }
405
406 $insert_page_id = wp_insert_post( [
407 'post_title' => $payload['label'],
408 'post_name' => $payload['slug'],
409 'post_content' => '',
410 'post_status' => 'publish',
411 'post_type' => 'page',
412 'post_parent' => $post_parent,
413 ] );
414
415 if ( $insert_page_id ) {
416 $payload['page_id'] = $insert_page_id;
417
418 if ( ! empty( $payload['parent_id'] ) ) {
419 foreach ( $settings as &$setting ) {
420 if ( $setting['page_id'] === $post_parent ) {
421 if ( ! isset( $setting['children'] ) ) {
422 $setting['children'] = [];
423 }
424 $setting['children'][] = $payload;
425 }
426 }
427 unset( $setting ); // important to break reference
428 } else {
429 $settings[] = $payload;
430 }
431
432 update_option( ABLOCKS_FRONTEND_DASHBOARD_SUB_PAGES_SETTINGS_NAME, wp_json_encode( $settings ) );
433 flush_rewrite_rules();
434 }
435
436 wp_send_json_success( get_post( $insert_page_id ) );
437 }
438
439 public function create_fronted_dashboard_link( array $payload ) : void {
440 global $wpdb;
441
442 if ( empty( $payload['link'] ) || empty( $payload['label'] ) ) {
443 wp_send_json_error( __( 'Link/Label is required.', 'ablocks' ) );
444 }
445
446 $settings = json_decode(
447 get_option( ABLOCKS_FRONTEND_DASHBOARD_SUB_PAGES_SETTINGS_NAME, '{}' ),
448 true
449 );
450
451 $payload['type'] = 'link';
452 $parent_id = $payload['parent_id'] = $payload['parent_id'] ?? 0;
453 $payload['page_id'] = 'link_' . time();
454 // print_r($payload);
455 // var_dump(( $parent_id ));
456 if ( empty( $parent_id ) || 'null' === $parent_id ) {
457 $settings[] = $payload;
458 } elseif ( ! is_null(
459 $index = array_search(
460 $parent_id,
461 array_column( $settings, 'page_id' )
462 )
463 )
464 ) {
465 $settings[ $index ]['children'][] = $payload;
466 } else {
467 wp_send_json_error( __( 'Invalid parent id.', 'ablocks' ) );
468 }
469
470 update_option(
471 ABLOCKS_FRONTEND_DASHBOARD_SUB_PAGES_SETTINGS_NAME,
472 wp_json_encode( array_values( $settings ) )
473 );
474
475 wp_send_json_success( $payload );
476 }
477
478 public function edit_fronted_dashboard_link( array $payload ) : void {
479 global $wpdb;
480
481 if ( empty( $payload['page_id'] ) ||
482 empty( $payload['link'] ) ||
483 empty( $payload['label'] )
484 ) {
485 wp_send_json_error( __( 'Link/Label/ID is required.', 'ablocks' ) );
486 }
487
488 $settings = json_decode(
489 get_option( ABLOCKS_FRONTEND_DASHBOARD_SUB_PAGES_SETTINGS_NAME, '{}' ),
490 true
491 );
492
493 $page_id = $payload['page_id'] = $payload['page_id'] ?? 0;
494
495 foreach ( $settings as &$items ) {
496 // echo $items['page_id'],"\n";
497 // var_dump($page_id , $items['page_id']);
498 if ( $page_id === $items['page_id'] ) {
499 $items['label'] = $payload['label'] ?: $items['label'];
500 $items['link'] = $payload['link'] ?: $items['link'];
501 $items['class_name'] = $payload['class_name'] ?: $items['class_name'];
502 $items['icon'] = $payload['icon'] ?: $items['icon'];
503 break;
504 } elseif ( ! empty( $items['children'] ) ) {
505 foreach ( $items['children'] as &$c_items ) {
506 if ( $page_id === $c_items['page_id'] ) {
507 $c_items['label'] = $payload['label'] ?: $c_items['label'];
508 $c_items['link'] = $payload['link'] ?: $c_items['link'];
509 $c_items['class_name'] = $payload['class_name'] ?: $c_items['class_name'];
510 $c_items['icon'] = $payload['icon'] ?: $c_items['icon'];
511 break 2;
512 }
513 }
514 }
515 }//end foreach
516
517 update_option(
518 ABLOCKS_FRONTEND_DASHBOARD_SUB_PAGES_SETTINGS_NAME,
519 wp_json_encode( array_values( $settings ) )
520 );
521
522 wp_send_json_success( $payload );
523 }
524
525 public function delete_fronted_dashboard_link( array $payload ) : void {
526 global $wpdb;
527 // print_r($payload);
528 if ( empty( $payload['page_id'] )
529 ) {
530 wp_send_json_error( __( 'ID is required.', 'ablocks' ) );
531 }
532
533 $settings = json_decode(
534 get_option( ABLOCKS_FRONTEND_DASHBOARD_SUB_PAGES_SETTINGS_NAME, '{}' ),
535 true
536 );
537
538 $page_id = $payload['page_id'] = $payload['page_id'] ?? 0;
539
540 foreach ( $settings as &$items ) {
541 // echo $items['page_id'],"\n";
542 // var_dump($page_id , $items['page_id']);
543 if ( $page_id === $items['page_id'] ) {
544 $items = null;
545 break;
546 } elseif ( ! empty( $items['children'] ) ) {
547 foreach ( $items['children'] as &$c_items ) {
548 if ( $page_id === $c_items['page_id'] ) {
549 $c_items = null;
550 $items['children'] = array_filter( $items['children'] );
551 break 2;
552 }
553 }
554 }
555 }
556
557 update_option(
558 ABLOCKS_FRONTEND_DASHBOARD_SUB_PAGES_SETTINGS_NAME,
559 wp_json_encode( array_filter( $settings ) )
560 );
561
562 wp_send_json_success( $payload );
563 }
564
565 public function edit_fronted_dashboard_page( $payload ) {
566 if ( empty( $payload['page_id'] ) ) {
567 wp_send_json_error( 'Page ID is required for updating.' );
568 }
569
570 wp_update_post( [
571 'ID' => $payload['page_id'],
572 'post_title' => $payload['label'],
573 'post_slug' => $payload['slug'],
574 'priority' => $payload['priority'],
575 'post_parent' => ! empty( $payload['parent_id'] ) ? (int) $payload['parent_id'] : 0,
576 ], true );
577
578 $settings = json_decode( get_option( ABLOCKS_FRONTEND_DASHBOARD_SUB_PAGES_SETTINGS_NAME, '{}' ), true );
579
580 $update_settings_recursive = null; // initialize first
581 $update_settings_recursive = function ( &$settings ) use ( &$update_settings_recursive, $payload ) {
582 foreach ( $settings as &$setting ) {
583 if ( $setting['page_id'] === $payload['page_id'] ) {
584 $setting['label'] = $payload['label'] ?? $setting['label'];
585 $setting['slug'] = $payload['slug'] ?? $setting['slug'];
586 $setting['icon'] = $payload['icon'] ?? $setting['icon'];
587 $setting['class_name'] = $payload['class_name'] ?? $setting['class_name'];
588 $setting['priority'] = $payload['priority'] ?? ( $setting['priority'] ?? 0 );
589 if ( isset( $payload['slug'] ) ||
590 isset( $payload['label'] )
591 ) {
592 wp_update_post( [
593 'ID' => $payload['page_id'],
594 'post_title' => $payload['label'] ?? $setting['label'],
595 'post_name' => $payload['slug'] ?? $setting['slug'],
596 ] );
597 }
598 if ( isset( $payload['children'] ) ) {
599 $setting['children'] = $payload['children'];
600 }
601 return true;
602 }//end if
603
604 if ( ! empty( $setting['children'] ) ) {
605 if ( $update_settings_recursive( $setting['children'] ) ) {
606 return true;
607 }
608 }
609 }//end foreach
610 return false;
611 };
612
613 $update_settings_recursive( $settings );
614 update_option( ABLOCKS_FRONTEND_DASHBOARD_SUB_PAGES_SETTINGS_NAME, wp_json_encode( $settings ) );
615
616 flush_rewrite_rules();
617 // Step 4: Return updated page
618 wp_send_json_success( get_post( $payload['page_id'] ) );
619 }
620
621 public function delete_fronted_dashboard_page( $payload ) {
622 // Load existing frontend dashboard sub-pages settings
623 $settings = json_decode( get_option( ABLOCKS_FRONTEND_DASHBOARD_SUB_PAGES_SETTINGS_NAME, '{}' ), true );
624 if ( ! is_array( $settings ) ) {
625 $settings = [];
626 }
627
628 $remove_id = $payload['page_id'] ?? null;
629 $remove_slug = $payload['slug'] ?? null;
630
631 if ( ! $remove_id && ! $remove_slug ) {
632 wp_send_json_error( 'Page ID or slug is required to delete.' );
633 }
634
635 // Step 1: Delete the post if ID is provided
636 if ( $remove_id ) {
637 wp_delete_post( (int) $remove_id, true );
638 } elseif ( $remove_slug ) {
639 // If only slug is provided, try to find the page
640 $page = get_page_by_path( $remove_slug, OBJECT, 'page' );
641 if ( $page ) {
642 $remove_id = $page->ID;
643 wp_delete_post( $remove_id, true );
644 }
645 }
646
647 // Step 2: Recursively remove from settings
648 $remove_from_settings = function ( &$settings ) use ( &$remove_from_settings, $remove_id, $remove_slug ) {
649 $settings = array_values(array_filter($settings, function ( $item ) use ( $remove_id, $remove_slug ) {
650 $match_id = isset( $item['page_id'] ) && $remove_id && $item['page_id'] == $remove_id;
651 $match_slug = isset( $item['slug'] ) && $remove_slug && $item['slug'] == $remove_slug;
652 return ! $match_id && ! $match_slug;
653 }));
654
655 // Recurse into children
656 foreach ( $settings as &$item ) {
657 if ( ! empty( $item['children'] ) ) {
658 $remove_from_settings( $item['children'] );
659 }
660 }
661 unset( $item );
662 };
663
664 $remove_from_settings( $settings );
665
666 // Step 3: Save updated settings
667 update_option( ABLOCKS_FRONTEND_DASHBOARD_SUB_PAGES_SETTINGS_NAME, wp_json_encode( $settings ) );
668
669 wp_send_json_success( $remove_id ?? $remove_slug );
670 }
671
672 public function move_fronted_dashboard_page( array $payload ) : void {
673 $reordered_settings = json_decode( $payload['reordered_items'] ?? '{}', true );
674 $settings = json_decode(
675 get_option(
676 ABLOCKS_FRONTEND_DASHBOARD_SUB_PAGES_SETTINGS_NAME, '{}'
677 ),
678 true
679 );
680
681 if ( is_array( $reordered_settings ) &&
682 count( $reordered_settings ) === count( $settings )
683 ) {
684 update_option(
685 ABLOCKS_FRONTEND_DASHBOARD_SUB_PAGES_SETTINGS_NAME,
686 $payload['reordered_items'] ?? '{}'
687 );
688 wp_send_json_success( __( 'Success!', 'ablocks' ) );
689 }
690 wp_send_json_error( __( 'Invalid data', 'ablocks' ), 400 );
691 }
692 }
693