1
|
|
|
<?php |
2
|
|
|
if ( ! defined('ABSPATH') ) { |
3
|
|
|
die('You are not allowed to call this page directly.'); |
4
|
|
|
} |
5
|
|
|
|
6
|
|
|
class FrmForm { |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @return int|boolean id on success or false on failure |
10
|
|
|
*/ |
11
|
|
|
public static function create( $values ) { |
12
|
|
|
global $wpdb; |
13
|
|
|
|
14
|
|
|
$new_values = array( |
15
|
|
|
'form_key' => FrmAppHelper::get_unique_key( $values['form_key'], $wpdb->prefix . 'frm_forms', 'form_key' ), |
16
|
|
|
'name' => $values['name'], |
17
|
|
|
'description' => $values['description'], |
18
|
|
|
'status' => isset($values['status']) ? $values['status'] : 'draft', |
19
|
|
|
'logged_in' => isset($values['logged_in']) ? $values['logged_in'] : 0, |
20
|
|
|
'is_template' => isset($values['is_template']) ? (int) $values['is_template'] : 0, |
21
|
|
|
'parent_form_id' => isset( $values['parent_form_id'] ) ? absint( $values['parent_form_id'] ) : 0, |
22
|
|
|
'editable' => isset($values['editable']) ? (int) $values['editable'] : 0, |
23
|
|
|
'default_template' => isset($values['default_template']) ? (int) $values['default_template'] : 0, |
24
|
|
|
'created_at' => isset($values['created_at']) ? $values['created_at'] : current_time('mysql', 1), |
25
|
|
|
); |
26
|
|
|
|
27
|
|
|
$options = isset( $values['options'] ) ? (array) $values['options'] : array(); |
28
|
|
|
FrmFormsHelper::fill_form_options( $options, $values ); |
29
|
|
|
|
30
|
|
|
$options['before_html'] = isset($values['options']['before_html']) ? $values['options']['before_html'] : FrmFormsHelper::get_default_html('before'); |
31
|
|
|
$options['after_html'] = isset($values['options']['after_html']) ? $values['options']['after_html'] : FrmFormsHelper::get_default_html('after'); |
32
|
|
|
$options['submit_html'] = isset($values['options']['submit_html']) ? $values['options']['submit_html'] : FrmFormsHelper::get_default_html('submit'); |
33
|
|
|
|
34
|
|
|
$options = apply_filters('frm_form_options_before_update', $options, $values); |
35
|
|
|
$new_values['options'] = serialize($options); |
36
|
|
|
|
37
|
|
|
//if(isset($values['id']) && is_numeric($values['id'])) |
38
|
|
|
// $new_values['id'] = $values['id']; |
39
|
|
|
|
40
|
|
|
$wpdb->insert( $wpdb->prefix . 'frm_forms', $new_values ); |
41
|
|
|
|
42
|
|
|
$id = $wpdb->insert_id; |
43
|
|
|
|
44
|
|
|
// Clear form caching |
45
|
|
|
self::clear_form_cache(); |
46
|
|
|
|
47
|
|
|
return $id; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return int|boolean ID on success or false on failure |
52
|
|
|
*/ |
53
|
|
|
public static function duplicate( $id, $template = false, $copy_keys = false, $blog_id = false ) { |
54
|
|
|
global $wpdb; |
55
|
|
|
|
56
|
|
|
$values = self::getOne( $id, $blog_id ); |
57
|
|
|
if ( ! $values ) { |
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$new_key = $copy_keys ? $values->form_key : ''; |
62
|
|
|
|
63
|
|
|
$new_values = array( |
64
|
|
|
'form_key' => FrmAppHelper::get_unique_key( $new_key, $wpdb->prefix . 'frm_forms', 'form_key' ), |
65
|
|
|
'name' => $values->name, |
66
|
|
|
'description' => $values->description, |
67
|
|
|
'status' => $template ? 'published' : 'draft', |
68
|
|
|
'logged_in' => $values->logged_in ? $values->logged_in : 0, |
69
|
|
|
'editable' => $values->editable ? $values->editable : 0, |
70
|
|
|
'created_at' => current_time('mysql', 1), |
71
|
|
|
'is_template' => $template ? 1 : 0, |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
if ( $blog_id ) { |
75
|
|
|
$new_values['status'] = 'published'; |
76
|
|
|
$new_options = maybe_unserialize($values->options); |
77
|
|
|
$new_options['email_to'] = get_option('admin_email'); |
78
|
|
|
$new_options['copy'] = false; |
79
|
|
|
$new_values['options'] = $new_options; |
80
|
|
|
} else { |
81
|
|
|
$new_values['options'] = $values->options; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if ( is_array($new_values['options']) ) { |
85
|
|
|
$new_values['options'] = serialize($new_values['options']); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$query_results = $wpdb->insert( $wpdb->prefix . 'frm_forms', $new_values ); |
89
|
|
|
|
90
|
|
|
if ( $query_results ) { |
91
|
|
|
// Clear form caching |
92
|
|
|
self::clear_form_cache(); |
93
|
|
|
|
94
|
|
|
$form_id = $wpdb->insert_id; |
95
|
|
|
FrmField::duplicate($id, $form_id, $copy_keys, $blog_id); |
96
|
|
|
|
97
|
|
|
// update form settings after fields are created |
98
|
|
|
do_action( 'frm_after_duplicate_form', $form_id, $new_values, array( 'old_id' => $id ) ); |
99
|
|
|
return $form_id; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public static function after_duplicate( $form_id, $values ) { |
106
|
|
|
$new_opts = $values['options'] = maybe_unserialize($values['options']); |
107
|
|
|
|
108
|
|
|
if ( isset($new_opts['success_msg']) ) { |
109
|
|
|
$new_opts['success_msg'] = FrmFieldsHelper::switch_field_ids($new_opts['success_msg']); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$new_opts = apply_filters('frm_after_duplicate_form_values', $new_opts, $form_id); |
113
|
|
|
|
114
|
|
|
if ( $new_opts != $values['options'] ) { |
115
|
|
|
global $wpdb; |
116
|
|
|
$wpdb->update( $wpdb->prefix . 'frm_forms', array( 'options' => maybe_serialize( $new_opts ) ), array( 'id' => $form_id ) ); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return int|boolean |
122
|
|
|
*/ |
123
|
|
|
public static function update( $id, $values, $create_link = false ) { |
124
|
|
|
global $wpdb; |
125
|
|
|
|
126
|
|
|
if ( ! isset( $values['status'] ) && ( $create_link || isset( $values['options'] ) || isset( $values['item_meta'] ) || isset( $values['field_options'] ) ) ) { |
127
|
|
|
$values['status'] = 'published'; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if ( isset($values['form_key']) ) { |
131
|
|
|
$values['form_key'] = FrmAppHelper::get_unique_key( $values['form_key'], $wpdb->prefix . 'frm_forms', 'form_key', $id ); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$form_fields = array( 'form_key', 'name', 'description', 'status', 'parent_form_id' ); |
135
|
|
|
|
136
|
|
|
$new_values = self::set_update_options( array(), $values); |
137
|
|
|
|
138
|
|
|
foreach ( $values as $value_key => $value ) { |
139
|
|
|
if ( $value_key && in_array( $value_key, $form_fields ) ) { |
140
|
|
|
$new_values[ $value_key ] = $value; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
if ( isset( $values['new_status'] ) && ! empty( $values['new_status'] ) ) { |
145
|
|
|
$new_values['status'] = $values['new_status']; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if ( ! empty( $new_values ) ) { |
149
|
|
|
$query_results = $wpdb->update( $wpdb->prefix . 'frm_forms', $new_values, array( 'id' => $id ) ); |
150
|
|
|
if ( $query_results ) { |
151
|
|
|
self::clear_form_cache(); |
152
|
|
|
} |
153
|
|
|
} else { |
154
|
|
|
$query_results = true; |
155
|
|
|
} |
156
|
|
|
unset($new_values); |
157
|
|
|
|
158
|
|
|
$values = self::update_fields($id, $values); |
159
|
|
|
|
160
|
|
|
do_action( 'frm_update_form', $id, $values ); |
161
|
|
|
do_action( 'frm_update_form_' . $id, $values ); |
162
|
|
|
|
163
|
|
|
return $query_results; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return array |
168
|
|
|
*/ |
169
|
|
|
public static function set_update_options( $new_values, $values ) { |
170
|
|
|
if ( ! isset($values['options']) ) { |
171
|
|
|
return $new_values; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$options = isset( $values['options'] ) ? (array) $values['options'] : array(); |
175
|
|
|
FrmFormsHelper::fill_form_options( $options, $values ); |
176
|
|
|
|
177
|
|
|
$options['custom_style'] = isset($values['options']['custom_style']) ? $values['options']['custom_style'] : 0; |
178
|
|
|
$options['before_html'] = isset($values['options']['before_html']) ? $values['options']['before_html'] : FrmFormsHelper::get_default_html('before'); |
179
|
|
|
$options['after_html'] = isset($values['options']['after_html']) ? $values['options']['after_html'] : FrmFormsHelper::get_default_html('after'); |
180
|
|
|
$options['submit_html'] = (isset($values['options']['submit_html']) && $values['options']['submit_html'] != '') ? $values['options']['submit_html'] : FrmFormsHelper::get_default_html('submit'); |
181
|
|
|
|
182
|
|
|
$options = apply_filters('frm_form_options_before_update', $options, $values); |
183
|
|
|
$new_values['options'] = serialize($options); |
184
|
|
|
|
185
|
|
|
return $new_values; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @return array |
191
|
|
|
*/ |
192
|
|
|
public static function update_fields( $id, $values ) { |
193
|
|
|
|
194
|
|
|
if ( ! isset($values['options']) && ! isset($values['item_meta']) && ! isset($values['field_options']) ) { |
195
|
|
|
return $values; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
$all_fields = FrmField::get_all_for_form($id); |
199
|
|
|
if ( empty($all_fields) ) { |
200
|
|
|
return $values; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
if ( ! isset($values['item_meta']) ) { |
204
|
|
|
$values['item_meta'] = array(); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
$field_array = array(); |
208
|
|
|
$existing_keys = array_keys($values['item_meta']); |
209
|
|
|
foreach ( $all_fields as $fid ) { |
210
|
|
|
if ( ! in_array($fid->id, $existing_keys) && ( isset($values['frm_fields_submitted']) && in_array($fid->id, $values['frm_fields_submitted']) ) || isset($values['options']) ) { |
211
|
|
|
$values['item_meta'][ $fid->id ] = ''; |
212
|
|
|
} |
213
|
|
|
$field_array[ $fid->id ] = $fid; |
214
|
|
|
} |
215
|
|
|
unset($all_fields); |
216
|
|
|
|
217
|
|
|
foreach ( $values['item_meta'] as $field_id => $default_value ) { |
218
|
|
View Code Duplication |
if ( isset( $field_array[ $field_id ] ) ) { |
|
|
|
|
219
|
|
|
$field = $field_array[ $field_id ]; |
220
|
|
|
} else { |
221
|
|
|
$field = FrmField::getOne($field_id); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
if ( ! $field ) { |
225
|
|
|
continue; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
$is_settings_page = ( isset( $values['options'] ) || isset( $values['field_options'][ 'custom_html_' . $field_id ] ) ); |
229
|
|
|
if ( $is_settings_page ) { |
230
|
|
|
//updating the settings page |
231
|
|
|
if ( isset( $values['field_options'][ 'custom_html_' . $field_id ] ) ) { |
232
|
|
|
$field->field_options['custom_html'] = isset( $values['field_options'][ 'custom_html_' . $field_id ] ) ? $values['field_options'][ 'custom_html_' . $field_id ] : ( isset( $field->field_options['custom_html'] ) ? $field->field_options['custom_html'] : FrmFieldsHelper::get_default_html( $field->type ) ); |
233
|
|
|
$field->field_options = apply_filters('frm_update_form_field_options', $field->field_options, $field, $values); |
234
|
|
|
FrmField::update( $field_id, array( 'field_options' => $field->field_options ) ); |
235
|
|
|
} else if ( $field->type == 'hidden' || $field->type == 'user_id' ) { |
236
|
|
|
$prev_opts = $field->field_options; |
237
|
|
|
$field->field_options = apply_filters('frm_update_form_field_options', $field->field_options, $field, $values); |
238
|
|
|
if ( $prev_opts != $field->field_options ) { |
239
|
|
|
FrmField::update( $field_id, array( 'field_options' => $field->field_options ) ); |
240
|
|
|
} |
241
|
|
|
unset($prev_opts); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
if ( $is_settings_page && ! defined( 'WP_IMPORTING' ) ) { |
246
|
|
|
continue; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
//updating the form |
250
|
|
|
$update_options = FrmFieldsHelper::get_default_field_opts( $field->type, $field, true ); |
251
|
|
|
unset( $update_options['custom_html'] ); // don't check for POST html |
252
|
|
|
$update_options = apply_filters( 'frm_field_options_to_update', $update_options ); |
253
|
|
|
|
254
|
|
|
foreach ( $update_options as $opt => $default ) { |
255
|
|
|
$field->field_options[ $opt ] = isset( $values['field_options'][ $opt . '_' . $field_id ] ) ? trim( sanitize_text_field( $values['field_options'][ $opt . '_' . $field_id ] ) ) : $default; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
$field->field_options = apply_filters('frm_update_field_options', $field->field_options, $field, $values); |
259
|
|
|
$default_value = maybe_serialize( $values['item_meta'][ $field_id ] ); |
260
|
|
|
|
261
|
|
|
$new_field = array( |
262
|
|
|
'field_options' => $field->field_options, |
263
|
|
|
'default_value' => $default_value, |
264
|
|
|
); |
265
|
|
|
|
266
|
|
|
self::prepare_field_update_values( $field, $values, $new_field ); |
267
|
|
|
|
268
|
|
|
FrmField::update( $field_id, $new_field ); |
269
|
|
|
|
270
|
|
|
FrmField::delete_form_transient($field->form_id); |
271
|
|
|
} |
272
|
|
|
self::clear_form_cache(); |
273
|
|
|
|
274
|
|
|
return $values; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
private static function prepare_field_update_values( $field, $values, &$new_field ) { |
278
|
|
|
$field_cols = array( |
279
|
|
|
'field_key' => '', 'required' => false, 'type' => '', |
280
|
|
|
'description' => '', 'options' => '', |
281
|
|
|
); |
282
|
|
|
foreach ( $field_cols as $col => $default ) { |
283
|
|
|
$default = ( $default == '' ) ? $field->{$col} : $default; |
284
|
|
|
$new_field[ $col ] = isset( $values['field_options'][ $col . '_' . $field->id ] ) ? $values['field_options'][ $col . '_' . $field->id ] : $default; |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @param string $status |
290
|
|
|
* @return int|boolean |
291
|
|
|
*/ |
292
|
|
|
public static function set_status( $id, $status ) { |
293
|
|
|
if ( 'trash' == $status ) { |
294
|
|
|
return self::trash($id); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
$statuses = array( 'published', 'draft', 'trash' ); |
298
|
|
|
if ( ! in_array( $status, $statuses ) ) { |
299
|
|
|
return false; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
global $wpdb; |
303
|
|
|
|
304
|
|
|
if ( is_array($id) ) { |
305
|
|
|
$where = array( 'id' => $id, 'parent_form_id' => $id, 'or' => 1 ); |
306
|
|
|
FrmDb::get_where_clause_and_values( $where ); |
307
|
|
|
array_unshift( $where['values'], $status ); |
308
|
|
|
|
309
|
|
|
$query_results = $wpdb->query( $wpdb->prepare( 'UPDATE ' . $wpdb->prefix . 'frm_forms SET status = %s ' . $where['where'], $where['values'] ) ); |
310
|
|
|
} else { |
311
|
|
|
$query_results = $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'status' => $status ), array( 'id' => $id ) ); |
312
|
|
|
$wpdb->update( $wpdb->prefix . 'frm_forms', array( 'status' => $status ), array( 'parent_form_id' => $id ) ); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
if ( $query_results ) { |
316
|
|
|
self::clear_form_cache(); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
return $query_results; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* @return int|boolean |
324
|
|
|
*/ |
325
|
|
|
public static function trash( $id ) { |
326
|
|
|
if ( ! EMPTY_TRASH_DAYS ) { |
327
|
|
|
return self::destroy( $id ); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
$form = self::getOne($id); |
331
|
|
|
if ( ! $form ) { |
332
|
|
|
return false; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
$options = $form->options; |
336
|
|
|
$options['trash_time'] = time(); |
337
|
|
|
|
338
|
|
|
global $wpdb; |
339
|
|
|
$query_results = $wpdb->update( |
340
|
|
|
$wpdb->prefix . 'frm_forms', |
341
|
|
|
array( 'status' => 'trash', 'options' => serialize( $options ) ), |
342
|
|
|
array( 'id' => $id ) |
343
|
|
|
); |
344
|
|
|
|
345
|
|
|
$wpdb->update( |
346
|
|
|
$wpdb->prefix . 'frm_forms', |
347
|
|
|
array( 'status' => 'trash', 'options' => serialize( $options ) ), |
348
|
|
|
array( 'parent_form_id' => $id ) |
349
|
|
|
); |
350
|
|
|
|
351
|
|
|
if ( $query_results ) { |
352
|
|
|
self::clear_form_cache(); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
return $query_results; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* @return int|boolean |
360
|
|
|
*/ |
361
|
|
|
public static function destroy( $id ) { |
362
|
|
|
global $wpdb; |
363
|
|
|
|
364
|
|
|
$form = self::getOne($id); |
365
|
|
|
if ( ! $form ) { |
366
|
|
|
return false; |
367
|
|
|
} |
368
|
|
|
$id = $form->id; |
369
|
|
|
|
370
|
|
|
// Disconnect the entries from this form |
371
|
|
|
$entries = FrmDb::get_col( $wpdb->prefix . 'frm_items', array( 'form_id' => $id ) ); |
372
|
|
|
foreach ( $entries as $entry_id ) { |
|
|
|
|
373
|
|
|
FrmEntry::destroy($entry_id); |
374
|
|
|
unset($entry_id); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
// Disconnect the fields from this form |
378
|
|
|
$wpdb->query( $wpdb->prepare( 'DELETE fi FROM ' . $wpdb->prefix . 'frm_fields AS fi LEFT JOIN ' . $wpdb->prefix . 'frm_forms fr ON (fi.form_id = fr.id) WHERE fi.form_id=%d OR parent_form_id=%d', $id, $id ) ); |
379
|
|
|
|
380
|
|
|
$query_results = $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_forms WHERE id=%d OR parent_form_id=%d', $id, $id ) ); |
381
|
|
|
if ( $query_results ) { |
382
|
|
|
// Delete all form actions linked to this form |
383
|
|
|
$action_control = FrmFormActionsController::get_form_actions( 'email' ); |
384
|
|
|
$action_control->destroy($id, 'all'); |
385
|
|
|
|
386
|
|
|
// Clear form caching |
387
|
|
|
self::clear_form_cache(); |
388
|
|
|
|
389
|
|
|
do_action( 'frm_destroy_form', $id ); |
390
|
|
|
do_action( 'frm_destroy_form_' . $id ); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
return $query_results; |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Delete trashed forms based on how long they have been trashed |
398
|
|
|
* @return int The number of forms deleted |
399
|
|
|
*/ |
400
|
|
|
public static function scheduled_delete( $delete_timestamp = '' ) { |
401
|
|
|
global $wpdb; |
402
|
|
|
|
403
|
|
|
$trash_forms = FrmDb::get_results( $wpdb->prefix . 'frm_forms', array( 'status' => 'trash' ), 'id, options' ); |
404
|
|
|
|
405
|
|
|
if ( ! $trash_forms ) { |
406
|
|
|
return; |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
if ( empty( $delete_timestamp ) ) { |
410
|
|
|
$delete_timestamp = time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS ); |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
$count = 0; |
414
|
|
|
foreach ( $trash_forms as $form ) { |
|
|
|
|
415
|
|
|
$form->options = maybe_unserialize( $form->options ); |
416
|
|
|
if ( ! isset( $form->options['trash_time'] ) || $form->options['trash_time'] < $delete_timestamp ) { |
417
|
|
|
self::destroy( $form->id ); |
418
|
|
|
$count++; |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
unset( $form ); |
422
|
|
|
} |
423
|
|
|
return $count; |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
/** |
427
|
|
|
* @return string form name |
428
|
|
|
*/ |
429
|
|
|
public static function &getName( $id ) { |
|
|
|
|
430
|
|
|
global $wpdb; |
431
|
|
|
|
432
|
|
|
$form = FrmAppHelper::check_cache($id, 'frm_form'); |
433
|
|
|
if ( $form ) { |
434
|
|
|
$r = stripslashes($form->name); |
435
|
|
|
return $r; |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
$query_key = is_numeric( $id ) ? 'id' : 'form_key'; |
439
|
|
|
$r = FrmDb::get_var( 'frm_forms', array( $query_key => $id ), 'name' ); |
440
|
|
|
$r = stripslashes($r); |
441
|
|
|
|
442
|
|
|
return $r; |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
/** |
446
|
|
|
* @param string $key |
447
|
|
|
* @return int form id |
448
|
|
|
*/ |
449
|
|
|
public static function &getIdByKey( $key ) { |
|
|
|
|
450
|
|
|
$id = FrmDb::get_var( 'frm_forms', array( 'form_key' => sanitize_title( $key ) ) ); |
451
|
|
|
return $id; |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* @param int $id |
456
|
|
|
* @return string form key |
457
|
|
|
*/ |
458
|
|
|
public static function &getKeyById( $id ) { |
|
|
|
|
459
|
|
|
$id = (int) $id; |
460
|
|
|
$cache = FrmAppHelper::check_cache($id, 'frm_form'); |
461
|
|
|
if ( $cache ) { |
462
|
|
|
return $cache->form_key; |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
$key = FrmDb::get_var( 'frm_forms', array( 'id' => $id ), 'form_key' ); |
466
|
|
|
|
467
|
|
|
return $key; |
|
|
|
|
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
/** |
471
|
|
|
* If $form is numeric, get the form object |
472
|
|
|
* @param object|int $form |
473
|
|
|
* @since 2.0.9 |
474
|
|
|
*/ |
475
|
|
|
public static function maybe_get_form( &$form ) { |
476
|
|
|
if ( ! is_object( $form ) && ! is_array( $form ) && ! empty( $form ) ) { |
477
|
|
|
$form = self::getOne( $form ); |
478
|
|
|
} |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
/** |
482
|
|
|
* @return object form |
483
|
|
|
*/ |
484
|
|
|
public static function getOne( $id, $blog_id = false ) { |
|
|
|
|
485
|
|
|
global $wpdb; |
486
|
|
|
|
487
|
|
|
if ( $blog_id && is_multisite() ) { |
488
|
|
|
global $wpmuBaseTablePrefix; |
489
|
|
|
$prefix = $wpmuBaseTablePrefix ? $wpmuBaseTablePrefix . $blog_id . '_' : $wpdb->get_blog_prefix( $blog_id ); |
490
|
|
|
|
491
|
|
|
$table_name = $prefix . 'frm_forms'; |
492
|
|
|
} else { |
493
|
|
|
$table_name = $wpdb->prefix . 'frm_forms'; |
494
|
|
|
$cache = wp_cache_get($id, 'frm_form'); |
495
|
|
|
if ( $cache ) { |
496
|
|
|
if ( isset($cache->options) ) { |
497
|
|
|
$cache->options = maybe_unserialize($cache->options); |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
return stripslashes_deep($cache); |
501
|
|
|
} |
502
|
|
|
} |
503
|
|
|
|
504
|
|
|
if ( is_numeric($id) ) { |
505
|
|
|
$where = array( 'id' => $id ); |
506
|
|
|
} else { |
507
|
|
|
$where = array( 'form_key' => $id ); |
508
|
|
|
} |
509
|
|
|
|
510
|
|
|
$results = FrmDb::get_row( $table_name, $where ); |
511
|
|
|
|
512
|
|
View Code Duplication |
if ( isset($results->options) ) { |
|
|
|
|
513
|
|
|
FrmAppHelper::set_cache( $results->id, $results, 'frm_form' ); |
514
|
|
|
$results->options = maybe_unserialize($results->options); |
515
|
|
|
} |
516
|
|
|
return stripslashes_deep($results); |
517
|
|
|
} |
518
|
|
|
|
519
|
|
|
/** |
520
|
|
|
* @return object|array of objects |
521
|
|
|
*/ |
522
|
|
|
public static function getAll( $where = array(), $order_by = '', $limit = '' ) { |
|
|
|
|
523
|
|
|
if ( is_array( $where ) && ! empty( $where ) ) { |
524
|
|
|
$results = FrmDb::get_results( 'frm_forms', $where, '*', array( 'order_by' => $order_by, 'limit' => $limit ) ); |
525
|
|
|
} else { |
526
|
|
|
global $wpdb; |
527
|
|
|
|
528
|
|
|
// the query has already been prepared if this is not an array |
529
|
|
|
$query = 'SELECT * FROM ' . $wpdb->prefix . 'frm_forms' . FrmAppHelper::prepend_and_or_where( ' WHERE ', $where ) . FrmAppHelper::esc_order( $order_by ) . FrmAppHelper::esc_limit( $limit ); |
|
|
|
|
530
|
|
|
$results = $wpdb->get_results( $query ); |
531
|
|
|
} |
532
|
|
|
|
533
|
|
View Code Duplication |
if ( $results ) { |
|
|
|
|
534
|
|
|
foreach ( $results as $result ) { |
535
|
|
|
FrmAppHelper::set_cache( $result->id, $result, 'frm_form' ); |
536
|
|
|
$result->options = maybe_unserialize( $result->options ); |
537
|
|
|
} |
538
|
|
|
} |
539
|
|
|
|
540
|
|
|
if ( $limit == ' LIMIT 1' || $limit == 1 ) { |
541
|
|
|
// return the first form object if we are only getting one form |
542
|
|
|
$results = reset( $results ); |
543
|
|
|
} |
544
|
|
|
|
545
|
|
|
return stripslashes_deep($results); |
546
|
|
|
} |
547
|
|
|
|
548
|
|
|
/** |
549
|
|
|
* Get all published forms |
550
|
|
|
* @since 2.0 |
551
|
|
|
* @return array of forms |
552
|
|
|
*/ |
553
|
|
|
public static function get_published_forms( $query = array(), $limit = 999, $inc_children = 'exclude' ) { |
554
|
|
|
$query['is_template'] = 0; |
555
|
|
|
$query['status'] = array( null, '', 'published' ); |
556
|
|
|
if ( $inc_children == 'exclude' ) { |
557
|
|
|
$query['parent_form_id'] = array( null, 0 ); |
558
|
|
|
} |
559
|
|
|
|
560
|
|
|
$forms = self::getAll( $query, 'name', $limit ); |
561
|
|
|
return $forms; |
562
|
|
|
} |
563
|
|
|
|
564
|
|
|
/** |
565
|
|
|
* @return int count of forms |
566
|
|
|
*/ |
567
|
|
|
public static function &get_count() { |
568
|
|
|
global $wpdb; |
569
|
|
|
|
570
|
|
|
$cache_key = 'frm_form_counts'; |
571
|
|
|
|
572
|
|
|
$counts = wp_cache_get( $cache_key, 'frm_form' ); |
573
|
|
|
if ( false !== $counts ) { |
574
|
|
|
return $counts; |
575
|
|
|
} |
576
|
|
|
|
577
|
|
|
$results = (array) FrmDb::get_results( 'frm_forms', array( 'or' => 1, 'parent_form_id' => null, 'parent_form_id <' => 0 ), 'status, is_template' ); |
578
|
|
|
|
579
|
|
|
$statuses = array( 'published', 'draft', 'template', 'trash' ); |
580
|
|
|
$counts = array_fill_keys( $statuses, 0 ); |
581
|
|
|
|
582
|
|
|
foreach ( $results as $row ) { |
583
|
|
|
if ( 'trash' != $row->status ) { |
584
|
|
|
if ( $row->is_template ) { |
585
|
|
|
$counts['template']++; |
586
|
|
|
} else { |
587
|
|
|
$counts['published']++; |
588
|
|
|
} |
589
|
|
|
} else { |
590
|
|
|
$counts['trash']++; |
591
|
|
|
} |
592
|
|
|
|
593
|
|
|
if ( 'draft' == $row->status ) { |
594
|
|
|
$counts['draft']++; |
595
|
|
|
} |
596
|
|
|
|
597
|
|
|
unset($row); |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
$counts = (object) $counts; |
601
|
|
|
FrmAppHelper::set_cache( $cache_key, $counts, 'frm_form' ); |
602
|
|
|
|
603
|
|
|
return $counts; |
604
|
|
|
} |
605
|
|
|
|
606
|
|
|
/** |
607
|
|
|
* Clear form caching |
608
|
|
|
* Called when a form is created, updated, duplicated, or deleted |
609
|
|
|
* or when the form status is changed |
610
|
|
|
* |
611
|
|
|
* @since 2.0.4 |
612
|
|
|
*/ |
613
|
|
|
public static function clear_form_cache() { |
614
|
|
|
FrmAppHelper::cache_delete_group( 'frm_form' ); |
615
|
|
|
} |
616
|
|
|
|
617
|
|
|
/** |
618
|
|
|
* @return array of errors |
619
|
|
|
*/ |
620
|
|
|
public static function validate( $values ) { |
621
|
|
|
$errors = array(); |
622
|
|
|
|
623
|
|
|
return apply_filters('frm_validate_form', $errors, $values); |
624
|
|
|
} |
625
|
|
|
|
626
|
|
|
public static function get_params( $form = null ) { |
627
|
|
|
global $frm_vars; |
628
|
|
|
|
629
|
|
|
if ( ! $form ) { |
630
|
|
|
$form = self::getAll( array(), 'name', 1 ); |
631
|
|
|
} else { |
632
|
|
|
self::maybe_get_form( $form ); |
633
|
|
|
} |
634
|
|
|
|
635
|
|
|
if ( isset( $frm_vars['form_params'] ) && is_array( $frm_vars['form_params'] ) && isset( $frm_vars['form_params'][ $form->id ] ) ) { |
636
|
|
|
return $frm_vars['form_params'][ $form->id ]; |
637
|
|
|
} |
638
|
|
|
|
639
|
|
|
$action_var = isset($_REQUEST['frm_action']) ? 'frm_action' : 'action'; |
|
|
|
|
640
|
|
|
$action = apply_filters( 'frm_show_new_entry_page', FrmAppHelper::get_param( $action_var, 'new', 'get', 'sanitize_title' ), $form ); |
641
|
|
|
|
642
|
|
|
$default_values = array( |
643
|
|
|
'id' => '', 'form_name' => '', 'paged' => 1, 'form' => $form->id, 'form_id' => $form->id, |
644
|
|
|
'field_id' => '', 'search' => '', 'sort' => '', 'sdir' => '', 'action' => $action, |
645
|
|
|
); |
646
|
|
|
|
647
|
|
|
$values = array(); |
648
|
|
|
$values['posted_form_id'] = FrmAppHelper::get_param( 'form_id', '', 'get', 'absint' ); |
649
|
|
|
if ( ! $values['posted_form_id'] ) { |
650
|
|
|
$values['posted_form_id'] = FrmAppHelper::get_param( 'form', '', 'get', 'absint' ); |
651
|
|
|
} |
652
|
|
|
|
653
|
|
|
if ( $form->id == $values['posted_form_id'] ) { |
654
|
|
|
//if there are two forms on the same page, make sure not to submit both |
655
|
|
|
foreach ( $default_values as $var => $default ) { |
656
|
|
|
if ( $var == 'action' ) { |
657
|
|
|
$values[ $var ] = FrmAppHelper::get_param( $action_var, $default, 'get', 'sanitize_title' ); |
658
|
|
|
} else { |
659
|
|
|
$values[ $var ] = FrmAppHelper::get_param( $var, $default ); |
660
|
|
|
} |
661
|
|
|
unset( $var, $default ); |
662
|
|
|
} |
663
|
|
|
} else { |
664
|
|
|
foreach ( $default_values as $var => $default ) { |
665
|
|
|
$values[ $var ] = $default; |
666
|
|
|
unset( $var, $default ); |
667
|
|
|
} |
668
|
|
|
} |
669
|
|
|
|
670
|
|
|
if ( in_array( $values['action'], array( 'create', 'update' ) ) && ( ! $_POST || ( ! isset( $_POST['action'] ) && ! isset( $_POST['frm_action'] ) ) ) ) { |
671
|
|
|
$values['action'] = 'new'; |
672
|
|
|
} |
673
|
|
|
|
674
|
|
|
return $values; |
675
|
|
|
} |
676
|
|
|
|
677
|
|
|
public static function list_page_params() { |
678
|
|
|
$values = array(); |
679
|
|
|
foreach ( array( 'template' => 0, 'id' => '', 'paged' => 1, 'form' => '', 'search' => '', 'sort' => '', 'sdir' => '' ) as $var => $default ) { |
680
|
|
|
$values[ $var ] = FrmAppHelper::get_param( $var, $default ); |
681
|
|
|
} |
682
|
|
|
|
683
|
|
|
return $values; |
684
|
|
|
} |
685
|
|
|
|
686
|
|
|
public static function get_admin_params( $form = null ) { |
687
|
|
|
$form_id = $form; |
688
|
|
|
if ( $form === null ) { |
689
|
|
|
$form_id = FrmForm::get_current_form_id(); |
690
|
|
|
} else if ( $form && is_object( $form ) ) { |
691
|
|
|
$form_id = $form->id; |
692
|
|
|
} |
693
|
|
|
|
694
|
|
|
$values = array(); |
695
|
|
|
foreach ( array( |
696
|
|
|
'id' => '', 'form_name' => '', 'paged' => 1, 'form' => $form_id, |
697
|
|
|
'field_id' => '', 'search' => '', 'sort' => '', 'sdir' => '', 'fid' => '', |
698
|
|
|
'keep_post' => '', |
699
|
|
|
) as $var => $default ) { |
700
|
|
|
$values[ $var ] = FrmAppHelper::get_param( $var, $default ); |
701
|
|
|
} |
702
|
|
|
|
703
|
|
|
return $values; |
704
|
|
|
} |
705
|
|
|
|
706
|
|
|
public static function get_current_form_id( $default_form = 'none' ) { |
707
|
|
|
if ( 'first' == $default_form ) { |
708
|
|
|
var_dump(debug_backtrace()); |
|
|
|
|
709
|
|
|
$form = self::get_current_form(); |
710
|
|
|
} else { |
711
|
|
|
$form = self::maybe_get_current_form(); |
712
|
|
|
} |
713
|
|
|
$form_id = $form ? $form->id : 0; |
714
|
|
|
|
715
|
|
|
return $form_id; |
716
|
|
|
} |
717
|
|
|
|
718
|
|
|
public static function maybe_get_current_form( $form_id = 0 ) { |
719
|
|
|
global $frm_vars; |
720
|
|
|
|
721
|
|
|
if ( isset( $frm_vars['current_form'] ) && $frm_vars['current_form'] && ( ! $form_id || $form_id == $frm_vars['current_form']->id ) ) { |
722
|
|
|
return $frm_vars['current_form']; |
723
|
|
|
} |
724
|
|
|
|
725
|
|
|
$form_id = FrmAppHelper::get_param( 'form', $form_id, 'get', 'absint' ); |
726
|
|
|
if ( $form_id ) { |
727
|
|
|
$form_id = self::set_current_form( $form_id ); |
728
|
|
|
} |
729
|
|
|
return $form_id; |
730
|
|
|
} |
731
|
|
|
|
732
|
|
|
public static function get_current_form( $form_id = 0 ) { |
733
|
|
|
$form = self::maybe_get_current_form( $form_id ); |
734
|
|
|
if ( is_numeric( $form ) ) { |
735
|
|
|
$form = self::set_current_form( $form ); |
736
|
|
|
} |
737
|
|
|
return $form; |
738
|
|
|
} |
739
|
|
|
|
740
|
|
|
public static function set_current_form( $form_id ) { |
741
|
|
|
global $frm_vars; |
742
|
|
|
|
743
|
|
|
$query = array(); |
744
|
|
|
if ( $form_id ) { |
745
|
|
|
$query['id'] = $form_id; |
746
|
|
|
} |
747
|
|
|
|
748
|
|
|
$frm_vars['current_form'] = self::get_published_forms( $query, 1 ); |
749
|
|
|
|
750
|
|
|
return $frm_vars['current_form']; |
751
|
|
|
} |
752
|
|
|
|
753
|
|
|
public static function is_form_loaded( $form, $this_load, $global_load ) { |
754
|
|
|
global $frm_vars; |
755
|
|
|
$small_form = new stdClass(); |
756
|
|
|
foreach ( array( 'id', 'form_key', 'name' ) as $var ) { |
757
|
|
|
$small_form->{$var} = $form->{$var}; |
758
|
|
|
unset($var); |
759
|
|
|
} |
760
|
|
|
|
761
|
|
|
$frm_vars['forms_loaded'][] = $small_form; |
762
|
|
|
|
763
|
|
|
if ( $this_load && empty($global_load) ) { |
764
|
|
|
$global_load = $frm_vars['load_css'] = true; |
765
|
|
|
} |
766
|
|
|
|
767
|
|
|
return ( ( ! isset($frm_vars['css_loaded']) || ! $frm_vars['css_loaded'] ) && $global_load ); |
768
|
|
|
} |
769
|
|
|
|
770
|
|
|
public static function show_submit( $form ) { |
771
|
|
|
$show = ( ! $form->is_template && $form->status == 'published' && ! FrmAppHelper::is_admin() ); |
772
|
|
|
$show = apply_filters( 'frm_show_submit_button', $show, $form ); |
773
|
|
|
return $show; |
774
|
|
|
} |
775
|
|
|
|
776
|
|
|
/** |
777
|
|
|
* @since 2.3 |
778
|
|
|
*/ |
779
|
|
|
public static function get_option( $atts ) { |
780
|
|
|
$form = $atts['form']; |
781
|
|
|
return isset( $form->options[ $atts['option'] ] ) ? $form->options[ $atts['option'] ] : $atts['default']; |
782
|
|
|
} |
783
|
|
|
} |
784
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.