| 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'] : 'published', |
| 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 |
'created_at' => isset( $values['created_at'] ) ? $values['created_at'] : current_time( 'mysql', 1 ), |
| 24 |
); |
| 25 |
|
| 26 |
$options = isset( $values['options'] ) ? (array) $values['options'] : array(); |
| 27 |
FrmFormsHelper::fill_form_options( $options, $values ); |
| 28 |
|
| 29 |
$options['before_html'] = isset( $values['options']['before_html'] ) ? $values['options']['before_html'] : FrmFormsHelper::get_default_html( 'before' ); |
| 30 |
$options['after_html'] = isset( $values['options']['after_html'] ) ? $values['options']['after_html'] : FrmFormsHelper::get_default_html( 'after' ); |
| 31 |
$options['submit_html'] = isset( $values['options']['submit_html'] ) ? $values['options']['submit_html'] : FrmFormsHelper::get_default_html( 'submit' ); |
| 32 |
|
| 33 |
$options = apply_filters( 'frm_form_options_before_update', $options, $values ); |
| 34 |
$new_values['options'] = serialize( $options ); |
| 35 |
|
| 36 |
//if(isset($values['id']) && is_numeric($values['id'])) |
| 37 |
// $new_values['id'] = $values['id']; |
| 38 |
|
| 39 |
$wpdb->insert( $wpdb->prefix . 'frm_forms', $new_values ); |
| 40 |
|
| 41 |
$id = $wpdb->insert_id; |
| 42 |
|
| 43 |
// Clear form caching |
| 44 |
self::clear_form_cache(); |
| 45 |
|
| 46 |
return $id; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* @return int|boolean ID on success or false on failure |
| 51 |
*/ |
| 52 |
public static function duplicate( $id, $template = false, $copy_keys = false, $blog_id = false ) { |
| 53 |
global $wpdb; |
| 54 |
|
| 55 |
$values = self::getOne( $id, $blog_id ); |
| 56 |
if ( ! $values ) { |
| 57 |
return false; |
| 58 |
} |
| 59 |
|
| 60 |
$new_key = $copy_keys ? $values->form_key : ''; |
| 61 |
|
| 62 |
$new_values = array( |
| 63 |
'form_key' => FrmAppHelper::get_unique_key( $new_key, $wpdb->prefix . 'frm_forms', 'form_key' ), |
| 64 |
'name' => $values->name, |
| 65 |
'description' => $values->description, |
| 66 |
'status' => $values->status ? $values->status : 'published', |
| 67 |
'logged_in' => $values->logged_in ? $values->logged_in : 0, |
| 68 |
'editable' => $values->editable ? $values->editable : 0, |
| 69 |
'created_at' => current_time( 'mysql', 1 ), |
| 70 |
'is_template' => $template ? 1 : 0, |
| 71 |
); |
| 72 |
|
| 73 |
if ( $blog_id ) { |
| 74 |
$new_values['status'] = 'published'; |
| 75 |
$new_options = $values->options; |
| 76 |
FrmAppHelper::unserialize_or_decode( $new_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 |
|
| 100 |
return $form_id; |
| 101 |
} |
| 102 |
|
| 103 |
return false; |
| 104 |
} |
| 105 |
|
| 106 |
public static function after_duplicate( $form_id, $values ) { |
| 107 |
$new_opts = $values['options']; |
| 108 |
FrmAppHelper::unserialize_or_decode( $new_opts ); |
| 109 |
$values['options'] = $new_opts; |
| 110 |
|
| 111 |
if ( isset( $new_opts['success_msg'] ) ) { |
| 112 |
$new_opts['success_msg'] = FrmFieldsHelper::switch_field_ids( $new_opts['success_msg'] ); |
| 113 |
} |
| 114 |
|
| 115 |
$new_opts = apply_filters( 'frm_after_duplicate_form_values', $new_opts, $form_id ); |
| 116 |
|
| 117 |
if ( $new_opts != $values['options'] ) { |
| 118 |
global $wpdb; |
| 119 |
$wpdb->update( $wpdb->prefix . 'frm_forms', array( 'options' => maybe_serialize( $new_opts ) ), array( 'id' => $form_id ) ); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* @return int|boolean |
| 125 |
*/ |
| 126 |
public static function update( $id, $values, $create_link = false ) { |
| 127 |
global $wpdb; |
| 128 |
|
| 129 |
if ( ! isset( $values['status'] ) && ( $create_link || isset( $values['options'] ) || isset( $values['item_meta'] ) || isset( $values['field_options'] ) ) ) { |
| 130 |
$values['status'] = 'published'; |
| 131 |
} |
| 132 |
|
| 133 |
if ( isset( $values['form_key'] ) ) { |
| 134 |
$values['form_key'] = FrmAppHelper::get_unique_key( $values['form_key'], $wpdb->prefix . 'frm_forms', 'form_key', $id ); |
| 135 |
} |
| 136 |
|
| 137 |
$form_fields = array( 'form_key', 'name', 'description', 'status', 'parent_form_id' ); |
| 138 |
|
| 139 |
$new_values = self::set_update_options( array(), $values ); |
| 140 |
|
| 141 |
foreach ( $values as $value_key => $value ) { |
| 142 |
if ( $value_key && in_array( $value_key, $form_fields ) ) { |
| 143 |
$new_values[ $value_key ] = $value; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
if ( isset( $values['new_status'] ) && ! empty( $values['new_status'] ) ) { |
| 148 |
$new_values['status'] = $values['new_status']; |
| 149 |
} |
| 150 |
|
| 151 |
if ( ! empty( $new_values ) ) { |
| 152 |
$query_results = $wpdb->update( $wpdb->prefix . 'frm_forms', $new_values, array( 'id' => $id ) ); |
| 153 |
if ( $query_results ) { |
| 154 |
self::clear_form_cache(); |
| 155 |
} |
| 156 |
} else { |
| 157 |
$query_results = true; |
| 158 |
} |
| 159 |
unset( $new_values ); |
| 160 |
|
| 161 |
$values = self::update_fields( $id, $values ); |
| 162 |
|
| 163 |
do_action( 'frm_update_form', $id, $values ); |
| 164 |
do_action( 'frm_update_form_' . $id, $values ); |
| 165 |
|
| 166 |
return $query_results; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* @return array |
| 171 |
*/ |
| 172 |
public static function set_update_options( $new_values, $values ) { |
| 173 |
if ( ! isset( $values['options'] ) ) { |
| 174 |
return $new_values; |
| 175 |
} |
| 176 |
|
| 177 |
$options = isset( $values['options'] ) ? (array) $values['options'] : array(); |
| 178 |
FrmFormsHelper::fill_form_options( $options, $values ); |
| 179 |
|
| 180 |
$options['custom_style'] = isset( $values['options']['custom_style'] ) ? $values['options']['custom_style'] : 0; |
| 181 |
$options['before_html'] = isset( $values['options']['before_html'] ) ? $values['options']['before_html'] : FrmFormsHelper::get_default_html( 'before' ); |
| 182 |
$options['after_html'] = isset( $values['options']['after_html'] ) ? $values['options']['after_html'] : FrmFormsHelper::get_default_html( 'after' ); |
| 183 |
$options['submit_html'] = ( isset( $values['options']['submit_html'] ) && '' !== $values['options']['submit_html'] ) ? $values['options']['submit_html'] : FrmFormsHelper::get_default_html( 'submit' ); |
| 184 |
|
| 185 |
$options = apply_filters( 'frm_form_options_before_update', $options, $values ); |
| 186 |
$new_values['options'] = serialize( $options ); |
| 187 |
|
| 188 |
return $new_values; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* @return array |
| 193 |
*/ |
| 194 |
public static function update_fields( $id, $values ) { |
| 195 |
|
| 196 |
if ( ! isset( $values['item_meta'] ) && ! isset( $values['field_options'] ) ) { |
| 197 |
return $values; |
| 198 |
} |
| 199 |
|
| 200 |
$all_fields = FrmField::get_all_for_form( $id ); |
| 201 |
if ( empty( $all_fields ) ) { |
| 202 |
return $values; |
| 203 |
} |
| 204 |
|
| 205 |
if ( ! isset( $values['item_meta'] ) ) { |
| 206 |
$values['item_meta'] = array(); |
| 207 |
} |
| 208 |
|
| 209 |
$field_array = array(); |
| 210 |
$existing_keys = array_keys( $values['item_meta'] ); |
| 211 |
foreach ( $all_fields as $fid ) { |
| 212 |
if ( ! in_array( $fid->id, $existing_keys ) && ( isset( $values['frm_fields_submitted'] ) && in_array( $fid->id, $values['frm_fields_submitted'] ) ) || isset( $values['options'] ) ) { |
| 213 |
$values['item_meta'][ $fid->id ] = ''; |
| 214 |
} |
| 215 |
$field_array[ $fid->id ] = $fid; |
| 216 |
} |
| 217 |
unset( $all_fields ); |
| 218 |
|
| 219 |
foreach ( $values['item_meta'] as $field_id => $default_value ) { |
| 220 |
if ( isset( $field_array[ $field_id ] ) ) { |
| 221 |
$field = $field_array[ $field_id ]; |
| 222 |
} else { |
| 223 |
$field = FrmField::getOne( $field_id ); |
| 224 |
} |
| 225 |
|
| 226 |
if ( ! $field ) { |
| 227 |
continue; |
| 228 |
} |
| 229 |
|
| 230 |
$is_settings_page = ( isset( $values['options'] ) || isset( $values['field_options'][ 'custom_html_' . $field_id ] ) ); |
| 231 |
if ( $is_settings_page ) { |
| 232 |
self::get_settings_page_html( $values, $field ); |
| 233 |
|
| 234 |
if ( ! defined( 'WP_IMPORTING' ) ) { |
| 235 |
continue; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
//updating the form |
| 240 |
$update_options = FrmFieldsHelper::get_default_field_options_from_field( $field ); |
| 241 |
unset( $update_options['custom_html'] ); // don't check for POST html |
| 242 |
$update_options = apply_filters( 'frm_field_options_to_update', $update_options ); |
| 243 |
|
| 244 |
foreach ( $update_options as $opt => $default ) { |
| 245 |
$field->field_options[ $opt ] = isset( $values['field_options'][ $opt . '_' . $field_id ] ) ? $values['field_options'][ $opt . '_' . $field_id ] : $default; |
| 246 |
self::sanitize_field_opt( $opt, $field->field_options[ $opt ] ); |
| 247 |
} |
| 248 |
|
| 249 |
$field->field_options = apply_filters( 'frm_update_field_options', $field->field_options, $field, $values ); |
| 250 |
|
| 251 |
$new_field = array( |
| 252 |
'field_options' => $field->field_options, |
| 253 |
'default_value' => isset( $values[ 'default_value_' . $field_id ] ) ? FrmAppHelper::maybe_json_encode( $values[ 'default_value_' . $field_id ] ) : '', |
| 254 |
); |
| 255 |
|
| 256 |
self::prepare_field_update_values( $field, $values, $new_field ); |
| 257 |
|
| 258 |
FrmField::update( $field_id, $new_field ); |
| 259 |
|
| 260 |
FrmField::delete_form_transient( $field->form_id ); |
| 261 |
} |
| 262 |
self::clear_form_cache(); |
| 263 |
|
| 264 |
return $values; |
| 265 |
} |
| 266 |
|
| 267 |
private static function sanitize_field_opt( $opt, &$value ) { |
| 268 |
if ( is_string( $value ) ) { |
| 269 |
if ( $opt === 'calc' ) { |
| 270 |
$value = self::sanitize_calc( $value ); |
| 271 |
} else { |
| 272 |
$value = FrmAppHelper::kses( $value, 'all' ); |
| 273 |
} |
| 274 |
$value = trim( $value ); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* @param string $value |
| 280 |
* @return string |
| 281 |
*/ |
| 282 |
private static function sanitize_calc( $value ) { |
| 283 |
if ( false !== strpos( $value, '<' ) ) { |
| 284 |
$value = self::normalize_calc_spaces( $value ); |
| 285 |
} |
| 286 |
$allow = array( '<= ', ' >=' ); // Allow <= and >= |
| 287 |
$temp = array( '< = ', ' > =' ); |
| 288 |
$value = str_replace( $allow, $temp, $value ); |
| 289 |
$value = strip_tags( $value ); |
| 290 |
$value = str_replace( $temp, $allow, $value ); |
| 291 |
return $value; |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* Format a comparison like 5<10 to 5 < 10. Also works on 5< 10, 5 <10, 5<=10 variations. |
| 296 |
* This is to avoid an issue with unspaced calculations being recognized as HTML that gets removed when strip_tags is called. |
| 297 |
* |
| 298 |
* @param string $calc |
| 299 |
* @return string |
| 300 |
*/ |
| 301 |
private static function normalize_calc_spaces( $calc ) { |
| 302 |
// Check for a pattern with 5 parts |
| 303 |
// $1 \d the first comparison digit. |
| 304 |
// $2 a space (optional). |
| 305 |
// $3 an equals sign (optional) that follows the < operator for <= comparisons. |
| 306 |
// $4 another space (optional). |
| 307 |
// $5 \d the second comparison digit. |
| 308 |
return preg_replace( '/(\d)( ){0,1}<(=){0,1}( ){0,1}(\d)/', '$1 <$3 $5', $calc ); |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* Updating the settings page |
| 313 |
*/ |
| 314 |
private static function get_settings_page_html( $values, &$field ) { |
| 315 |
if ( isset( $values['field_options'][ 'custom_html_' . $field->id ] ) ) { |
| 316 |
$prev_opts = array(); |
| 317 |
$fallback_html = isset( $field->field_options['custom_html'] ) ? $field->field_options['custom_html'] : FrmFieldsHelper::get_default_html( $field->type ); |
| 318 |
|
| 319 |
$field->field_options['custom_html'] = isset( $values['field_options'][ 'custom_html_' . $field->id ] ) ? $values['field_options'][ 'custom_html_' . $field->id ] : $fallback_html; |
| 320 |
} elseif ( $field->type == 'hidden' || $field->type == 'user_id' ) { |
| 321 |
$prev_opts = $field->field_options; |
| 322 |
} |
| 323 |
|
| 324 |
if ( isset( $prev_opts ) ) { |
| 325 |
$field->field_options = apply_filters( 'frm_update_form_field_options', $field->field_options, $field, $values ); |
| 326 |
if ( $prev_opts != $field->field_options ) { |
| 327 |
FrmField::update( $field->id, array( 'field_options' => $field->field_options ) ); |
| 328 |
} |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
private static function prepare_field_update_values( $field, $values, &$new_field ) { |
| 333 |
$field_cols = array( |
| 334 |
'field_order' => 0, |
| 335 |
'field_key' => '', |
| 336 |
'required' => false, |
| 337 |
'type' => '', |
| 338 |
'description' => '', |
| 339 |
'options' => '', |
| 340 |
'name' => '', |
| 341 |
); |
| 342 |
foreach ( $field_cols as $col => $default ) { |
| 343 |
$default = ( $default === '' ) ? $field->{$col} : $default; |
| 344 |
|
| 345 |
$new_field[ $col ] = isset( $values['field_options'][ $col . '_' . $field->id ] ) ? $values['field_options'][ $col . '_' . $field->id ] : $default; |
| 346 |
} |
| 347 |
|
| 348 |
// Don't save the template option. |
| 349 |
if ( is_array( $new_field['options'] ) && isset( $new_field['options']['000'] ) ) { |
| 350 |
unset( $new_field['options']['000'] ); |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
/** |
| 355 |
* Get a list of all form settings that should be translated |
| 356 |
* on a multilingual site. |
| 357 |
* |
| 358 |
* @since 3.06.01 |
| 359 |
* @param object $form - The form object |
| 360 |
*/ |
| 361 |
public static function translatable_strings( $form ) { |
| 362 |
$strings = array( |
| 363 |
'name', |
| 364 |
'description', |
| 365 |
'submit_value', |
| 366 |
'submit_msg', |
| 367 |
'success_msg', |
| 368 |
); |
| 369 |
|
| 370 |
return apply_filters( 'frm_form_strings', $strings, $form ); |
| 371 |
} |
| 372 |
|
| 373 |
/** |
| 374 |
* @param string $status |
| 375 |
* |
| 376 |
* @return int|boolean |
| 377 |
*/ |
| 378 |
public static function set_status( $id, $status ) { |
| 379 |
if ( 'trash' == $status ) { |
| 380 |
return self::trash( $id ); |
| 381 |
} |
| 382 |
|
| 383 |
$statuses = array( 'published', 'draft', 'trash' ); |
| 384 |
if ( ! in_array( $status, $statuses ) ) { |
| 385 |
return false; |
| 386 |
} |
| 387 |
|
| 388 |
global $wpdb; |
| 389 |
|
| 390 |
if ( is_array( $id ) ) { |
| 391 |
$where = array( |
| 392 |
'id' => $id, |
| 393 |
'parent_form_id' => $id, |
| 394 |
'or' => 1, |
| 395 |
); |
| 396 |
FrmDb::get_where_clause_and_values( $where ); |
| 397 |
array_unshift( $where['values'], $status ); |
| 398 |
|
| 399 |
$query_results = $wpdb->query( $wpdb->prepare( 'UPDATE ' . $wpdb->prefix . 'frm_forms SET status = %s ' . $where['where'], $where['values'] ) ); // WPCS: unprepared SQL ok. |
| 400 |
} else { |
| 401 |
$query_results = $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'status' => $status ), array( 'id' => $id ) ); |
| 402 |
$wpdb->update( $wpdb->prefix . 'frm_forms', array( 'status' => $status ), array( 'parent_form_id' => $id ) ); |
| 403 |
} |
| 404 |
|
| 405 |
if ( $query_results ) { |
| 406 |
self::clear_form_cache(); |
| 407 |
} |
| 408 |
|
| 409 |
return $query_results; |
| 410 |
} |
| 411 |
|
| 412 |
/** |
| 413 |
* @return int|boolean |
| 414 |
*/ |
| 415 |
public static function trash( $id ) { |
| 416 |
if ( ! EMPTY_TRASH_DAYS ) { |
| 417 |
return self::destroy( $id ); |
| 418 |
} |
| 419 |
|
| 420 |
$form = self::getOne( $id ); |
| 421 |
if ( ! $form ) { |
| 422 |
return false; |
| 423 |
} |
| 424 |
|
| 425 |
$options = $form->options; |
| 426 |
$options['trash_time'] = time(); |
| 427 |
|
| 428 |
global $wpdb; |
| 429 |
$query_results = $wpdb->update( |
| 430 |
$wpdb->prefix . 'frm_forms', |
| 431 |
array( |
| 432 |
'status' => 'trash', |
| 433 |
'options' => serialize( $options ), |
| 434 |
), |
| 435 |
array( |
| 436 |
'id' => $id, |
| 437 |
) |
| 438 |
); |
| 439 |
|
| 440 |
$wpdb->update( |
| 441 |
$wpdb->prefix . 'frm_forms', |
| 442 |
array( |
| 443 |
'status' => 'trash', |
| 444 |
'options' => serialize( $options ), |
| 445 |
), |
| 446 |
array( |
| 447 |
'parent_form_id' => $id, |
| 448 |
) |
| 449 |
); |
| 450 |
|
| 451 |
if ( $query_results ) { |
| 452 |
self::clear_form_cache(); |
| 453 |
} |
| 454 |
|
| 455 |
return $query_results; |
| 456 |
} |
| 457 |
|
| 458 |
/** |
| 459 |
* @return int|boolean |
| 460 |
*/ |
| 461 |
public static function destroy( $id ) { |
| 462 |
global $wpdb; |
| 463 |
|
| 464 |
$form = self::getOne( $id ); |
| 465 |
if ( ! $form ) { |
| 466 |
return false; |
| 467 |
} |
| 468 |
$id = $form->id; |
| 469 |
|
| 470 |
// Disconnect the entries from this form |
| 471 |
$entries = FrmDb::get_col( $wpdb->prefix . 'frm_items', array( 'form_id' => $id ) ); |
| 472 |
foreach ( $entries as $entry_id ) { |
| 473 |
FrmEntry::destroy( $entry_id ); |
| 474 |
unset( $entry_id ); |
| 475 |
} |
| 476 |
|
| 477 |
// Disconnect the fields from this form |
| 478 |
$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 ) ); |
| 479 |
|
| 480 |
$query_results = $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_forms WHERE id=%d OR parent_form_id=%d', $id, $id ) ); |
| 481 |
if ( $query_results ) { |
| 482 |
// Delete all form actions linked to this form |
| 483 |
$action_control = FrmFormActionsController::get_form_actions( 'email' ); |
| 484 |
$action_control->destroy( $id, 'all' ); |
| 485 |
|
| 486 |
// Clear form caching |
| 487 |
self::clear_form_cache(); |
| 488 |
|
| 489 |
do_action( 'frm_destroy_form', $id ); |
| 490 |
do_action( 'frm_destroy_form_' . $id ); |
| 491 |
} |
| 492 |
|
| 493 |
return $query_results; |
| 494 |
} |
| 495 |
|
| 496 |
/** |
| 497 |
* Delete trashed forms based on how long they have been trashed |
| 498 |
* |
| 499 |
* @return int The number of forms deleted |
| 500 |
*/ |
| 501 |
public static function scheduled_delete( $delete_timestamp = '' ) { |
| 502 |
global $wpdb; |
| 503 |
|
| 504 |
$trash_forms = FrmDb::get_results( $wpdb->prefix . 'frm_forms', array( 'status' => 'trash' ), 'id, options' ); |
| 505 |
|
| 506 |
if ( ! $trash_forms ) { |
| 507 |
return; |
| 508 |
} |
| 509 |
|
| 510 |
if ( empty( $delete_timestamp ) ) { |
| 511 |
$delete_timestamp = time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS ); |
| 512 |
} |
| 513 |
|
| 514 |
$count = 0; |
| 515 |
foreach ( $trash_forms as $form ) { |
| 516 |
FrmAppHelper::unserialize_or_decode( $form->options ); |
| 517 |
if ( ! isset( $form->options['trash_time'] ) || $form->options['trash_time'] < $delete_timestamp ) { |
| 518 |
self::destroy( $form->id ); |
| 519 |
$count ++; |
| 520 |
} |
| 521 |
|
| 522 |
unset( $form ); |
| 523 |
} |
| 524 |
|
| 525 |
return $count; |
| 526 |
} |
| 527 |
|
| 528 |
/** |
| 529 |
* @return string form name |
| 530 |
*/ |
| 531 |
public static function getName( $id ) { |
| 532 |
$form = FrmDb::check_cache( $id, 'frm_form' ); |
| 533 |
if ( $form ) { |
| 534 |
$r = stripslashes( $form->name ); |
| 535 |
|
| 536 |
return $r; |
| 537 |
} |
| 538 |
|
| 539 |
$query_key = is_numeric( $id ) ? 'id' : 'form_key'; |
| 540 |
$r = FrmDb::get_var( 'frm_forms', array( $query_key => $id ), 'name' ); |
| 541 |
$r = stripslashes( $r ); |
| 542 |
|
| 543 |
return $r; |
| 544 |
} |
| 545 |
|
| 546 |
/** |
| 547 |
* @since 3.0 |
| 548 |
* |
| 549 |
* @param string $key |
| 550 |
* |
| 551 |
* @return int form id |
| 552 |
*/ |
| 553 |
public static function get_id_by_key( $key ) { |
| 554 |
return (int) FrmDb::get_var( 'frm_forms', array( 'form_key' => sanitize_title( $key ) ) ); |
| 555 |
} |
| 556 |
|
| 557 |
/** |
| 558 |
* @since 3.0 |
| 559 |
* |
| 560 |
* @param int $id |
| 561 |
* |
| 562 |
* @return string form key |
| 563 |
*/ |
| 564 |
public static function get_key_by_id( $id ) { |
| 565 |
$id = (int) $id; |
| 566 |
$cache = FrmDb::check_cache( $id, 'frm_form' ); |
| 567 |
if ( $cache ) { |
| 568 |
return $cache->form_key; |
| 569 |
} |
| 570 |
|
| 571 |
$key = FrmDb::get_var( 'frm_forms', array( 'id' => $id ), 'form_key' ); |
| 572 |
|
| 573 |
return $key; |
| 574 |
} |
| 575 |
|
| 576 |
/** |
| 577 |
* If $form is numeric, get the form object |
| 578 |
* |
| 579 |
* @param object|int $form |
| 580 |
* |
| 581 |
* @since 2.0.9 |
| 582 |
*/ |
| 583 |
public static function maybe_get_form( &$form ) { |
| 584 |
if ( ! is_object( $form ) && ! is_array( $form ) && ! empty( $form ) ) { |
| 585 |
$form = self::getOne( $form ); |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
/** |
| 590 |
* @return object form |
| 591 |
*/ |
| 592 |
public static function getOne( $id, $blog_id = false ) { |
| 593 |
global $wpdb; |
| 594 |
|
| 595 |
if ( $blog_id && is_multisite() ) { |
| 596 |
global $wpmuBaseTablePrefix; |
| 597 |
$prefix = $wpmuBaseTablePrefix ? $wpmuBaseTablePrefix . $blog_id . '_' : $wpdb->get_blog_prefix( $blog_id ); |
| 598 |
|
| 599 |
$table_name = $prefix . 'frm_forms'; |
| 600 |
} else { |
| 601 |
$table_name = $wpdb->prefix . 'frm_forms'; |
| 602 |
$cache = wp_cache_get( $id, 'frm_form' ); |
| 603 |
if ( $cache ) { |
| 604 |
if ( isset( $cache->options ) ) { |
| 605 |
FrmAppHelper::unserialize_or_decode( $cache->options ); |
| 606 |
} |
| 607 |
|
| 608 |
return wp_unslash( $cache ); |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
if ( is_numeric( $id ) ) { |
| 613 |
$where = array( 'id' => $id ); |
| 614 |
} else { |
| 615 |
$where = array( 'form_key' => $id ); |
| 616 |
} |
| 617 |
|
| 618 |
$results = FrmDb::get_row( $table_name, $where ); |
| 619 |
|
| 620 |
if ( isset( $results->options ) ) { |
| 621 |
FrmDb::set_cache( $results->id, $results, 'frm_form' ); |
| 622 |
FrmAppHelper::unserialize_or_decode( $results->options ); |
| 623 |
} |
| 624 |
|
| 625 |
return apply_filters( 'frm_form_object', wp_unslash( $results ) ); |
| 626 |
} |
| 627 |
|
| 628 |
/** |
| 629 |
* @return object|array of objects |
| 630 |
*/ |
| 631 |
public static function getAll( $where = array(), $order_by = '', $limit = '' ) { |
| 632 |
if ( is_array( $where ) && ! empty( $where ) ) { |
| 633 |
if ( isset( $where['is_template'] ) && $where['is_template'] && ! isset( $where['status'] ) && ! isset( $where['status !'] ) ) { |
| 634 |
// don't get trashed templates |
| 635 |
$where['status'] = array( null, '', 'published' ); |
| 636 |
} |
| 637 |
|
| 638 |
$results = FrmDb::get_results( 'frm_forms', $where, '*', compact( 'order_by', 'limit' ) ); |
| 639 |
} else { |
| 640 |
global $wpdb; |
| 641 |
|
| 642 |
// the query has already been prepared if this is not an array |
| 643 |
$query = 'SELECT * FROM ' . $wpdb->prefix . 'frm_forms' . FrmDb::prepend_and_or_where( ' WHERE ', $where ) . FrmDb::esc_order( $order_by ) . FrmDb::esc_limit( $limit ); |
| 644 |
$results = $wpdb->get_results( $query ); // WPCS: unprepared SQL ok. |
| 645 |
} |
| 646 |
|
| 647 |
if ( $results ) { |
| 648 |
foreach ( $results as $result ) { |
| 649 |
FrmDb::set_cache( $result->id, $result, 'frm_form' ); |
| 650 |
FrmAppHelper::unserialize_or_decode( $result->options ); |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
if ( $limit == ' LIMIT 1' || $limit == 1 ) { |
| 655 |
// return the first form object if we are only getting one form |
| 656 |
$results = reset( $results ); |
| 657 |
} |
| 658 |
|
| 659 |
return wp_unslash( $results ); |
| 660 |
} |
| 661 |
|
| 662 |
/** |
| 663 |
* Get all published forms |
| 664 |
* |
| 665 |
* @since 2.0 |
| 666 |
* @return array of forms |
| 667 |
*/ |
| 668 |
public static function get_published_forms( $query = array(), $limit = 999, $inc_children = 'exclude' ) { |
| 669 |
$query['is_template'] = 0; |
| 670 |
$query['status'] = array( null, '', 'published' ); |
| 671 |
if ( $inc_children == 'exclude' ) { |
| 672 |
$query['parent_form_id'] = array( null, 0 ); |
| 673 |
} |
| 674 |
|
| 675 |
$forms = self::getAll( $query, 'name', $limit ); |
| 676 |
|
| 677 |
return $forms; |
| 678 |
} |
| 679 |
|
| 680 |
/** |
| 681 |
* @return object count of forms |
| 682 |
*/ |
| 683 |
public static function get_count() { |
| 684 |
global $wpdb; |
| 685 |
|
| 686 |
$cache_key = 'frm_form_counts'; |
| 687 |
|
| 688 |
$counts = wp_cache_get( $cache_key, 'frm_form' ); |
| 689 |
if ( false !== $counts ) { |
| 690 |
return $counts; |
| 691 |
} |
| 692 |
|
| 693 |
$results = (array) FrmDb::get_results( |
| 694 |
'frm_forms', |
| 695 |
array( |
| 696 |
'or' => 1, |
| 697 |
'parent_form_id' => null, |
| 698 |
'parent_form_id <' => 0, |
| 699 |
), |
| 700 |
'status, is_template' |
| 701 |
); |
| 702 |
|
| 703 |
$statuses = array( 'published', 'draft', 'template', 'trash' ); |
| 704 |
$counts = array_fill_keys( $statuses, 0 ); |
| 705 |
|
| 706 |
foreach ( $results as $row ) { |
| 707 |
if ( 'trash' != $row->status ) { |
| 708 |
if ( $row->is_template ) { |
| 709 |
$counts['template'] ++; |
| 710 |
} else { |
| 711 |
$counts['published'] ++; |
| 712 |
} |
| 713 |
} else { |
| 714 |
$counts['trash'] ++; |
| 715 |
} |
| 716 |
|
| 717 |
if ( 'draft' == $row->status ) { |
| 718 |
$counts['draft'] ++; |
| 719 |
} |
| 720 |
|
| 721 |
unset( $row ); |
| 722 |
} |
| 723 |
|
| 724 |
$counts = (object) $counts; |
| 725 |
FrmDb::set_cache( $cache_key, $counts, 'frm_form' ); |
| 726 |
|
| 727 |
return $counts; |
| 728 |
} |
| 729 |
|
| 730 |
/** |
| 731 |
* Clear form caching |
| 732 |
* Called when a form is created, updated, duplicated, or deleted |
| 733 |
* or when the form status is changed |
| 734 |
* |
| 735 |
* @since 2.0.4 |
| 736 |
*/ |
| 737 |
public static function clear_form_cache() { |
| 738 |
FrmDb::cache_delete_group( 'frm_form' ); |
| 739 |
} |
| 740 |
|
| 741 |
/** |
| 742 |
* @return array of errors |
| 743 |
*/ |
| 744 |
public static function validate( $values ) { |
| 745 |
$errors = array(); |
| 746 |
|
| 747 |
return apply_filters( 'frm_validate_form', $errors, $values ); |
| 748 |
} |
| 749 |
|
| 750 |
public static function get_params( $form = null ) { |
| 751 |
global $frm_vars; |
| 752 |
|
| 753 |
if ( ! $form ) { |
| 754 |
$form = self::getAll( array(), 'name', 1 ); |
| 755 |
} else { |
| 756 |
self::maybe_get_form( $form ); |
| 757 |
} |
| 758 |
|
| 759 |
if ( isset( $frm_vars['form_params'] ) && is_array( $frm_vars['form_params'] ) && isset( $frm_vars['form_params'][ $form->id ] ) ) { |
| 760 |
return $frm_vars['form_params'][ $form->id ]; |
| 761 |
} |
| 762 |
|
| 763 |
$action_var = isset( $_REQUEST['frm_action'] ) ? 'frm_action' : 'action'; // WPCS: CSRF ok. |
| 764 |
$action = apply_filters( 'frm_show_new_entry_page', FrmAppHelper::get_param( $action_var, 'new', 'get', 'sanitize_title' ), $form ); |
| 765 |
|
| 766 |
$default_values = array( |
| 767 |
'id' => '', |
| 768 |
'form_name' => '', |
| 769 |
'paged' => 1, |
| 770 |
'form' => $form->id, |
| 771 |
'form_id' => $form->id, |
| 772 |
'field_id' => '', |
| 773 |
'search' => '', |
| 774 |
'sort' => '', |
| 775 |
'sdir' => '', |
| 776 |
'action' => $action, |
| 777 |
); |
| 778 |
|
| 779 |
$values = array(); |
| 780 |
$values['posted_form_id'] = FrmAppHelper::get_param( 'form_id', '', 'get', 'absint' ); |
| 781 |
if ( ! $values['posted_form_id'] ) { |
| 782 |
$values['posted_form_id'] = FrmAppHelper::get_param( 'form', '', 'get', 'absint' ); |
| 783 |
} |
| 784 |
|
| 785 |
if ( $form->id == $values['posted_form_id'] ) { |
| 786 |
//if there are two forms on the same page, make sure not to submit both |
| 787 |
foreach ( $default_values as $var => $default ) { |
| 788 |
if ( $var == 'action' ) { |
| 789 |
$values[ $var ] = FrmAppHelper::get_param( $action_var, $default, 'get', 'sanitize_title' ); |
| 790 |
} else { |
| 791 |
$values[ $var ] = FrmAppHelper::get_param( $var, $default, 'get', 'sanitize_text_field' ); |
| 792 |
} |
| 793 |
unset( $var, $default ); |
| 794 |
} |
| 795 |
} else { |
| 796 |
foreach ( $default_values as $var => $default ) { |
| 797 |
$values[ $var ] = $default; |
| 798 |
unset( $var, $default ); |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
if ( in_array( $values['action'], array( 'create', 'update' ) ) && |
| 803 |
( ! $_POST || ( ! isset( $_POST['action'] ) && ! isset( $_POST['frm_action'] ) ) ) // WPCS: CSRF ok. |
| 804 |
) { |
| 805 |
$values['action'] = 'new'; |
| 806 |
} |
| 807 |
|
| 808 |
return $values; |
| 809 |
} |
| 810 |
|
| 811 |
public static function list_page_params() { |
| 812 |
$values = array(); |
| 813 |
$defaults = array( |
| 814 |
'template' => 0, |
| 815 |
'id' => '', |
| 816 |
'paged' => 1, |
| 817 |
'form' => '', |
| 818 |
'search' => '', |
| 819 |
'sort' => '', |
| 820 |
'sdir' => '', |
| 821 |
); |
| 822 |
foreach ( $defaults as $var => $default ) { |
| 823 |
$values[ $var ] = FrmAppHelper::get_param( $var, $default, 'get', 'sanitize_text_field' ); |
| 824 |
} |
| 825 |
|
| 826 |
return $values; |
| 827 |
} |
| 828 |
|
| 829 |
public static function get_admin_params( $form = null ) { |
| 830 |
$form_id = $form; |
| 831 |
if ( $form === null ) { |
| 832 |
$form_id = self::get_current_form_id(); |
| 833 |
} elseif ( $form && is_object( $form ) ) { |
| 834 |
$form_id = $form->id; |
| 835 |
} |
| 836 |
|
| 837 |
$values = array(); |
| 838 |
$defaults = array( |
| 839 |
'id' => '', |
| 840 |
'form_name' => '', |
| 841 |
'paged' => 1, |
| 842 |
'form' => $form_id, |
| 843 |
'field_id' => '', |
| 844 |
'search' => '', |
| 845 |
'sort' => '', |
| 846 |
'sdir' => '', |
| 847 |
'fid' => '', |
| 848 |
'keep_post' => '', |
| 849 |
); |
| 850 |
foreach ( $defaults as $var => $default ) { |
| 851 |
$values[ $var ] = FrmAppHelper::get_param( $var, $default, 'get', 'sanitize_text_field' ); |
| 852 |
} |
| 853 |
|
| 854 |
return $values; |
| 855 |
} |
| 856 |
|
| 857 |
public static function get_current_form_id( $default_form = 'none' ) { |
| 858 |
if ( 'first' == $default_form ) { |
| 859 |
$form = self::get_current_form(); |
| 860 |
} else { |
| 861 |
$form = self::maybe_get_current_form(); |
| 862 |
} |
| 863 |
$form_id = $form ? $form->id : 0; |
| 864 |
|
| 865 |
return $form_id; |
| 866 |
} |
| 867 |
|
| 868 |
public static function maybe_get_current_form( $form_id = 0 ) { |
| 869 |
global $frm_vars; |
| 870 |
|
| 871 |
if ( isset( $frm_vars['current_form'] ) && $frm_vars['current_form'] && ( ! $form_id || $form_id == $frm_vars['current_form']->id ) ) { |
| 872 |
return $frm_vars['current_form']; |
| 873 |
} |
| 874 |
|
| 875 |
$form_id = FrmAppHelper::get_param( 'form', $form_id, 'get', 'absint' ); |
| 876 |
if ( $form_id ) { |
| 877 |
$form_id = self::set_current_form( $form_id ); |
| 878 |
} |
| 879 |
|
| 880 |
return $form_id; |
| 881 |
} |
| 882 |
|
| 883 |
public static function get_current_form( $form_id = 0 ) { |
| 884 |
$form = self::maybe_get_current_form( $form_id ); |
| 885 |
if ( is_numeric( $form ) ) { |
| 886 |
$form = self::set_current_form( $form ); |
| 887 |
} |
| 888 |
|
| 889 |
return $form; |
| 890 |
} |
| 891 |
|
| 892 |
public static function set_current_form( $form_id ) { |
| 893 |
global $frm_vars; |
| 894 |
|
| 895 |
$query = array(); |
| 896 |
if ( $form_id ) { |
| 897 |
$query['id'] = $form_id; |
| 898 |
} |
| 899 |
|
| 900 |
$frm_vars['current_form'] = self::get_published_forms( $query, 1 ); |
| 901 |
|
| 902 |
return $frm_vars['current_form']; |
| 903 |
} |
| 904 |
|
| 905 |
public static function is_form_loaded( $form, $this_load, $global_load ) { |
| 906 |
global $frm_vars; |
| 907 |
$small_form = new stdClass(); |
| 908 |
foreach ( array( 'id', 'form_key', 'name' ) as $var ) { |
| 909 |
$small_form->{$var} = $form->{$var}; |
| 910 |
unset( $var ); |
| 911 |
} |
| 912 |
|
| 913 |
$frm_vars['forms_loaded'][] = $small_form; |
| 914 |
|
| 915 |
if ( $this_load && empty( $global_load ) ) { |
| 916 |
$global_load = true; |
| 917 |
$frm_vars['load_css'] = true; |
| 918 |
} |
| 919 |
|
| 920 |
return ( ( ! isset( $frm_vars['css_loaded'] ) || ! $frm_vars['css_loaded'] ) && $global_load ); |
| 921 |
} |
| 922 |
|
| 923 |
/** |
| 924 |
* @since 4.06.03 |
| 925 |
* |
| 926 |
* @param object $form |
| 927 |
* |
| 928 |
* @return bool |
| 929 |
*/ |
| 930 |
public static function &is_visible_to_user( $form ) { |
| 931 |
if ( $form->logged_in && isset( $form->options['logged_in_role'] ) ) { |
| 932 |
$visible = FrmAppHelper::user_has_permission( $form->options['logged_in_role'] ); |
| 933 |
} else { |
| 934 |
$visible = true; |
| 935 |
} |
| 936 |
|
| 937 |
return $visible; |
| 938 |
} |
| 939 |
|
| 940 |
public static function show_submit( $form ) { |
| 941 |
$show = ( ! $form->is_template && $form->status == 'published' && ! FrmAppHelper::is_admin() ); |
| 942 |
$show = apply_filters( 'frm_show_submit_button', $show, $form ); |
| 943 |
|
| 944 |
return $show; |
| 945 |
} |
| 946 |
|
| 947 |
/** |
| 948 |
* @since 2.3 |
| 949 |
*/ |
| 950 |
public static function get_option( $atts ) { |
| 951 |
$form = $atts['form']; |
| 952 |
$default = isset( $atts['default'] ) ? $atts['default'] : ''; |
| 953 |
|
| 954 |
return isset( $form->options[ $atts['option'] ] ) ? $form->options[ $atts['option'] ] : $default; |
| 955 |
} |
| 956 |
|
| 957 |
/** |
| 958 |
* Get the link to edit this form. |
| 959 |
* |
| 960 |
* @since 4.0 |
| 961 |
* @param int $form_id The id of the form. |
| 962 |
*/ |
| 963 |
public static function get_edit_link( $form_id ) { |
| 964 |
return admin_url( 'admin.php?page=formidable&frm_action=edit&id=' . $form_id ); |
| 965 |
} |
| 966 |
|
| 967 |
/** |
| 968 |
* @deprecated 3.0 |
| 969 |
* @codeCoverageIgnore |
| 970 |
* |
| 971 |
* @param string $key |
| 972 |
* |
| 973 |
* @return int form id |
| 974 |
*/ |
| 975 |
public static function getIdByKey( $key ) { |
| 976 |
return FrmFormDeprecated::getIdByKey( $key ); |
| 977 |
} |
| 978 |
|
| 979 |
/** |
| 980 |
* @deprecated 3.0 |
| 981 |
* @codeCoverageIgnore |
| 982 |
*/ |
| 983 |
public static function getKeyById( $id ) { |
| 984 |
return FrmFormDeprecated::getKeyById( $id ); |
| 985 |
} |
| 986 |
} |
| 987 |
|