Plugin Directory

source: wp-recall/tags/16.26.14/admin/index.php

Last change on this file was 3256922, checked in by wppost, 8 months ago

16.26.14

File size: 23.0 KB
Line 
1<?php
2require_once "admin-menu.php";
3require_once "metaboxes.php";
4
5add_action( 'admin_init', 'rcl_admin_scripts', 10 );
6
7add_filter( 'display_post_states', 'rcl_mark_own_page', 10, 2 );
8function rcl_mark_own_page( $post_states, $post ) {
9
10        if ( $post->post_type === 'page' ) {
11
12                $plugin_pages = get_site_option( 'rcl_plugin_pages' );
13
14                if ( ! $plugin_pages ) {
15                        return $post_states;
16                }
17
18                if ( in_array( $post->ID, $plugin_pages ) ) {
19                        $post_states[] = __( 'The page of plugin WP-Recall' );
20                }
21        }
22
23        return $post_states;
24}
25
26add_filter( 'rcl_field_options', 'rcl_edit_field_options', 10, 3 );
27function rcl_edit_field_options( $options, $field, $manager_id ) {
28
29        $types = [ 'range', 'runner' ];
30
31        if ( in_array( $field->type, $types ) ) {
32
33                foreach ( $options as $k => $option ) {
34
35                        if ( $option['slug'] == 'required' ) {
36                                unset( $options[ $k ] );
37                        }
38                }
39        }
40
41        return $options;
42}
43
44function rmag_global_options() {
45
46        require_once RCL_PATH . 'admin/classes/class-rcl-options-manager.php';
47
48        $Manager = new Rcl_Options_Manager( [
49                'option_name'  => 'primary-rmag-options',
50                'page_options' => 'manage-wpm-options',
51        ] );
52
53        $Manager = apply_filters( 'rcl_commerce_options', $Manager );
54
55        $content = '<h2>' . esc_html__( 'Settings of commerce', 'wp-recall' ) . '</h2>';
56
57        $content .= $Manager->get_content();
58
59        echo wp_kses( $content, rcl_kses_allowed_html() );
60}
61
62function rmag_update_options() {
63        if ( current_user_can( 'administrator' ) && isset( $_POST['primary-rmag-options'], $_POST['_wpnonce'] ) ) {
64                if ( ! wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), 'update-options-rmag' ) ) {
65                        return false;
66                }
67
68                if ( isset( $_POST['global'] ) ) {
69                        //phpcs:ignore
70                        foreach ( $_POST['global'] as $key => $value ) {
71                                if ( $key == 'primary-rmag-options' ) {
72                                        continue;
73                                }
74                                //TODO sanitize options
75                                $options[ sanitize_key( $key ) ] = $value;
76                        }
77
78                        update_site_option( 'primary-rmag-options', $options );
79                }
80
81                if ( isset( $_POST['local'] ) ) {
82                        //phpcs:ignore
83                        foreach ( ( array ) $_POST['local'] as $key => $value ) {
84                                //TODO sanitize options
85                                update_site_option( sanitize_key( $key ), $value );
86                        }
87                }
88
89                wp_safe_redirect( admin_url( 'admin.php?page=manage-wpm-options' ) );
90                exit;
91        }
92}
93
94add_action( 'init', 'rmag_update_options' );
95function rcl_wp_list_current_action() {
96        if ( isset( $_REQUEST['filter_action'] ) && ! empty( $_REQUEST['filter_action'] ) ) {
97                return false;
98        }
99
100        if ( isset( $_REQUEST['action'] ) && - 1 != $_REQUEST['action'] ) {
101                return sanitize_text_field( wp_unslash( $_REQUEST['action'] ) );
102        }
103
104        if ( isset( $_REQUEST['action2'] ) && - 1 != $_REQUEST['action2'] ) {
105                return sanitize_text_field( wp_unslash( $_REQUEST['action2'] ) );
106        }
107
108        return false;
109}
110
111if ( is_admin() ) {
112        add_action( 'admin_init', 'rcl_postmeta_post' );
113}
114function rcl_postmeta_post() {
115        add_meta_box( 'recall_meta', __( 'WP-Recall settings', 'wp-recall' ), 'rcl_options_box', 'post', 'normal', 'high' );
116        add_meta_box( 'recall_meta', __( 'WP-Recall settings', 'wp-recall' ), 'rcl_options_box', 'page', 'normal', 'high' );
117}
118
119function rcl_options_box( $post ) {
120        //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
121        echo apply_filters( 'rcl_post_options', '', $post );
122        ?>
123    <input type="hidden" name="rcl_fields_nonce" value="<?php echo esc_attr( wp_create_nonce( __FILE__ ) ); ?>"/>
124        <?php
125}
126
127function rcl_postmeta_update( $post_id ) {
128        if ( ! isset( $_POST['rcl_fields_nonce'] ) ) {
129                return false;
130        }
131        if ( ! wp_verify_nonce( sanitize_key( $_POST['rcl_fields_nonce'] ), __FILE__ ) ) {
132                return false;
133        }
134        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
135                return false;
136        }
137        if ( ! current_user_can( 'edit_post', $post_id ) ) {
138                return false;
139        }
140
141        if ( ! isset( $_POST['wprecall'] ) ) {
142                return false;
143        }
144        //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
145        $POST = rcl_recursive_map( 'sanitize_text_field', wp_unslash( $_POST['wprecall'] ) );
146
147        foreach ( $POST as $key => $value ) {
148                if ( ! is_array( $value ) ) {
149                        $value = trim( $value );
150                }
151                if ( $value == '' ) {
152                        delete_post_meta( $post_id, $key );
153                } else {
154                        update_post_meta( $post_id, $key, $value );
155                }
156        }
157
158        return $post_id;
159}
160
161rcl_ajax_action( 'rcl_update_options', false );
162function rcl_update_options() {
163
164        if ( ! current_user_can( 'administrator' ) ) {
165                wp_send_json( [ 'error' => esc_html__( 'Error', 'wp-recall' ) ] );
166        }
167
168        rcl_verify_ajax_nonce();
169
170        $POST = $_POST;
171
172        array_walk_recursive(
173                $POST, function ( &$v, $k ) {
174                //TODO sanitize options
175                $v = trim( $v );
176        } );
177
178        foreach ( $POST as $option_name => $values ) {
179
180                if ( ! is_array( $values ) ) {
181                        continue;
182                }
183
184                $values = apply_filters( $option_name . '_pre_update', $values );
185
186                if ( $option_name == 'local' ) {
187
188                        foreach ( $values as $local_name => $value ) {
189                                update_site_option( $local_name, $value );
190                        }
191                } else {
192                        update_site_option( $option_name, $values );
193                }
194        }
195
196        do_action( 'rcl_update_options' );
197
198        wp_send_json( [
199                'success' => esc_html__( 'Settings saved!', 'wp-recall' ),
200        ] );
201}
202
203add_action( 'rcl_update_options', 'rcl_delete_temp_default_avatar_cover', 10 );
204function rcl_delete_temp_default_avatar_cover() {
205
206        if ( isset( $_POST['rcl_global_options']['default_avatar'] ) ) {
207                rcl_delete_temp_media( intval( $_POST['rcl_global_options']['default_avatar'] ) );
208        }
209
210        if ( isset( $_POST['rcl_global_options']['default_cover'] ) ) {
211                rcl_delete_temp_media( intval( $_POST['rcl_global_options']['default_cover'] ) );
212        }
213}
214
215function rcl_add_cover_options( $options ) {
216
217        $options->box( 'primary' )->group( 'design' )->add_options( [
218                [
219                        'type'      => 'radio',
220                        'slug'      => 'rcl_hide_cover',
221                        'title'     => __( 'Disable cover uploader in personal account?', 'wp-recall' ),
222                        'values'    => [ __( 'No', 'wp-recall' ), __( 'Yes', 'wp-recall' ) ],
223                        'default'   => 0,
224                        'childrens' => [
225                                0 => [
226                                        [
227                                                'type'       => 'uploader',
228                                                'temp_media' => 1,
229                                                'max_size'   => 5120,
230                                                'multiple'   => 0,
231                                                'crop'       => [ 'ratio' => 0 ],
232                                                'filetitle'  => 'rcl-default-cover',
233                                                'filename'   => 'rcl-default-cover',
234                                                'slug'       => 'default_cover',
235                                                'title'      => __( 'Default cover', 'wp-recall' ),
236                                        ],
237                                ],
238                        ],
239                ],
240        ] );
241
242        return $options;
243}
244
245function wp_enqueue_theme_rcl( $url ) {
246        wp_enqueue_style( 'theme_rcl', $url );
247}
248
249/* 16.0.0 */
250add_action( 'admin_init', 'rcl_update_custom_fields', 10 );
251function rcl_update_custom_fields() {
252        global $wpdb;
253
254        if ( ! current_user_can( 'administrator' ) ) {
255                return false;
256        }
257
258        if ( ! isset( $_POST['rcl_save_custom_fields'] ) || ! isset( $_POST['_wpnonce'] ) ) {
259                return false;
260        }
261
262        if ( ! wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), 'rcl-update-custom-fields' ) ) {
263                return false;
264        }
265
266        $fields = [];
267
268        $table = 'postmeta';
269
270        if ( isset( $_POST['rcl-fields-options']['name-option'] ) && $_POST['rcl-fields-options']['name-option'] == 'rcl_profile_fields' ) {
271                $table = 'usermeta';
272        }
273
274        $POSTDATA = apply_filters( 'rcl_pre_update_custom_fields_options', $_POST );
275
276        if ( ! $POSTDATA ) {
277                return false;
278        }
279
280        if ( isset( $POSTDATA['rcl_deleted_custom_fields'] ) ) {
281
282                $deleted = explode( ',', $POSTDATA['rcl_deleted_custom_fields'] );
283
284                if ( $deleted ) {
285
286                        foreach ( $deleted as $slug ) {
287                                $wpdb->query( $wpdb->prepare( "DELETE FROM " . $wpdb->$table . " WHERE meta_key = %s", $slug ) );
288                        }
289                }
290        }
291
292        $newFields = [];
293
294        if ( isset( $POSTDATA['new-field'] ) ) {
295
296                $nKey = 0;
297
298                foreach ( $POSTDATA['new-field'] as $optionSlug => $vals ) {
299                        $newFields[ $nKey ] = $vals;
300                        $nKey ++;
301                }
302        }
303
304        $fields = [];
305        $nKey   = 0;
306
307        foreach ( $POSTDATA['fields'] as $k => $slug ) {
308
309                if ( ! $slug ) {
310
311                        if ( ! isset( $newFields[ $nKey ] ) || ! $newFields[ $nKey ]['title'] ) {
312                                continue;
313                        }
314
315                        if ( isset( $newFields[ $nKey ]['slug'] ) && $newFields[ $nKey ]['slug'] ) {
316                                $slug = $newFields[ $nKey ]['slug'];
317                        } else {
318                                $slug = str_replace( [
319                                        '-',
320                                        ' ',
321                                ], '_', rcl_sanitize_string( $newFields[ $nKey ]['title'] ) . '-' . uniqid() );
322                        }
323
324                        $field = $newFields[ $nKey ];
325
326                        $nKey ++;
327                } else {
328
329                        if ( ! isset( $POSTDATA['field'][ $slug ] ) ) {
330                                continue;
331                        }
332
333                        $field = $POSTDATA['field'][ $slug ];
334                }
335
336                $field['slug'] = $slug;
337
338                $fields[] = $field;
339        }
340
341        foreach ( $fields as $k => $field ) {
342
343                if ( isset( $field['values'] ) && $field['values'] && is_array( $field['values'] ) ) {
344
345                        $values = [];
346                        foreach ( $field['values'] as $val ) {
347                                if ( $val == '' ) {
348                                        continue;
349                                }
350                                $values[] = $val;
351                        }
352
353                        $fields[ $k ]['values'] = $values;
354                }
355        }
356
357        if ( isset( $POSTDATA['options'] ) ) {
358                $fields['options'] = $POSTDATA['options'];
359        }
360
361        update_site_option( sanitize_key( $_POST['rcl-fields-options']['name-option'] ), $fields );
362
363        do_action( 'rcl_update_custom_fields', $fields, $POSTDATA );
364
365        if ( isset( $_POST['_wp_http_referer'] ) ) {
366                wp_safe_redirect( wp_unslash( $_POST['_wp_http_referer'] ) );
367        }
368        exit;
369}
370
371rcl_ajax_action( 'rcl_get_new_custom_field', false );
372function rcl_get_new_custom_field() {
373
374        $post_type = isset( $_POST['post_type'] ) ? sanitize_key( $_POST['post_type'] ) : '';
375        //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
376        $primary = isset( $_POST['primary_options'] ) ? rcl_recursive_map( 'sanitize_text_field', (array) json_decode( wp_unslash( $_POST['primary_options'] ) ) ) : [];
377        //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
378        $default = isset( $_POST['default_options'] ) ? rcl_recursive_map( 'sanitize_text_field', (array) json_decode( wp_unslash( $_POST['default_options'] ) ) ) : [];
379
380
381        $manageFields = new Rcl_Custom_Fields_Manager( $post_type, $primary );
382
383        if ( $default ) {
384
385                $manageFields->defaultOptions = [];
386
387                foreach ( $default as $option ) {
388                        $manageFields->defaultOptions[] = ( array ) $option;
389                }
390        }
391
392        $content = $manageFields->empty_field();
393
394        wp_send_json( [
395                'content' => $content,
396        ] );
397}
398
399rcl_ajax_action( 'rcl_get_custom_field_options', false );
400function rcl_get_custom_field_options() {
401
402        $type_field = isset( $_POST['type_field'] ) ? sanitize_key( $_POST['type_field'] ) : '';
403        $old_type   = isset( $_POST['old_type'] ) ? sanitize_key( $_POST['old_type'] ) : '';
404        $post_type  = isset( $_POST['post_type'] ) ? sanitize_key( $_POST['post_type'] ) : '';
405        $slug_field = isset( $_POST['slug_field'] ) ? sanitize_key( $_POST['slug_field'] ) : '';
406        //phpcs:disable
407        $primary = isset( $_POST['primary_options'] ) ? ( array ) json_decode( wp_unslash( $_POST['primary_options'] ) ) : [];
408        $default = isset( $_POST['default_options'] ) ? ( array ) json_decode( wp_unslash( $_POST['default_options'] ) ) : [];
409        //phpcs:enable
410        $manageFields = new Rcl_Custom_Fields_Manager( $post_type, $primary );
411
412        if ( $default ) {
413
414                $manageFields->defaultOptions = [];
415
416                foreach ( $default as $option ) {
417                        $manageFields->defaultOptions[] = ( array ) $option;
418                }
419        }
420
421        $manageFields->field = [ 'type' => $type_field ];
422
423        if ( strpos( $slug_field, 'CreateNewField' ) === false ) {
424
425                $manageFields->field['slug'] = $slug_field;
426        } else {
427
428                $manageFields->field['slug'] = '';
429                $manageFields->new_slug      = $slug_field;
430        }
431
432        $content = $manageFields->get_options();
433
434        $multiVars = [
435                'select',
436                'radio',
437                'checkbox',
438                'multiselect',
439        ];
440
441        if ( in_array( $type_field, $multiVars ) ) {
442
443                $content .= '<script>'
444                            . "jQuery('#field-" . $slug_field . " .rcl-field-input .dynamic-values').sortable({
445             containment: 'parent',
446             placeholder: 'ui-sortable-placeholder',
447             distance: 15,
448             stop: function( event, ui ) {
449                 var items = ui.item.parents('.dynamic-values').find('.dynamic-value');
450                 items.each(function(f){
451                     if(items.length == (f+1)){
452                         jQuery(this).children('a').attr('onclick','rcl_add_dynamic_field(this);return false;').children('i').attr('class','fa-plus');
453                     }else{
454                         jQuery(this).children('a').attr('onclick','rcl_remove_dynamic_field(this);return false;').children('i').attr('class','fa-minus');
455                     }
456                 });
457
458             }
459         });"
460                            . '</script>';
461        }
462
463        wp_send_json( [
464                'content' => $content,
465        ] );
466}
467
468add_filter( 'admin_footer_text', 'rcl_admin_footer_text', 10 );
469function rcl_admin_footer_text( $footer_text ) {
470        $current_screen = get_current_screen();
471
472        $dlm_page_ids = [
473                'toplevel_page_manage-wprecall',
474                'wp-recall_page_rcl-options',
475                'wp-recall_page_rcl-repository',
476                'wp-recall_page_manage-addon-recall',
477                'wp-recall_page_manage-templates-recall',
478                'wp-recall_page_rcl-tabs-manager',
479                'wp-recall_page_manage-userfield',
480                'wp-recall_page_manage-public-form',
481        ];
482
483        if ( isset( $current_screen->id ) && in_array( $current_screen->id, $dlm_page_ids ) ) {
484                $footer_text = sprintf( __( 'If you liked plugin %sWP-Recall%s, please vote for it in repository %s★★★★★%s. Thank you so much!', 'wp-recall' ), '<strong>', '</strong>', '<a href="https://wordpress.org/support/view/plugin-reviews/wp-recall?filter=5#new-post" target="_blank">', '</a>' );
485        }
486
487        return $footer_text;
488}
489
490function rcl_send_addon_activation_notice( $addon_id, $addon_headers ) {
491        wp_remote_post( RCL_SERVICE_HOST . '/products-files/api/add-ons.php?rcl-addon-info=add-notice', [
492                        'body' => [
493                                'rcl-key'  => get_site_option( 'rcl-key' ),
494                                'addon-id' => $addon_id,
495                                'headers'  => [
496                                        'version' => $addon_headers['version'],
497                                        'item-id' => $addon_headers['item-id'],
498                                        'key-id'  => $addon_headers['key-id'],
499                                ],
500                                'host'     => ( isset( $_SERVER['SERVER_NAME'] ) ? sanitize_text_field( wp_unslash( $_SERVER['SERVER_NAME'] ) ) : '' ),
501                        ],
502                ]
503        );
504}
505
506/* new fields manager functions */
507
508rcl_ajax_action( 'rcl_manager_get_new_field', false );
509function rcl_manager_get_new_field() {
510        if ( ! current_user_can( 'administrator' ) ) {
511                wp_send_json( [
512                        'error' => esc_html__( 'Error', 'wp-recall' ),
513                ] );
514        }
515        //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
516        $managerProps = isset( $_POST['props'] ) ? rcl_recursive_map( 'sanitize_text_field', wp_unslash( $_POST['props'] ) ) : [];
517
518        $Manager = new Rcl_Fields_Manager( $managerProps['manager_id'], $managerProps );
519
520        $field_id = 'newField-' . uniqid();
521
522        $Manager->add_field( [
523                'slug' => $field_id,
524                'type' => $Manager->types[0],
525                '_new' => true,
526        ] );
527
528        wp_send_json( [
529                'content' => $Manager->get_field_manager( $field_id ),
530        ] );
531}
532
533rcl_ajax_action( 'rcl_manager_get_custom_field_options', false );
534function rcl_manager_get_custom_field_options() {
535
536        if ( ! current_user_can( 'administrator' ) ) {
537                wp_send_json( [
538                        'error' => esc_html__( 'Error', 'wp-recall' ),
539                ] );
540        }
541
542        $new_type = isset( $_POST['newType'] ) ? sanitize_key( $_POST['newType'] ) : '';
543        $field_id = isset( $_POST['fieldId'] ) ? sanitize_text_field( wp_unslash( $_POST['fieldId'] ) ) : '';
544        //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
545        $managerProps = isset( $_POST['manager'] ) ? rcl_recursive_map( 'sanitize_text_field', wp_unslash( $_POST['manager'] ) ) : [];
546
547        $Manager = new Rcl_Fields_Manager( $managerProps['manager_id'], $managerProps );
548
549        if ( stristr( $field_id, 'newField' ) !== false ) {
550
551                $Manager->add_field( [
552                        'slug' => $field_id,
553                        'type' => $new_type,
554                        '_new' => true,
555                ] );
556        } else {
557
558                $Manager->set_field_prop( $field_id, 'type', $new_type );
559
560                $Manager->fields[ $field_id ] = $Manager::setup( ( array ) $Manager->fields[ $field_id ] );
561        }
562
563        $content = $Manager->get_field_options_content( $field_id );
564
565        $multiVars = [
566                'select',
567                'radio',
568                'checkbox',
569                'multiselect',
570        ];
571
572        if ( in_array( $new_type, $multiVars ) ) {
573
574                $content .= $Manager->sortable_dynamic_values_script( $field_id );
575        }
576
577        wp_send_json( [
578                'content' => $content,
579        ] );
580}
581
582rcl_ajax_action( 'rcl_manager_get_new_area', false );
583function rcl_manager_get_new_area() {
584
585        if ( ! current_user_can( 'administrator' ) ) {
586                wp_send_json( [
587                        'error' => esc_html__( 'Error', 'wp-recall' ),
588                ] );
589        }
590
591        //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
592        $managerProps = isset( $_POST['props'] ) ? rcl_recursive_map( 'sanitize_text_field', wp_unslash( $_POST['props'] ) ) : [];
593
594        $Manager = new Rcl_Fields_Manager( 'any', $managerProps );
595
596        wp_send_json( [
597                'content' => $Manager->get_active_area(),
598        ] );
599}
600
601rcl_ajax_action( 'rcl_manager_get_new_group', false );
602function rcl_manager_get_new_group() {
603
604        if ( ! current_user_can( 'administrator' ) ) {
605                wp_send_json( [
606                        'error' => esc_html__( 'Error', 'wp-recall' ),
607                ] );
608        }
609
610        //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
611        $managerProps = isset( $_POST['props'] ) ? rcl_recursive_map( 'sanitize_text_field', wp_unslash( $_POST['props'] ) ) : [];
612
613        $Manager = new Rcl_Fields_Manager( 'any', $managerProps );
614
615        wp_send_json( [
616                'content' => $Manager->get_group_areas(),
617        ] );
618}
619
620rcl_ajax_action( 'rcl_manager_update_fields_by_ajax', false );
621function rcl_manager_update_fields_by_ajax() {
622
623        if ( ! current_user_can( 'administrator' ) ) {
624                wp_send_json( [ 'error' => __( 'Error', 'wp-recall' ) ] );
625        }
626
627        rcl_verify_ajax_nonce();
628
629        $args = rcl_manager_update_data_fields();
630
631        wp_send_json( $args );
632}
633
634add_action( 'admin_init', 'rcl_manager_update_fields_by_post', 10 );
635function rcl_manager_update_fields_by_post() {
636        global $wpdb;
637
638        if ( ! isset( $_POST['rcl_manager_update_fields_by_post'] ) || ! isset( $_POST['_wpnonce'] ) ) {
639                return false;
640        }
641
642        if ( ! current_user_can( 'administrator' ) ) {
643                return false;
644        }
645
646        if ( ! wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), 'rcl-update-custom-fields' ) ) {
647                return false;
648        }
649
650        rcl_manager_update_data_fields();
651        if ( isset( $_POST['_wp_http_referer'] ) ) {
652                wp_safe_redirect( wp_unslash( $_POST['_wp_http_referer'] ) );
653        }
654        exit;
655}
656
657function rcl_manager_update_data_fields() {
658        global $wpdb;
659
660        $copy        = isset( $_POST['copy'] ) ? sanitize_text_field( wp_unslash( $_POST['copy'] ) ) : '';
661        $manager_id  = isset( $_POST['manager_id'] ) ? sanitize_key( $_POST['manager_id'] ) : '';
662        $option_name = isset( $_POST['option_name'] ) ? sanitize_key( $_POST['option_name'] ) : '';
663        //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
664        $fieldsData = isset( $_POST['fields'] ) ? wp_unslash( $_POST['fields'] ) : [];
665        //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
666        $structure = isset( $_POST['structure'] ) ? rcl_recursive_map( 'sanitize_text_field', wp_unslash( $_POST['structure'] ) ) : false;
667
668        $fields    = [];
669        $keyFields = [];
670        $changeIds = [];
671        $isset_new = false;
672
673        foreach ( $fieldsData as $field_id => $field ) {
674
675                if ( ! $field['title'] ) {
676                        continue;
677                }
678
679                if ( isset( $field['values'] ) ) {
680                        //удаляем из массива values пустые значения
681                        $values = [];
682                        foreach ( $field['values'] as $k => $v ) {
683                                if ( $v == '' ) {
684                                        continue;
685                                }
686                                $values[ $k ] = $v;
687                        }
688                        $field['values'] = $values;
689                }
690
691                if ( stristr( $field_id, 'newField' ) !== false ) {
692
693                        $isset_new = true;
694
695                        $old_id = $field_id;
696
697                        if ( ! $field['id'] ) {
698
699                                $field_id = str_replace( [
700                                        '-',
701                                        ' ',
702                                ], '_', rcl_sanitize_string( $field['title'] ) . '-' . uniqid() );
703                        } else {
704                                $field_id = $field['id'];
705                        }
706
707                        $changeIds[ $old_id ] = $field_id;
708                }
709
710                $field['slug'] = $field_id;
711
712                $keyFields[ $field_id ] = 1;
713
714                unset( $field['id'] );
715
716                $fields[] = $field;
717        }
718
719        if ( $structure ) {
720
721                $strArray = [];
722                $area_id  = - 1;
723                $group_id = 0;
724                foreach ( $structure as $value ) {
725
726                        if ( is_array( $value ) ) {
727
728                                if ( isset( $value['group_id'] ) ) {
729                                        $group_id = $value['group_id'];
730                                        //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
731                                        $strArray[ $group_id ] = isset( $_POST['structure-groups'][ $group_id ] ) ? rcl_recursive_map( 'sanitize_text_field', wp_unslash( $_POST['structure-groups'][ $group_id ] ) ) : [];
732                                } else if ( isset( $value['field_id'] ) ) {
733                                        $strArray[ $group_id ]['areas'][ $area_id ]['fields'][] = $value['field_id'];
734                                }
735                        } else {
736                                $area_id ++;
737                                $strArray[ $group_id ]['areas'][ $area_id ]['width'] = isset( $_POST['structure-areas'][ $area_id ]['width'] ) ? intval( $_POST['structure-areas'][ $area_id ]['width'] ) : 0;
738                        }
739                }
740
741                $endStructure = [];
742
743                foreach ( $strArray as $group_id => $group ) {
744
745                        if ( isset( $group['id'] ) && $group_id != $group['id'] ) {
746                                $group_id = $group['id'];
747                        }
748
749                        $endStructure[ $group_id ]          = $group;
750                        $endStructure[ $group_id ]['areas'] = [];
751
752                        foreach ( $group['areas'] as $area ) {
753
754                                $fieldsArea = [];
755
756                                if ( ! empty( $area['fields'] ) ) {
757                                        foreach ( $area['fields'] as $k => $field_id ) {
758
759                                                if ( isset( $changeIds[ $field_id ] ) ) {
760                                                        $field_id = $changeIds[ $field_id ];
761                                                }
762
763                                                if ( ! isset( $keyFields[ $field_id ] ) ) {
764                                                        unset( $area['fields'][ $k ] );
765                                                        continue;
766                                                }
767
768                                                $fieldsArea[] = $field_id;
769                                        }
770                                }
771
772                                $endStructure[ $group_id ]['areas'][] = [
773                                        'width'  => round( $area['width'], 0 ),
774                                        'fields' => $fieldsArea,
775                                ];
776                        }
777                }
778
779                $structure = $endStructure;
780        }
781
782        $fields = apply_filters( 'rcl_pre_update_manager_fields', $fields, $manager_id );
783
784        update_site_option( $option_name, $fields );
785
786        $args = [
787                'success' => esc_html__( 'Settings saved!', 'wp-recall' ),
788        ];
789
790        if ( $structure ) {
791                update_site_option( 'rcl_fields_' . $manager_id . '_structure', $structure );
792        } else {
793                delete_site_option( 'rcl_fields_' . $manager_id . '_structure' );
794        }
795
796        if ( ! empty( $_POST['deleted_fields'] ) && isset( $_POST['delete_table_data'] ) ) {
797                //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
798                $deleteFields = rcl_recursive_map( 'sanitize_text_field', wp_unslash( $_POST['delete_table_data'] ) );
799                if ( ! empty( $deleteFields ) ) {
800                        foreach ( $deleteFields as $table_name => $colname ) {
801                                //phpcs:disable
802                                $fields_to_delete = rcl_recursive_map( 'sanitize_text_field', wp_unslash( $_POST['deleted_fields'] ) );
803                                $wpdb->query( "DELETE FROM $table_name WHERE $colname IN ('" . implode( "','", $fields_to_delete ) . "')" );
804                                //phpcs:enable
805                        }
806
807                        $args['reload'] = true;
808                }
809        }
810
811        if ( $copy ) {
812
813                update_site_option( 'rcl_fields_' . $copy, $fields );
814
815                if ( $structure ) {
816                        update_site_option( 'rcl_fields_' . $copy . '_structure', $structure );
817                }
818
819                do_action( 'rcl_fields_copy', $fields, $manager_id, $copy );
820
821                $args['reload'] = true;
822        }
823
824        if ( $isset_new ) {
825                $args['reload'] = true;
826        }
827
828        do_action( 'rcl_fields_update', $fields, $manager_id );
829
830        return $args;
831}
832
833/* new fields manager functions end */
834
Note: See TracBrowser for help on using the repository browser.