Passed
Push — master ( ec16eb...0357f3 )
by Stephanie
03:05
created

FrmForm::scheduled_delete()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 7
nop 1
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
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,
0 ignored issues
show
Bug introduced by
The function absint was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

21
			'parent_form_id' => isset( $values['parent_form_id'] ) ? /** @scrutinizer ignore-call */ absint( $values['parent_form_id'] ) : 0,
Loading history...
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),
0 ignored issues
show
Bug introduced by
The function current_time was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
            'created_at'    => isset($values['created_at']) ? $values['created_at'] : /** @scrutinizer ignore-call */ current_time('mysql', 1),
Loading history...
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);
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
        $options = /** @scrutinizer ignore-call */ apply_filters('frm_form_options_before_update', $options, $values);
Loading history...
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 ) {
0 ignored issues
show
introduced by
The condition ! $values can never be false.
Loading history...
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),
0 ignored issues
show
Bug introduced by
The function current_time was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
            'created_at'    => /** @scrutinizer ignore-call */ current_time('mysql', 1),
Loading history...
71
            'is_template'   => $template ? 1 : 0,
72
        );
73
74
        if ( $blog_id ) {
75
            $new_values['status'] = 'published';
76
            $new_options = maybe_unserialize($values->options);
0 ignored issues
show
Bug introduced by
The function maybe_unserialize was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
            $new_options = /** @scrutinizer ignore-call */ maybe_unserialize($values->options);
Loading history...
77
            $new_options['email_to'] = get_option('admin_email');
0 ignored issues
show
Bug introduced by
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
            $new_options['email_to'] = /** @scrutinizer ignore-call */ get_option('admin_email');
Loading history...
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 ) );
0 ignored issues
show
Bug introduced by
The function do_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

98
			/** @scrutinizer ignore-call */ 
99
   do_action( 'frm_after_duplicate_form', $form_id, $new_values, array( 'old_id' => $id ) );
Loading history...
99
            return $form_id;
100
        }
101
102
        return false;
103
    }
104
105
	public static function after_duplicate( $form_id, $values ) {
106
		$new_opts = maybe_unserialize( $values['options'] );
0 ignored issues
show
Bug introduced by
The function maybe_unserialize was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
		$new_opts = /** @scrutinizer ignore-call */ maybe_unserialize( $values['options'] );
Loading history...
107
		$values['options'] = $new_opts;
108
109
        if ( isset($new_opts['success_msg']) ) {
110
            $new_opts['success_msg'] = FrmFieldsHelper::switch_field_ids($new_opts['success_msg']);
111
        }
112
113
        $new_opts = apply_filters('frm_after_duplicate_form_values', $new_opts, $form_id);
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

113
        $new_opts = /** @scrutinizer ignore-call */ apply_filters('frm_after_duplicate_form_values', $new_opts, $form_id);
Loading history...
114
115
        if ( $new_opts != $values['options'] ) {
116
            global $wpdb;
117
			$wpdb->update( $wpdb->prefix . 'frm_forms', array( 'options' => maybe_serialize( $new_opts ) ), array( 'id' => $form_id ) );
0 ignored issues
show
Bug introduced by
The function maybe_serialize was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

117
			$wpdb->update( $wpdb->prefix . 'frm_forms', array( 'options' => /** @scrutinizer ignore-call */ maybe_serialize( $new_opts ) ), array( 'id' => $form_id ) );
Loading history...
118
        }
119
    }
120
121
    /**
122
     * @return int|boolean
123
     */
124
    public static function update( $id, $values, $create_link = false ) {
125
        global $wpdb;
126
127
        if ( ! isset( $values['status'] ) && ( $create_link || isset( $values['options'] ) || isset( $values['item_meta'] ) || isset( $values['field_options'] ) ) ) {
128
            $values['status'] = 'published';
129
        }
130
131
        if ( isset($values['form_key']) ) {
132
			$values['form_key'] = FrmAppHelper::get_unique_key( $values['form_key'], $wpdb->prefix . 'frm_forms', 'form_key', $id );
133
        }
134
135
		$form_fields = array( 'form_key', 'name', 'description', 'status', 'parent_form_id' );
136
137
        $new_values = self::set_update_options( array(), $values);
138
139
        foreach ( $values as $value_key => $value ) {
140
			if ( $value_key && in_array( $value_key, $form_fields ) ) {
141
				$new_values[ $value_key ] = $value;
142
            }
143
        }
144
145
        if ( isset( $values['new_status'] ) && ! empty( $values['new_status'] ) ) {
146
            $new_values['status'] = $values['new_status'];
147
        }
148
149
        if ( ! empty( $new_values ) ) {
150
			$query_results = $wpdb->update( $wpdb->prefix . 'frm_forms', $new_values, array( 'id' => $id ) );
151
            if ( $query_results ) {
152
				self::clear_form_cache();
153
            }
154
        } else {
155
            $query_results = true;
156
        }
157
        unset($new_values);
158
159
        $values = self::update_fields($id, $values);
160
161
		do_action( 'frm_update_form', $id, $values );
0 ignored issues
show
Bug introduced by
The function do_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

161
		/** @scrutinizer ignore-call */ 
162
  do_action( 'frm_update_form', $id, $values );
Loading history...
162
		do_action( 'frm_update_form_' . $id, $values );
163
164
        return $query_results;
165
    }
166
167
    /**
168
     * @return array
169
     */
170
	public static function set_update_options( $new_values, $values ) {
171
        if ( ! isset($values['options']) ) {
172
            return $new_values;
173
        }
174
175
		$options = isset( $values['options'] ) ? (array) $values['options'] : array();
176
		FrmFormsHelper::fill_form_options( $options, $values );
177
178
		$options['custom_style'] = isset( $values['options']['custom_style'] ) ? $values['options']['custom_style'] : 0;
179
		$options['before_html'] = isset( $values['options']['before_html'] ) ? $values['options']['before_html'] : FrmFormsHelper::get_default_html( 'before' );
180
		$options['after_html'] = isset( $values['options']['after_html'] ) ? $values['options']['after_html'] : FrmFormsHelper::get_default_html( 'after' );
181
		$options['submit_html'] = ( isset( $values['options']['submit_html'] ) && '' !== $values['options']['submit_html'] ) ? $values['options']['submit_html'] : FrmFormsHelper::get_default_html( 'submit' );
182
183
		$options = apply_filters( 'frm_form_options_before_update', $options, $values );
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

183
		$options = /** @scrutinizer ignore-call */ apply_filters( 'frm_form_options_before_update', $options, $values );
Loading history...
184
		$new_values['options'] = serialize( $options );
185
186
		return $new_values;
187
    }
188
189
190
    /**
191
     * @return array
192
     */
193
	public static function update_fields( $id, $values ) {
194
195
        if ( ! isset($values['item_meta']) && ! isset($values['field_options']) ) {
196
            return $values;
197
        }
198
199
        $all_fields = FrmField::get_all_for_form($id);
200
        if ( empty($all_fields) ) {
201
            return $values;
202
        }
203
204
        if ( ! isset($values['item_meta']) ) {
205
            $values['item_meta'] = array();
206
        }
207
208
        $field_array = array();
209
        $existing_keys = array_keys($values['item_meta']);
210
        foreach ( $all_fields as $fid ) {
211
            if ( ! in_array($fid->id, $existing_keys) && ( isset($values['frm_fields_submitted']) && in_array($fid->id, $values['frm_fields_submitted']) ) || isset($values['options']) ) {
212
				$values['item_meta'][ $fid->id ] = '';
213
            }
214
			$field_array[ $fid->id ] = $fid;
215
        }
216
        unset($all_fields);
217
218
        foreach ( $values['item_meta'] as $field_id => $default_value ) {
219
			if ( isset( $field_array[ $field_id ] ) ) {
220
				$field = $field_array[ $field_id ];
221
            } else {
222
                $field = FrmField::getOne($field_id);
223
            }
224
225
            if ( ! $field ) {
226
                continue;
227
            }
228
229
			$is_settings_page = ( isset( $values['options'] ) || isset( $values['field_options'][ 'custom_html_' . $field_id ] ) );
230
			if ( $is_settings_page ) {
231
				self::get_settings_page_html( $values, $field );
232
233
				if ( ! defined( 'WP_IMPORTING' ) ) {
234
					continue;
235
				}
236
			}
237
238
			//updating the form
239
			$update_options = FrmFieldsHelper::get_default_field_options_from_field( $field );
240
			unset( $update_options['custom_html'] ); // don't check for POST html
241
			$update_options = apply_filters( 'frm_field_options_to_update', $update_options );
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

241
			$update_options = /** @scrutinizer ignore-call */ apply_filters( 'frm_field_options_to_update', $update_options );
Loading history...
242
243
			foreach ( $update_options as $opt => $default ) {
244
				$field->field_options[ $opt ] = isset( $values['field_options'][ $opt . '_' . $field_id ] ) ? $values['field_options'][ $opt . '_' . $field_id ] : $default;
245
				if ( is_string( $field->field_options[ $opt ] ) ) {
246
					$field->field_options[ $opt ] = trim( sanitize_text_field( $field->field_options[ $opt ] ) );
0 ignored issues
show
Bug introduced by
The function sanitize_text_field was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

246
					$field->field_options[ $opt ] = trim( /** @scrutinizer ignore-call */ sanitize_text_field( $field->field_options[ $opt ] ) );
Loading history...
247
				}
248
            }
249
250
            $field->field_options = apply_filters('frm_update_field_options', $field->field_options, $field, $values);
251
			$default_value = maybe_serialize( $values['item_meta'][ $field_id ] );
0 ignored issues
show
Bug introduced by
The function maybe_serialize was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

251
			$default_value = /** @scrutinizer ignore-call */ maybe_serialize( $values['item_meta'][ $field_id ] );
Loading history...
252
253
			$new_field = array(
254
				'field_options' => $field->field_options,
255
				'default_value' => $default_value,
256
			);
257
258
			self::prepare_field_update_values( $field, $values, $new_field );
259
260
			FrmField::update( $field_id, $new_field );
261
262
            FrmField::delete_form_transient($field->form_id);
263
        }
264
		self::clear_form_cache();
265
266
        return $values;
267
    }
268
269
	/**
270
	 * updating the settings page
271
	 */
272
	private static function get_settings_page_html( $values, &$field ) {
273
		if ( isset( $values['field_options'][ 'custom_html_' . $field->id ] ) ) {
274
			$prev_opts = array();
275
			$fallback_html = isset( $field->field_options['custom_html'] ) ? $field->field_options['custom_html'] : FrmFieldsHelper::get_default_html( $field->type );
276
			$field->field_options['custom_html'] = isset( $values['field_options'][ 'custom_html_' . $field->id ] ) ? $values['field_options'][ 'custom_html_' . $field->id ] : $fallback_html;
277
		} elseif ( $field->type == 'hidden' || $field->type == 'user_id' ) {
278
			$prev_opts = $field->field_options;
279
		}
280
281
		if ( isset( $prev_opts ) ) {
282
			$field->field_options = apply_filters( 'frm_update_form_field_options', $field->field_options, $field, $values );
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

282
			$field->field_options = /** @scrutinizer ignore-call */ apply_filters( 'frm_update_form_field_options', $field->field_options, $field, $values );
Loading history...
283
			if ( $prev_opts != $field->field_options ) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $prev_opts does not seem to be defined for all execution paths leading up to this point.
Loading history...
284
				FrmField::update( $field->id, array( 'field_options' => $field->field_options ) );
285
			}
286
		}
287
	}
288
289
	private static function prepare_field_update_values( $field, $values, &$new_field ) {
290
		$field_cols = array(
291
			'field_key'   => '',
292
			'required'    => false,
293
			'type'        => '',
294
			'description' => '',
295
			'options'     => '',
296
			'name'        => '',
297
		);
298
		foreach ( $field_cols as $col => $default ) {
299
			$default = ( $default === '' ) ? $field->{$col} : $default;
300
			$new_field[ $col ] = isset( $values['field_options'][ $col . '_' . $field->id ] ) ? $values['field_options'][ $col . '_' . $field->id ] : $default;
301
		}
302
	}
303
304
    /**
305
     * @param string $status
306
     * @return int|boolean
307
     */
308
	public static function set_status( $id, $status ) {
309
        if ( 'trash' == $status ) {
310
            return self::trash($id);
311
        }
312
313
		$statuses  = array( 'published', 'draft', 'trash' );
314
        if ( ! in_array( $status, $statuses ) ) {
315
            return false;
316
        }
317
318
        global $wpdb;
319
320
        if ( is_array($id) ) {
321
			$where = array(
322
				'id' => $id,
323
				'parent_form_id' => $id,
324
				'or' => 1,
325
			);
326
			FrmDb::get_where_clause_and_values( $where );
327
			array_unshift( $where['values'], $status );
0 ignored issues
show
Bug introduced by
It seems like $where['values'] can also be of type integer; however, parameter $array of array_unshift() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

327
			array_unshift( /** @scrutinizer ignore-type */ $where['values'], $status );
Loading history...
328
329
			$query_results = $wpdb->query( $wpdb->prepare( 'UPDATE ' . $wpdb->prefix . 'frm_forms SET status = %s ' . $where['where'], $where['values'] ) );
0 ignored issues
show
Bug introduced by
Are you sure $where['where'] of type integer|array can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

329
			$query_results = $wpdb->query( $wpdb->prepare( 'UPDATE ' . $wpdb->prefix . 'frm_forms SET status = %s ' . /** @scrutinizer ignore-type */ $where['where'], $where['values'] ) );
Loading history...
330
        } else {
331
			$query_results = $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'status' => $status ), array( 'id' => $id ) );
332
			$wpdb->update( $wpdb->prefix . 'frm_forms', array( 'status' => $status ), array( 'parent_form_id' => $id ) );
333
        }
334
335
        if ( $query_results ) {
336
			self::clear_form_cache();
337
        }
338
339
        return $query_results;
340
    }
341
342
    /**
343
     * @return int|boolean
344
     */
345
	public static function trash( $id ) {
346
        if ( ! EMPTY_TRASH_DAYS ) {
0 ignored issues
show
Bug introduced by
The constant EMPTY_TRASH_DAYS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
347
            return self::destroy( $id );
348
        }
349
350
        $form = self::getOne($id);
351
        if ( ! $form ) {
0 ignored issues
show
introduced by
The condition ! $form can never be false.
Loading history...
352
            return false;
353
        }
354
355
        $options = $form->options;
356
        $options['trash_time'] = time();
357
358
		global $wpdb;
359
		$query_results = $wpdb->update(
360
			$wpdb->prefix . 'frm_forms',
361
			array(
362
				'status'  => 'trash',
363
				'options' => serialize( $options ),
364
			),
365
			array(
366
				'id' => $id,
367
			)
368
		);
369
370
		$wpdb->update(
371
			$wpdb->prefix . 'frm_forms',
372
			array(
373
				'status'  => 'trash',
374
				'options' => serialize( $options ),
375
			),
376
			array(
377
				'parent_form_id' => $id,
378
			)
379
		);
380
381
        if ( $query_results ) {
382
			self::clear_form_cache();
383
        }
384
385
        return $query_results;
386
    }
387
388
    /**
389
     * @return int|boolean
390
     */
391
	public static function destroy( $id ) {
392
        global $wpdb;
393
394
        $form = self::getOne($id);
395
        if ( ! $form ) {
0 ignored issues
show
introduced by
The condition ! $form can never be false.
Loading history...
396
            return false;
397
        }
398
		$id = $form->id;
399
400
        // Disconnect the entries from this form
401
		$entries = FrmDb::get_col( $wpdb->prefix . 'frm_items', array( 'form_id' => $id ) );
402
        foreach ( $entries as $entry_id ) {
403
            FrmEntry::destroy($entry_id);
404
            unset($entry_id);
405
        }
406
407
        // Disconnect the fields from this form
408
		$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 ) );
409
410
		$query_results = $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_forms WHERE id=%d OR parent_form_id=%d', $id, $id ) );
411
        if ( $query_results ) {
412
            // Delete all form actions linked to this form
413
            $action_control = FrmFormActionsController::get_form_actions( 'email' );
414
            $action_control->destroy($id, 'all');
415
416
			// Clear form caching
417
			self::clear_form_cache();
418
419
			do_action( 'frm_destroy_form', $id );
0 ignored issues
show
Bug introduced by
The function do_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

419
			/** @scrutinizer ignore-call */ 
420
   do_action( 'frm_destroy_form', $id );
Loading history...
420
			do_action( 'frm_destroy_form_' . $id );
421
        }
422
423
        return $query_results;
424
    }
425
426
	/**
427
	 * Delete trashed forms based on how long they have been trashed
428
	 * @return int The number of forms deleted
429
	 */
430
	public static function scheduled_delete( $delete_timestamp = '' ) {
431
		global $wpdb;
432
433
		$trash_forms = FrmDb::get_results( $wpdb->prefix . 'frm_forms', array( 'status' => 'trash' ), 'id, options' );
434
435
		if ( ! $trash_forms ) {
436
			return;
437
		}
438
439
		if ( empty( $delete_timestamp ) ) {
440
			$delete_timestamp = time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS );
0 ignored issues
show
Bug introduced by
The constant DAY_IN_SECONDS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant EMPTY_TRASH_DAYS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
441
		}
442
443
		$count = 0;
444
		foreach ( $trash_forms as $form ) {
445
			$form->options = maybe_unserialize( $form->options );
0 ignored issues
show
Bug introduced by
The function maybe_unserialize was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

445
			$form->options = /** @scrutinizer ignore-call */ maybe_unserialize( $form->options );
Loading history...
446
			if ( ! isset( $form->options['trash_time'] ) || $form->options['trash_time'] < $delete_timestamp ) {
447
				self::destroy( $form->id );
448
				$count++;
449
			}
450
451
			unset( $form );
452
		}
453
		return $count;
454
	}
455
456
    /**
457
     * @return string form name
458
     */
459
    public static function getName( $id ) {
0 ignored issues
show
Coding Style introduced by
The function name getName is in camel caps, but expected get_name instead as per the coding standard.
Loading history...
460
		$form = FrmDb::check_cache( $id, 'frm_form' );
461
        if ( $form ) {
462
            $r = stripslashes($form->name);
463
            return $r;
464
        }
465
466
        $query_key = is_numeric( $id ) ? 'id' : 'form_key';
467
        $r = FrmDb::get_var( 'frm_forms', array( $query_key => $id ), 'name' );
468
        $r = stripslashes($r);
469
470
        return $r;
471
    }
472
473
    /**
474
	 * @since 3.0
475
     * @param string $key
476
     * @return int form id
477
     */
478
	public static function get_id_by_key( $key ) {
479
		return (int) FrmDb::get_var( 'frm_forms', array( 'form_key' => sanitize_title( $key ) ) );
0 ignored issues
show
Bug introduced by
The function sanitize_title was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

479
		return (int) FrmDb::get_var( 'frm_forms', array( 'form_key' => /** @scrutinizer ignore-call */ sanitize_title( $key ) ) );
Loading history...
480
    }
481
482
    /**
483
     * @param string $key
484
     * @return int form id
485
     */
486
	public static function getIdByKey( $key ) {
0 ignored issues
show
Coding Style introduced by
The function name getIdByKey is in camel caps, but expected get_id_by_key instead as per the coding standard.
Loading history...
487
		_deprecated_function( __METHOD__, '3.0', 'FrmForm::get_id_by_key' );
0 ignored issues
show
Bug introduced by
The function _deprecated_function was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

487
		/** @scrutinizer ignore-call */ 
488
  _deprecated_function( __METHOD__, '3.0', 'FrmForm::get_id_by_key' );
Loading history...
488
		return self::get_id_by_key( $key );
489
    }
490
491
    /**
492
	 * @since 3.0
493
     * @param int $id
494
     * @return string form key
495
     */
496
	public static function get_key_by_id( $id ) {
497
        $id = (int) $id;
498
		$cache = FrmDb::check_cache( $id, 'frm_form' );
499
        if ( $cache ) {
500
            return $cache->form_key;
501
        }
502
503
        $key = FrmDb::get_var( 'frm_forms', array( 'id' => $id ), 'form_key' );
504
505
        return $key;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $key also could return the type object|array which is incompatible with the documented return type string.
Loading history...
506
    }
507
508
	public static function getKeyById( $id ) {
0 ignored issues
show
Coding Style introduced by
The function name getKeyById is in camel caps, but expected get_key_by_id instead as per the coding standard.
Loading history...
509
		_deprecated_function( __METHOD__, '3.0', 'FrmForm::get_key_by_id' );
0 ignored issues
show
Bug introduced by
The function _deprecated_function was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

509
		/** @scrutinizer ignore-call */ 
510
  _deprecated_function( __METHOD__, '3.0', 'FrmForm::get_key_by_id' );
Loading history...
510
		return self::get_key_by_id( $id );
511
	}
512
513
	/**
514
	 * If $form is numeric, get the form object
515
	 * @param object|int $form
516
	 * @since 2.0.9
517
	 */
518
	public static function maybe_get_form( &$form ) {
519
		if ( ! is_object( $form ) && ! is_array( $form ) && ! empty( $form ) ) {
520
			$form = self::getOne( $form );
521
		}
522
	}
523
524
    /**
525
     * @return object form
526
     */
527
    public static function getOne( $id, $blog_id = false ) {
0 ignored issues
show
Coding Style introduced by
The function name getOne is in camel caps, but expected get_one instead as per the coding standard.
Loading history...
528
        global $wpdb;
529
530
        if ( $blog_id && is_multisite() ) {
0 ignored issues
show
Bug introduced by
The function is_multisite was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

530
        if ( $blog_id && /** @scrutinizer ignore-call */ is_multisite() ) {
Loading history...
531
            global $wpmuBaseTablePrefix;
532
			$prefix = $wpmuBaseTablePrefix ? $wpmuBaseTablePrefix . $blog_id . '_' : $wpdb->get_blog_prefix( $blog_id );
533
534
			$table_name = $prefix . 'frm_forms';
535
        } else {
536
			$table_name = $wpdb->prefix . 'frm_forms';
537
            $cache = wp_cache_get($id, 'frm_form');
0 ignored issues
show
Bug introduced by
The function wp_cache_get was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

537
            $cache = /** @scrutinizer ignore-call */ wp_cache_get($id, 'frm_form');
Loading history...
538
            if ( $cache ) {
539
                if ( isset($cache->options) ) {
540
                    $cache->options = maybe_unserialize($cache->options);
0 ignored issues
show
Bug introduced by
The function maybe_unserialize was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

540
                    $cache->options = /** @scrutinizer ignore-call */ maybe_unserialize($cache->options);
Loading history...
541
                }
542
543
                return stripslashes_deep($cache);
0 ignored issues
show
Bug introduced by
The function stripslashes_deep was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

543
                return /** @scrutinizer ignore-call */ stripslashes_deep($cache);
Loading history...
544
            }
545
        }
546
547
        if ( is_numeric($id) ) {
548
            $where = array( 'id' => $id );
549
        } else {
550
            $where = array( 'form_key' => $id );
551
        }
552
553
        $results = FrmDb::get_row( $table_name, $where );
554
555
        if ( isset($results->options) ) {
556
			FrmDb::set_cache( $results->id, $results, 'frm_form' );
557
            $results->options = maybe_unserialize($results->options);
558
        }
559
        return stripslashes_deep($results);
560
    }
561
562
    /**
563
     * @return object|array of objects
564
     */
565
	public static function getAll( $where = array(), $order_by = '', $limit = '' ) {
0 ignored issues
show
Coding Style introduced by
The function name getAll is in camel caps, but expected get_all instead as per the coding standard.
Loading history...
566
		if ( is_array( $where ) && ! empty( $where ) ) {
567
			$results = FrmDb::get_results( 'frm_forms', $where, '*', array(
568
				'order_by' => $order_by,
569
				'limit'    => $limit,
570
			) );
571
		} else {
572
			global $wpdb;
573
574
			// the query has already been prepared if this is not an array
575
			$query = 'SELECT * FROM ' . $wpdb->prefix . 'frm_forms' . FrmDb::prepend_and_or_where( ' WHERE ', $where ) . FrmDb::esc_order( $order_by ) . FrmDb::esc_limit( $limit );
576
			$results = $wpdb->get_results( $query );
577
		}
578
579
		if ( $results ) {
580
			foreach ( $results as $result ) {
581
				FrmDb::set_cache( $result->id, $result, 'frm_form' );
582
				$result->options = maybe_unserialize( $result->options );
0 ignored issues
show
Bug introduced by
The function maybe_unserialize was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

582
				$result->options = /** @scrutinizer ignore-call */ maybe_unserialize( $result->options );
Loading history...
583
			}
584
		}
585
586
		if ( $limit == ' LIMIT 1' || $limit == 1 ) {
587
			// return the first form object if we are only getting one form
588
			$results = reset( $results );
589
		}
590
591
        return stripslashes_deep($results);
0 ignored issues
show
Bug introduced by
The function stripslashes_deep was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

591
        return /** @scrutinizer ignore-call */ stripslashes_deep($results);
Loading history...
592
    }
593
594
	/**
595
	 * Get all published forms
596
	 * @since 2.0
597
	 * @return array of forms
598
	 */
599
	public static function get_published_forms( $query = array(), $limit = 999, $inc_children = 'exclude' ) {
600
		$query['is_template'] = 0;
601
		$query['status'] = array( null, '', 'published' );
602
		if ( $inc_children == 'exclude' ) {
603
			$query['parent_form_id'] = array( null, 0 );
604
		}
605
606
		$forms = self::getAll( $query, 'name', $limit );
607
		return $forms;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $forms also could return the type object which is incompatible with the documented return type array.
Loading history...
608
	}
609
610
    /**
611
     * @return int count of forms
612
     */
613
    public static function get_count() {
614
    	global $wpdb;
615
616
    	$cache_key = 'frm_form_counts';
617
618
    	$counts = wp_cache_get( $cache_key, 'frm_form' );
0 ignored issues
show
Bug introduced by
The function wp_cache_get was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

618
    	$counts = /** @scrutinizer ignore-call */ wp_cache_get( $cache_key, 'frm_form' );
Loading history...
619
    	if ( false !== $counts ) {
620
    	    return $counts;
621
    	}
622
623
		$results = (array) FrmDb::get_results( 'frm_forms', array(
624
			'or' => 1,
625
			'parent_form_id' => null,
626
			'parent_form_id <' => 0,
627
		), 'status, is_template' );
628
629
		$statuses = array( 'published', 'draft', 'template', 'trash' );
630
    	$counts = array_fill_keys( $statuses, 0 );
631
632
    	foreach ( $results as $row ) {
633
            if ( 'trash' != $row->status ) {
634
    	        if ( $row->is_template ) {
635
					$counts['template']++;
636
    	        } else {
637
					$counts['published']++;
638
    	        }
639
    	    } else {
640
				$counts['trash']++;
641
        	}
642
643
    	    if ( 'draft' == $row->status ) {
644
				$counts['draft']++;
645
    	    }
646
647
    		unset($row);
648
    	}
649
650
    	$counts = (object) $counts;
651
		FrmDb::set_cache( $cache_key, $counts, 'frm_form' );
652
653
    	return $counts;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $counts returns the type object which is incompatible with the documented return type integer.
Loading history...
654
    }
655
656
	/**
657
	 * Clear form caching
658
	 * Called when a form is created, updated, duplicated, or deleted
659
	 * or when the form status is changed
660
	 *
661
	 * @since 2.0.4
662
	 */
663
	public static function clear_form_cache() {
664
		FrmDb::cache_delete_group( 'frm_form' );
665
	}
666
667
    /**
668
     * @return array of errors
669
     */
670
	public static function validate( $values ) {
671
        $errors = array();
672
673
        return apply_filters('frm_validate_form', $errors, $values);
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

673
        return /** @scrutinizer ignore-call */ apply_filters('frm_validate_form', $errors, $values);
Loading history...
674
    }
675
676
	public static function get_params( $form = null ) {
677
		global $frm_vars;
678
679
		if ( ! $form ) {
680
			$form = self::getAll( array(), 'name', 1 );
681
		} else {
682
			self::maybe_get_form( $form );
683
		}
684
685
		if ( isset( $frm_vars['form_params'] ) && is_array( $frm_vars['form_params'] ) && isset( $frm_vars['form_params'][ $form->id ] ) ) {
686
			return $frm_vars['form_params'][ $form->id ];
687
		}
688
689
		$action_var = isset($_REQUEST['frm_action']) ? 'frm_action' : 'action';
690
		$action = apply_filters( 'frm_show_new_entry_page', FrmAppHelper::get_param( $action_var, 'new', 'get', 'sanitize_title' ), $form );
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

690
		$action = /** @scrutinizer ignore-call */ apply_filters( 'frm_show_new_entry_page', FrmAppHelper::get_param( $action_var, 'new', 'get', 'sanitize_title' ), $form );
Loading history...
691
692
		$default_values = array(
693
			'id'        => '',
694
			'form_name' => '',
695
			'paged'     => 1,
696
			'form'      => $form->id,
697
			'form_id'   => $form->id,
698
			'field_id'  => '',
699
			'search'    => '',
700
			'sort'      => '',
701
			'sdir'      => '',
702
			'action'    => $action,
703
		);
704
705
		$values = array();
706
		$values['posted_form_id'] = FrmAppHelper::get_param( 'form_id', '', 'get', 'absint' );
707
		if ( ! $values['posted_form_id'] ) {
708
			$values['posted_form_id'] = FrmAppHelper::get_param( 'form', '', 'get', 'absint' );
709
		}
710
711
		if ( $form->id == $values['posted_form_id'] ) {
712
			//if there are two forms on the same page, make sure not to submit both
713
			foreach ( $default_values as $var => $default ) {
714
				if ( $var == 'action' ) {
715
					$values[ $var ] = FrmAppHelper::get_param( $action_var, $default, 'get', 'sanitize_title' );
716
				} else {
717
					$values[ $var ] = FrmAppHelper::get_param( $var, $default, 'get', 'sanitize_text_field' );
718
				}
719
				unset( $var, $default );
720
			}
721
		} else {
722
			foreach ( $default_values as $var => $default ) {
723
				$values[ $var ] = $default;
724
				unset( $var, $default );
725
			}
726
		}
727
728
		if ( in_array( $values['action'], array( 'create', 'update' ) ) && ( ! $_POST || ( ! isset( $_POST['action'] ) && ! isset( $_POST['frm_action'] ) ) ) ) {
729
			$values['action'] = 'new';
730
		}
731
732
		return $values;
733
	}
734
735
	public static function list_page_params() {
736
		$values = array();
737
		$defaults = array(
738
			'template' => 0,
739
			'id'     => '',
740
			'paged'  => 1,
741
			'form'   => '',
742
			'search' => '',
743
			'sort'   => '',
744
			'sdir'   => '',
745
		);
746
		foreach ( $defaults as $var => $default ) {
747
			$values[ $var ] = FrmAppHelper::get_param( $var, $default, 'get', 'sanitize_text_field' );
748
		}
749
750
		return $values;
751
	}
752
753
	public static function get_admin_params( $form = null ) {
754
		$form_id = $form;
755
		if ( $form === null ) {
756
			$form_id = FrmForm::get_current_form_id();
757
		} else if ( $form && is_object( $form ) ) {
758
			$form_id = $form->id;
759
		}
760
761
		$values = array();
762
		$defaults = array(
763
			'id'        => '',
764
			'form_name' => '',
765
			'paged'     => 1,
766
			'form'      => $form_id,
767
			'field_id'  => '',
768
			'search'    => '',
769
			'sort'      => '',
770
			'sdir'      => '',
771
			'fid'       => '',
772
			'keep_post' => '',
773
		);
774
		foreach ( $defaults as $var => $default ) {
775
			$values[ $var ] = FrmAppHelper::get_param( $var, $default, 'get', 'sanitize_text_field' );
776
		}
777
778
		return $values;
779
	}
780
781
	public static function get_current_form_id( $default_form = 'none' ) {
782
		if ( 'first' == $default_form ) {
783
			$form = self::get_current_form();
784
		} else {
785
			$form = self::maybe_get_current_form();
786
		}
787
		$form_id = $form ? $form->id : 0;
788
789
		return $form_id;
790
	}
791
792
	public static function maybe_get_current_form( $form_id = 0 ) {
793
		global $frm_vars;
794
795
		if ( isset( $frm_vars['current_form'] ) && $frm_vars['current_form'] && ( ! $form_id || $form_id == $frm_vars['current_form']->id ) ) {
796
			return $frm_vars['current_form'];
797
		}
798
799
		$form_id = FrmAppHelper::get_param( 'form', $form_id, 'get', 'absint' );
800
		if ( $form_id ) {
801
			$form_id = self::set_current_form( $form_id );
802
		}
803
		return $form_id;
804
	}
805
806
	public static function get_current_form( $form_id = 0 ) {
807
		$form = self::maybe_get_current_form( $form_id );
808
		if ( is_numeric( $form ) ) {
809
			 $form = self::set_current_form( $form );
810
		}
811
		return $form;
812
	}
813
814
	public static function set_current_form( $form_id ) {
815
		global $frm_vars;
816
817
		$query = array();
818
		if ( $form_id ) {
819
			$query['id'] = $form_id;
820
		}
821
822
		$frm_vars['current_form'] = self::get_published_forms( $query, 1 );
823
824
		return $frm_vars['current_form'];
825
	}
826
827
	public static function is_form_loaded( $form, $this_load, $global_load ) {
828
		global $frm_vars;
829
		$small_form = new stdClass();
830
		foreach ( array( 'id', 'form_key', 'name' ) as $var ) {
831
			$small_form->{$var} = $form->{$var};
832
			unset($var);
833
		}
834
835
		$frm_vars['forms_loaded'][] = $small_form;
836
837
		if ( $this_load && empty( $global_load ) ) {
838
			$global_load = true;
839
			$frm_vars['load_css'] = true;
840
		}
841
842
		return ( ( ! isset($frm_vars['css_loaded']) || ! $frm_vars['css_loaded'] ) && $global_load );
843
	}
844
845
	public static function show_submit( $form ) {
846
		$show = ( ! $form->is_template && $form->status == 'published' && ! FrmAppHelper::is_admin() );
847
		$show = apply_filters( 'frm_show_submit_button', $show, $form );
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

847
		$show = /** @scrutinizer ignore-call */ apply_filters( 'frm_show_submit_button', $show, $form );
Loading history...
848
		return $show;
849
	}
850
851
	/**
852
	 * @since 2.3
853
	 */
854
	public static function get_option( $atts ) {
855
		$form = $atts['form'];
856
		return isset( $form->options[ $atts['option'] ] ) ? $form->options[ $atts['option'] ] : $atts['default'];
857
	}
858
}
859