| @@ -5,49 +5,38 @@ | ||
| 5 | 5 | |
| 6 | 6 | class FrmForm { |
| 7 | 7 | |
| 8 | 8 | /** |
| 9 | - * @param array $values | |
| 10 | - * | |
| 11 | - * @return bool|int id on success or false on failure. | |
| 9 | + * @return int|boolean id on success or false on failure | |
| 12 | 10 | */ |
| 13 | 11 | public static function create( $values ) { |
| 14 | 12 | global $wpdb; |
| 15 | 13 | |
| 16 | - $values = FrmAppHelper::maybe_filter_array( $values, array( 'name', 'description' ) ); | |
| 17 | - | |
| 18 | 14 | $new_values = array( |
| 19 | 15 | 'form_key' => FrmAppHelper::get_unique_key( $values['form_key'], $wpdb->prefix . 'frm_forms', 'form_key' ), |
| 20 | 16 | 'name' => $values['name'], |
| 21 | 17 | 'description' => $values['description'], |
| 22 | - 'status' => $values['status'] ?? 'published', | |
| 23 | - 'logged_in' => $values['logged_in'] ?? 0, | |
| 18 | + 'status' => isset( $values['status'] ) ? $values['status'] : 'published', | |
| 19 | + 'logged_in' => isset( $values['logged_in'] ) ? $values['logged_in'] : 0, | |
| 24 | 20 | 'is_template' => isset( $values['is_template'] ) ? (int) $values['is_template'] : 0, |
| 25 | 21 | 'parent_form_id' => isset( $values['parent_form_id'] ) ? absint( $values['parent_form_id'] ) : 0, |
| 26 | 22 | 'editable' => isset( $values['editable'] ) ? (int) $values['editable'] : 0, |
| 27 | - 'created_at' => $values['created_at'] ?? current_time( 'mysql', 1 ), | |
| 23 | + 'created_at' => isset( $values['created_at'] ) ? $values['created_at'] : current_time( 'mysql', 1 ), | |
| 28 | 24 | ); |
| 29 | 25 | |
| 30 | 26 | $options = isset( $values['options'] ) ? (array) $values['options'] : array(); |
| 31 | 27 | FrmFormsHelper::fill_form_options( $options, $values ); |
| 32 | 28 | |
| 33 | - $options['before_html'] = $values['options']['before_html'] ?? FrmFormsHelper::get_default_html( 'before' ); | |
| 34 | - $options['after_html'] = $values['options']['after_html'] ?? FrmFormsHelper::get_default_html( 'after' ); | |
| 35 | - $options['submit_html'] = $values['options']['submit_html'] ?? FrmFormsHelper::get_default_html( 'submit' ); | |
| 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' ); | |
| 36 | 32 | |
| 37 | - /** | |
| 38 | - * Allows modifying form options before updating or creating. | |
| 39 | - * | |
| 40 | - * @since 5.4 Add the third param. | |
| 41 | - * | |
| 42 | - * @param array $options Form options. | |
| 43 | - * @param array $values Form data. | |
| 44 | - * @param bool $update Is form updating or creating. It's `true` if is updating. | |
| 45 | - */ | |
| 46 | - $options = apply_filters( 'frm_form_options_before_update', $options, $values, false ); | |
| 47 | - $options = self::maybe_filter_form_options( $options ); | |
| 33 | + $options = apply_filters( 'frm_form_options_before_update', $options, $values ); | |
| 48 | 34 | $new_values['options'] = serialize( $options ); |
| 49 | 35 | |
| 36 | + //if(isset($values['id']) && is_numeric($values['id'])) | |
| 37 | + // $new_values['id'] = $values['id']; | |
| 38 | + | |
| 50 | 39 | $wpdb->insert( $wpdb->prefix . 'frm_forms', $new_values ); |
| 51 | 40 | |
| 52 | 41 | $id = $wpdb->insert_id; |
| 53 | 42 | |
| @@ -57,36 +46,14 @@ | ||
| 57 | 46 | return $id; |
| 58 | 47 | } |
| 59 | 48 | |
| 60 | 49 | /** |
| 61 | - * @since 5.0.08 | |
| 62 | - * | |
| 63 | - * @param array $options | |
| 64 | - * | |
| 65 | - * @return array | |
| 50 | + * @return int|boolean ID on success or false on failure | |
| 66 | 51 | */ |
| 67 | - private static function maybe_filter_form_options( $options ) { | |
| 68 | - if ( ! FrmAppHelper::allow_unfiltered_html() && ! empty( $options['submit_html'] ) ) { | |
| 69 | - $options['submit_html'] = FrmAppHelper::kses_submit_button( $options['submit_html'] ); | |
| 70 | - } | |
| 71 | - return FrmAppHelper::maybe_filter_array( $options, array( 'submit_value', 'success_msg', 'before_html', 'after_html' ) ); | |
| 72 | - } | |
| 73 | - | |
| 74 | - /** | |
| 75 | - * Duplicate a form. | |
| 76 | - * | |
| 77 | - * @param int $id Form ID to duplicate. | |
| 78 | - * @param bool $template Whether the duplicated form is a template. | |
| 79 | - * @param bool $copy_keys Whether to copy the original form key. | |
| 80 | - * @param false|int $blog_id Blog ID when duplicating across sites, or false for current site. | |
| 81 | - * | |
| 82 | - * @return bool|int ID on success or false on failure | |
| 83 | - */ | |
| 84 | 52 | public static function duplicate( $id, $template = false, $copy_keys = false, $blog_id = false ) { |
| 85 | 53 | global $wpdb; |
| 86 | 54 | |
| 87 | 55 | $values = self::getOne( $id, $blog_id ); |
| 88 | - | |
| 89 | 56 | if ( ! $values ) { |
| 90 | 57 | return false; |
| 91 | 58 | } |
| 92 | 59 | |
| @@ -103,10 +70,10 @@ | ||
| 103 | 70 | 'is_template' => $template ? 1 : 0, |
| 104 | 71 | ); |
| 105 | 72 | |
| 106 | 73 | if ( $blog_id ) { |
| 107 | - $new_values['status'] = 'published'; | |
| 108 | - $new_options = $values->options; | |
| 74 | + $new_values['status'] = 'published'; | |
| 75 | + $new_options = $values->options; | |
| 109 | 76 | FrmAppHelper::unserialize_or_decode( $new_options ); |
| 110 | 77 | $new_options['email_to'] = get_option( 'admin_email' ); |
| 111 | 78 | $new_options['copy'] = false; |
| 112 | 79 | $new_values['options'] = $new_options; |
| @@ -135,16 +102,10 @@ | ||
| 135 | 102 | |
| 136 | 103 | return false; |
| 137 | 104 | } |
| 138 | 105 | |
| 139 | - /** | |
| 140 | - * @param int $form_id | |
| 141 | - * @param array $values | |
| 142 | - * | |
| 143 | - * @return void | |
| 144 | - */ | |
| 145 | 106 | public static function after_duplicate( $form_id, $values ) { |
| 146 | - $new_opts = $values['options']; | |
| 107 | + $new_opts = $values['options']; | |
| 147 | 108 | FrmAppHelper::unserialize_or_decode( $new_opts ); |
| 148 | 109 | $values['options'] = $new_opts; |
| 149 | 110 | |
| 150 | 111 | if ( isset( $new_opts['success_msg'] ) ) { |
| @@ -156,105 +117,16 @@ | ||
| 156 | 117 | if ( $new_opts != $values['options'] ) { |
| 157 | 118 | global $wpdb; |
| 158 | 119 | $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'options' => maybe_serialize( $new_opts ) ), array( 'id' => $form_id ) ); |
| 159 | 120 | } |
| 160 | - | |
| 161 | - self::switch_field_ids_in_fields( $form_id ); | |
| 162 | 121 | } |
| 163 | 122 | |
| 164 | 123 | /** |
| 165 | - * Switches field ID in fields. | |
| 166 | - * | |
| 167 | - * @since 5.3 | |
| 168 | - * | |
| 169 | - * @param int $form_id Form ID. | |
| 170 | - * | |
| 171 | - * @return void | |
| 124 | + * @return int|boolean | |
| 172 | 125 | */ |
| 173 | - private static function switch_field_ids_in_fields( $form_id ) { | |
| 174 | - global $wpdb; | |
| 175 | - | |
| 176 | - // Keys of fields that you want to check to replace field ID. | |
| 177 | - $keys = array( 'default_value', 'field_options' ); | |
| 178 | - $sql_cols = 'fi.id'; | |
| 179 | - | |
| 180 | - foreach ( $keys as $key ) { | |
| 181 | - $sql_cols .= ',fi.' . $key; | |
| 182 | - } | |
| 183 | - | |
| 184 | - $fields = FrmDb::get_results( | |
| 185 | - "{$wpdb->prefix}frm_fields AS fi LEFT OUTER JOIN {$wpdb->prefix}frm_forms AS fr ON fi.form_id = fr.id", | |
| 186 | - array( | |
| 187 | - 'or' => 1, | |
| 188 | - 'fi.form_id' => $form_id, | |
| 189 | - 'fr.parent_form_id' => $form_id, | |
| 190 | - ), | |
| 191 | - $sql_cols | |
| 192 | - ); | |
| 193 | - | |
| 194 | - if ( ! $fields || ! is_array( $fields ) ) { | |
| 195 | - return; | |
| 196 | - } | |
| 197 | - | |
| 198 | - foreach ( $fields as $field ) { | |
| 199 | - self::switch_field_ids_in_field( (array) $field ); | |
| 200 | - } | |
| 201 | - } | |
| 202 | - | |
| 203 | - /** | |
| 204 | - * Switches field ID in a field. | |
| 205 | - * | |
| 206 | - * @since 5.3 | |
| 207 | - * | |
| 208 | - * @param array $field Field array. | |
| 209 | - * | |
| 210 | - * @return void | |
| 211 | - */ | |
| 212 | - private static function switch_field_ids_in_field( $field ) { | |
| 213 | - $new_values = array(); | |
| 214 | - | |
| 215 | - foreach ( $field as $key => $value ) { | |
| 216 | - if ( 'id' === $key || ! $value ) { | |
| 217 | - continue; | |
| 218 | - } | |
| 219 | - | |
| 220 | - if ( ! is_string( $value ) && ! is_array( $value ) ) { | |
| 221 | - continue; | |
| 222 | - } | |
| 223 | - | |
| 224 | - if ( 'field_options' === $key ) { | |
| 225 | - // Need to loop through field_options to prevent breaking serialized string when length changed. | |
| 226 | - FrmAppHelper::unserialize_or_decode( $value ); | |
| 227 | - $new_val = FrmFieldsHelper::switch_field_ids( $value ); | |
| 228 | - $new_val = serialize( $new_val ); | |
| 229 | - } else { | |
| 230 | - $new_val = FrmFieldsHelper::switch_field_ids( $value ); | |
| 231 | - } | |
| 232 | - | |
| 233 | - if ( $new_val !== $value ) { | |
| 234 | - $new_values[ $key ] = $new_val; | |
| 235 | - } | |
| 236 | - }//end foreach | |
| 237 | - | |
| 238 | - if ( ! empty( $new_values ) ) { | |
| 239 | - FrmField::update( $field['id'], $new_values ); | |
| 240 | - } | |
| 241 | - } | |
| 242 | - | |
| 243 | - /** | |
| 244 | - * Update a form. | |
| 245 | - * | |
| 246 | - * @param int $id Form ID. | |
| 247 | - * @param array $values Form values to update. | |
| 248 | - * @param bool $create_link Whether this is being called from link creation. | |
| 249 | - * | |
| 250 | - * @return bool|int | |
| 251 | - */ | |
| 252 | 126 | public static function update( $id, $values, $create_link = false ) { |
| 253 | 127 | global $wpdb; |
| 254 | 128 | |
| 255 | - $values = FrmAppHelper::maybe_filter_array( $values, array( 'name', 'description' ) ); | |
| 256 | - | |
| 257 | 129 | if ( ! isset( $values['status'] ) && ( $create_link || isset( $values['options'] ) || isset( $values['item_meta'] ) || isset( $values['field_options'] ) ) ) { |
| 258 | 130 | $values['status'] = 'published'; |
| 259 | 131 | } |
| 260 | 132 | |
| @@ -263,9 +135,9 @@ | ||
| 263 | 135 | } |
| 264 | 136 | |
| 265 | 137 | $form_fields = array( 'form_key', 'name', 'description', 'status', 'parent_form_id' ); |
| 266 | 138 | |
| 267 | - $new_values = self::set_update_options( array(), $values, array( 'form_id' => $id ) ); | |
| 139 | + $new_values = self::set_update_options( array(), $values ); | |
| 268 | 140 | |
| 269 | 141 | foreach ( $values as $value_key => $value ) { |
| 270 | 142 | if ( $value_key && in_array( $value_key, $form_fields ) ) { |
| 271 | 143 | $new_values[ $value_key ] = $value; |
| @@ -271,15 +143,14 @@ | ||
| 271 | 143 | $new_values[ $value_key ] = $value; |
| 272 | 144 | } |
| 273 | 145 | } |
| 274 | 146 | |
| 275 | - if ( ! empty( $values['new_status'] ) ) { | |
| 147 | + if ( isset( $values['new_status'] ) && ! empty( $values['new_status'] ) ) { | |
| 276 | 148 | $new_values['status'] = $values['new_status']; |
| 277 | 149 | } |
| 278 | 150 | |
| 279 | 151 | if ( ! empty( $new_values ) ) { |
| 280 | 152 | $query_results = $wpdb->update( $wpdb->prefix . 'frm_forms', $new_values, array( 'id' => $id ) ); |
| 281 | - | |
| 282 | 153 | if ( $query_results ) { |
| 283 | 154 | self::clear_form_cache(); |
| 284 | 155 | } |
| 285 | 156 | } else { |
| @@ -295,38 +166,24 @@ | ||
| 295 | 166 | return $query_results; |
| 296 | 167 | } |
| 297 | 168 | |
| 298 | 169 | /** |
| 299 | - * @param array $new_values | |
| 300 | - * @param array $values | |
| 301 | - * @param array $args | |
| 302 | - * | |
| 303 | 170 | * @return array |
| 304 | 171 | */ |
| 305 | - public static function set_update_options( $new_values, $values, $args = array() ) { | |
| 172 | + public static function set_update_options( $new_values, $values ) { | |
| 306 | 173 | if ( ! isset( $values['options'] ) ) { |
| 307 | 174 | return $new_values; |
| 308 | 175 | } |
| 309 | 176 | |
| 310 | - $options = ! empty( $values['options'] ) ? (array) $values['options'] : array(); | |
| 177 | + $options = isset( $values['options'] ) ? (array) $values['options'] : array(); | |
| 311 | 178 | FrmFormsHelper::fill_form_options( $options, $values ); |
| 312 | 179 | |
| 313 | - $options['custom_style'] = $values['options']['custom_style'] ?? 0; | |
| 314 | - $options['before_html'] = $values['options']['before_html'] ?? FrmFormsHelper::get_default_html( 'before' ); | |
| 315 | - $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' ); | |
| 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' ); | |
| 317 | 184 | |
| 318 | - /** | |
| 319 | - * Allows modifying form options before updating or creating. | |
| 320 | - * | |
| 321 | - * @since 5.4 Added the third param. | |
| 322 | - * | |
| 323 | - * @param array $options Form options. | |
| 324 | - * @param array $values Form data. | |
| 325 | - * @param bool $update Is form updating or creating. It's `true` if is updating. | |
| 326 | - */ | |
| 327 | - $options = apply_filters( 'frm_form_options_before_update', $options, $values, true ); | |
| 328 | - $options = self::maybe_filter_form_options( $options ); | |
| 185 | + $options = apply_filters( 'frm_form_options_before_update', $options, $values ); | |
| 329 | 186 | $new_values['options'] = serialize( $options ); |
| 330 | 187 | |
| 331 | 188 | return $new_values; |
| 332 | 189 | } |
| @@ -331,11 +188,8 @@ | ||
| 331 | 188 | return $new_values; |
| 332 | 189 | } |
| 333 | 190 | |
| 334 | 191 | /** |
| 335 | - * @param int $id Form ID. | |
| 336 | - * @param array $values Form values array. | |
| 337 | - * | |
| 338 | 192 | * @return array |
| 339 | 193 | */ |
| 340 | 194 | public static function update_fields( $id, $values ) { |
| 341 | 195 | |
| @@ -343,9 +197,8 @@ | ||
| 343 | 197 | return $values; |
| 344 | 198 | } |
| 345 | 199 | |
| 346 | 200 | $all_fields = FrmField::get_all_for_form( $id ); |
| 347 | - | |
| 348 | 201 | if ( empty( $all_fields ) ) { |
| 349 | 202 | return $values; |
| 350 | 203 | } |
| 351 | 204 | |
| @@ -354,9 +207,8 @@ | ||
| 354 | 207 | } |
| 355 | 208 | |
| 356 | 209 | $field_array = array(); |
| 357 | 210 | $existing_keys = array_keys( $values['item_meta'] ); |
| 358 | - | |
| 359 | 211 | foreach ( $all_fields as $fid ) { |
| 360 | 212 | if ( ! in_array( $fid->id, $existing_keys ) && ( isset( $values['frm_fields_submitted'] ) && in_array( $fid->id, $values['frm_fields_submitted'] ) ) || isset( $values['options'] ) ) { |
| 361 | 213 | $values['item_meta'][ $fid->id ] = ''; |
| 362 | 214 | } |
| @@ -375,9 +227,8 @@ | ||
| 375 | 227 | continue; |
| 376 | 228 | } |
| 377 | 229 | |
| 378 | 230 | $is_settings_page = ( isset( $values['options'] ) || isset( $values['field_options'][ 'custom_html_' . $field_id ] ) ); |
| 379 | - | |
| 380 | 231 | if ( $is_settings_page ) { |
| 381 | 232 | self::get_settings_page_html( $values, $field ); |
| 382 | 233 | |
| 383 | 234 | if ( ! defined( 'WP_IMPORTING' ) ) { |
| @@ -384,20 +235,15 @@ | ||
| 384 | 235 | continue; |
| 385 | 236 | } |
| 386 | 237 | } |
| 387 | 238 | |
| 388 | - // Updating the form. | |
| 239 | + //updating the form | |
| 389 | 240 | $update_options = FrmFieldsHelper::get_default_field_options_from_field( $field ); |
| 390 | - // Don't check for POST html. | |
| 391 | - unset( $update_options['custom_html'] ); | |
| 241 | + unset( $update_options['custom_html'] ); // don't check for POST html | |
| 392 | 242 | $update_options = apply_filters( 'frm_field_options_to_update', $update_options ); |
| 393 | 243 | |
| 394 | - if ( ! is_array( $field->field_options ) ) { | |
| 395 | - $field->field_options = array(); | |
| 396 | - } | |
| 397 | - | |
| 398 | 244 | foreach ( $update_options as $opt => $default ) { |
| 399 | - $field->field_options[ $opt ] = $values['field_options'][ $opt . '_' . $field_id ] ?? $default; | |
| 245 | + $field->field_options[ $opt ] = isset( $values['field_options'][ $opt . '_' . $field_id ] ) ? $values['field_options'][ $opt . '_' . $field_id ] : $default; | |
| 400 | 246 | self::sanitize_field_opt( $opt, $field->field_options[ $opt ] ); |
| 401 | 247 | } |
| 402 | 248 | |
| 403 | 249 | $field->field_options = apply_filters( 'frm_update_field_options', $field->field_options, $field, $values ); |
| @@ -406,94 +252,32 @@ | ||
| 406 | 252 | 'field_options' => $field->field_options, |
| 407 | 253 | 'default_value' => isset( $values[ 'default_value_' . $field_id ] ) ? FrmAppHelper::maybe_json_encode( $values[ 'default_value_' . $field_id ] ) : '', |
| 408 | 254 | ); |
| 409 | 255 | |
| 410 | - if ( ! FrmAppHelper::allow_unfiltered_html() && isset( $values['field_options'][ 'options_' . $field_id ] ) && is_array( $values['field_options'][ 'options_' . $field_id ] ) ) { | |
| 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 | - } | |
| 416 | - } | |
| 417 | - } | |
| 418 | - } | |
| 419 | - | |
| 420 | 256 | self::prepare_field_update_values( $field, $values, $new_field ); |
| 421 | - self::maybe_update_max_option( $field, $values, $new_field ); | |
| 422 | 257 | |
| 423 | 258 | FrmField::update( $field_id, $new_field ); |
| 424 | 259 | |
| 425 | 260 | FrmField::delete_form_transient( $field->form_id ); |
| 426 | - }//end foreach | |
| 261 | + } | |
| 427 | 262 | self::clear_form_cache(); |
| 428 | 263 | |
| 429 | 264 | return $values; |
| 430 | 265 | } |
| 431 | 266 | |
| 432 | - /** | |
| 433 | - * Resets the 'max' option of a field when changing paragraph field type to other field types like text, email etc. | |
| 434 | - * | |
| 435 | - * @since 6.7 | |
| 436 | - * | |
| 437 | - * @param array $field | |
| 438 | - * @param array $values | |
| 439 | - * @param array $new_field | |
| 440 | - * | |
| 441 | - * @return void | |
| 442 | - */ | |
| 443 | - 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 ) ) { | |
| 447 | - | |
| 448 | - $new_field['field_options']['max'] = ''; | |
| 449 | - | |
| 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 | - } | |
| 456 | - } | |
| 457 | - | |
| 458 | - /** | |
| 459 | - * @param string $opt | |
| 460 | - * @param mixed $value | |
| 461 | - * | |
| 462 | - * @return void | |
| 463 | - */ | |
| 464 | 267 | private static function sanitize_field_opt( $opt, &$value ) { |
| 465 | - if ( ! is_string( $value ) ) { | |
| 466 | - return; | |
| 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 ); | |
| 467 | 275 | } |
| 468 | - | |
| 469 | - /** | |
| 470 | - * Allow the option to turn off sanitization for a field. This way a custom rule can be used instead. | |
| 471 | - * Make sure to add custom sanitization using the frm_update_field_options filter as the data will no longer be sanitized. | |
| 472 | - * | |
| 473 | - * @since 6.0 | |
| 474 | - * | |
| 475 | - * @param bool $should_sanitize | |
| 476 | - * @param string $opt | |
| 477 | - */ | |
| 478 | - $should_sanitize = apply_filters( 'frm_should_sanitize_field_opt_string', true, $opt ); | |
| 479 | - | |
| 480 | - if ( ! $should_sanitize ) { | |
| 481 | - return; | |
| 482 | - } | |
| 483 | - | |
| 484 | - if ( $opt === 'calc' ) { | |
| 485 | - $value = self::sanitize_calc( $value ); | |
| 486 | - } else { | |
| 487 | - $value = FrmAppHelper::kses( $value, 'all' ); | |
| 488 | - } | |
| 489 | - | |
| 490 | - $value = trim( $value ); | |
| 491 | 276 | } |
| 492 | 277 | |
| 493 | 278 | /** |
| 494 | 279 | * @param string $value |
| 495 | - * | |
| 496 | 280 | * @return string |
| 497 | 281 | */ |
| 498 | 282 | private static function sanitize_calc( $value ) { |
| 499 | 283 | if ( false !== strpos( $value, '<' ) ) { |
| @@ -498,10 +282,9 @@ | ||
| 498 | 282 | private static function sanitize_calc( $value ) { |
| 499 | 283 | if ( false !== strpos( $value, '<' ) ) { |
| 500 | 284 | $value = self::normalize_calc_spaces( $value ); |
| 501 | 285 | } |
| 502 | - // Allow <= and >=. | |
| 503 | - $allow = array( '<= ', ' >=' ); | |
| 286 | + $allow = array( '<= ', ' >=' ); // Allow <= and >= | |
| 504 | 287 | $temp = array( '< = ', ' > =' ); |
| 505 | 288 | $value = str_replace( $allow, $temp, $value ); |
| 506 | 289 | $value = strip_tags( $value ); |
| 507 | 290 | $value = str_replace( $temp, $allow, $value ); |
| @@ -512,44 +295,35 @@ | ||
| 512 | 295 | * Format a comparison like 5<10 to 5 < 10. Also works on 5< 10, 5 <10, 5<=10 variations. |
| 513 | 296 | * This is to avoid an issue with unspaced calculations being recognized as HTML that gets removed when strip_tags is called. |
| 514 | 297 | * |
| 515 | 298 | * @param string $calc |
| 516 | - * | |
| 517 | 299 | * @return string |
| 518 | 300 | */ |
| 519 | 301 | private static function normalize_calc_spaces( $calc ) { |
| 520 | 302 | // Check for a pattern with 5 parts |
| 521 | - // $1 \d|\] the first comparison digit or the end of a comparison shortcode. | |
| 303 | + // $1 \d the first comparison digit. | |
| 522 | 304 | // $2 a space (optional). |
| 523 | 305 | // $3 an equals sign (optional) that follows the < operator for <= comparisons. |
| 524 | 306 | // $4 another space (optional). |
| 525 | - // $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; | |
| 307 | + // $5 \d the second comparison digit. | |
| 308 | + return preg_replace( '/(\d)( ){0,1}<(=){0,1}( ){0,1}(\d)/', '$1 <$3 $5', $calc ); | |
| 529 | 309 | } |
| 530 | 310 | |
| 531 | 311 | /** |
| 532 | 312 | * Updating the settings page |
| 533 | - * | |
| 534 | - * @param array $values Form values array. | |
| 535 | - * @param object $field Field object, passed by reference. | |
| 536 | - * | |
| 537 | - * @return void | |
| 538 | 313 | */ |
| 539 | 314 | private static function get_settings_page_html( $values, &$field ) { |
| 540 | 315 | if ( isset( $values['field_options'][ 'custom_html_' . $field->id ] ) ) { |
| 541 | 316 | $prev_opts = array(); |
| 542 | - $fallback_html = $field->field_options['custom_html'] ?? FrmFieldsHelper::get_default_html( $field->type ); | |
| 317 | + $fallback_html = isset( $field->field_options['custom_html'] ) ? $field->field_options['custom_html'] : FrmFieldsHelper::get_default_html( $field->type ); | |
| 543 | 318 | |
| 544 | - $field->field_options['custom_html'] = $values['field_options'][ 'custom_html_' . $field->id ] ?? $fallback_html; | |
| 545 | - } elseif ( $field->type === 'hidden' || $field->type === 'user_id' ) { | |
| 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' ) { | |
| 546 | 321 | $prev_opts = $field->field_options; |
| 547 | 322 | } |
| 548 | 323 | |
| 549 | 324 | if ( isset( $prev_opts ) ) { |
| 550 | 325 | $field->field_options = apply_filters( 'frm_update_form_field_options', $field->field_options, $field, $values ); |
| 551 | - | |
| 552 | 326 | if ( $prev_opts != $field->field_options ) { |
| 553 | 327 | FrmField::update( $field->id, array( 'field_options' => $field->field_options ) ); |
| 554 | 328 | } |
| 555 | 329 | } |
| @@ -554,15 +328,8 @@ | ||
| 554 | 328 | } |
| 555 | 329 | } |
| 556 | 330 | } |
| 557 | 331 | |
| 558 | - /** | |
| 559 | - * @param object $field | |
| 560 | - * @param array $values | |
| 561 | - * @param array $new_field | |
| 562 | - * | |
| 563 | - * @return void | |
| 564 | - */ | |
| 565 | 332 | private static function prepare_field_update_values( $field, $values, &$new_field ) { |
| 566 | 333 | $field_cols = array( |
| 567 | 334 | 'field_order' => 0, |
| 568 | 335 | 'field_key' => '', |
| @@ -571,16 +338,12 @@ | ||
| 571 | 338 | 'description' => '', |
| 572 | 339 | 'options' => '', |
| 573 | 340 | 'name' => '', |
| 574 | 341 | ); |
| 575 | - | |
| 576 | 342 | foreach ( $field_cols as $col => $default ) { |
| 577 | - $default = $default === '' ? $field->{$col} : $default; | |
| 578 | - $new_field[ $col ] = $values['field_options'][ $col . '_' . $field->id ] ?? $default; | |
| 579 | - } | |
| 343 | + $default = ( $default === '' ) ? $field->{$col} : $default; | |
| 580 | 344 | |
| 581 | - if ( $field->type === 'submit' && isset( $new_field['field_order'] ) && (int) $new_field['field_order'] === FrmSubmitHelper::DEFAULT_ORDER ) { | |
| 582 | - $new_field['field_order'] = $field->field_order; | |
| 345 | + $new_field[ $col ] = isset( $values['field_options'][ $col . '_' . $field->id ] ) ? $values['field_options'][ $col . '_' . $field->id ] : $default; | |
| 583 | 346 | } |
| 584 | 347 | |
| 585 | 348 | // Don't save the template option. |
| 586 | 349 | if ( is_array( $new_field['options'] ) && isset( $new_field['options']['000'] ) ) { |
| @@ -592,12 +355,9 @@ | ||
| 592 | 355 | * Get a list of all form settings that should be translated |
| 593 | 356 | * on a multilingual site. |
| 594 | 357 | * |
| 595 | 358 | * @since 3.06.01 |
| 596 | - * | |
| 597 | - * @param object $form The form object. | |
| 598 | - * | |
| 599 | - * @return array | |
| 359 | + * @param object $form - The form object | |
| 600 | 360 | */ |
| 601 | 361 | public static function translatable_strings( $form ) { |
| 602 | 362 | $strings = array( |
| 603 | 363 | 'name', |
| @@ -604,12 +364,8 @@ | ||
| 604 | 364 | 'description', |
| 605 | 365 | 'submit_value', |
| 606 | 366 | 'submit_msg', |
| 607 | 367 | 'success_msg', |
| 608 | - 'invalid_msg', | |
| 609 | - 'failed_msg', | |
| 610 | - 'login_msg', | |
| 611 | - 'admin_permission', | |
| 612 | 368 | ); |
| 613 | 369 | |
| 614 | 370 | return apply_filters( 'frm_form_strings', $strings, $form ); |
| 615 | 371 | } |
| @@ -614,12 +370,11 @@ | ||
| 614 | 370 | return apply_filters( 'frm_form_strings', $strings, $form ); |
| 615 | 371 | } |
| 616 | 372 | |
| 617 | 373 | /** |
| 618 | - * @param int $id | |
| 619 | 374 | * @param string $status |
| 620 | 375 | * |
| 621 | - * @return bool|int | |
| 376 | + * @return int|boolean | |
| 622 | 377 | */ |
| 623 | 378 | public static function set_status( $id, $status ) { |
| 624 | 379 | if ( 'trash' == $status ) { |
| 625 | 380 | return self::trash( $id ); |
| @@ -625,10 +380,9 @@ | ||
| 625 | 380 | return self::trash( $id ); |
| 626 | 381 | } |
| 627 | 382 | |
| 628 | 383 | $statuses = array( 'published', 'draft', 'trash' ); |
| 629 | - | |
| 630 | - if ( ! in_array( $status, $statuses, true ) ) { | |
| 384 | + if ( ! in_array( $status, $statuses ) ) { | |
| 631 | 385 | return false; |
| 632 | 386 | } |
| 633 | 387 | |
| 634 | 388 | global $wpdb; |
| @@ -641,9 +395,9 @@ | ||
| 641 | 395 | ); |
| 642 | 396 | FrmDb::get_where_clause_and_values( $where ); |
| 643 | 397 | array_unshift( $where['values'], $status ); |
| 644 | 398 | |
| 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 | |
| 399 | + $query_results = $wpdb->query( $wpdb->prepare( 'UPDATE ' . $wpdb->prefix . 'frm_forms SET status = %s ' . $where['where'], $where['values'] ) ); // WPCS: unprepared SQL ok. | |
| 646 | 400 | } else { |
| 647 | 401 | $query_results = $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'status' => $status ), array( 'id' => $id ) ); |
| 648 | 402 | $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'status' => $status ), array( 'parent_form_id' => $id ) ); |
| 649 | 403 | } |
| @@ -655,11 +409,9 @@ | ||
| 655 | 409 | return $query_results; |
| 656 | 410 | } |
| 657 | 411 | |
| 658 | 412 | /** |
| 659 | - * @param int|string $id Form ID. | |
| 660 | - * | |
| 661 | - * @return bool|int | |
| 413 | + * @return int|boolean | |
| 662 | 414 | */ |
| 663 | 415 | public static function trash( $id ) { |
| 664 | 416 | if ( ! EMPTY_TRASH_DAYS ) { |
| 665 | 417 | return self::destroy( $id ); |
| @@ -665,17 +417,12 @@ | ||
| 665 | 417 | return self::destroy( $id ); |
| 666 | 418 | } |
| 667 | 419 | |
| 668 | 420 | $form = self::getOne( $id ); |
| 669 | - | |
| 670 | 421 | if ( ! $form ) { |
| 671 | 422 | return false; |
| 672 | 423 | } |
| 673 | 424 | |
| 674 | - if ( $form->status === 'trash' ) { | |
| 675 | - return false; | |
| 676 | - } | |
| 677 | - | |
| 678 | 425 | $options = $form->options; |
| 679 | 426 | $options['trash_time'] = time(); |
| 680 | 427 | |
| 681 | 428 | global $wpdb; |
| @@ -708,26 +455,21 @@ | ||
| 708 | 455 | return $query_results; |
| 709 | 456 | } |
| 710 | 457 | |
| 711 | 458 | /** |
| 712 | - * @param int|string $id Form ID. | |
| 713 | - * | |
| 714 | - * @return bool|int | |
| 459 | + * @return int|boolean | |
| 715 | 460 | */ |
| 716 | 461 | public static function destroy( $id ) { |
| 717 | 462 | global $wpdb; |
| 718 | 463 | |
| 719 | 464 | $form = self::getOne( $id ); |
| 720 | - | |
| 721 | 465 | if ( ! $form ) { |
| 722 | 466 | return false; |
| 723 | 467 | } |
| 724 | - | |
| 725 | 468 | $id = $form->id; |
| 726 | 469 | |
| 727 | 470 | // Disconnect the entries from this form |
| 728 | 471 | $entries = FrmDb::get_col( $wpdb->prefix . 'frm_items', array( 'form_id' => $id ) ); |
| 729 | - | |
| 730 | 472 | foreach ( $entries as $entry_id ) { |
| 731 | 473 | FrmEntry::destroy( $entry_id ); |
| 732 | 474 | unset( $entry_id ); |
| 733 | 475 | } |
| @@ -735,14 +477,10 @@ | ||
| 735 | 477 | // Disconnect the fields from this form |
| 736 | 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 ) ); |
| 737 | 479 | |
| 738 | 480 | $query_results = $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_forms WHERE id=%d OR parent_form_id=%d', $id, $id ) ); |
| 739 | - | |
| 740 | 481 | if ( $query_results ) { |
| 741 | 482 | // Delete all form actions linked to this form |
| 742 | - /** | |
| 743 | - * @var FrmFormAction | |
| 744 | - */ | |
| 745 | 483 | $action_control = FrmFormActionsController::get_form_actions( 'email' ); |
| 746 | 484 | $action_control->destroy( $id, 'all' ); |
| 747 | 485 | |
| 748 | 486 | // Clear form caching |
| @@ -757,19 +495,17 @@ | ||
| 757 | 495 | |
| 758 | 496 | /** |
| 759 | 497 | * Delete trashed forms based on how long they have been trashed |
| 760 | 498 | * |
| 761 | - * @param int|string $delete_timestamp Timestamp cutoff for deletion. | |
| 762 | - * | |
| 763 | 499 | * @return int The number of forms deleted |
| 764 | 500 | */ |
| 765 | 501 | public static function scheduled_delete( $delete_timestamp = '' ) { |
| 766 | 502 | global $wpdb; |
| 767 | 503 | |
| 768 | - $trash_forms = FrmDb::get_results( $wpdb->prefix . 'frm_forms', array( 'status' => 'trash' ), 'id, parent_form_id, options' ); | |
| 504 | + $trash_forms = FrmDb::get_results( $wpdb->prefix . 'frm_forms', array( 'status' => 'trash' ), 'id, options' ); | |
| 769 | 505 | |
| 770 | 506 | if ( ! $trash_forms ) { |
| 771 | - return 0; | |
| 507 | + return; | |
| 772 | 508 | } |
| 773 | 509 | |
| 774 | 510 | if ( empty( $delete_timestamp ) ) { |
| 775 | 511 | $delete_timestamp = time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS ); |
| @@ -775,18 +511,13 @@ | ||
| 775 | 511 | $delete_timestamp = time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS ); |
| 776 | 512 | } |
| 777 | 513 | |
| 778 | 514 | $count = 0; |
| 779 | - | |
| 780 | 515 | foreach ( $trash_forms as $form ) { |
| 781 | 516 | FrmAppHelper::unserialize_or_decode( $form->options ); |
| 782 | - | |
| 783 | 517 | if ( ! isset( $form->options['trash_time'] ) || $form->options['trash_time'] < $delete_timestamp ) { |
| 784 | 518 | self::destroy( $form->id ); |
| 785 | - | |
| 786 | - if ( empty( $form->parent_form_id ) ) { | |
| 787 | - ++$count; | |
| 788 | - } | |
| 519 | + $count ++; | |
| 789 | 520 | } |
| 790 | 521 | |
| 791 | 522 | unset( $form ); |
| 792 | 523 | } |
| @@ -794,15 +525,12 @@ | ||
| 794 | 525 | return $count; |
| 795 | 526 | } |
| 796 | 527 | |
| 797 | 528 | /** |
| 798 | - * @param int|string $id Form ID or key. | |
| 799 | - * | |
| 800 | 529 | * @return string form name |
| 801 | 530 | */ |
| 802 | 531 | public static function getName( $id ) { |
| 803 | 532 | $form = FrmDb::check_cache( $id, 'frm_form' ); |
| 804 | - | |
| 805 | 533 | if ( $form ) { |
| 806 | 534 | $r = stripslashes( $form->name ); |
| 807 | 535 | |
| 808 | 536 | return $r; |
| @@ -809,12 +537,10 @@ | ||
| 809 | 537 | } |
| 810 | 538 | |
| 811 | 539 | $query_key = is_numeric( $id ) ? 'id' : 'form_key'; |
| 812 | 540 | $r = FrmDb::get_var( 'frm_forms', array( $query_key => $id ), 'name' ); |
| 541 | + $r = stripslashes( $r ); | |
| 813 | 542 | |
| 814 | - // An empty form name can result in a null value. | |
| 815 | - $r = is_null( $r ) ? '' : stripslashes( $r ); | |
| 816 | - | |
| 817 | 543 | return $r; |
| 818 | 544 | } |
| 819 | 545 | |
| 820 | 546 | /** |
| @@ -830,9 +556,9 @@ | ||
| 830 | 556 | |
| 831 | 557 | /** |
| 832 | 558 | * @since 3.0 |
| 833 | 559 | * |
| 834 | - * @param int|string $id | |
| 560 | + * @param int $id | |
| 835 | 561 | * |
| 836 | 562 | * @return string form key |
| 837 | 563 | */ |
| 838 | 564 | public static function get_key_by_id( $id ) { |
| @@ -837,9 +563,8 @@ | ||
| 837 | 563 | */ |
| 838 | 564 | public static function get_key_by_id( $id ) { |
| 839 | 565 | $id = (int) $id; |
| 840 | 566 | $cache = FrmDb::check_cache( $id, 'frm_form' ); |
| 841 | - | |
| 842 | 567 | if ( $cache ) { |
| 843 | 568 | return $cache->form_key; |
| 844 | 569 | } |
| 845 | 570 | |
| @@ -850,13 +575,11 @@ | ||
| 850 | 575 | |
| 851 | 576 | /** |
| 852 | 577 | * If $form is numeric, get the form object |
| 853 | 578 | * |
| 579 | + * @param object|int $form | |
| 580 | + * | |
| 854 | 581 | * @since 2.0.9 |
| 855 | - * | |
| 856 | - * @param int|object $form | |
| 857 | - * | |
| 858 | - * @return void | |
| 859 | 582 | */ |
| 860 | 583 | public static function maybe_get_form( &$form ) { |
| 861 | 584 | if ( ! is_object( $form ) && ! is_array( $form ) && ! empty( $form ) ) { |
| 862 | 585 | $form = self::getOne( $form ); |
| @@ -863,28 +586,27 @@ | ||
| 863 | 586 | } |
| 864 | 587 | } |
| 865 | 588 | |
| 866 | 589 | /** |
| 867 | - * @param int|string $id | |
| 868 | - * @param false|int $blog_id | |
| 869 | - * | |
| 870 | - * @return stdClass|null | |
| 590 | + * @return object form | |
| 871 | 591 | */ |
| 872 | 592 | public static function getOne( $id, $blog_id = false ) { |
| 873 | 593 | global $wpdb; |
| 874 | 594 | |
| 875 | 595 | if ( $blog_id && is_multisite() ) { |
| 876 | - $prefix = $wpdb->get_blog_prefix( $blog_id ); | |
| 596 | + global $wpmuBaseTablePrefix; | |
| 597 | + $prefix = $wpmuBaseTablePrefix ? $wpmuBaseTablePrefix . $blog_id . '_' : $wpdb->get_blog_prefix( $blog_id ); | |
| 598 | + | |
| 877 | 599 | $table_name = $prefix . 'frm_forms'; |
| 878 | 600 | } else { |
| 879 | 601 | $table_name = $wpdb->prefix . 'frm_forms'; |
| 880 | 602 | $cache = wp_cache_get( $id, 'frm_form' ); |
| 881 | - | |
| 882 | 603 | if ( $cache ) { |
| 883 | 604 | if ( isset( $cache->options ) ) { |
| 884 | 605 | FrmAppHelper::unserialize_or_decode( $cache->options ); |
| 885 | 606 | } |
| 886 | - return self::prepare_form_row_data( $cache ); | |
| 607 | + | |
| 608 | + return wp_unslash( $cache ); | |
| 887 | 609 | } |
| 888 | 610 | } |
| 889 | 611 | |
| 890 | 612 | if ( is_numeric( $id ) ) { |
| @@ -899,49 +621,17 @@ | ||
| 899 | 621 | FrmDb::set_cache( $results->id, $results, 'frm_form' ); |
| 900 | 622 | FrmAppHelper::unserialize_or_decode( $results->options ); |
| 901 | 623 | } |
| 902 | 624 | |
| 903 | - return self::prepare_form_row_data( $results ); | |
| 625 | + return apply_filters( 'frm_form_object', wp_unslash( $results ) ); | |
| 904 | 626 | } |
| 905 | 627 | |
| 906 | 628 | /** |
| 907 | - * Make sure that if $row is an object, that $row->options is an array and not a string. | |
| 908 | - * | |
| 909 | - * @since 6.8.3 | |
| 910 | - * | |
| 911 | - * @param stdClass|null $row The database row for a target form. | |
| 912 | - * | |
| 913 | - * @return stdClass|null | |
| 629 | + * @return object|array of objects | |
| 914 | 630 | */ |
| 915 | - private static function prepare_form_row_data( $row ) { | |
| 916 | - $row = wp_unslash( $row ); | |
| 917 | - | |
| 918 | - if ( ! is_object( $row ) ) { | |
| 919 | - return $row; | |
| 920 | - } | |
| 921 | - | |
| 922 | - if ( ! is_array( $row->options ) ) { | |
| 923 | - $row->options = FrmFormsHelper::get_default_opts(); | |
| 924 | - } | |
| 925 | - | |
| 926 | - /** | |
| 927 | - * @since 4.03.02 | |
| 928 | - * | |
| 929 | - * @param stdClass $row | |
| 930 | - */ | |
| 931 | - return apply_filters( 'frm_form_object', $row ); | |
| 932 | - } | |
| 933 | - | |
| 934 | - /** | |
| 935 | - * @param array|string $where Where conditions array or raw WHERE string. | |
| 936 | - * @param string $order_by Order by clause. | |
| 937 | - * @param int|string $limit Limit clause or number. | |
| 938 | - * | |
| 939 | - * @return array|object of objects | |
| 940 | - */ | |
| 941 | 631 | public static function getAll( $where = array(), $order_by = '', $limit = '' ) { |
| 942 | 632 | if ( is_array( $where ) && ! empty( $where ) ) { |
| 943 | - if ( ! empty( $where['is_template'] ) && ! isset( $where['status'] ) && ! isset( $where['status !'] ) ) { | |
| 633 | + if ( isset( $where['is_template'] ) && $where['is_template'] && ! isset( $where['status'] ) && ! isset( $where['status !'] ) ) { | |
| 944 | 634 | // don't get trashed templates |
| 945 | 635 | $where['status'] = array( null, '', 'published' ); |
| 946 | 636 | } |
| 947 | 637 | |
| @@ -948,11 +638,11 @@ | ||
| 948 | 638 | $results = FrmDb::get_results( 'frm_forms', $where, '*', compact( 'order_by', 'limit' ) ); |
| 949 | 639 | } else { |
| 950 | 640 | global $wpdb; |
| 951 | 641 | |
| 952 | - // The query has already been prepared if this is not an array. | |
| 642 | + // the query has already been prepared if this is not an array | |
| 953 | 643 | $query = 'SELECT * FROM ' . $wpdb->prefix . 'frm_forms' . FrmDb::prepend_and_or_where( ' WHERE ', $where ) . FrmDb::esc_order( $order_by ) . FrmDb::esc_limit( $limit ); |
| 954 | - $results = $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 644 | + $results = $wpdb->get_results( $query ); // WPCS: unprepared SQL ok. | |
| 955 | 645 | } |
| 956 | 646 | |
| 957 | 647 | if ( $results ) { |
| 958 | 648 | foreach ( $results as $result ) { |
| @@ -960,9 +650,9 @@ | ||
| 960 | 650 | FrmAppHelper::unserialize_or_decode( $result->options ); |
| 961 | 651 | } |
| 962 | 652 | } |
| 963 | 653 | |
| 964 | - if ( $limit === ' LIMIT 1' || $limit == 1 ) { | |
| 654 | + if ( $limit == ' LIMIT 1' || $limit == 1 ) { | |
| 965 | 655 | // return the first form object if we are only getting one form |
| 966 | 656 | $results = reset( $results ); |
| 967 | 657 | } |
| 968 | 658 | |
| @@ -972,20 +662,14 @@ | ||
| 972 | 662 | /** |
| 973 | 663 | * Get all published forms |
| 974 | 664 | * |
| 975 | 665 | * @since 2.0 |
| 976 | - * | |
| 977 | - * @param array $query | |
| 978 | - * @param int $limit | |
| 979 | - * @param string $inc_children | |
| 980 | - * | |
| 981 | - * @return array|object of forms A single form object would be passed if $limit was set to 1. | |
| 666 | + * @return array of forms | |
| 982 | 667 | */ |
| 983 | 668 | public static function get_published_forms( $query = array(), $limit = 999, $inc_children = 'exclude' ) { |
| 984 | 669 | $query['is_template'] = 0; |
| 985 | 670 | $query['status'] = array( null, '', 'published' ); |
| 986 | - | |
| 987 | - if ( $inc_children === 'exclude' ) { | |
| 671 | + if ( $inc_children == 'exclude' ) { | |
| 988 | 672 | $query['parent_form_id'] = array( null, 0 ); |
| 989 | 673 | } |
| 990 | 674 | |
| 991 | 675 | $forms = self::getAll( $query, 'name', $limit ); |
| @@ -1001,14 +685,13 @@ | ||
| 1001 | 685 | |
| 1002 | 686 | $cache_key = 'frm_form_counts'; |
| 1003 | 687 | |
| 1004 | 688 | $counts = wp_cache_get( $cache_key, 'frm_form' ); |
| 1005 | - | |
| 1006 | 689 | if ( false !== $counts ) { |
| 1007 | 690 | return $counts; |
| 1008 | 691 | } |
| 1009 | 692 | |
| 1010 | - $results = FrmDb::get_results( | |
| 693 | + $results = (array) FrmDb::get_results( | |
| 1011 | 694 | 'frm_forms', |
| 1012 | 695 | array( |
| 1013 | 696 | 'or' => 1, |
| 1014 | 697 | 'parent_form_id' => null, |
| @@ -1022,18 +705,18 @@ | ||
| 1022 | 705 | |
| 1023 | 706 | foreach ( $results as $row ) { |
| 1024 | 707 | if ( 'trash' != $row->status ) { |
| 1025 | 708 | if ( $row->is_template ) { |
| 1026 | - ++$counts['template']; | |
| 709 | + $counts['template'] ++; | |
| 1027 | 710 | } else { |
| 1028 | - ++$counts['published']; | |
| 711 | + $counts['published'] ++; | |
| 1029 | 712 | } |
| 1030 | 713 | } else { |
| 1031 | - ++$counts['trash']; | |
| 714 | + $counts['trash'] ++; | |
| 1032 | 715 | } |
| 1033 | 716 | |
| 1034 | 717 | if ( 'draft' == $row->status ) { |
| 1035 | - ++$counts['draft']; | |
| 718 | + $counts['draft'] ++; | |
| 1036 | 719 | } |
| 1037 | 720 | |
| 1038 | 721 | unset( $row ); |
| 1039 | 722 | } |
| @@ -1049,10 +732,8 @@ | ||
| 1049 | 732 | * Called when a form is created, updated, duplicated, or deleted |
| 1050 | 733 | * or when the form status is changed |
| 1051 | 734 | * |
| 1052 | 735 | * @since 2.0.4 |
| 1053 | - * | |
| 1054 | - * @return void | |
| 1055 | 736 | */ |
| 1056 | 737 | public static function clear_form_cache() { |
| 1057 | 738 | FrmDb::cache_delete_group( 'frm_form' ); |
| 1058 | 739 | } |
| @@ -1057,22 +738,16 @@ | ||
| 1057 | 738 | FrmDb::cache_delete_group( 'frm_form' ); |
| 1058 | 739 | } |
| 1059 | 740 | |
| 1060 | 741 | /** |
| 1061 | - * @param array $values Form values to validate. | |
| 1062 | - * | |
| 1063 | 742 | * @return array of errors |
| 1064 | 743 | */ |
| 1065 | 744 | public static function validate( $values ) { |
| 1066 | 745 | $errors = array(); |
| 746 | + | |
| 1067 | 747 | return apply_filters( 'frm_validate_form', $errors, $values ); |
| 1068 | 748 | } |
| 1069 | 749 | |
| 1070 | - /** | |
| 1071 | - * @param object|null $form | |
| 1072 | - * | |
| 1073 | - * @return array | |
| 1074 | - */ | |
| 1075 | 750 | public static function get_params( $form = null ) { |
| 1076 | 751 | global $frm_vars; |
| 1077 | 752 | |
| 1078 | 753 | if ( ! $form ) { |
| @@ -1084,9 +759,9 @@ | ||
| 1084 | 759 | if ( isset( $frm_vars['form_params'] ) && is_array( $frm_vars['form_params'] ) && isset( $frm_vars['form_params'][ $form->id ] ) ) { |
| 1085 | 760 | return $frm_vars['form_params'][ $form->id ]; |
| 1086 | 761 | } |
| 1087 | 762 | |
| 1088 | - $action_var = isset( $_REQUEST['frm_action'] ) ? 'frm_action' : 'action'; // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 763 | + $action_var = isset( $_REQUEST['frm_action'] ) ? 'frm_action' : 'action'; // WPCS: CSRF ok. | |
| 1089 | 764 | $action = apply_filters( 'frm_show_new_entry_page', FrmAppHelper::get_param( $action_var, 'new', 'get', 'sanitize_title' ), $form ); |
| 1090 | 765 | |
| 1091 | 766 | $default_values = array( |
| 1092 | 767 | 'id' => '', |
| @@ -1102,17 +777,16 @@ | ||
| 1102 | 777 | ); |
| 1103 | 778 | |
| 1104 | 779 | $values = array(); |
| 1105 | 780 | $values['posted_form_id'] = FrmAppHelper::get_param( 'form_id', '', 'get', 'absint' ); |
| 1106 | - | |
| 1107 | 781 | if ( ! $values['posted_form_id'] ) { |
| 1108 | 782 | $values['posted_form_id'] = FrmAppHelper::get_param( 'form', '', 'get', 'absint' ); |
| 1109 | 783 | } |
| 1110 | 784 | |
| 1111 | 785 | if ( $form->id == $values['posted_form_id'] ) { |
| 1112 | - // If there are two forms on the same page, make sure not to submit both. | |
| 786 | + //if there are two forms on the same page, make sure not to submit both | |
| 1113 | 787 | foreach ( $default_values as $var => $default ) { |
| 1114 | - if ( $var === 'action' ) { | |
| 788 | + if ( $var == 'action' ) { | |
| 1115 | 789 | $values[ $var ] = FrmAppHelper::get_param( $action_var, $default, 'get', 'sanitize_title' ); |
| 1116 | 790 | } else { |
| 1117 | 791 | $values[ $var ] = FrmAppHelper::get_param( $var, $default, 'get', 'sanitize_text_field' ); |
| 1118 | 792 | } |
| @@ -1125,9 +799,9 @@ | ||
| 1125 | 799 | } |
| 1126 | 800 | } |
| 1127 | 801 | |
| 1128 | 802 | if ( in_array( $values['action'], array( 'create', 'update' ) ) && |
| 1129 | - ( ! $_POST || ( ! isset( $_POST['action'] ) && ! isset( $_POST['frm_action'] ) ) ) // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 803 | + ( ! $_POST || ( ! isset( $_POST['action'] ) && ! isset( $_POST['frm_action'] ) ) ) // WPCS: CSRF ok. | |
| 1130 | 804 | ) { |
| 1131 | 805 | $values['action'] = 'new'; |
| 1132 | 806 | } |
| 1133 | 807 | |
| @@ -1133,11 +807,8 @@ | ||
| 1133 | 807 | |
| 1134 | 808 | return $values; |
| 1135 | 809 | } |
| 1136 | 810 | |
| 1137 | - /** | |
| 1138 | - * @return array | |
| 1139 | - */ | |
| 1140 | 811 | public static function list_page_params() { |
| 1141 | 812 | $values = array(); |
| 1142 | 813 | $defaults = array( |
| 1143 | 814 | 'template' => 0, |
| @@ -1147,9 +818,8 @@ | ||
| 1147 | 818 | 'search' => '', |
| 1148 | 819 | 'sort' => '', |
| 1149 | 820 | 'sdir' => '', |
| 1150 | 821 | ); |
| 1151 | - | |
| 1152 | 822 | foreach ( $defaults as $var => $default ) { |
| 1153 | 823 | $values[ $var ] = FrmAppHelper::get_param( $var, $default, 'get', 'sanitize_text_field' ); |
| 1154 | 824 | } |
| 1155 | 825 | |
| @@ -1155,16 +825,10 @@ | ||
| 1155 | 825 | |
| 1156 | 826 | return $values; |
| 1157 | 827 | } |
| 1158 | 828 | |
| 1159 | - /** | |
| 1160 | - * @param int|object|string|null $form | |
| 1161 | - * | |
| 1162 | - * @return array | |
| 1163 | - */ | |
| 1164 | 829 | public static function get_admin_params( $form = null ) { |
| 1165 | 830 | $form_id = $form; |
| 1166 | - | |
| 1167 | 831 | if ( $form === null ) { |
| 1168 | 832 | $form_id = self::get_current_form_id(); |
| 1169 | 833 | } elseif ( $form && is_object( $form ) ) { |
| 1170 | 834 | $form_id = $form->id; |
| @@ -1182,9 +846,8 @@ | ||
| 1182 | 846 | 'sdir' => '', |
| 1183 | 847 | 'fid' => '', |
| 1184 | 848 | 'keep_post' => '', |
| 1185 | 849 | ); |
| 1186 | - | |
| 1187 | 850 | foreach ( $defaults as $var => $default ) { |
| 1188 | 851 | $values[ $var ] = FrmAppHelper::get_param( $var, $default, 'get', 'sanitize_text_field' ); |
| 1189 | 852 | } |
| 1190 | 853 | |
| @@ -1190,39 +853,27 @@ | ||
| 1190 | 853 | |
| 1191 | 854 | return $values; |
| 1192 | 855 | } |
| 1193 | 856 | |
| 1194 | - /** | |
| 1195 | - * @param string $default_form | |
| 1196 | - * | |
| 1197 | - * @return int|string | |
| 1198 | - */ | |
| 1199 | 857 | public static function get_current_form_id( $default_form = 'none' ) { |
| 1200 | - if ( 'first' === $default_form ) { | |
| 858 | + if ( 'first' == $default_form ) { | |
| 1201 | 859 | $form = self::get_current_form(); |
| 1202 | 860 | } else { |
| 1203 | 861 | $form = self::maybe_get_current_form(); |
| 1204 | 862 | } |
| 1205 | - | |
| 1206 | 863 | $form_id = $form ? $form->id : 0; |
| 1207 | 864 | |
| 1208 | 865 | return $form_id; |
| 1209 | 866 | } |
| 1210 | 867 | |
| 1211 | - /** | |
| 1212 | - * @param int|string $form_id | |
| 1213 | - * | |
| 1214 | - * @return false|int|object|string | |
| 1215 | - */ | |
| 1216 | 868 | public static function maybe_get_current_form( $form_id = 0 ) { |
| 1217 | 869 | global $frm_vars; |
| 1218 | 870 | |
| 1219 | - if ( ! empty( $frm_vars['current_form'] ) && ( ! $form_id || $form_id == $frm_vars['current_form']->id ) ) { | |
| 871 | + if ( isset( $frm_vars['current_form'] ) && $frm_vars['current_form'] && ( ! $form_id || $form_id == $frm_vars['current_form']->id ) ) { | |
| 1220 | 872 | return $frm_vars['current_form']; |
| 1221 | 873 | } |
| 1222 | 874 | |
| 1223 | 875 | $form_id = FrmAppHelper::get_param( 'form', $form_id, 'get', 'absint' ); |
| 1224 | - | |
| 1225 | 876 | if ( $form_id ) { |
| 1226 | 877 | $form_id = self::set_current_form( $form_id ); |
| 1227 | 878 | } |
| 1228 | 879 | |
| @@ -1228,16 +879,10 @@ | ||
| 1228 | 879 | |
| 1229 | 880 | return $form_id; |
| 1230 | 881 | } |
| 1231 | 882 | |
| 1232 | - /** | |
| 1233 | - * @param int $form_id | |
| 1234 | - * | |
| 1235 | - * @return false|object | |
| 1236 | - */ | |
| 1237 | 883 | public static function get_current_form( $form_id = 0 ) { |
| 1238 | 884 | $form = self::maybe_get_current_form( $form_id ); |
| 1239 | - | |
| 1240 | 885 | if ( is_numeric( $form ) ) { |
| 1241 | 886 | $form = self::set_current_form( $form ); |
| 1242 | 887 | } |
| 1243 | 888 | |
| @@ -1243,18 +888,12 @@ | ||
| 1243 | 888 | |
| 1244 | 889 | return $form; |
| 1245 | 890 | } |
| 1246 | 891 | |
| 1247 | - /** | |
| 1248 | - * @param int $form_id | |
| 1249 | - * | |
| 1250 | - * @return false|object | |
| 1251 | - */ | |
| 1252 | 892 | public static function set_current_form( $form_id ) { |
| 1253 | 893 | global $frm_vars; |
| 1254 | 894 | |
| 1255 | 895 | $query = array(); |
| 1256 | - | |
| 1257 | 896 | if ( $form_id ) { |
| 1258 | 897 | $query['id'] = $form_id; |
| 1259 | 898 | } |
| 1260 | 899 | |
| @@ -1262,19 +901,11 @@ | ||
| 1262 | 901 | |
| 1263 | 902 | return $frm_vars['current_form']; |
| 1264 | 903 | } |
| 1265 | 904 | |
| 1266 | - /** | |
| 1267 | - * @param object $form | |
| 1268 | - * @param int|string $this_load | |
| 1269 | - * @param bool $global_load | |
| 1270 | - * | |
| 1271 | - * @return bool | |
| 1272 | - */ | |
| 1273 | 905 | public static function is_form_loaded( $form, $this_load, $global_load ) { |
| 1274 | 906 | global $frm_vars; |
| 1275 | 907 | $small_form = new stdClass(); |
| 1276 | - | |
| 1277 | 908 | foreach ( array( 'id', 'form_key', 'name' ) as $var ) { |
| 1278 | 909 | $small_form->{$var} = $form->{$var}; |
| 1279 | 910 | unset( $var ); |
| 1280 | 911 | } |
| @@ -1285,9 +916,9 @@ | ||
| 1285 | 916 | $global_load = true; |
| 1286 | 917 | $frm_vars['load_css'] = true; |
| 1287 | 918 | } |
| 1288 | 919 | |
| 1289 | - return empty( $frm_vars['css_loaded'] ) && $global_load; | |
| 920 | + return ( ( ! isset( $frm_vars['css_loaded'] ) || ! $frm_vars['css_loaded'] ) && $global_load ); | |
| 1290 | 921 | } |
| 1291 | 922 | |
| 1292 | 923 | /** |
| 1293 | 924 | * @since 4.06.03 |
| @@ -1302,26 +933,13 @@ | ||
| 1302 | 933 | } else { |
| 1303 | 934 | $visible = true; |
| 1304 | 935 | } |
| 1305 | 936 | |
| 1306 | - /** | |
| 1307 | - * @since 6.25 | |
| 1308 | - * | |
| 1309 | - * @param bool $visible | |
| 1310 | - * @param object $form | |
| 1311 | - */ | |
| 1312 | - $visible = (bool) apply_filters( 'frm_form_is_visible', $visible, $form ); | |
| 1313 | - | |
| 1314 | 937 | return $visible; |
| 1315 | 938 | } |
| 1316 | 939 | |
| 1317 | - /** | |
| 1318 | - * @param object $form | |
| 1319 | - * | |
| 1320 | - * @return bool | |
| 1321 | - */ | |
| 1322 | 940 | public static function show_submit( $form ) { |
| 1323 | - $show = ( ! $form->is_template && $form->status === 'published' && ! FrmAppHelper::is_admin() ); | |
| 941 | + $show = ( ! $form->is_template && $form->status == 'published' && ! FrmAppHelper::is_admin() ); | |
| 1324 | 942 | $show = apply_filters( 'frm_show_submit_button', $show, $form ); |
| 1325 | 943 | |
| 1326 | 944 | return $show; |
| 1327 | 945 | } |
| @@ -1327,18 +945,14 @@ | ||
| 1327 | 945 | } |
| 1328 | 946 | |
| 1329 | 947 | /** |
| 1330 | 948 | * @since 2.3 |
| 1331 | - * | |
| 1332 | - * @param array $atts Attributes including form, option, and default. | |
| 1333 | - * | |
| 1334 | - * @return mixed | |
| 1335 | 949 | */ |
| 1336 | 950 | public static function get_option( $atts ) { |
| 1337 | - $form = $atts['form']; | |
| 1338 | - $default = $atts['default'] ?? ''; | |
| 951 | + $form = $atts['form']; | |
| 952 | + $default = isset( $atts['default'] ) ? $atts['default'] : ''; | |
| 1339 | 953 | |
| 1340 | - return $form->options[ $atts['option'] ] ?? $default; | |
| 954 | + return isset( $form->options[ $atts['option'] ] ) ? $form->options[ $atts['option'] ] : $default; | |
| 1341 | 955 | } |
| 1342 | 956 | |
| 1343 | 957 | /** |
| 1344 | 958 | * Get the link to edit this form. |
| @@ -1343,12 +957,9 @@ | ||
| 1343 | 957 | /** |
| 1344 | 958 | * Get the link to edit this form. |
| 1345 | 959 | * |
| 1346 | 960 | * @since 4.0 |
| 1347 | - * | |
| 1348 | 961 | * @param int $form_id The id of the form. |
| 1349 | - * | |
| 1350 | - * @return string | |
| 1351 | 962 | */ |
| 1352 | 963 | public static function get_edit_link( $form_id ) { |
| 1353 | 964 | return admin_url( 'admin.php?page=formidable&frm_action=edit&id=' . $form_id ); |
| 1354 | 965 | } |
| @@ -1353,67 +964,9 @@ | ||
| 1353 | 964 | return admin_url( 'admin.php?page=formidable&frm_action=edit&id=' . $form_id ); |
| 1354 | 965 | } |
| 1355 | 966 | |
| 1356 | 967 | /** |
| 1357 | - * Check if the "Submit this form with AJAX" setting is toggled on. | |
| 1358 | - * | |
| 1359 | - * @since 6.2 | |
| 1360 | - * | |
| 1361 | - * @param stdClass $form | |
| 1362 | - * | |
| 1363 | - * @return bool | |
| 1364 | - */ | |
| 1365 | - public static function is_ajax_on( $form ) { | |
| 1366 | - return ! empty( $form->options['ajax_submit'] ); | |
| 1367 | - } | |
| 1368 | - | |
| 1369 | - /** | |
| 1370 | - * Get the latest form available. | |
| 1371 | - * | |
| 1372 | - * @since 6.8 | |
| 1373 | - * | |
| 1374 | - * @return object | |
| 1375 | - */ | |
| 1376 | - public static function get_latest_form() { | |
| 1377 | - | |
| 1378 | - $args = array( | |
| 1379 | - array( | |
| 1380 | - 'or' => 1, | |
| 1381 | - 'parent_form_id' => null, | |
| 1382 | - 'parent_form_id <' => 1, | |
| 1383 | - ), | |
| 1384 | - 'is_template' => 0, | |
| 1385 | - 'status !' => 'trash', | |
| 1386 | - ); | |
| 1387 | - | |
| 1388 | - return self::getAll( $args, 'created_at desc', 1 ); | |
| 1389 | - } | |
| 1390 | - | |
| 1391 | - /** | |
| 1392 | - * Count and return total forms. | |
| 1393 | - * | |
| 1394 | - * @since 6.8 | |
| 1395 | - * | |
| 1396 | - * @return int | |
| 1397 | - */ | |
| 1398 | - public static function get_forms_count() { | |
| 1399 | - | |
| 1400 | - $args = array( | |
| 1401 | - array( | |
| 1402 | - 'or' => 1, | |
| 1403 | - 'parent_form_id' => null, | |
| 1404 | - 'parent_form_id <' => 1, | |
| 1405 | - ), | |
| 1406 | - 'is_template' => 0, | |
| 1407 | - 'status !' => 'trash', | |
| 1408 | - ); | |
| 1409 | - | |
| 1410 | - return FrmDb::get_count( 'frm_forms', $args ); | |
| 1411 | - } | |
| 1412 | - | |
| 1413 | - /** | |
| 1414 | - * @deprecated 2.03.05 This is still referenced in a few add ons (API, locations). | |
| 1415 | - * | |
| 968 | + * @deprecated 3.0 | |
| 1416 | 969 | * @codeCoverageIgnore |
| 1417 | 970 | * |
| 1418 | 971 | * @param string $key |
| 1419 | 972 | * |
| @@ -1419,22 +972,15 @@ | ||
| 1419 | 972 | * |
| 1420 | 973 | * @return int form id |
| 1421 | 974 | */ |
| 1422 | 975 | public static function getIdByKey( $key ) { |
| 1423 | - _deprecated_function( __FUNCTION__, '2.03.05', 'FrmForm::get_id_by_key' ); | |
| 1424 | - return self::get_id_by_key( $key ); | |
| 976 | + return FrmFormDeprecated::getIdByKey( $key ); | |
| 1425 | 977 | } |
| 1426 | 978 | |
| 1427 | 979 | /** |
| 1428 | - * @deprecated 2.03.05 This is still referenced in the API add on as of v1.13. | |
| 1429 | - * | |
| 980 | + * @deprecated 3.0 | |
| 1430 | 981 | * @codeCoverageIgnore |
| 1431 | - * | |
| 1432 | - * @param int|string $id | |
| 1433 | - * | |
| 1434 | - * @return string | |
| 1435 | 982 | */ |
| 1436 | 983 | public static function getKeyById( $id ) { |
| 1437 | - _deprecated_function( __FUNCTION__, '2.03.05', 'FrmForm::get_key_by_id' ); | |
| 1438 | - return self::get_key_by_id( $id ); | |
| 984 | + return FrmFormDeprecated::getKeyById( $id ); | |
| 1439 | 985 | } |
| 1440 | 986 | } |