class-admin-dragdrop.php
7.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
namespace um\admin\core;
if ( ! defined( 'ABSPATH' ) ) exit;
if ( ! class_exists( 'um\admin\core\Admin_DragDrop' ) ) {
/**
* Class Admin_DragDrop
* @package um\admin\core
*/
class Admin_DragDrop {
/**
* Admin_DragDrop constructor.
*/
function __construct() {
add_action( 'admin_footer', array( &$this, 'load_field_order' ), 9 );
}
/**
* Update order of fields
*/
public function update_order() {
UM()->admin()->check_ajax_nonce();
if ( ! is_user_logged_in() || ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( __( 'Please login as administrator', 'ultimate-member' ) );
}
/**
* @var $form_id
*/
extract( $_POST );
if ( isset( $form_id ) ) {
$form_id = absint( $form_id );
}
$fields = UM()->query()->get_attr( 'custom_fields', $form_id );
$this->row_data = get_option( 'um_form_rowdata_' . $form_id, array() );
$this->exist_rows = array();
if ( ! empty( $fields ) ) {
foreach ( $fields as $key => $array ) {
if ( 'row' === $array['type'] ) {
$this->row_data[ $key ] = $array;
unset( $fields[ $key ] );
}
}
} else {
$fields = array();
}
foreach ( $_POST as $key => $value ) {
// don't use sanitize_key here because of a key can be in Uppercase
$key = sanitize_text_field( $key );
// adding rows
if ( 0 === strpos( $key, '_um_row_' ) ) {
$update_args = null;
$row_id = str_replace( '_um_row_', '', $key );
if ( strstr( $_POST[ '_um_rowcols_' . $row_id . '_cols' ], ':' ) ) {
$cols = sanitize_text_field( $_POST[ '_um_rowcols_' . $row_id . '_cols' ] );
} else {
$cols = absint( $_POST[ '_um_rowcols_' . $row_id . '_cols' ] );
}
$row_array = array(
'type' => 'row',
'id' => sanitize_key( $value ),
'sub_rows' => absint( $_POST[ '_um_rowsub_' . $row_id . '_rows' ] ),
'cols' => $cols,
'origin' => sanitize_key( $_POST[ '_um_roworigin_' . $row_id . '_val' ] ),
);
$row_args = $row_array;
if ( isset( $this->row_data[ $row_array['origin'] ] ) ) {
foreach ( $this->row_data[ $row_array['origin'] ] as $k => $v ) {
if ( 'position' !== $k && 'metakey' !== $k ) {
$update_args[ $k ] = $v;
}
}
if ( isset( $update_args ) ) {
$row_args = array_merge( $update_args, $row_array );
}
$this->exist_rows[] = $key;
}
$fields[ $key ] = $row_args;
}
// change field position
if ( 0 === strpos( $key, 'um_position_' ) ) {
$field_key = str_replace( 'um_position_', '', $key );
if ( isset( $fields[ $field_key ] ) ) {
$fields[ $field_key ]['position'] = absint( $value );
}
}
// change field master row
if ( 0 === strpos( $key, 'um_row_' ) ) {
$field_key = str_replace( 'um_row_', '', $key );
if ( isset( $fields[ $field_key ] ) ) {
$fields[ $field_key ]['in_row'] = sanitize_key( $value );
}
}
// change field sub row
if ( 0 === strpos( $key, 'um_subrow_' ) ) {
$field_key = str_replace( 'um_subrow_', '', $key );
if ( isset( $fields[ $field_key ] ) ) {
$fields[ $field_key ]['in_sub_row'] = sanitize_key( $value );
}
}
// change field column
if ( 0 === strpos( $key, 'um_col_' ) ) {
$field_key = str_replace( 'um_col_', '', $key );
if ( isset( $fields[ $field_key ] ) ) {
$fields[ $field_key ]['in_column'] = absint( $value );
}
}
// add field to group
if ( 0 === strpos( $key, 'um_group_' ) ) {
$field_key = str_replace( 'um_group_', '', $key );
if ( isset( $fields[ $field_key ] ) ) {
$fields[ $field_key ]['in_group'] = ! empty( $value ) ? absint( $value ) : '';
}
}
}
foreach ( $this->row_data as $k => $v ) {
if ( ! in_array( $k, $this->exist_rows, true ) ) {
unset( $this->row_data[ $k ] );
}
}
update_option( 'um_existing_rows_' . $form_id, $this->exist_rows );
update_option( 'um_form_rowdata_' . $form_id, $this->row_data );
UM()->query()->update_attr( 'custom_fields', $form_id, $fields );
}
/**
* Load form to maintain form order
*/
public function load_field_order() {
$screen = get_current_screen();
if ( ! isset( $screen->id ) || 'um_form' !== $screen->id ) {
return;
} ?>
<div class="um-col-demon-settings" data-in_row="" data-in_sub_row="" data-in_column="" data-in_group=""></div>
<div class="um-col-demon-row" style="display:none;">
<div class="um-admin-drag-row-icons">
<a href="javascript:void(0);" class="um-admin-drag-rowsub-add um-admin-tipsy-n" title="<?php esc_attr_e( 'Add Row', 'ultimate-member' ); ?>" data-row_action="add_subrow"><i class="um-icon-plus"></i></a>
<a href="javascript:void(0);" class="um-admin-drag-row-edit um-admin-tipsy-n" title="<?php esc_attr_e( 'Edit Row', 'ultimate-member' ); ?>" data-modal="UM_edit_row" data-modal-size="normal" data-dynamic-content="um_admin_edit_field_popup" data-arg1="row" data-arg2="<?php echo esc_attr( get_the_ID() ); ?>"><i class="um-faicon-pencil"></i></a>
<span class="um-admin-drag-row-start"><i class="um-icon-arrow-move"></i></span>
<a href="javascript:void(0);" class="um-admin-tipsy-n" title="<?php esc_attr_e( 'Delete Row', 'ultimate-member' ); ?>" data-remove_element="um-admin-drag-row"><i class="um-faicon-trash-o"></i></a>
</div>
<div class="um-admin-clear"></div>
<div class="um-admin-drag-rowsubs">
<div class="um-admin-drag-rowsub">
<div class="um-admin-drag-ctrls columns">
<a href="javascript:void(0);" class="active" data-cols="1"></a>
<a href="javascript:void(0);" data-cols="2"></a>
<a href="javascript:void(0);" data-cols="3"></a>
</div>
<div class="um-admin-drag-rowsub-icons">
<span class="um-admin-drag-rowsub-start"><i class="um-icon-arrow-move"></i></span>
<a href="javascript:void(0);" class="um-admin-tipsy-n" title="<?php esc_attr_e( 'Delete Row', 'ultimate-member' ); ?>" data-remove_element="um-admin-drag-rowsub"><i class="um-faicon-trash-o"></i></a>
</div><div class="um-admin-clear"></div>
<div class="um-admin-drag-col">
</div>
<div class="um-admin-drag-col-dynamic"></div>
<div class="um-admin-clear"></div>
</div>
</div>
</div>
<div class="um-col-demon-subrow" style="display:none;">
<div class="um-admin-drag-ctrls columns">
<a href="javascript:void(0);" class="active" data-cols="1"></a>
<a href="javascript:void(0);" data-cols="2"></a>
<a href="javascript:void(0);" data-cols="3"></a>
</div>
<div class="um-admin-drag-rowsub-icons">
<span class="um-admin-drag-rowsub-start"><i class="um-icon-arrow-move"></i></span>
<a href="javascript:void(0);" class="um-admin-tipsy-n" title="<?php esc_attr_e( 'Delete Row', 'ultimate-member' ); ?>" data-remove_element="um-admin-drag-rowsub"><i class="um-faicon-trash-o"></i></a>
</div><div class="um-admin-clear"></div>
<div class="um-admin-drag-col">
</div>
<div class="um-admin-drag-col-dynamic"></div>
<div class="um-admin-clear"></div>
</div>
<form action="" method="post" class="um_update_order">
<input type="hidden" name="form_id" id="form_id" value="<?php echo esc_attr( get_the_ID() ); ?>" />
<input type="hidden" name="action" value="um_update_order" />
<input type="hidden" name="nonce" value="<?php echo esc_attr( wp_create_nonce( 'um-admin-nonce' ) ) ?>" />
<div class="um_update_order_fields">
</div>
</form>
<?php
}
}
}