| @@ -16,9 +16,9 @@ | ||
| 16 | 16 | $values = FrmAppHelper::maybe_filter_array( $values, array( 'name', 'description' ) ); |
| 17 | 17 | |
| 18 | 18 | $new_values = array( |
| 19 | 19 | 'form_key' => FrmAppHelper::get_unique_key( $values['form_key'], $wpdb->prefix . 'frm_forms', 'form_key' ), |
| 20 | - 'name' => $values['name'], | |
| 20 | + 'name' => FrmAppHelper::truncate( $values['name'], 255, 1, '', true ), | |
| 21 | 21 | 'description' => $values['description'], |
| 22 | 22 | 'status' => $values['status'] ?? 'published', |
| 23 | 23 | 'logged_in' => $values['logged_in'] ?? 0, |
| 24 | 24 | 'is_template' => isset( $values['is_template'] ) ? (int) $values['is_template'] : 0, |
| @@ -126,9 +126,9 @@ | ||
| 126 | 126 | |
| 127 | 127 | $form_id = $wpdb->insert_id; |
| 128 | 128 | FrmField::duplicate( $id, $form_id, $copy_keys, $blog_id ); |
| 129 | 129 | |
| 130 | - // update form settings after fields are created | |
| 130 | + // Update form settings after fields are created | |
| 131 | 131 | do_action( 'frm_after_duplicate_form', $form_id, $new_values, array( 'old_id' => $id ) ); |
| 132 | 132 | |
| 133 | 133 | return $form_id; |
| 134 | 134 | } |
| @@ -152,8 +152,9 @@ | ||
| 152 | 152 | } |
| 153 | 153 | |
| 154 | 154 | $new_opts = apply_filters( 'frm_after_duplicate_form_values', $new_opts, $form_id ); |
| 155 | 155 | |
| 156 | + // phpcs:ignore Universal.Operators.StrictComparisons | |
| 156 | 157 | if ( $new_opts != $values['options'] ) { |
| 157 | 158 | global $wpdb; |
| 158 | 159 | $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'options' => maybe_serialize( $new_opts ) ), array( 'id' => $form_id ) ); |
| 159 | 160 | } |
| @@ -164,8 +165,10 @@ | ||
| 164 | 165 | /** |
| 165 | 166 | * Switches field ID in fields. |
| 166 | 167 | * |
| 167 | 168 | * @since 5.3 |
| 169 | + * @since 6.35 The description column is checked too, so a field id in a description survives a | |
| 170 | + * duplicate or import when the field it points at is created afterwards. | |
| 168 | 171 | * |
| 169 | 172 | * @param int $form_id Form ID. |
| 170 | 173 | * |
| 171 | 174 | * @return void |
| @@ -173,9 +176,9 @@ | ||
| 173 | 176 | private static function switch_field_ids_in_fields( $form_id ) { |
| 174 | 177 | global $wpdb; |
| 175 | 178 | |
| 176 | 179 | // Keys of fields that you want to check to replace field ID. |
| 177 | - $keys = array( 'default_value', 'field_options' ); | |
| 180 | + $keys = array( 'default_value', 'description', 'field_options' ); | |
| 178 | 181 | $sql_cols = 'fi.id'; |
| 179 | 182 | |
| 180 | 183 | foreach ( $keys as $key ) { |
| 181 | 184 | $sql_cols .= ',fi.' . $key; |
| @@ -234,9 +237,9 @@ | ||
| 234 | 237 | $new_values[ $key ] = $new_val; |
| 235 | 238 | } |
| 236 | 239 | }//end foreach |
| 237 | 240 | |
| 238 | - if ( ! empty( $new_values ) ) { | |
| 241 | + if ( $new_values ) { | |
| 239 | 242 | FrmField::update( $field['id'], $new_values ); |
| 240 | 243 | } |
| 241 | 244 | } |
| 242 | 245 | |
| @@ -262,14 +265,13 @@ | ||
| 262 | 265 | $values['form_key'] = FrmAppHelper::get_unique_key( $values['form_key'], $wpdb->prefix . 'frm_forms', 'form_key', $id ); |
| 263 | 266 | } |
| 264 | 267 | |
| 265 | 268 | $form_fields = array( 'form_key', 'name', 'description', 'status', 'parent_form_id' ); |
| 269 | + $new_values = self::set_update_options( array(), $values, array( 'form_id' => $id ) ); | |
| 266 | 270 | |
| 267 | - $new_values = self::set_update_options( array(), $values, array( 'form_id' => $id ) ); | |
| 268 | - | |
| 269 | 271 | foreach ( $values as $value_key => $value ) { |
| 270 | - if ( $value_key && in_array( $value_key, $form_fields ) ) { | |
| 271 | - $new_values[ $value_key ] = $value; | |
| 272 | + if ( $value_key && in_array( $value_key, $form_fields, true ) ) { | |
| 273 | + $new_values[ $value_key ] = 'name' === $value_key ? FrmAppHelper::truncate( $value, 255, 1, '', true ) : $value; | |
| 272 | 274 | } |
| 273 | 275 | } |
| 274 | 276 | |
| 275 | 277 | if ( ! empty( $values['new_status'] ) ) { |
| @@ -275,9 +277,9 @@ | ||
| 275 | 277 | if ( ! empty( $values['new_status'] ) ) { |
| 276 | 278 | $new_values['status'] = $values['new_status']; |
| 277 | 279 | } |
| 278 | 280 | |
| 279 | - if ( ! empty( $new_values ) ) { | |
| 281 | + if ( $new_values ) { | |
| 280 | 282 | $query_results = $wpdb->update( $wpdb->prefix . 'frm_forms', $new_values, array( 'id' => $id ) ); |
| 281 | 283 | |
| 282 | 284 | if ( $query_results ) { |
| 283 | 285 | self::clear_form_cache(); |
| @@ -312,9 +314,9 @@ | ||
| 312 | 314 | |
| 313 | 315 | $options['custom_style'] = $values['options']['custom_style'] ?? 0; |
| 314 | 316 | $options['before_html'] = $values['options']['before_html'] ?? FrmFormsHelper::get_default_html( 'before' ); |
| 315 | 317 | $options['after_html'] = $values['options']['after_html'] ?? FrmFormsHelper::get_default_html( 'after' ); |
| 316 | - $options['submit_html'] = isset( $values['options']['submit_html'] ) && '' !== $values['options']['submit_html'] ? $values['options']['submit_html'] : FrmFormsHelper::get_default_html( 'submit' ); | |
| 318 | + $options['submit_html'] = isset( $values['options']['submit_html'] ) && '' !== $values['options']['submit_html'] ? $values['options']['submit_html'] : FrmFormsHelper::get_default_html( 'submit' ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 317 | 319 | |
| 318 | 320 | /** |
| 319 | 321 | * Allows modifying form options before updating or creating. |
| 320 | 322 | * |
| @@ -336,9 +338,9 @@ | ||
| 336 | 338 | * @param array $values Form values array. |
| 337 | 339 | * |
| 338 | 340 | * @return array |
| 339 | 341 | */ |
| 340 | - public static function update_fields( $id, $values ) { | |
| 342 | + public static function update_fields( $id, $values ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh | |
| 341 | 343 | |
| 342 | 344 | if ( ! isset( $values['item_meta'] ) && ! isset( $values['field_options'] ) ) { |
| 343 | 345 | return $values; |
| 344 | 346 | } |
| @@ -344,9 +346,9 @@ | ||
| 344 | 346 | } |
| 345 | 347 | |
| 346 | 348 | $all_fields = FrmField::get_all_for_form( $id ); |
| 347 | 349 | |
| 348 | - if ( empty( $all_fields ) ) { | |
| 350 | + if ( ! $all_fields ) { | |
| 349 | 351 | return $values; |
| 350 | 352 | } |
| 351 | 353 | |
| 352 | 354 | if ( ! isset( $values['item_meta'] ) ) { |
| @@ -356,27 +358,25 @@ | ||
| 356 | 358 | $field_array = array(); |
| 357 | 359 | $existing_keys = array_keys( $values['item_meta'] ); |
| 358 | 360 | |
| 359 | 361 | foreach ( $all_fields as $fid ) { |
| 362 | + // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict, SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 360 | 363 | if ( ! in_array( $fid->id, $existing_keys ) && ( isset( $values['frm_fields_submitted'] ) && in_array( $fid->id, $values['frm_fields_submitted'] ) ) || isset( $values['options'] ) ) { |
| 361 | 364 | $values['item_meta'][ $fid->id ] = ''; |
| 362 | 365 | } |
| 366 | + | |
| 363 | 367 | $field_array[ $fid->id ] = $fid; |
| 364 | 368 | } |
| 365 | 369 | unset( $all_fields ); |
| 366 | 370 | |
| 367 | 371 | foreach ( $values['item_meta'] as $field_id => $default_value ) { |
| 368 | - if ( isset( $field_array[ $field_id ] ) ) { | |
| 369 | - $field = $field_array[ $field_id ]; | |
| 370 | - } else { | |
| 371 | - $field = FrmField::getOne( $field_id ); | |
| 372 | - } | |
| 372 | + $field = $field_array[ $field_id ] ?? FrmField::getOne( $field_id ); | |
| 373 | 373 | |
| 374 | 374 | if ( ! $field ) { |
| 375 | 375 | continue; |
| 376 | 376 | } |
| 377 | 377 | |
| 378 | - $is_settings_page = ( isset( $values['options'] ) || isset( $values['field_options'][ 'custom_html_' . $field_id ] ) ); | |
| 378 | + $is_settings_page = isset( $values['options'] ) || isset( $values['field_options'][ 'custom_html_' . $field_id ] ); | |
| 379 | 379 | |
| 380 | 380 | if ( $is_settings_page ) { |
| 381 | 381 | self::get_settings_page_html( $values, $field ); |
| 382 | 382 | |
| @@ -406,15 +406,17 @@ | ||
| 406 | 406 | 'field_options' => $field->field_options, |
| 407 | 407 | 'default_value' => isset( $values[ 'default_value_' . $field_id ] ) ? FrmAppHelper::maybe_json_encode( $values[ 'default_value_' . $field_id ] ) : '', |
| 408 | 408 | ); |
| 409 | 409 | |
| 410 | - if ( ! FrmAppHelper::allow_unfiltered_html() && isset( $values['field_options'][ 'options_' . $field_id ] ) && is_array( $values['field_options'][ 'options_' . $field_id ] ) ) { | |
| 410 | + if ( ! FrmAppHelper::allow_unfiltered_html() && isset( $values['field_options'][ 'options_' . $field_id ] ) && is_array( $values['field_options'][ 'options_' . $field_id ] ) ) { // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 411 | 411 | foreach ( $values['field_options'][ 'options_' . $field_id ] as $option_key => $option ) { |
| 412 | - if ( is_array( $option ) ) { | |
| 413 | - foreach ( $option as $key => $item ) { | |
| 414 | - $values['field_options'][ 'options_' . $field_id ][ $option_key ][ $key ] = FrmAppHelper::kses( $item, 'all' ); | |
| 415 | - } | |
| 412 | + if ( ! is_array( $option ) ) { | |
| 413 | + continue; | |
| 416 | 414 | } |
| 415 | + | |
| 416 | + foreach ( $option as $key => $item ) { | |
| 417 | + $values['field_options'][ 'options_' . $field_id ][ $option_key ][ $key ] = FrmAppHelper::kses( $item, 'all' ); | |
| 418 | + } | |
| 417 | 419 | } |
| 418 | 420 | } |
| 419 | 421 | |
| 420 | 422 | self::prepare_field_update_values( $field, $values, $new_field ); |
| @@ -433,27 +435,28 @@ | ||
| 433 | 435 | * Resets the 'max' option of a field when changing paragraph field type to other field types like text, email etc. |
| 434 | 436 | * |
| 435 | 437 | * @since 6.7 |
| 436 | 438 | * |
| 437 | - * @param array $field | |
| 438 | - * @param array $values | |
| 439 | - * @param array $new_field | |
| 439 | + * @param object $field | |
| 440 | + * @param array $values | |
| 441 | + * @param array $new_field | |
| 440 | 442 | * |
| 441 | 443 | * @return void |
| 442 | 444 | */ |
| 443 | 445 | private static function maybe_update_max_option( $field, $values, &$new_field ) { |
| 444 | - if ( $field->type === 'textarea' && | |
| 445 | - ! empty( $values['field_options'][ 'type_' . $field->id ] ) && | |
| 446 | - in_array( $values['field_options'][ 'type_' . $field->id ], array( 'text', 'email', 'url', 'password', 'phone' ), true ) ) { | |
| 446 | + if ( $field->type !== 'textarea' || | |
| 447 | + empty( $values['field_options'][ 'type_' . $field->id ] ) || | |
| 448 | + ! in_array( $values['field_options'][ 'type_' . $field->id ], array( 'text', 'email', 'url', 'password', 'phone' ), true ) ) { | |
| 449 | + return; | |
| 450 | + } | |
| 447 | 451 | |
| 448 | - $new_field['field_options']['max'] = ''; | |
| 452 | + $new_field['field_options']['max'] = ''; | |
| 449 | 453 | |
| 450 | - /** | |
| 451 | - * Update posted field setting so that new 'max' option is displayed after form is saved and page reloads. | |
| 452 | - * FrmFieldsHelper::fill_default_field_opts populates field options by calling self::get_posted_field_setting. | |
| 453 | - */ | |
| 454 | - $_POST['field_options'][ 'max_' . $field->id ] = ''; | |
| 455 | - } | |
| 454 | + /** | |
| 455 | + * Update posted field setting so that new 'max' option is displayed after form is saved and page reloads. | |
| 456 | + * FrmFieldsHelper::fill_default_field_opts populates field options by calling self::get_posted_field_setting. | |
| 457 | + */ | |
| 458 | + $_POST['field_options'][ 'max_' . $field->id ] = ''; | |
| 456 | 459 | } |
| 457 | 460 | |
| 458 | 461 | /** |
| 459 | 462 | * @param string $opt |
| @@ -480,14 +483,9 @@ | ||
| 480 | 483 | if ( ! $should_sanitize ) { |
| 481 | 484 | return; |
| 482 | 485 | } |
| 483 | 486 | |
| 484 | - if ( $opt === 'calc' ) { | |
| 485 | - $value = self::sanitize_calc( $value ); | |
| 486 | - } else { | |
| 487 | - $value = FrmAppHelper::kses( $value, 'all' ); | |
| 488 | - } | |
| 489 | - | |
| 487 | + $value = $opt === 'calc' ? self::sanitize_calc( $value ) : FrmAppHelper::kses( $value, 'all' ); | |
| 490 | 488 | $value = trim( $value ); |
| 491 | 489 | } |
| 492 | 490 | |
| 493 | 491 | /** |
| @@ -495,9 +493,9 @@ | ||
| 495 | 493 | * |
| 496 | 494 | * @return string |
| 497 | 495 | */ |
| 498 | 496 | private static function sanitize_calc( $value ) { |
| 499 | - if ( false !== strpos( $value, '<' ) ) { | |
| 497 | + if ( str_contains( $value, '<' ) ) { | |
| 500 | 498 | $value = self::normalize_calc_spaces( $value ); |
| 501 | 499 | } |
| 502 | 500 | // Allow <= and >=. |
| 503 | 501 | $allow = array( '<= ', ' >=' ); |
| @@ -503,10 +501,9 @@ | ||
| 503 | 501 | $allow = array( '<= ', ' >=' ); |
| 504 | 502 | $temp = array( '< = ', ' > =' ); |
| 505 | 503 | $value = str_replace( $allow, $temp, $value ); |
| 506 | 504 | $value = strip_tags( $value ); |
| 507 | - $value = str_replace( $temp, $allow, $value ); | |
| 508 | - return $value; | |
| 505 | + return str_replace( $temp, $allow, $value ); | |
| 509 | 506 | } |
| 510 | 507 | |
| 511 | 508 | /** |
| 512 | 509 | * Format a comparison like 5<10 to 5 < 10. Also works on 5< 10, 5 <10, 5<=10 variations. |
| @@ -522,11 +519,9 @@ | ||
| 522 | 519 | // $2 a space (optional). |
| 523 | 520 | // $3 an equals sign (optional) that follows the < operator for <= comparisons. |
| 524 | 521 | // $4 another space (optional). |
| 525 | 522 | // $5 \d|\[ the second comparison digit or the start of a comparison shortcode. |
| 526 | - $calc = preg_replace( '/(\d|\])( ){0,1}<(=){0,1}( ){0,1}(\d|\[)/', '$1 <$3 $5', $calc ); | |
| 527 | - | |
| 528 | - return $calc; | |
| 523 | + return preg_replace( '/(\d|\])( ){0,1}<(=){0,1}( ){0,1}(\d|\[)/', '$1 <$3 $5', $calc ); | |
| 529 | 524 | } |
| 530 | 525 | |
| 531 | 526 | /** |
| 532 | 527 | * Updating the settings page |
| @@ -545,14 +540,17 @@ | ||
| 545 | 540 | } elseif ( $field->type === 'hidden' || $field->type === 'user_id' ) { |
| 546 | 541 | $prev_opts = $field->field_options; |
| 547 | 542 | } |
| 548 | 543 | |
| 549 | - if ( isset( $prev_opts ) ) { | |
| 550 | - $field->field_options = apply_filters( 'frm_update_form_field_options', $field->field_options, $field, $values ); | |
| 544 | + if ( ! isset( $prev_opts ) ) { | |
| 545 | + return; | |
| 546 | + } | |
| 551 | 547 | |
| 552 | - if ( $prev_opts != $field->field_options ) { | |
| 553 | - FrmField::update( $field->id, array( 'field_options' => $field->field_options ) ); | |
| 554 | - } | |
| 548 | + $field->field_options = apply_filters( 'frm_update_form_field_options', $field->field_options, $field, $values ); | |
| 549 | + | |
| 550 | + // phpcs:ignore Universal.Operators.StrictComparisons | |
| 551 | + if ( $prev_opts != $field->field_options ) { | |
| 552 | + FrmField::update( $field->id, array( 'field_options' => $field->field_options ) ); | |
| 555 | 553 | } |
| 556 | 554 | } |
| 557 | 555 | |
| 558 | 556 | /** |
| @@ -614,15 +612,15 @@ | ||
| 614 | 612 | return apply_filters( 'frm_form_strings', $strings, $form ); |
| 615 | 613 | } |
| 616 | 614 | |
| 617 | 615 | /** |
| 618 | - * @param int $id | |
| 619 | - * @param string $status | |
| 616 | + * @param array|int $id | |
| 617 | + * @param string $status | |
| 620 | 618 | * |
| 621 | 619 | * @return bool|int |
| 622 | 620 | */ |
| 623 | 621 | public static function set_status( $id, $status ) { |
| 624 | - if ( 'trash' == $status ) { | |
| 622 | + if ( 'trash' === $status ) { | |
| 625 | 623 | return self::trash( $id ); |
| 626 | 624 | } |
| 627 | 625 | |
| 628 | 626 | $statuses = array( 'published', 'draft', 'trash' ); |
| @@ -641,9 +639,9 @@ | ||
| 641 | 639 | ); |
| 642 | 640 | FrmDb::get_where_clause_and_values( $where ); |
| 643 | 641 | array_unshift( $where['values'], $status ); |
| 644 | 642 | |
| 645 | - $query_results = $wpdb->query( $wpdb->prepare( 'UPDATE ' . $wpdb->prefix . 'frm_forms SET status = %s ' . $where['where'], $where['values'] ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 643 | + $query_results = $wpdb->query( $wpdb->prepare( 'UPDATE ' . $wpdb->prefix . 'frm_forms SET status = %s ' . $where['where'], $where['values'] ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 646 | 644 | } else { |
| 647 | 645 | $query_results = $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'status' => $status ), array( 'id' => $id ) ); |
| 648 | 646 | $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'status' => $status ), array( 'parent_form_id' => $id ) ); |
| 649 | 647 | } |
| @@ -724,9 +722,9 @@ | ||
| 724 | 722 | |
| 725 | 723 | $id = $form->id; |
| 726 | 724 | |
| 727 | 725 | // Disconnect the entries from this form |
| 728 | - $entries = FrmDb::get_col( $wpdb->prefix . 'frm_items', array( 'form_id' => $id ) ); | |
| 726 | + $entries = FrmDb::get_col( 'frm_items', array( 'form_id' => $id ) ); | |
| 729 | 727 | |
| 730 | 728 | foreach ( $entries as $entry_id ) { |
| 731 | 729 | FrmEntry::destroy( $entry_id ); |
| 732 | 730 | unset( $entry_id ); |
| @@ -732,27 +730,29 @@ | ||
| 732 | 730 | unset( $entry_id ); |
| 733 | 731 | } |
| 734 | 732 | |
| 735 | 733 | // Disconnect the fields from this form |
| 736 | - $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 ) ); | |
| 734 | + $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 ) ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 737 | 735 | |
| 738 | 736 | $query_results = $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_forms WHERE id=%d OR parent_form_id=%d', $id, $id ) ); |
| 739 | 737 | |
| 740 | - if ( $query_results ) { | |
| 741 | - // Delete all form actions linked to this form | |
| 742 | - /** | |
| 743 | - * @var FrmFormAction | |
| 744 | - */ | |
| 745 | - $action_control = FrmFormActionsController::get_form_actions( 'email' ); | |
| 746 | - $action_control->destroy( $id, 'all' ); | |
| 738 | + if ( ! $query_results ) { | |
| 739 | + return $query_results; | |
| 740 | + } | |
| 747 | 741 | |
| 748 | - // Clear form caching | |
| 749 | - self::clear_form_cache(); | |
| 742 | + // Delete all form actions linked to this form | |
| 743 | + /** | |
| 744 | + * @var FrmFormAction | |
| 745 | + */ | |
| 746 | + $action_control = FrmFormActionsController::get_form_actions( 'email' ); | |
| 747 | + $action_control->destroy( $id, 'all' ); | |
| 750 | 748 | |
| 751 | - do_action( 'frm_destroy_form', $id ); | |
| 752 | - do_action( 'frm_destroy_form_' . $id ); | |
| 753 | - } | |
| 749 | + // Clear form caching | |
| 750 | + self::clear_form_cache(); | |
| 754 | 751 | |
| 752 | + do_action( 'frm_destroy_form', $id ); | |
| 753 | + do_action( 'frm_destroy_form_' . $id ); | |
| 754 | + | |
| 755 | 755 | return $query_results; |
| 756 | 756 | } |
| 757 | 757 | |
| 758 | 758 | /** |
| @@ -762,17 +762,15 @@ | ||
| 762 | 762 | * |
| 763 | 763 | * @return int The number of forms deleted |
| 764 | 764 | */ |
| 765 | 765 | public static function scheduled_delete( $delete_timestamp = '' ) { |
| 766 | - global $wpdb; | |
| 766 | + $trash_forms = FrmDb::get_results( 'frm_forms', array( 'status' => 'trash' ), 'id, parent_form_id, options' ); | |
| 767 | 767 | |
| 768 | - $trash_forms = FrmDb::get_results( $wpdb->prefix . 'frm_forms', array( 'status' => 'trash' ), 'id, parent_form_id, options' ); | |
| 769 | - | |
| 770 | 768 | if ( ! $trash_forms ) { |
| 771 | 769 | return 0; |
| 772 | 770 | } |
| 773 | 771 | |
| 774 | - if ( empty( $delete_timestamp ) ) { | |
| 772 | + if ( ! $delete_timestamp ) { | |
| 775 | 773 | $delete_timestamp = time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS ); |
| 776 | 774 | } |
| 777 | 775 | |
| 778 | 776 | $count = 0; |
| @@ -802,11 +800,9 @@ | ||
| 802 | 800 | public static function getName( $id ) { |
| 803 | 801 | $form = FrmDb::check_cache( $id, 'frm_form' ); |
| 804 | 802 | |
| 805 | 803 | if ( $form ) { |
| 806 | - $r = stripslashes( $form->name ); | |
| 807 | - | |
| 808 | - return $r; | |
| 804 | + return stripslashes( $form->name ); | |
| 809 | 805 | } |
| 810 | 806 | |
| 811 | 807 | $query_key = is_numeric( $id ) ? 'id' : 'form_key'; |
| 812 | 808 | $r = FrmDb::get_var( 'frm_forms', array( $query_key => $id ), 'name' ); |
| @@ -811,11 +807,9 @@ | ||
| 811 | 807 | $query_key = is_numeric( $id ) ? 'id' : 'form_key'; |
| 812 | 808 | $r = FrmDb::get_var( 'frm_forms', array( $query_key => $id ), 'name' ); |
| 813 | 809 | |
| 814 | 810 | // An empty form name can result in a null value. |
| 815 | - $r = is_null( $r ) ? '' : stripslashes( $r ); | |
| 816 | - | |
| 817 | - return $r; | |
| 811 | + return is_null( $r ) ? '' : stripslashes( $r ); | |
| 818 | 812 | } |
| 819 | 813 | |
| 820 | 814 | /** |
| 821 | 815 | * @since 3.0 |
| @@ -842,11 +836,9 @@ | ||
| 842 | 836 | if ( $cache ) { |
| 843 | 837 | return $cache->form_key; |
| 844 | 838 | } |
| 845 | 839 | |
| 846 | - $key = FrmDb::get_var( 'frm_forms', array( 'id' => $id ), 'form_key' ); | |
| 847 | - | |
| 848 | - return $key; | |
| 840 | + return FrmDb::get_var( 'frm_forms', array( 'id' => $id ), 'form_key' ); | |
| 849 | 841 | } |
| 850 | 842 | |
| 851 | 843 | /** |
| 852 | 844 | * If $form is numeric, get the form object |
| @@ -852,14 +844,14 @@ | ||
| 852 | 844 | * If $form is numeric, get the form object |
| 853 | 845 | * |
| 854 | 846 | * @since 2.0.9 |
| 855 | 847 | * |
| 856 | - * @param int|object $form | |
| 848 | + * @param array|int|object $form | |
| 857 | 849 | * |
| 858 | 850 | * @return void |
| 859 | 851 | */ |
| 860 | 852 | public static function maybe_get_form( &$form ) { |
| 861 | - if ( ! is_object( $form ) && ! is_array( $form ) && ! empty( $form ) ) { | |
| 853 | + if ( ! is_object( $form ) && ! is_array( $form ) && $form ) { | |
| 862 | 854 | $form = self::getOne( $form ); |
| 863 | 855 | } |
| 864 | 856 | } |
| 865 | 857 | |
| @@ -882,18 +874,14 @@ | ||
| 882 | 874 | if ( $cache ) { |
| 883 | 875 | if ( isset( $cache->options ) ) { |
| 884 | 876 | FrmAppHelper::unserialize_or_decode( $cache->options ); |
| 885 | 877 | } |
| 878 | + | |
| 886 | 879 | return self::prepare_form_row_data( $cache ); |
| 887 | 880 | } |
| 888 | 881 | } |
| 889 | 882 | |
| 890 | - if ( is_numeric( $id ) ) { | |
| 891 | - $where = array( 'id' => $id ); | |
| 892 | - } else { | |
| 893 | - $where = array( 'form_key' => $id ); | |
| 894 | - } | |
| 895 | - | |
| 883 | + $where = is_numeric( $id ) ? array( 'id' => $id ) : array( 'form_key' => $id ); | |
| 896 | 884 | $results = FrmDb::get_row( $table_name, $where ); |
| 897 | 885 | |
| 898 | 886 | if ( isset( $results->options ) ) { |
| 899 | 887 | FrmDb::set_cache( $results->id, $results, 'frm_form' ); |
| @@ -935,14 +923,14 @@ | ||
| 935 | 923 | * @param array|string $where Where conditions array or raw WHERE string. |
| 936 | 924 | * @param string $order_by Order by clause. |
| 937 | 925 | * @param int|string $limit Limit clause or number. |
| 938 | 926 | * |
| 939 | - * @return array|object of objects | |
| 927 | + * @return array|object Array of objects. If $limit is 1, a single object is returned. | |
| 940 | 928 | */ |
| 941 | 929 | public static function getAll( $where = array(), $order_by = '', $limit = '' ) { |
| 942 | - if ( is_array( $where ) && ! empty( $where ) ) { | |
| 930 | + if ( is_array( $where ) && $where ) { | |
| 943 | 931 | if ( ! empty( $where['is_template'] ) && ! isset( $where['status'] ) && ! isset( $where['status !'] ) ) { |
| 944 | - // don't get trashed templates | |
| 932 | + // Don't get trashed templates | |
| 945 | 933 | $where['status'] = array( null, '', 'published' ); |
| 946 | 934 | } |
| 947 | 935 | |
| 948 | 936 | $results = FrmDb::get_results( 'frm_forms', $where, '*', compact( 'order_by', 'limit' ) ); |
| @@ -949,9 +937,9 @@ | ||
| 949 | 937 | } else { |
| 950 | 938 | global $wpdb; |
| 951 | 939 | |
| 952 | 940 | // The query has already been prepared if this is not an array. |
| 953 | - $query = 'SELECT * FROM ' . $wpdb->prefix . 'frm_forms' . FrmDb::prepend_and_or_where( ' WHERE ', $where ) . FrmDb::esc_order( $order_by ) . FrmDb::esc_limit( $limit ); | |
| 941 | + $query = 'SELECT * FROM ' . $wpdb->prefix . 'frm_forms' . FrmDb::prepend_and_or_where( ' WHERE ', $where ) . FrmDb::esc_order( $order_by ) . FrmDb::esc_limit( $limit ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 954 | 942 | $results = $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 955 | 943 | } |
| 956 | 944 | |
| 957 | 945 | if ( $results ) { |
| @@ -960,10 +948,10 @@ | ||
| 960 | 948 | FrmAppHelper::unserialize_or_decode( $result->options ); |
| 961 | 949 | } |
| 962 | 950 | } |
| 963 | 951 | |
| 964 | - if ( $limit === ' LIMIT 1' || $limit == 1 ) { | |
| 965 | - // return the first form object if we are only getting one form | |
| 952 | + if ( $limit === ' LIMIT 1' || (int) $limit === 1 ) { | |
| 953 | + // Return the first form object if we are only getting one form | |
| 966 | 954 | $results = reset( $results ); |
| 967 | 955 | } |
| 968 | 956 | |
| 969 | 957 | return wp_unslash( $results ); |
| @@ -977,9 +965,9 @@ | ||
| 977 | 965 | * @param array $query |
| 978 | 966 | * @param int $limit |
| 979 | 967 | * @param string $inc_children |
| 980 | 968 | * |
| 981 | - * @return array|object of forms A single form object would be passed if $limit was set to 1. | |
| 969 | + * @return array|object Array of forms. A single form object is returned if $limit is set to 1. | |
| 982 | 970 | */ |
| 983 | 971 | public static function get_published_forms( $query = array(), $limit = 999, $inc_children = 'exclude' ) { |
| 984 | 972 | $query['is_template'] = 0; |
| 985 | 973 | $query['status'] = array( null, '', 'published' ); |
| @@ -987,11 +975,9 @@ | ||
| 987 | 975 | if ( $inc_children === 'exclude' ) { |
| 988 | 976 | $query['parent_form_id'] = array( null, 0 ); |
| 989 | 977 | } |
| 990 | 978 | |
| 991 | - $forms = self::getAll( $query, 'name', $limit ); | |
| 992 | - | |
| 993 | - return $forms; | |
| 979 | + return self::getAll( $query, 'name', $limit ); | |
| 994 | 980 | } |
| 995 | 981 | |
| 996 | 982 | /** |
| 997 | 983 | * @return object count of forms |
| @@ -996,14 +982,11 @@ | ||
| 996 | 982 | /** |
| 997 | 983 | * @return object count of forms |
| 998 | 984 | */ |
| 999 | 985 | public static function get_count() { |
| 1000 | - global $wpdb; | |
| 1001 | - | |
| 1002 | 986 | $cache_key = 'frm_form_counts'; |
| 987 | + $counts = wp_cache_get( $cache_key, 'frm_form' ); | |
| 1003 | 988 | |
| 1004 | - $counts = wp_cache_get( $cache_key, 'frm_form' ); | |
| 1005 | - | |
| 1006 | 989 | if ( false !== $counts ) { |
| 1007 | 990 | return $counts; |
| 1008 | 991 | } |
| 1009 | 992 | |
| @@ -1020,19 +1003,17 @@ | ||
| 1020 | 1003 | $statuses = array( 'published', 'draft', 'template', 'trash' ); |
| 1021 | 1004 | $counts = array_fill_keys( $statuses, 0 ); |
| 1022 | 1005 | |
| 1023 | 1006 | foreach ( $results as $row ) { |
| 1024 | - if ( 'trash' != $row->status ) { | |
| 1025 | - if ( $row->is_template ) { | |
| 1026 | - ++$counts['template']; | |
| 1027 | - } else { | |
| 1028 | - ++$counts['published']; | |
| 1029 | - } | |
| 1007 | + if ( 'trash' === $row->status ) { | |
| 1008 | + ++$counts['trash']; | |
| 1009 | + } elseif ( $row->is_template ) { | |
| 1010 | + ++$counts['template']; | |
| 1030 | 1011 | } else { |
| 1031 | - ++$counts['trash']; | |
| 1012 | + ++$counts['published']; | |
| 1032 | 1013 | } |
| 1033 | 1014 | |
| 1034 | - if ( 'draft' == $row->status ) { | |
| 1015 | + if ( 'draft' === $row->status ) { | |
| 1035 | 1016 | ++$counts['draft']; |
| 1036 | 1017 | } |
| 1037 | 1018 | |
| 1038 | 1019 | unset( $row ); |
| @@ -1074,12 +1055,12 @@ | ||
| 1074 | 1055 | */ |
| 1075 | 1056 | public static function get_params( $form = null ) { |
| 1076 | 1057 | global $frm_vars; |
| 1077 | 1058 | |
| 1078 | - if ( ! $form ) { | |
| 1059 | + if ( $form ) { | |
| 1060 | + self::maybe_get_form( $form ); | |
| 1061 | + } else { | |
| 1079 | 1062 | $form = self::getAll( array(), 'name', 1 ); |
| 1080 | - } else { | |
| 1081 | - self::maybe_get_form( $form ); | |
| 1082 | 1063 | } |
| 1083 | 1064 | |
| 1084 | 1065 | if ( isset( $frm_vars['form_params'] ) && is_array( $frm_vars['form_params'] ) && isset( $frm_vars['form_params'][ $form->id ] ) ) { |
| 1085 | 1066 | return $frm_vars['form_params'][ $form->id ]; |
| @@ -1107,8 +1088,9 @@ | ||
| 1107 | 1088 | if ( ! $values['posted_form_id'] ) { |
| 1108 | 1089 | $values['posted_form_id'] = FrmAppHelper::get_param( 'form', '', 'get', 'absint' ); |
| 1109 | 1090 | } |
| 1110 | 1091 | |
| 1092 | + // phpcs:ignore Universal.Operators.StrictComparisons | |
| 1111 | 1093 | if ( $form->id == $values['posted_form_id'] ) { |
| 1112 | 1094 | // If there are two forms on the same page, make sure not to submit both. |
| 1113 | 1095 | foreach ( $default_values as $var => $default ) { |
| 1114 | 1096 | if ( $var === 'action' ) { |
| @@ -1124,9 +1106,9 @@ | ||
| 1124 | 1106 | unset( $var, $default ); |
| 1125 | 1107 | } |
| 1126 | 1108 | } |
| 1127 | 1109 | |
| 1128 | - if ( in_array( $values['action'], array( 'create', 'update' ) ) && | |
| 1110 | + if ( in_array( $values['action'], array( 'create', 'update' ), true ) && | |
| 1129 | 1111 | ( ! $_POST || ( ! isset( $_POST['action'] ) && ! isset( $_POST['frm_action'] ) ) ) // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 1130 | 1112 | ) { |
| 1131 | 1113 | $values['action'] = 'new'; |
| 1132 | 1114 | } |
| @@ -1196,17 +1178,10 @@ | ||
| 1196 | 1178 | * |
| 1197 | 1179 | * @return int|string |
| 1198 | 1180 | */ |
| 1199 | 1181 | public static function get_current_form_id( $default_form = 'none' ) { |
| 1200 | - if ( 'first' === $default_form ) { | |
| 1201 | - $form = self::get_current_form(); | |
| 1202 | - } else { | |
| 1203 | - $form = self::maybe_get_current_form(); | |
| 1204 | - } | |
| 1205 | - | |
| 1206 | - $form_id = $form ? $form->id : 0; | |
| 1207 | - | |
| 1208 | - return $form_id; | |
| 1182 | + $form = 'first' === $default_form ? self::get_current_form() : self::maybe_get_current_form(); | |
| 1183 | + return $form ? $form->id : 0; | |
| 1209 | 1184 | } |
| 1210 | 1185 | |
| 1211 | 1186 | /** |
| 1212 | 1187 | * @param int|string $form_id |
| @@ -1215,19 +1190,15 @@ | ||
| 1215 | 1190 | */ |
| 1216 | 1191 | public static function maybe_get_current_form( $form_id = 0 ) { |
| 1217 | 1192 | global $frm_vars; |
| 1218 | 1193 | |
| 1219 | - if ( ! empty( $frm_vars['current_form'] ) && ( ! $form_id || $form_id == $frm_vars['current_form']->id ) ) { | |
| 1194 | + if ( ! empty( $frm_vars['current_form'] ) && ( ! $form_id || (int) $form_id === (int) $frm_vars['current_form']->id ) ) { | |
| 1220 | 1195 | return $frm_vars['current_form']; |
| 1221 | 1196 | } |
| 1222 | 1197 | |
| 1223 | 1198 | $form_id = FrmAppHelper::get_param( 'form', $form_id, 'get', 'absint' ); |
| 1224 | 1199 | |
| 1225 | - if ( $form_id ) { | |
| 1226 | - $form_id = self::set_current_form( $form_id ); | |
| 1227 | - } | |
| 1228 | - | |
| 1229 | - return $form_id; | |
| 1200 | + return $form_id ? self::set_current_form( $form_id ) : $form_id; | |
| 1230 | 1201 | } |
| 1231 | 1202 | |
| 1232 | 1203 | /** |
| 1233 | 1204 | * @param int $form_id |
| @@ -1235,14 +1206,9 @@ | ||
| 1235 | 1206 | * @return false|object |
| 1236 | 1207 | */ |
| 1237 | 1208 | public static function get_current_form( $form_id = 0 ) { |
| 1238 | 1209 | $form = self::maybe_get_current_form( $form_id ); |
| 1239 | - | |
| 1240 | - if ( is_numeric( $form ) ) { | |
| 1241 | - $form = self::set_current_form( $form ); | |
| 1242 | - } | |
| 1243 | - | |
| 1244 | - return $form; | |
| 1210 | + return is_numeric( $form ) ? self::set_current_form( $form ) : $form; | |
| 1245 | 1211 | } |
| 1246 | 1212 | |
| 1247 | 1213 | /** |
| 1248 | 1214 | * @param int $form_id |
| @@ -1280,9 +1246,9 @@ | ||
| 1280 | 1246 | } |
| 1281 | 1247 | |
| 1282 | 1248 | $frm_vars['forms_loaded'][] = $small_form; |
| 1283 | 1249 | |
| 1284 | - if ( $this_load && empty( $global_load ) ) { | |
| 1250 | + if ( $this_load && ! $global_load ) { | |
| 1285 | 1251 | $global_load = true; |
| 1286 | 1252 | $frm_vars['load_css'] = true; |
| 1287 | 1253 | } |
| 1288 | 1254 | |
| @@ -1295,9 +1261,9 @@ | ||
| 1295 | 1261 | * @param object $form |
| 1296 | 1262 | * |
| 1297 | 1263 | * @return bool |
| 1298 | 1264 | */ |
| 1299 | - public static function &is_visible_to_user( $form ) { | |
| 1265 | + public static function is_visible_to_user( $form ) { | |
| 1300 | 1266 | if ( $form->logged_in && isset( $form->options['logged_in_role'] ) ) { |
| 1301 | 1267 | $visible = FrmAppHelper::user_has_permission( $form->options['logged_in_role'] ); |
| 1302 | 1268 | } else { |
| 1303 | 1269 | $visible = true; |
| @@ -1308,11 +1274,9 @@ | ||
| 1308 | 1274 | * |
| 1309 | 1275 | * @param bool $visible |
| 1310 | 1276 | * @param object $form |
| 1311 | 1277 | */ |
| 1312 | - $visible = (bool) apply_filters( 'frm_form_is_visible', $visible, $form ); | |
| 1313 | - | |
| 1314 | - return $visible; | |
| 1278 | + return (bool) apply_filters( 'frm_form_is_visible', $visible, $form ); | |
| 1315 | 1279 | } |
| 1316 | 1280 | |
| 1317 | 1281 | /** |
| 1318 | 1282 | * @param object $form |
| @@ -1319,12 +1283,10 @@ | ||
| 1319 | 1283 | * |
| 1320 | 1284 | * @return bool |
| 1321 | 1285 | */ |
| 1322 | 1286 | public static function show_submit( $form ) { |
| 1323 | - $show = ( ! $form->is_template && $form->status === 'published' && ! FrmAppHelper::is_admin() ); | |
| 1324 | - $show = apply_filters( 'frm_show_submit_button', $show, $form ); | |
| 1325 | - | |
| 1326 | - return $show; | |
| 1287 | + $show = ! $form->is_template && $form->status === 'published' && ! FrmAppHelper::is_admin(); | |
| 1288 | + return apply_filters( 'frm_show_submit_button', $show, $form ); | |
| 1327 | 1289 | } |
| 1328 | 1290 | |
| 1329 | 1291 | /** |
| 1330 | 1292 | * @since 2.3 |
| @@ -1373,9 +1335,8 @@ | ||
| 1373 | 1335 | * |
| 1374 | 1336 | * @return object |
| 1375 | 1337 | */ |
| 1376 | 1338 | public static function get_latest_form() { |
| 1377 | - | |
| 1378 | 1339 | $args = array( |
| 1379 | 1340 | array( |
| 1380 | 1341 | 'or' => 1, |
| 1381 | 1342 | 'parent_form_id' => null, |
| @@ -1395,9 +1356,8 @@ | ||
| 1395 | 1356 | * |
| 1396 | 1357 | * @return int |
| 1397 | 1358 | */ |
| 1398 | 1359 | public static function get_forms_count() { |
| 1399 | - | |
| 1400 | 1360 | $args = array( |
| 1401 | 1361 | array( |
| 1402 | 1362 | 'or' => 1, |
| 1403 | 1363 | 'parent_form_id' => null, |