| @@ -6,9 +6,9 @@ | ||
| 6 | 6 | class FrmForm { |
| 7 | 7 | |
| 8 | 8 | /** |
| 9 | 9 | * @param array $values |
| 10 | - * @return bool|int id on success or false on failure. | |
| 10 | + * @return int|boolean id on success or false on failure | |
| 11 | 11 | */ |
| 12 | 12 | public static function create( $values ) { |
| 13 | 13 | global $wpdb; |
| 14 | 14 | |
| @@ -17,33 +17,24 @@ | ||
| 17 | 17 | $new_values = array( |
| 18 | 18 | 'form_key' => FrmAppHelper::get_unique_key( $values['form_key'], $wpdb->prefix . 'frm_forms', 'form_key' ), |
| 19 | 19 | 'name' => $values['name'], |
| 20 | 20 | 'description' => $values['description'], |
| 21 | - 'status' => $values['status'] ?? 'published', | |
| 22 | - 'logged_in' => $values['logged_in'] ?? 0, | |
| 21 | + 'status' => isset( $values['status'] ) ? $values['status'] : 'published', | |
| 22 | + 'logged_in' => isset( $values['logged_in'] ) ? $values['logged_in'] : 0, | |
| 23 | 23 | 'is_template' => isset( $values['is_template'] ) ? (int) $values['is_template'] : 0, |
| 24 | 24 | 'parent_form_id' => isset( $values['parent_form_id'] ) ? absint( $values['parent_form_id'] ) : 0, |
| 25 | 25 | 'editable' => isset( $values['editable'] ) ? (int) $values['editable'] : 0, |
| 26 | - 'created_at' => $values['created_at'] ?? current_time( 'mysql', 1 ), | |
| 26 | + 'created_at' => isset( $values['created_at'] ) ? $values['created_at'] : current_time( 'mysql', 1 ), | |
| 27 | 27 | ); |
| 28 | 28 | |
| 29 | 29 | $options = isset( $values['options'] ) ? (array) $values['options'] : array(); |
| 30 | 30 | FrmFormsHelper::fill_form_options( $options, $values ); |
| 31 | 31 | |
| 32 | - $options['before_html'] = $values['options']['before_html'] ?? FrmFormsHelper::get_default_html( 'before' ); | |
| 33 | - $options['after_html'] = $values['options']['after_html'] ?? FrmFormsHelper::get_default_html( 'after' ); | |
| 34 | - $options['submit_html'] = $values['options']['submit_html'] ?? FrmFormsHelper::get_default_html( 'submit' ); | |
| 32 | + $options['before_html'] = isset( $values['options']['before_html'] ) ? $values['options']['before_html'] : FrmFormsHelper::get_default_html( 'before' ); | |
| 33 | + $options['after_html'] = isset( $values['options']['after_html'] ) ? $values['options']['after_html'] : FrmFormsHelper::get_default_html( 'after' ); | |
| 34 | + $options['submit_html'] = isset( $values['options']['submit_html'] ) ? $values['options']['submit_html'] : FrmFormsHelper::get_default_html( 'submit' ); | |
| 35 | 35 | |
| 36 | - /** | |
| 37 | - * Allows modifying form options before updating or creating. | |
| 38 | - * | |
| 39 | - * @since 5.4 Add the third param. | |
| 40 | - * | |
| 41 | - * @param array $options Form options. | |
| 42 | - * @param array $values Form data. | |
| 43 | - * @param bool $update Is form updating or creating. It's `true` if is updating. | |
| 44 | - */ | |
| 45 | - $options = apply_filters( 'frm_form_options_before_update', $options, $values, false ); | |
| 36 | + $options = apply_filters( 'frm_form_options_before_update', $options, $values ); | |
| 46 | 37 | $options = self::maybe_filter_form_options( $options ); |
| 47 | 38 | $new_values['options'] = serialize( $options ); |
| 48 | 39 | |
| 49 | 40 | $wpdb->insert( $wpdb->prefix . 'frm_forms', $new_values ); |
| @@ -69,9 +60,9 @@ | ||
| 69 | 60 | return FrmAppHelper::maybe_filter_array( $options, array( 'submit_value', 'success_msg', 'before_html', 'after_html' ) ); |
| 70 | 61 | } |
| 71 | 62 | |
| 72 | 63 | /** |
| 73 | - * @return bool|int ID on success or false on failure | |
| 64 | + * @return int|boolean ID on success or false on failure | |
| 74 | 65 | */ |
| 75 | 66 | public static function duplicate( $id, $template = false, $copy_keys = false, $blog_id = false ) { |
| 76 | 67 | global $wpdb; |
| 77 | 68 | |
| @@ -93,10 +84,10 @@ | ||
| 93 | 84 | 'is_template' => $template ? 1 : 0, |
| 94 | 85 | ); |
| 95 | 86 | |
| 96 | 87 | if ( $blog_id ) { |
| 97 | - $new_values['status'] = 'published'; | |
| 98 | - $new_options = $values->options; | |
| 88 | + $new_values['status'] = 'published'; | |
| 89 | + $new_options = $values->options; | |
| 99 | 90 | FrmAppHelper::unserialize_or_decode( $new_options ); |
| 100 | 91 | $new_options['email_to'] = get_option( 'admin_email' ); |
| 101 | 92 | $new_options['copy'] = false; |
| 102 | 93 | $new_values['options'] = $new_options; |
| @@ -126,9 +117,9 @@ | ||
| 126 | 117 | return false; |
| 127 | 118 | } |
| 128 | 119 | |
| 129 | 120 | public static function after_duplicate( $form_id, $values ) { |
| 130 | - $new_opts = $values['options']; | |
| 121 | + $new_opts = $values['options']; | |
| 131 | 122 | FrmAppHelper::unserialize_or_decode( $new_opts ); |
| 132 | 123 | $values['options'] = $new_opts; |
| 133 | 124 | |
| 134 | 125 | if ( isset( $new_opts['success_msg'] ) ) { |
| @@ -140,88 +131,13 @@ | ||
| 140 | 131 | if ( $new_opts != $values['options'] ) { |
| 141 | 132 | global $wpdb; |
| 142 | 133 | $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'options' => maybe_serialize( $new_opts ) ), array( 'id' => $form_id ) ); |
| 143 | 134 | } |
| 144 | - | |
| 145 | - self::switch_field_ids_in_fields( $form_id ); | |
| 146 | 135 | } |
| 147 | 136 | |
| 148 | 137 | /** |
| 149 | - * Switches field ID in fields. | |
| 150 | - * | |
| 151 | - * @since 5.3 | |
| 152 | - * | |
| 153 | - * @param int $form_id Form ID. | |
| 138 | + * @return int|boolean | |
| 154 | 139 | */ |
| 155 | - private static function switch_field_ids_in_fields( $form_id ) { | |
| 156 | - global $wpdb; | |
| 157 | - | |
| 158 | - // Keys of fields that you want to check to replace field ID. | |
| 159 | - $keys = array( 'default_value', 'field_options' ); | |
| 160 | - $sql_cols = 'fi.id'; | |
| 161 | - foreach ( $keys as $key ) { | |
| 162 | - $sql_cols .= ',fi.' . $key; | |
| 163 | - } | |
| 164 | - | |
| 165 | - $fields = FrmDb::get_results( | |
| 166 | - "{$wpdb->prefix}frm_fields AS fi LEFT OUTER JOIN {$wpdb->prefix}frm_forms AS fr ON fi.form_id = fr.id", | |
| 167 | - array( | |
| 168 | - 'or' => 1, | |
| 169 | - 'fi.form_id' => $form_id, | |
| 170 | - 'fr.parent_form_id' => $form_id, | |
| 171 | - ), | |
| 172 | - $sql_cols | |
| 173 | - ); | |
| 174 | - | |
| 175 | - if ( ! $fields || ! is_array( $fields ) ) { | |
| 176 | - return; | |
| 177 | - } | |
| 178 | - | |
| 179 | - foreach ( $fields as $field ) { | |
| 180 | - self::switch_field_ids_in_field( (array) $field ); | |
| 181 | - } | |
| 182 | - } | |
| 183 | - | |
| 184 | - /** | |
| 185 | - * Switches field ID in a field. | |
| 186 | - * | |
| 187 | - * @since 5.3 | |
| 188 | - * | |
| 189 | - * @param array $field Field array. | |
| 190 | - */ | |
| 191 | - private static function switch_field_ids_in_field( $field ) { | |
| 192 | - $new_values = array(); | |
| 193 | - foreach ( $field as $key => $value ) { | |
| 194 | - if ( 'id' === $key || ! $value ) { | |
| 195 | - continue; | |
| 196 | - } | |
| 197 | - | |
| 198 | - if ( ! is_string( $value ) && ! is_array( $value ) ) { | |
| 199 | - continue; | |
| 200 | - } | |
| 201 | - | |
| 202 | - if ( 'field_options' === $key ) { | |
| 203 | - // Need to loop through field_options to prevent breaking serialized string when length changed. | |
| 204 | - FrmAppHelper::unserialize_or_decode( $value ); | |
| 205 | - $new_val = FrmFieldsHelper::switch_field_ids( $value ); | |
| 206 | - $new_val = serialize( $new_val ); | |
| 207 | - } else { | |
| 208 | - $new_val = FrmFieldsHelper::switch_field_ids( $value ); | |
| 209 | - } | |
| 210 | - | |
| 211 | - if ( $new_val !== $value ) { | |
| 212 | - $new_values[ $key ] = $new_val; | |
| 213 | - } | |
| 214 | - }//end foreach | |
| 215 | - | |
| 216 | - if ( ! empty( $new_values ) ) { | |
| 217 | - FrmField::update( $field['id'], $new_values ); | |
| 218 | - } | |
| 219 | - } | |
| 220 | - | |
| 221 | - /** | |
| 222 | - * @return bool|int | |
| 223 | - */ | |
| 224 | 140 | public static function update( $id, $values, $create_link = false ) { |
| 225 | 141 | global $wpdb; |
| 226 | 142 | |
| 227 | 143 | $values = FrmAppHelper::maybe_filter_array( $values, array( 'name', 'description' ) ); |
| @@ -243,9 +159,9 @@ | ||
| 243 | 159 | $new_values[ $value_key ] = $value; |
| 244 | 160 | } |
| 245 | 161 | } |
| 246 | 162 | |
| 247 | - if ( ! empty( $values['new_status'] ) ) { | |
| 163 | + if ( isset( $values['new_status'] ) && ! empty( $values['new_status'] ) ) { | |
| 248 | 164 | $new_values['status'] = $values['new_status']; |
| 249 | 165 | } |
| 250 | 166 | |
| 251 | 167 | if ( ! empty( $new_values ) ) { |
| @@ -276,26 +192,22 @@ | ||
| 276 | 192 | if ( ! isset( $values['options'] ) ) { |
| 277 | 193 | return $new_values; |
| 278 | 194 | } |
| 279 | 195 | |
| 280 | - $options = ! empty( $values['options'] ) ? (array) $values['options'] : array(); | |
| 196 | + $options = isset( $values['options'] ) ? (array) $values['options'] : array(); | |
| 281 | 197 | FrmFormsHelper::fill_form_options( $options, $values ); |
| 282 | 198 | |
| 283 | - $options['custom_style'] = $values['options']['custom_style'] ?? 0; | |
| 284 | - $options['before_html'] = $values['options']['before_html'] ?? FrmFormsHelper::get_default_html( 'before' ); | |
| 285 | - $options['after_html'] = $values['options']['after_html'] ?? FrmFormsHelper::get_default_html( 'after' ); | |
| 286 | - $options['submit_html'] = isset( $values['options']['submit_html'] ) && '' !== $values['options']['submit_html'] ? $values['options']['submit_html'] : FrmFormsHelper::get_default_html( 'submit' ); | |
| 199 | + $options['custom_style'] = isset( $values['options']['custom_style'] ) ? $values['options']['custom_style'] : 0; | |
| 200 | + $options['before_html'] = isset( $values['options']['before_html'] ) ? $values['options']['before_html'] : FrmFormsHelper::get_default_html( 'before' ); | |
| 201 | + $options['after_html'] = isset( $values['options']['after_html'] ) ? $values['options']['after_html'] : FrmFormsHelper::get_default_html( 'after' ); | |
| 202 | + $options['submit_html'] = ( isset( $values['options']['submit_html'] ) && '' !== $values['options']['submit_html'] ) ? $values['options']['submit_html'] : FrmFormsHelper::get_default_html( 'submit' ); | |
| 287 | 203 | |
| 288 | - /** | |
| 289 | - * Allows modifying form options before updating or creating. | |
| 290 | - * | |
| 291 | - * @since 5.4 Added the third param. | |
| 292 | - * | |
| 293 | - * @param array $options Form options. | |
| 294 | - * @param array $values Form data. | |
| 295 | - * @param bool $update Is form updating or creating. It's `true` if is updating. | |
| 296 | - */ | |
| 297 | - $options = apply_filters( 'frm_form_options_before_update', $options, $values, true ); | |
| 204 | + if ( ! empty( $options['success_url'] ) && ! empty( $args['form_id'] ) ) { | |
| 205 | + $options['success_url'] = FrmFormsHelper::maybe_add_sanitize_url_attr( $options['success_url'], (int) $args['form_id'] ); | |
| 206 | + $values['options']['success_url'] = $options['success_url']; | |
| 207 | + } | |
| 208 | + | |
| 209 | + $options = apply_filters( 'frm_form_options_before_update', $options, $values ); | |
| 298 | 210 | $options = self::maybe_filter_form_options( $options ); |
| 299 | 211 | $new_values['options'] = serialize( $options ); |
| 300 | 212 | |
| 301 | 213 | return $new_values; |
| @@ -348,20 +260,15 @@ | ||
| 348 | 260 | continue; |
| 349 | 261 | } |
| 350 | 262 | } |
| 351 | 263 | |
| 352 | - // Updating the form. | |
| 264 | + //updating the form | |
| 353 | 265 | $update_options = FrmFieldsHelper::get_default_field_options_from_field( $field ); |
| 354 | - // Don't check for POST html. | |
| 355 | - unset( $update_options['custom_html'] ); | |
| 266 | + unset( $update_options['custom_html'] ); // don't check for POST html | |
| 356 | 267 | $update_options = apply_filters( 'frm_field_options_to_update', $update_options ); |
| 357 | 268 | |
| 358 | - if ( ! is_array( $field->field_options ) ) { | |
| 359 | - $field->field_options = array(); | |
| 360 | - } | |
| 361 | - | |
| 362 | 269 | foreach ( $update_options as $opt => $default ) { |
| 363 | - $field->field_options[ $opt ] = $values['field_options'][ $opt . '_' . $field_id ] ?? $default; | |
| 270 | + $field->field_options[ $opt ] = isset( $values['field_options'][ $opt . '_' . $field_id ] ) ? $values['field_options'][ $opt . '_' . $field_id ] : $default; | |
| 364 | 271 | self::sanitize_field_opt( $opt, $field->field_options[ $opt ] ); |
| 365 | 272 | } |
| 366 | 273 | |
| 367 | 274 | $field->field_options = apply_filters( 'frm_update_field_options', $field->field_options, $field, $values ); |
| @@ -370,87 +277,28 @@ | ||
| 370 | 277 | 'field_options' => $field->field_options, |
| 371 | 278 | 'default_value' => isset( $values[ 'default_value_' . $field_id ] ) ? FrmAppHelper::maybe_json_encode( $values[ 'default_value_' . $field_id ] ) : '', |
| 372 | 279 | ); |
| 373 | 280 | |
| 374 | - if ( ! FrmAppHelper::allow_unfiltered_html() && isset( $values['field_options'][ 'options_' . $field_id ] ) && is_array( $values['field_options'][ 'options_' . $field_id ] ) ) { | |
| 375 | - foreach ( $values['field_options'][ 'options_' . $field_id ] as $option_key => $option ) { | |
| 376 | - if ( is_array( $option ) ) { | |
| 377 | - foreach ( $option as $key => $item ) { | |
| 378 | - $values['field_options'][ 'options_' . $field_id ][ $option_key ][ $key ] = FrmAppHelper::kses( $item, 'all' ); | |
| 379 | - } | |
| 380 | - } | |
| 381 | - } | |
| 382 | - } | |
| 383 | - | |
| 384 | 281 | self::prepare_field_update_values( $field, $values, $new_field ); |
| 385 | - self::maybe_update_max_option( $field, $values, $new_field ); | |
| 386 | 282 | |
| 387 | 283 | FrmField::update( $field_id, $new_field ); |
| 388 | 284 | |
| 389 | 285 | FrmField::delete_form_transient( $field->form_id ); |
| 390 | - }//end foreach | |
| 286 | + } | |
| 391 | 287 | self::clear_form_cache(); |
| 392 | 288 | |
| 393 | 289 | return $values; |
| 394 | 290 | } |
| 395 | 291 | |
| 396 | - /** | |
| 397 | - * Resets the 'max' option of a field when changing paragraph field type to other field types like text, email etc. | |
| 398 | - * | |
| 399 | - * @since 6.7 | |
| 400 | - * | |
| 401 | - * @param array $field | |
| 402 | - * @param array $values | |
| 403 | - * @param array $new_field | |
| 404 | - * @return void | |
| 405 | - */ | |
| 406 | - private static function maybe_update_max_option( $field, $values, &$new_field ) { | |
| 407 | - if ( $field->type === 'textarea' && | |
| 408 | - ! empty( $values['field_options'][ 'type_' . $field->id ] ) && | |
| 409 | - in_array( $values['field_options'][ 'type_' . $field->id ], array( 'text', 'email', 'url', 'password', 'phone' ), true ) ) { | |
| 410 | - | |
| 411 | - $new_field['field_options']['max'] = ''; | |
| 412 | - | |
| 413 | - /** | |
| 414 | - * Update posted field setting so that new 'max' option is displayed after form is saved and page reloads. | |
| 415 | - * FrmFieldsHelper::fill_default_field_opts populates field options by calling self::get_posted_field_setting. | |
| 416 | - */ | |
| 417 | - $_POST['field_options'][ 'max_' . $field->id ] = ''; | |
| 418 | - } | |
| 419 | - } | |
| 420 | - | |
| 421 | - /** | |
| 422 | - * @param string $opt | |
| 423 | - * @param mixed $value | |
| 424 | - * @return void | |
| 425 | - */ | |
| 426 | 292 | private static function sanitize_field_opt( $opt, &$value ) { |
| 427 | - if ( ! is_string( $value ) ) { | |
| 428 | - return; | |
| 293 | + if ( is_string( $value ) ) { | |
| 294 | + if ( $opt === 'calc' ) { | |
| 295 | + $value = self::sanitize_calc( $value ); | |
| 296 | + } else { | |
| 297 | + $value = FrmAppHelper::kses( $value, 'all' ); | |
| 298 | + } | |
| 299 | + $value = trim( $value ); | |
| 429 | 300 | } |
| 430 | - | |
| 431 | - /** | |
| 432 | - * Allow the option to turn off sanitization for a field. This way a custom rule can be used instead. | |
| 433 | - * Make sure to add custom sanitization using the frm_update_field_options filter as the data will no longer be sanitized. | |
| 434 | - * | |
| 435 | - * @since 6.0 | |
| 436 | - * | |
| 437 | - * @param bool $should_sanitize | |
| 438 | - * @param string $opt | |
| 439 | - */ | |
| 440 | - $should_sanitize = apply_filters( 'frm_should_sanitize_field_opt_string', true, $opt ); | |
| 441 | - | |
| 442 | - if ( ! $should_sanitize ) { | |
| 443 | - return; | |
| 444 | - } | |
| 445 | - | |
| 446 | - if ( $opt === 'calc' ) { | |
| 447 | - $value = self::sanitize_calc( $value ); | |
| 448 | - } else { | |
| 449 | - $value = FrmAppHelper::kses( $value, 'all' ); | |
| 450 | - } | |
| 451 | - | |
| 452 | - $value = trim( $value ); | |
| 453 | 301 | } |
| 454 | 302 | |
| 455 | 303 | /** |
| 456 | 304 | * @param string $value |
| @@ -459,10 +307,9 @@ | ||
| 459 | 307 | private static function sanitize_calc( $value ) { |
| 460 | 308 | if ( false !== strpos( $value, '<' ) ) { |
| 461 | 309 | $value = self::normalize_calc_spaces( $value ); |
| 462 | 310 | } |
| 463 | - // Allow <= and >=. | |
| 464 | - $allow = array( '<= ', ' >=' ); | |
| 311 | + $allow = array( '<= ', ' >=' ); // Allow <= and >= | |
| 465 | 312 | $temp = array( '< = ', ' > =' ); |
| 466 | 313 | $value = str_replace( $allow, $temp, $value ); |
| 467 | 314 | $value = strip_tags( $value ); |
| 468 | 315 | $value = str_replace( $temp, $allow, $value ); |
| @@ -477,16 +324,14 @@ | ||
| 477 | 324 | * @return string |
| 478 | 325 | */ |
| 479 | 326 | private static function normalize_calc_spaces( $calc ) { |
| 480 | 327 | // Check for a pattern with 5 parts |
| 481 | - // $1 \d|\] the first comparison digit or the end of a comparison shortcode. | |
| 328 | + // $1 \d the first comparison digit. | |
| 482 | 329 | // $2 a space (optional). |
| 483 | 330 | // $3 an equals sign (optional) that follows the < operator for <= comparisons. |
| 484 | 331 | // $4 another space (optional). |
| 485 | - // $5 \d|\[ the second comparison digit or the start of a comparison shortcode. | |
| 486 | - $calc = preg_replace( '/(\d|\])( ){0,1}<(=){0,1}( ){0,1}(\d|\[)/', '$1 <$3 $5', $calc ); | |
| 487 | - | |
| 488 | - return $calc; | |
| 332 | + // $5 \d the second comparison digit. | |
| 333 | + return preg_replace( '/(\d)( ){0,1}<(=){0,1}( ){0,1}(\d)/', '$1 <$3 $5', $calc ); | |
| 489 | 334 | } |
| 490 | 335 | |
| 491 | 336 | /** |
| 492 | 337 | * Updating the settings page |
| @@ -493,12 +338,12 @@ | ||
| 493 | 338 | */ |
| 494 | 339 | private static function get_settings_page_html( $values, &$field ) { |
| 495 | 340 | if ( isset( $values['field_options'][ 'custom_html_' . $field->id ] ) ) { |
| 496 | 341 | $prev_opts = array(); |
| 497 | - $fallback_html = $field->field_options['custom_html'] ?? FrmFieldsHelper::get_default_html( $field->type ); | |
| 342 | + $fallback_html = isset( $field->field_options['custom_html'] ) ? $field->field_options['custom_html'] : FrmFieldsHelper::get_default_html( $field->type ); | |
| 498 | 343 | |
| 499 | - $field->field_options['custom_html'] = $values['field_options'][ 'custom_html_' . $field->id ] ?? $fallback_html; | |
| 500 | - } elseif ( $field->type === 'hidden' || $field->type === 'user_id' ) { | |
| 344 | + $field->field_options['custom_html'] = isset( $values['field_options'][ 'custom_html_' . $field->id ] ) ? $values['field_options'][ 'custom_html_' . $field->id ] : $fallback_html; | |
| 345 | + } elseif ( $field->type == 'hidden' || $field->type == 'user_id' ) { | |
| 501 | 346 | $prev_opts = $field->field_options; |
| 502 | 347 | } |
| 503 | 348 | |
| 504 | 349 | if ( isset( $prev_opts ) ) { |
| @@ -519,14 +364,11 @@ | ||
| 519 | 364 | 'options' => '', |
| 520 | 365 | 'name' => '', |
| 521 | 366 | ); |
| 522 | 367 | foreach ( $field_cols as $col => $default ) { |
| 523 | - $default = $default === '' ? $field->{$col} : $default; | |
| 524 | - $new_field[ $col ] = $values['field_options'][ $col . '_' . $field->id ] ?? $default; | |
| 525 | - } | |
| 368 | + $default = ( $default === '' ) ? $field->{$col} : $default; | |
| 526 | 369 | |
| 527 | - if ( $field->type === 'submit' && isset( $new_field['field_order'] ) && (int) $new_field['field_order'] === FrmSubmitHelper::DEFAULT_ORDER ) { | |
| 528 | - $new_field['field_order'] = $field->field_order; | |
| 370 | + $new_field[ $col ] = isset( $values['field_options'][ $col . '_' . $field->id ] ) ? $values['field_options'][ $col . '_' . $field->id ] : $default; | |
| 529 | 371 | } |
| 530 | 372 | |
| 531 | 373 | // Don't save the template option. |
| 532 | 374 | if ( is_array( $new_field['options'] ) && isset( $new_field['options']['000'] ) ) { |
| @@ -538,9 +380,9 @@ | ||
| 538 | 380 | * Get a list of all form settings that should be translated |
| 539 | 381 | * on a multilingual site. |
| 540 | 382 | * |
| 541 | 383 | * @since 3.06.01 |
| 542 | - * @param object $form The form object. | |
| 384 | + * @param object $form - The form object | |
| 543 | 385 | */ |
| 544 | 386 | public static function translatable_strings( $form ) { |
| 545 | 387 | $strings = array( |
| 546 | 388 | 'name', |
| @@ -547,12 +389,8 @@ | ||
| 547 | 389 | 'description', |
| 548 | 390 | 'submit_value', |
| 549 | 391 | 'submit_msg', |
| 550 | 392 | 'success_msg', |
| 551 | - 'invalid_msg', | |
| 552 | - 'failed_msg', | |
| 553 | - 'login_msg', | |
| 554 | - 'admin_permission', | |
| 555 | 393 | ); |
| 556 | 394 | |
| 557 | 395 | return apply_filters( 'frm_form_strings', $strings, $form ); |
| 558 | 396 | } |
| @@ -557,12 +395,11 @@ | ||
| 557 | 395 | return apply_filters( 'frm_form_strings', $strings, $form ); |
| 558 | 396 | } |
| 559 | 397 | |
| 560 | 398 | /** |
| 561 | - * @param int $id | |
| 562 | 399 | * @param string $status |
| 563 | 400 | * |
| 564 | - * @return bool|int | |
| 401 | + * @return int|boolean | |
| 565 | 402 | */ |
| 566 | 403 | public static function set_status( $id, $status ) { |
| 567 | 404 | if ( 'trash' == $status ) { |
| 568 | 405 | return self::trash( $id ); |
| @@ -568,9 +405,9 @@ | ||
| 568 | 405 | return self::trash( $id ); |
| 569 | 406 | } |
| 570 | 407 | |
| 571 | 408 | $statuses = array( 'published', 'draft', 'trash' ); |
| 572 | - if ( ! in_array( $status, $statuses, true ) ) { | |
| 409 | + if ( ! in_array( $status, $statuses ) ) { | |
| 573 | 410 | return false; |
| 574 | 411 | } |
| 575 | 412 | |
| 576 | 413 | global $wpdb; |
| @@ -597,9 +434,9 @@ | ||
| 597 | 434 | return $query_results; |
| 598 | 435 | } |
| 599 | 436 | |
| 600 | 437 | /** |
| 601 | - * @return bool|int | |
| 438 | + * @return int|boolean | |
| 602 | 439 | */ |
| 603 | 440 | public static function trash( $id ) { |
| 604 | 441 | if ( ! EMPTY_TRASH_DAYS ) { |
| 605 | 442 | return self::destroy( $id ); |
| @@ -609,12 +446,8 @@ | ||
| 609 | 446 | if ( ! $form ) { |
| 610 | 447 | return false; |
| 611 | 448 | } |
| 612 | 449 | |
| 613 | - if ( $form->status === 'trash' ) { | |
| 614 | - return false; | |
| 615 | - } | |
| 616 | - | |
| 617 | 450 | $options = $form->options; |
| 618 | 451 | $options['trash_time'] = time(); |
| 619 | 452 | |
| 620 | 453 | global $wpdb; |
| @@ -647,9 +480,9 @@ | ||
| 647 | 480 | return $query_results; |
| 648 | 481 | } |
| 649 | 482 | |
| 650 | 483 | /** |
| 651 | - * @return bool|int | |
| 484 | + * @return int|boolean | |
| 652 | 485 | */ |
| 653 | 486 | public static function destroy( $id ) { |
| 654 | 487 | global $wpdb; |
| 655 | 488 | |
| @@ -671,11 +504,8 @@ | ||
| 671 | 504 | |
| 672 | 505 | $query_results = $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_forms WHERE id=%d OR parent_form_id=%d', $id, $id ) ); |
| 673 | 506 | if ( $query_results ) { |
| 674 | 507 | // Delete all form actions linked to this form |
| 675 | - /** | |
| 676 | - * @var FrmFormAction | |
| 677 | - */ | |
| 678 | 508 | $action_control = FrmFormActionsController::get_form_actions( 'email' ); |
| 679 | 509 | $action_control->destroy( $id, 'all' ); |
| 680 | 510 | |
| 681 | 511 | // Clear form caching |
| @@ -695,9 +525,9 @@ | ||
| 695 | 525 | */ |
| 696 | 526 | public static function scheduled_delete( $delete_timestamp = '' ) { |
| 697 | 527 | global $wpdb; |
| 698 | 528 | |
| 699 | - $trash_forms = FrmDb::get_results( $wpdb->prefix . 'frm_forms', array( 'status' => 'trash' ), 'id, parent_form_id, options' ); | |
| 529 | + $trash_forms = FrmDb::get_results( $wpdb->prefix . 'frm_forms', array( 'status' => 'trash' ), 'id, options' ); | |
| 700 | 530 | |
| 701 | 531 | if ( ! $trash_forms ) { |
| 702 | 532 | return 0; |
| 703 | 533 | } |
| @@ -710,11 +540,9 @@ | ||
| 710 | 540 | foreach ( $trash_forms as $form ) { |
| 711 | 541 | FrmAppHelper::unserialize_or_decode( $form->options ); |
| 712 | 542 | if ( ! isset( $form->options['trash_time'] ) || $form->options['trash_time'] < $delete_timestamp ) { |
| 713 | 543 | self::destroy( $form->id ); |
| 714 | - if ( empty( $form->parent_form_id ) ) { | |
| 715 | - ++$count; | |
| 716 | - } | |
| 544 | + $count ++; | |
| 717 | 545 | } |
| 718 | 546 | |
| 719 | 547 | unset( $form ); |
| 720 | 548 | } |
| @@ -734,12 +562,10 @@ | ||
| 734 | 562 | } |
| 735 | 563 | |
| 736 | 564 | $query_key = is_numeric( $id ) ? 'id' : 'form_key'; |
| 737 | 565 | $r = FrmDb::get_var( 'frm_forms', array( $query_key => $id ), 'name' ); |
| 566 | + $r = stripslashes( $r ); | |
| 738 | 567 | |
| 739 | - // An empty form name can result in a null value. | |
| 740 | - $r = is_null( $r ) ? '' : stripslashes( $r ); | |
| 741 | - | |
| 742 | 568 | return $r; |
| 743 | 569 | } |
| 744 | 570 | |
| 745 | 571 | /** |
| @@ -774,10 +600,11 @@ | ||
| 774 | 600 | |
| 775 | 601 | /** |
| 776 | 602 | * If $form is numeric, get the form object |
| 777 | 603 | * |
| 604 | + * @param object|int $form | |
| 605 | + * | |
| 778 | 606 | * @since 2.0.9 |
| 779 | - * @param int|object $form | |
| 780 | 607 | */ |
| 781 | 608 | public static function maybe_get_form( &$form ) { |
| 782 | 609 | if ( ! is_object( $form ) && ! is_array( $form ) && ! empty( $form ) ) { |
| 783 | 610 | $form = self::getOne( $form ); |
| @@ -784,11 +611,9 @@ | ||
| 784 | 611 | } |
| 785 | 612 | } |
| 786 | 613 | |
| 787 | 614 | /** |
| 788 | - * @param int|string $id | |
| 789 | - * @param false|int $blog_id | |
| 790 | - * @return stdClass|null | |
| 615 | + * @return object form | |
| 791 | 616 | */ |
| 792 | 617 | public static function getOne( $id, $blog_id = false ) { |
| 793 | 618 | global $wpdb; |
| 794 | 619 | |
| @@ -803,9 +628,10 @@ | ||
| 803 | 628 | if ( $cache ) { |
| 804 | 629 | if ( isset( $cache->options ) ) { |
| 805 | 630 | FrmAppHelper::unserialize_or_decode( $cache->options ); |
| 806 | 631 | } |
| 807 | - return self::prepare_form_row_data( $cache ); | |
| 632 | + | |
| 633 | + return wp_unslash( $cache ); | |
| 808 | 634 | } |
| 809 | 635 | } |
| 810 | 636 | |
| 811 | 637 | if ( is_numeric( $id ) ) { |
| @@ -820,43 +646,17 @@ | ||
| 820 | 646 | FrmDb::set_cache( $results->id, $results, 'frm_form' ); |
| 821 | 647 | FrmAppHelper::unserialize_or_decode( $results->options ); |
| 822 | 648 | } |
| 823 | 649 | |
| 824 | - return self::prepare_form_row_data( $results ); | |
| 650 | + return apply_filters( 'frm_form_object', wp_unslash( $results ) ); | |
| 825 | 651 | } |
| 826 | 652 | |
| 827 | 653 | /** |
| 828 | - * Make sure that if $row is an object, that $row->options is an array and not a string. | |
| 829 | - * | |
| 830 | - * @since 6.8.3 | |
| 831 | - * | |
| 832 | - * @param stdClass|null $row The database row for a target form. | |
| 833 | - * @return stdClass|null | |
| 654 | + * @return object|array of objects | |
| 834 | 655 | */ |
| 835 | - private static function prepare_form_row_data( $row ) { | |
| 836 | - $row = wp_unslash( $row ); | |
| 837 | - if ( ! is_object( $row ) ) { | |
| 838 | - return $row; | |
| 839 | - } | |
| 840 | - | |
| 841 | - if ( ! is_array( $row->options ) ) { | |
| 842 | - $row->options = FrmFormsHelper::get_default_opts(); | |
| 843 | - } | |
| 844 | - | |
| 845 | - /** | |
| 846 | - * @since 4.03.02 | |
| 847 | - * | |
| 848 | - * @param stdClass $row | |
| 849 | - */ | |
| 850 | - return apply_filters( 'frm_form_object', $row ); | |
| 851 | - } | |
| 852 | - | |
| 853 | - /** | |
| 854 | - * @return array|object of objects | |
| 855 | - */ | |
| 856 | 656 | public static function getAll( $where = array(), $order_by = '', $limit = '' ) { |
| 857 | 657 | if ( is_array( $where ) && ! empty( $where ) ) { |
| 858 | - if ( ! empty( $where['is_template'] ) && ! isset( $where['status'] ) && ! isset( $where['status !'] ) ) { | |
| 658 | + if ( isset( $where['is_template'] ) && $where['is_template'] && ! isset( $where['status'] ) && ! isset( $where['status !'] ) ) { | |
| 859 | 659 | // don't get trashed templates |
| 860 | 660 | $where['status'] = array( null, '', 'published' ); |
| 861 | 661 | } |
| 862 | 662 | |
| @@ -863,9 +663,9 @@ | ||
| 863 | 663 | $results = FrmDb::get_results( 'frm_forms', $where, '*', compact( 'order_by', 'limit' ) ); |
| 864 | 664 | } else { |
| 865 | 665 | global $wpdb; |
| 866 | 666 | |
| 867 | - // The query has already been prepared if this is not an array. | |
| 667 | + // the query has already been prepared if this is not an array | |
| 868 | 668 | $query = 'SELECT * FROM ' . $wpdb->prefix . 'frm_forms' . FrmDb::prepend_and_or_where( ' WHERE ', $where ) . FrmDb::esc_order( $order_by ) . FrmDb::esc_limit( $limit ); |
| 869 | 669 | $results = $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 870 | 670 | } |
| 871 | 671 | |
| @@ -875,9 +675,9 @@ | ||
| 875 | 675 | FrmAppHelper::unserialize_or_decode( $result->options ); |
| 876 | 676 | } |
| 877 | 677 | } |
| 878 | 678 | |
| 879 | - if ( $limit === ' LIMIT 1' || $limit == 1 ) { | |
| 679 | + if ( $limit == ' LIMIT 1' || $limit == 1 ) { | |
| 880 | 680 | // return the first form object if we are only getting one form |
| 881 | 681 | $results = reset( $results ); |
| 882 | 682 | } |
| 883 | 683 | |
| @@ -887,18 +687,14 @@ | ||
| 887 | 687 | /** |
| 888 | 688 | * Get all published forms |
| 889 | 689 | * |
| 890 | 690 | * @since 2.0 |
| 891 | - * | |
| 892 | - * @param array $query | |
| 893 | - * @param int $limit | |
| 894 | - * @param string $inc_children | |
| 895 | - * @return array|object of forms A single form object would be passed if $limit was set to 1. | |
| 691 | + * @return array of forms | |
| 896 | 692 | */ |
| 897 | 693 | public static function get_published_forms( $query = array(), $limit = 999, $inc_children = 'exclude' ) { |
| 898 | 694 | $query['is_template'] = 0; |
| 899 | 695 | $query['status'] = array( null, '', 'published' ); |
| 900 | - if ( $inc_children === 'exclude' ) { | |
| 696 | + if ( $inc_children == 'exclude' ) { | |
| 901 | 697 | $query['parent_form_id'] = array( null, 0 ); |
| 902 | 698 | } |
| 903 | 699 | |
| 904 | 700 | $forms = self::getAll( $query, 'name', $limit ); |
| @@ -934,18 +730,18 @@ | ||
| 934 | 730 | |
| 935 | 731 | foreach ( $results as $row ) { |
| 936 | 732 | if ( 'trash' != $row->status ) { |
| 937 | 733 | if ( $row->is_template ) { |
| 938 | - ++$counts['template']; | |
| 734 | + $counts['template'] ++; | |
| 939 | 735 | } else { |
| 940 | - ++$counts['published']; | |
| 736 | + $counts['published'] ++; | |
| 941 | 737 | } |
| 942 | 738 | } else { |
| 943 | - ++$counts['trash']; | |
| 739 | + $counts['trash'] ++; | |
| 944 | 740 | } |
| 945 | 741 | |
| 946 | 742 | if ( 'draft' == $row->status ) { |
| 947 | - ++$counts['draft']; | |
| 743 | + $counts['draft'] ++; | |
| 948 | 744 | } |
| 949 | 745 | |
| 950 | 746 | unset( $row ); |
| 951 | 747 | } |
| @@ -1011,11 +807,11 @@ | ||
| 1011 | 807 | $values['posted_form_id'] = FrmAppHelper::get_param( 'form', '', 'get', 'absint' ); |
| 1012 | 808 | } |
| 1013 | 809 | |
| 1014 | 810 | if ( $form->id == $values['posted_form_id'] ) { |
| 1015 | - // If there are two forms on the same page, make sure not to submit both. | |
| 811 | + //if there are two forms on the same page, make sure not to submit both | |
| 1016 | 812 | foreach ( $default_values as $var => $default ) { |
| 1017 | - if ( $var === 'action' ) { | |
| 813 | + if ( $var == 'action' ) { | |
| 1018 | 814 | $values[ $var ] = FrmAppHelper::get_param( $action_var, $default, 'get', 'sanitize_title' ); |
| 1019 | 815 | } else { |
| 1020 | 816 | $values[ $var ] = FrmAppHelper::get_param( $var, $default, 'get', 'sanitize_text_field' ); |
| 1021 | 817 | } |
| @@ -1083,9 +879,9 @@ | ||
| 1083 | 879 | return $values; |
| 1084 | 880 | } |
| 1085 | 881 | |
| 1086 | 882 | public static function get_current_form_id( $default_form = 'none' ) { |
| 1087 | - if ( 'first' === $default_form ) { | |
| 883 | + if ( 'first' == $default_form ) { | |
| 1088 | 884 | $form = self::get_current_form(); |
| 1089 | 885 | } else { |
| 1090 | 886 | $form = self::maybe_get_current_form(); |
| 1091 | 887 | } |
| @@ -1096,9 +892,9 @@ | ||
| 1096 | 892 | |
| 1097 | 893 | public static function maybe_get_current_form( $form_id = 0 ) { |
| 1098 | 894 | global $frm_vars; |
| 1099 | 895 | |
| 1100 | - if ( ! empty( $frm_vars['current_form'] ) && ( ! $form_id || $form_id == $frm_vars['current_form']->id ) ) { | |
| 896 | + if ( isset( $frm_vars['current_form'] ) && $frm_vars['current_form'] && ( ! $form_id || $form_id == $frm_vars['current_form']->id ) ) { | |
| 1101 | 897 | return $frm_vars['current_form']; |
| 1102 | 898 | } |
| 1103 | 899 | |
| 1104 | 900 | $form_id = FrmAppHelper::get_param( 'form', $form_id, 'get', 'absint' ); |
| @@ -1145,9 +941,9 @@ | ||
| 1145 | 941 | $global_load = true; |
| 1146 | 942 | $frm_vars['load_css'] = true; |
| 1147 | 943 | } |
| 1148 | 944 | |
| 1149 | - return empty( $frm_vars['css_loaded'] ) && $global_load; | |
| 945 | + return ( ( ! isset( $frm_vars['css_loaded'] ) || ! $frm_vars['css_loaded'] ) && $global_load ); | |
| 1150 | 946 | } |
| 1151 | 947 | |
| 1152 | 948 | /** |
| 1153 | 949 | * @since 4.06.03 |
| @@ -1162,21 +958,13 @@ | ||
| 1162 | 958 | } else { |
| 1163 | 959 | $visible = true; |
| 1164 | 960 | } |
| 1165 | 961 | |
| 1166 | - /** | |
| 1167 | - * @since 6.25 | |
| 1168 | - * | |
| 1169 | - * @param bool $visible | |
| 1170 | - * @param object $form | |
| 1171 | - */ | |
| 1172 | - $visible = (bool) apply_filters( 'frm_form_is_visible', $visible, $form ); | |
| 1173 | - | |
| 1174 | 962 | return $visible; |
| 1175 | 963 | } |
| 1176 | 964 | |
| 1177 | 965 | public static function show_submit( $form ) { |
| 1178 | - $show = ( ! $form->is_template && $form->status === 'published' && ! FrmAppHelper::is_admin() ); | |
| 966 | + $show = ( ! $form->is_template && $form->status == 'published' && ! FrmAppHelper::is_admin() ); | |
| 1179 | 967 | $show = apply_filters( 'frm_show_submit_button', $show, $form ); |
| 1180 | 968 | |
| 1181 | 969 | return $show; |
| 1182 | 970 | } |
| @@ -1184,12 +972,12 @@ | ||
| 1184 | 972 | /** |
| 1185 | 973 | * @since 2.3 |
| 1186 | 974 | */ |
| 1187 | 975 | public static function get_option( $atts ) { |
| 1188 | - $form = $atts['form']; | |
| 1189 | - $default = $atts['default'] ?? ''; | |
| 976 | + $form = $atts['form']; | |
| 977 | + $default = isset( $atts['default'] ) ? $atts['default'] : ''; | |
| 1190 | 978 | |
| 1191 | - return $form->options[ $atts['option'] ] ?? $default; | |
| 979 | + return isset( $form->options[ $atts['option'] ] ) ? $form->options[ $atts['option'] ] : $default; | |
| 1192 | 980 | } |
| 1193 | 981 | |
| 1194 | 982 | /** |
| 1195 | 983 | * Get the link to edit this form. |
| @@ -1201,81 +989,23 @@ | ||
| 1201 | 989 | return admin_url( 'admin.php?page=formidable&frm_action=edit&id=' . $form_id ); |
| 1202 | 990 | } |
| 1203 | 991 | |
| 1204 | 992 | /** |
| 1205 | - * Check if the "Submit this form with AJAX" setting is toggled on. | |
| 1206 | - * | |
| 1207 | - * @since 6.2 | |
| 1208 | - * | |
| 1209 | - * @param stdClass $form | |
| 1210 | - * @return bool | |
| 1211 | - */ | |
| 1212 | - public static function is_ajax_on( $form ) { | |
| 1213 | - return ! empty( $form->options['ajax_submit'] ); | |
| 1214 | - } | |
| 1215 | - | |
| 1216 | - /** | |
| 1217 | - * Get the latest form available. | |
| 1218 | - * | |
| 1219 | - * @since 6.8 | |
| 1220 | - * @return object | |
| 1221 | - */ | |
| 1222 | - public static function get_latest_form() { | |
| 1223 | - | |
| 1224 | - $args = array( | |
| 1225 | - array( | |
| 1226 | - 'or' => 1, | |
| 1227 | - 'parent_form_id' => null, | |
| 1228 | - 'parent_form_id <' => 1, | |
| 1229 | - ), | |
| 1230 | - 'is_template' => 0, | |
| 1231 | - 'status !' => 'trash', | |
| 1232 | - ); | |
| 1233 | - | |
| 1234 | - return self::getAll( $args, 'created_at desc', 1 ); | |
| 1235 | - } | |
| 1236 | - | |
| 1237 | - /** | |
| 1238 | - * Count and return total forms. | |
| 1239 | - * | |
| 1240 | - * @since 6.8 | |
| 1241 | - * @return int | |
| 1242 | - */ | |
| 1243 | - public static function get_forms_count() { | |
| 1244 | - | |
| 1245 | - $args = array( | |
| 1246 | - array( | |
| 1247 | - 'or' => 1, | |
| 1248 | - 'parent_form_id' => null, | |
| 1249 | - 'parent_form_id <' => 1, | |
| 1250 | - ), | |
| 1251 | - 'is_template' => 0, | |
| 1252 | - 'status !' => 'trash', | |
| 1253 | - ); | |
| 1254 | - | |
| 1255 | - return FrmDb::get_count( 'frm_forms', $args ); | |
| 1256 | - } | |
| 1257 | - | |
| 1258 | - /** | |
| 1259 | - * @deprecated 2.03.05 This is still referenced in a few add ons (API, locations). | |
| 993 | + * @deprecated 3.0 | |
| 1260 | 994 | * @codeCoverageIgnore |
| 1261 | 995 | * |
| 1262 | 996 | * @param string $key |
| 997 | + * | |
| 1263 | 998 | * @return int form id |
| 1264 | 999 | */ |
| 1265 | 1000 | public static function getIdByKey( $key ) { |
| 1266 | - _deprecated_function( __FUNCTION__, '2.03.05', 'FrmForm::get_id_by_key' ); | |
| 1267 | - return self::get_id_by_key( $key ); | |
| 1001 | + return FrmFormDeprecated::getIdByKey( $key ); | |
| 1268 | 1002 | } |
| 1269 | 1003 | |
| 1270 | 1004 | /** |
| 1271 | - * @deprecated 2.03.05 This is still referenced in the API add on as of v1.13. | |
| 1005 | + * @deprecated 3.0 | |
| 1272 | 1006 | * @codeCoverageIgnore |
| 1273 | - * | |
| 1274 | - * @param int|string $id | |
| 1275 | - * @return string | |
| 1276 | 1007 | */ |
| 1277 | 1008 | public static function getKeyById( $id ) { |
| 1278 | - _deprecated_function( __FUNCTION__, '2.03.05', 'FrmForm::get_key_by_id' ); | |
| 1279 | - return self::get_key_by_id( $id ); | |
| 1009 | + return FrmFormDeprecated::getKeyById( $id ); | |
| 1280 | 1010 | } |
| 1281 | 1011 | } |