| @@ -5,67 +5,45 @@ | ||
| 5 | 5 | |
| 6 | 6 | class FrmEntry { |
| 7 | 7 | |
| 8 | 8 | /** |
| 9 | - * @since 6.16.3 | |
| 10 | - * | |
| 11 | - * @var array | |
| 12 | - */ | |
| 13 | - private static $unique_id_match_checks = array(); | |
| 14 | - | |
| 15 | - /** | |
| 16 | 9 | * Create a new entry |
| 17 | 10 | * |
| 18 | 11 | * @param array $values |
| 19 | 12 | * |
| 20 | - * @return bool|int $entry_id | |
| 13 | + * @return int | boolean $entry_id | |
| 21 | 14 | */ |
| 22 | 15 | public static function create( $values ) { |
| 23 | - return self::create_entry( $values, 'standard' ); | |
| 16 | + $entry_id = self::create_entry( $values, 'standard' ); | |
| 17 | + | |
| 18 | + return $entry_id; | |
| 24 | 19 | } |
| 25 | 20 | |
| 26 | 21 | /** |
| 27 | 22 | * Create a new entry with some differences depending on type |
| 28 | 23 | * |
| 29 | - * @param array $values | |
| 24 | + * @param array $values | |
| 30 | 25 | * @param string $type |
| 31 | 26 | * |
| 32 | - * @return bool|int $entry_id | |
| 27 | + * @return int | boolean $entry_id | |
| 33 | 28 | */ |
| 34 | 29 | private static function create_entry( $values, $type ) { |
| 35 | 30 | $new_values = self::before_insert_entry_in_database( $values, $type ); |
| 36 | 31 | |
| 37 | 32 | // Don't check XML entries for duplicates |
| 38 | - if ( $type !== 'xml' && self::is_duplicate( $new_values, $values ) ) { | |
| 33 | + if ( $type != 'xml' && self::is_duplicate( $new_values, $values ) ) { | |
| 39 | 34 | return false; |
| 40 | 35 | } |
| 41 | 36 | |
| 42 | - return self::continue_to_create_entry( $values, $new_values ); | |
| 43 | - } | |
| 37 | + $entry_id = self::continue_to_create_entry( $values, $new_values ); | |
| 44 | 38 | |
| 45 | - /** | |
| 46 | - * Flag the memoized unique id check after a new entry is created. | |
| 47 | - * This prevents possibly DB requests and helps avoid issues when creating repeater entries. | |
| 48 | - * | |
| 49 | - * @since 6.16.3 | |
| 50 | - * | |
| 51 | - * @param string $unique_id | |
| 52 | - * | |
| 53 | - * @return void | |
| 54 | - */ | |
| 55 | - private static function flag_new_unique_key( $unique_id ) { | |
| 56 | - if ( ! isset( self::$unique_id_match_checks[ $unique_id ] ) ) { | |
| 57 | - self::$unique_id_match_checks[ $unique_id ] = false; | |
| 58 | - } | |
| 39 | + return $entry_id; | |
| 59 | 40 | } |
| 60 | 41 | |
| 61 | 42 | /** |
| 62 | 43 | * Check for duplicate entries created in the last minute |
| 63 | 44 | * |
| 64 | - * @param array $new_values New values. | |
| 65 | - * @param array $values Values. | |
| 66 | - * | |
| 67 | - * @return bool | |
| 45 | + * @return boolean | |
| 68 | 46 | */ |
| 69 | 47 | public static function is_duplicate( $new_values, $values ) { |
| 70 | 48 | $duplicate_entry_time = apply_filters( 'frm_time_to_check_duplicates', 60, $new_values ); |
| 71 | 49 | |
| @@ -72,18 +50,13 @@ | ||
| 72 | 50 | if ( false === self::is_duplicate_check_needed( $values, $duplicate_entry_time ) ) { |
| 73 | 51 | return false; |
| 74 | 52 | } |
| 75 | 53 | |
| 76 | - if ( self::maybe_check_for_unique_id_match( $values, $new_values['created_at'] ) ) { | |
| 77 | - return true; | |
| 78 | - } | |
| 79 | - | |
| 80 | 54 | $check_val = $new_values; |
| 81 | - $check_val['created_at >'] = gmdate( 'Y-m-d H:i:s', strtotime( $new_values['created_at'] ) - absint( $duplicate_entry_time ) ); | |
| 55 | + $check_val['created_at >'] = gmdate( 'Y-m-d H:i:s', ( strtotime( $new_values['created_at'] ) - absint( $duplicate_entry_time ) ) ); | |
| 82 | 56 | |
| 83 | 57 | unset( $check_val['created_at'], $check_val['updated_at'], $check_val['is_draft'], $check_val['id'], $check_val['item_key'] ); |
| 84 | 58 | |
| 85 | - // phpcs:ignore Universal.Operators.StrictComparisons | |
| 86 | 59 | if ( $new_values['item_key'] == $new_values['name'] ) { |
| 87 | 60 | unset( $check_val['name'] ); |
| 88 | 61 | } |
| 89 | 62 | |
| @@ -97,10 +70,10 @@ | ||
| 97 | 70 | } |
| 98 | 71 | |
| 99 | 72 | global $frm_vars; |
| 100 | 73 | $frm_vars['checking_duplicates'] = true; |
| 101 | - $is_duplicate = false; | |
| 102 | 74 | |
| 75 | + $is_duplicate = false; | |
| 103 | 76 | foreach ( $entry_exists as $entry_exist ) { |
| 104 | 77 | $is_duplicate = true; |
| 105 | 78 | |
| 106 | 79 | // make sure it's a duplicate |
| @@ -105,14 +78,9 @@ | ||
| 105 | 78 | |
| 106 | 79 | // make sure it's a duplicate |
| 107 | 80 | $metas = FrmEntryMeta::get_entry_meta_info( $entry_exist ); |
| 108 | 81 | $field_metas = array(); |
| 109 | - | |
| 110 | 82 | foreach ( $metas as $meta ) { |
| 111 | - if ( 0 === (int) $meta->field_id ) { | |
| 112 | - continue; | |
| 113 | - } | |
| 114 | - | |
| 115 | 83 | $field_metas[ $meta->field_id ] = $meta->meta_value; |
| 116 | 84 | } |
| 117 | 85 | |
| 118 | 86 | $filtered_vals = array_filter( $values['item_meta'] ); |
| @@ -119,9 +87,9 @@ | ||
| 119 | 87 | $filtered_vals = self::convert_values_to_their_saved_value( $filtered_vals, $entry_exist ); |
| 120 | 88 | $field_metas = array_filter( $field_metas ); |
| 121 | 89 | |
| 122 | 90 | // If prev entry is empty and current entry is not, they are not duplicates |
| 123 | - if ( ! $field_metas && $filtered_vals ) { | |
| 91 | + if ( empty( $field_metas ) && ! empty( $filtered_vals ) ) { | |
| 124 | 92 | return false; |
| 125 | 93 | } |
| 126 | 94 | |
| 127 | 95 | // compare serialized values and not arrays |
| @@ -138,12 +106,12 @@ | ||
| 138 | 106 | continue; |
| 139 | 107 | } |
| 140 | 108 | |
| 141 | 109 | $diff = array_diff_assoc( $field_metas, $new_meta ); |
| 142 | - | |
| 143 | - foreach ( $diff as $meta_value ) { | |
| 110 | + foreach ( $diff as $field_id => $meta_value ) { | |
| 144 | 111 | if ( ! empty( $meta_value ) ) { |
| 145 | 112 | $is_duplicate = false; |
| 113 | + continue; | |
| 146 | 114 | } |
| 147 | 115 | } |
| 148 | 116 | |
| 149 | 117 | if ( $is_duplicate ) { |
| @@ -148,9 +116,9 @@ | ||
| 148 | 116 | |
| 149 | 117 | if ( $is_duplicate ) { |
| 150 | 118 | break; |
| 151 | 119 | } |
| 152 | - }//end foreach | |
| 120 | + } | |
| 153 | 121 | |
| 154 | 122 | $frm_vars['checking_duplicates'] = false; |
| 155 | 123 | |
| 156 | 124 | return $is_duplicate; |
| @@ -156,95 +124,25 @@ | ||
| 156 | 124 | return $is_duplicate; |
| 157 | 125 | } |
| 158 | 126 | |
| 159 | 127 | /** |
| 160 | - * @since 6.16.3 | |
| 161 | - * | |
| 162 | - * @param array $values POST request data. | |
| 163 | - * @param string $created_at The timestamp of the entry we are checking for. | |
| 164 | - * | |
| 165 | - * @return bool | |
| 166 | - */ | |
| 167 | - private static function maybe_check_for_unique_id_match( $values, $created_at ) { | |
| 168 | - if ( ! self::should_check_for_unique_id_match() ) { | |
| 169 | - return false; | |
| 170 | - } | |
| 171 | - | |
| 172 | - if ( empty( $values['unique_id'] ) ) { | |
| 173 | - return false; | |
| 174 | - } | |
| 175 | - | |
| 176 | - $unique_id = sanitize_key( $values['unique_id'] ); | |
| 177 | - | |
| 178 | - if ( ! $unique_id ) { | |
| 179 | - // Only continue if a unique ID was generated on form submit. | |
| 180 | - return false; | |
| 181 | - } | |
| 182 | - | |
| 183 | - if ( isset( self::$unique_id_match_checks[ $unique_id ] ) ) { | |
| 184 | - return self::$unique_id_match_checks[ $unique_id ]; | |
| 185 | - } | |
| 186 | - | |
| 187 | - $timestamp = strtotime( $created_at ); | |
| 188 | - | |
| 189 | - if ( false === $timestamp ) { | |
| 190 | - $timestamp = time(); | |
| 191 | - } | |
| 192 | - | |
| 193 | - self::$unique_id_match_checks[ $unique_id ] = (bool) FrmDb::get_var( | |
| 194 | - 'frm_item_metas', | |
| 195 | - array( | |
| 196 | - 'field_id' => 0, | |
| 197 | - 'meta_value' => serialize( compact( 'unique_id' ) ), | |
| 198 | - 'created_at >' => gmdate( 'Y-m-d H:i:s', $timestamp - MONTH_IN_SECONDS ), | |
| 199 | - ), | |
| 200 | - 'id' | |
| 201 | - ); | |
| 202 | - | |
| 203 | - return self::$unique_id_match_checks[ $unique_id ]; | |
| 204 | - } | |
| 205 | - | |
| 206 | - /** | |
| 207 | - * @since 6.16.3 | |
| 208 | - * | |
| 209 | - * @return bool | |
| 210 | - */ | |
| 211 | - private static function should_check_for_unique_id_match() { | |
| 212 | - /** | |
| 213 | - * Allow users to opt out of the DB query, in case it causes performance issues. | |
| 214 | - * | |
| 215 | - * @since 6.16.3 | |
| 216 | - * | |
| 217 | - * @param bool $should_extend | |
| 218 | - */ | |
| 219 | - $should_check = apply_filters( 'frm_check_for_unique_id_match', true ); | |
| 220 | - return (bool) $should_check; | |
| 221 | - } | |
| 222 | - | |
| 223 | - /** | |
| 224 | 128 | * Convert form data to the actual value that would be saved into the database. |
| 129 | + * This is important for the duplicate check as something like 'a:2:{s:5:"typed";s:0:"";s:6:"output";s:0:"";}' (a signature value) is actually an empty string and does not get saved. | |
| 225 | 130 | * |
| 226 | - * This is important for the duplicate check as something like 'a:2:{s:5:"typed";s:0:"";s:6:"output";s:0:"";}' | |
| 227 | - * (a signature value) is actually an empty string and does not get saved. | |
| 228 | - * | |
| 229 | 131 | * @param array $filter_vals |
| 230 | 132 | * @param int $entry_id |
| 231 | - * | |
| 232 | 133 | * @return array |
| 233 | 134 | */ |
| 234 | 135 | private static function convert_values_to_their_saved_value( $filter_vals, $entry_id ) { |
| 235 | 136 | $reduced = array(); |
| 236 | - | |
| 237 | 137 | foreach ( $filter_vals as $field_id => $value ) { |
| 238 | 138 | $field = FrmFieldFactory::get_field_object( $field_id ); |
| 239 | 139 | $reduced[ $field_id ] = $field->get_value_to_save( $value, array( 'entry_id' => $entry_id ) ); |
| 240 | 140 | $reduced[ $field_id ] = $field->set_value_before_save( $reduced[ $field_id ] ); |
| 241 | - | |
| 242 | 141 | if ( '' === $reduced[ $field_id ] || ( is_array( $reduced[ $field_id ] ) && 0 === count( $reduced[ $field_id ] ) ) ) { |
| 243 | 142 | unset( $reduced[ $field_id ] ); |
| 244 | 143 | } |
| 245 | 144 | } |
| 246 | - | |
| 247 | 145 | return $reduced; |
| 248 | 146 | } |
| 249 | 147 | |
| 250 | 148 | /** |
| @@ -252,15 +150,15 @@ | ||
| 252 | 150 | * |
| 253 | 151 | * @since 2.0.23 |
| 254 | 152 | * |
| 255 | 153 | * @param array $values |
| 256 | - * @param int $duplicate_entry_time | |
| 154 | + * @param int $duplicate_entry_time | |
| 257 | 155 | * |
| 258 | 156 | * @return bool |
| 259 | 157 | */ |
| 260 | 158 | private static function is_duplicate_check_needed( $values, $duplicate_entry_time ) { |
| 261 | 159 | // If time for checking duplicates is set to an empty value, don't check for duplicates |
| 262 | - if ( ! $duplicate_entry_time ) { | |
| 160 | + if ( empty( $duplicate_entry_time ) ) { | |
| 263 | 161 | return false; |
| 264 | 162 | } |
| 265 | 163 | |
| 266 | 164 | // If CSV is importing, don't check for duplicates |
| @@ -268,20 +166,20 @@ | ||
| 268 | 166 | return false; |
| 269 | 167 | } |
| 270 | 168 | |
| 271 | 169 | // If repeating field entries are getting created, don't check for duplicates |
| 272 | - return empty( $values['parent_form_id'] ); | |
| 170 | + if ( isset( $values['parent_form_id'] ) && $values['parent_form_id'] ) { | |
| 171 | + return false; | |
| 172 | + } | |
| 173 | + | |
| 174 | + return true; | |
| 273 | 175 | } |
| 274 | 176 | |
| 275 | - /** | |
| 276 | - * @param int|string $id | |
| 277 | - * | |
| 278 | - * @return false|int | |
| 279 | - */ | |
| 280 | 177 | public static function duplicate( $id ) { |
| 281 | 178 | global $wpdb; |
| 282 | 179 | |
| 283 | - $values = self::getOne( $id ); | |
| 180 | + $values = self::getOne( $id ); | |
| 181 | + | |
| 284 | 182 | $new_values = array(); |
| 285 | 183 | $new_values['item_key'] = FrmAppHelper::get_unique_key( '', $wpdb->prefix . 'frm_items', 'item_key' ); |
| 286 | 184 | $new_values['name'] = $values->name; |
| 287 | 185 | $new_values['is_draft'] = $values->is_draft; |
| @@ -291,9 +189,8 @@ | ||
| 291 | 189 | $new_values['created_at'] = current_time( 'mysql', 1 ); |
| 292 | 190 | $new_values['updated_at'] = $new_values['created_at']; |
| 293 | 191 | |
| 294 | 192 | $query_results = $wpdb->insert( $wpdb->prefix . 'frm_items', $new_values ); |
| 295 | - | |
| 296 | 193 | if ( ! $query_results ) { |
| 297 | 194 | return false; |
| 298 | 195 | } |
| 299 | 196 | |
| @@ -299,9 +196,8 @@ | ||
| 299 | 196 | |
| 300 | 197 | $entry_id = $wpdb->insert_id; |
| 301 | 198 | |
| 302 | 199 | global $frm_vars; |
| 303 | - | |
| 304 | 200 | if ( ! isset( $frm_vars['saved_entries'] ) ) { |
| 305 | 201 | $frm_vars['saved_entries'] = array(); |
| 306 | 202 | } |
| 307 | 203 | $frm_vars['saved_entries'][] = (int) $entry_id; |
| @@ -316,15 +212,17 @@ | ||
| 316 | 212 | |
| 317 | 213 | /** |
| 318 | 214 | * Update an entry (not via XML) |
| 319 | 215 | * |
| 320 | - * @param int $id | |
| 216 | + * @param int $id | |
| 321 | 217 | * @param array $values |
| 322 | 218 | * |
| 323 | - * @return bool|int $update_results | |
| 219 | + * @return boolean|int $update_results | |
| 324 | 220 | */ |
| 325 | 221 | public static function update( $id, $values ) { |
| 326 | - return self::update_entry( $id, $values, 'standard' ); | |
| 222 | + $update_results = self::update_entry( $id, $values, 'standard' ); | |
| 223 | + | |
| 224 | + return $update_results; | |
| 327 | 225 | } |
| 328 | 226 | |
| 329 | 227 | /** |
| 330 | 228 | * Update an entry with some differences depending on the update type |
| @@ -330,24 +228,23 @@ | ||
| 330 | 228 | * Update an entry with some differences depending on the update type |
| 331 | 229 | * |
| 332 | 230 | * @since 2.0.16 |
| 333 | 231 | * |
| 334 | - * @param int $id | |
| 335 | - * @param array $values | |
| 336 | - * @param string $update_type | |
| 232 | + * @param int $id | |
| 233 | + * @param array $values | |
| 337 | 234 | * |
| 338 | - * @return bool|int $query_results | |
| 235 | + * @return boolean|int $query_results | |
| 339 | 236 | */ |
| 340 | 237 | private static function update_entry( $id, $values, $update_type ) { |
| 341 | 238 | global $wpdb; |
| 342 | 239 | |
| 343 | 240 | $update = self::before_update_entry( $id, $values, $update_type ); |
| 344 | - | |
| 345 | 241 | if ( ! $update ) { |
| 346 | 242 | return false; |
| 347 | 243 | } |
| 348 | 244 | |
| 349 | - $new_values = self::package_entry_to_update( $id, $values ); | |
| 245 | + $new_values = self::package_entry_to_update( $id, $values ); | |
| 246 | + | |
| 350 | 247 | $query_results = $wpdb->update( $wpdb->prefix . 'frm_items', $new_values, compact( 'id' ) ); |
| 351 | 248 | |
| 352 | 249 | self::after_update_entry( $query_results, $id, $values, $new_values ); |
| 353 | 250 | |
| @@ -353,32 +250,19 @@ | ||
| 353 | 250 | |
| 354 | 251 | return $query_results; |
| 355 | 252 | } |
| 356 | 253 | |
| 357 | - /** | |
| 358 | - * Delete an entry. | |
| 359 | - * | |
| 360 | - * @param int|string $id | |
| 361 | - * | |
| 362 | - * @return bool True on success, false if nothing was deleted. | |
| 363 | - */ | |
| 364 | 254 | public static function destroy( $id ) { |
| 365 | 255 | global $wpdb; |
| 366 | 256 | $id = (int) $id; |
| 367 | 257 | |
| 368 | - // Item meta is required for conditional logic in actions with 'delete' events. | |
| 369 | - $entry = self::getOne( $id, true ); | |
| 258 | + $entry = self::getOne( $id ); | |
| 259 | + if ( ! $entry ) { | |
| 260 | + $result = false; | |
| 370 | 261 | |
| 371 | - if ( ! $entry ) { | |
| 372 | - return false; | |
| 262 | + return $result; | |
| 373 | 263 | } |
| 374 | 264 | |
| 375 | - /** | |
| 376 | - * Trigger an action to run custom logic before the entry is deleted. | |
| 377 | - * | |
| 378 | - * @param int $id The id of the entry that was destroyed. | |
| 379 | - * @param stdClass $entry The entry object. | |
| 380 | - */ | |
| 381 | 265 | do_action( 'frm_before_destroy_entry', $id, $entry ); |
| 382 | 266 | |
| 383 | 267 | $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_item_metas WHERE item_id=%d', $id ) ); |
| 384 | 268 | $result = $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_items WHERE id=%d', $id ) ); |
| @@ -384,34 +268,15 @@ | ||
| 384 | 268 | $result = $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_items WHERE id=%d', $id ) ); |
| 385 | 269 | |
| 386 | 270 | self::clear_cache(); |
| 387 | 271 | |
| 388 | - /** | |
| 389 | - * Trigger an action to run custom logic after the entry is deleted. | |
| 390 | - * Use this hook if you need to update caching after an entry is deleted. | |
| 391 | - * | |
| 392 | - * @since 5.4.1 | |
| 393 | - * | |
| 394 | - * @param int $id The id of the entry that was destroyed. | |
| 395 | - * @param stdClass $entry The entry object. | |
| 396 | - */ | |
| 397 | - do_action( 'frm_after_destroy_entry', $id, $entry ); | |
| 398 | - | |
| 399 | 272 | return $result; |
| 400 | 273 | } |
| 401 | 274 | |
| 402 | - /** | |
| 403 | - * @param int $id | |
| 404 | - * @param mixed $value | |
| 405 | - * @param int|string $form_id | |
| 406 | - * | |
| 407 | - * @return false|int | |
| 408 | - */ | |
| 409 | 275 | public static function update_form( $id, $value, $form_id ) { |
| 410 | 276 | global $wpdb; |
| 411 | 277 | $form_id = isset( $value ) ? $form_id : null; |
| 412 | 278 | $result = $wpdb->update( $wpdb->prefix . 'frm_items', array( 'form_id' => $form_id ), array( 'id' => $id ) ); |
| 413 | - | |
| 414 | 279 | if ( $result ) { |
| 415 | 280 | self::clear_cache(); |
| 416 | 281 | } |
| 417 | 282 | |
| @@ -422,10 +287,8 @@ | ||
| 422 | 287 | * Clear entry caching |
| 423 | 288 | * Called when an entry is changed |
| 424 | 289 | * |
| 425 | 290 | * @since 2.0.5 |
| 426 | - * | |
| 427 | - * @return void | |
| 428 | 291 | */ |
| 429 | 292 | public static function clear_cache() { |
| 430 | 293 | FrmDb::cache_delete_group( 'frm_entry' ); |
| 431 | 294 | FrmDb::cache_delete_group( 'frm_item' ); |
| @@ -437,17 +300,11 @@ | ||
| 437 | 300 | * After switching to the wp_loaded hook for processing entries, |
| 438 | 301 | * we can no longer use 'name', but check it as a fallback |
| 439 | 302 | * |
| 440 | 303 | * @since 2.0.11 |
| 441 | - * | |
| 442 | - * @param array $values | |
| 443 | - * @param array|string $default | |
| 444 | - * | |
| 445 | - * @return string | |
| 446 | 304 | */ |
| 447 | 305 | public static function get_new_entry_name( $values, $default = '' ) { |
| 448 | - $name = $values['item_name'] ?? $values['name'] ?? $default; | |
| 449 | - | |
| 306 | + $name = isset( $values['item_name'] ) ? $values['item_name'] : ( isset( $values['name'] ) ? $values['name'] : $default ); | |
| 450 | 307 | if ( is_array( $name ) ) { |
| 451 | 308 | $name = reset( $name ); |
| 452 | 309 | } |
| 453 | 310 | |
| @@ -456,28 +313,20 @@ | ||
| 456 | 313 | |
| 457 | 314 | /** |
| 458 | 315 | * If $entry is numeric, get the entry object |
| 459 | 316 | * |
| 317 | + * @param int|object $entry by reference | |
| 318 | + * | |
| 460 | 319 | * @since 2.0.9 |
| 461 | - * | |
| 462 | - * @param int|object $entry By reference. | |
| 463 | - * | |
| 464 | - * @return void | |
| 465 | 320 | */ |
| 466 | 321 | public static function maybe_get_entry( &$entry ) { |
| 467 | 322 | if ( $entry && is_numeric( $entry ) ) { |
| 468 | 323 | $entry = self::getOne( $entry ); |
| 469 | - } elseif ( ! $entry || 'false' === $entry ) { | |
| 324 | + } elseif ( empty( $entry ) || 'false' === $entry ) { | |
| 470 | 325 | $entry = false; |
| 471 | 326 | } |
| 472 | 327 | } |
| 473 | 328 | |
| 474 | - /** | |
| 475 | - * @param int|string $id | |
| 476 | - * @param bool $meta | |
| 477 | - * | |
| 478 | - * @return object|null | |
| 479 | - */ | |
| 480 | 329 | public static function getOne( $id, $meta = false ) { |
| 481 | 330 | global $wpdb; |
| 482 | 331 | |
| 483 | 332 | $query = "SELECT it.*, fr.name as form_name, fr.form_key as form_key FROM {$wpdb->prefix}frm_items it |
| @@ -482,9 +331,9 @@ | ||
| 482 | 331 | |
| 483 | 332 | $query = "SELECT it.*, fr.name as form_name, fr.form_key as form_key FROM {$wpdb->prefix}frm_items it |
| 484 | 333 | LEFT OUTER JOIN {$wpdb->prefix}frm_forms fr ON it.form_id=fr.id WHERE "; |
| 485 | 334 | |
| 486 | - $query .= is_numeric( $id ) ? 'it.id=%d' : 'it.item_key=%s'; | |
| 335 | + $query .= is_numeric( $id ) ? 'it.id=%d' : 'it.item_key=%s'; | |
| 487 | 336 | $query_args = array( $id ); |
| 488 | 337 | $query = $wpdb->prepare( $query, $query_args ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 489 | 338 | |
| 490 | 339 | if ( ! $meta ) { |
| @@ -493,9 +342,8 @@ | ||
| 493 | 342 | return $entry; |
| 494 | 343 | } |
| 495 | 344 | |
| 496 | 345 | $entry = FrmDb::check_cache( $id, 'frm_entry' ); |
| 497 | - | |
| 498 | 346 | if ( $entry !== false ) { |
| 499 | 347 | self::prepare_entry( $entry ); |
| 500 | 348 | return $entry; |
| 501 | 349 | } |
| @@ -510,19 +358,16 @@ | ||
| 510 | 358 | /** |
| 511 | 359 | * @since 4.02.03 |
| 512 | 360 | * |
| 513 | 361 | * @param object $entry |
| 514 | - * | |
| 515 | - * @return void | |
| 516 | 362 | */ |
| 517 | 363 | private static function prepare_entry( &$entry ) { |
| 518 | - if ( ! $entry ) { | |
| 364 | + if ( empty( $entry ) ) { | |
| 519 | 365 | return; |
| 520 | 366 | } |
| 521 | 367 | |
| 522 | 368 | FrmAppHelper::unserialize_or_decode( $entry->description ); |
| 523 | - // TODO: Remove slashes on input only, not output. | |
| 524 | - $entry = wp_unslash( $entry ); | |
| 369 | + $entry = wp_unslash( $entry ); // TODO: Remove slashes on input only, not output. | |
| 525 | 370 | } |
| 526 | 371 | |
| 527 | 372 | /** |
| 528 | 373 | * @since 4.02.03 |
| @@ -527,10 +372,8 @@ | ||
| 527 | 372 | /** |
| 528 | 373 | * @since 4.02.03 |
| 529 | 374 | * |
| 530 | 375 | * @param array $entries |
| 531 | - * | |
| 532 | - * @return void | |
| 533 | 376 | */ |
| 534 | 377 | private static function prepare_entries( &$entries ) { |
| 535 | 378 | foreach ( $entries as $k => $entry ) { |
| 536 | 379 | self::prepare_entry( $entry ); |
| @@ -537,13 +380,8 @@ | ||
| 537 | 380 | $entries[ $k ] = $entry; |
| 538 | 381 | } |
| 539 | 382 | } |
| 540 | 383 | |
| 541 | - /** | |
| 542 | - * @param object|null $entry | |
| 543 | - * | |
| 544 | - * @return object|null | |
| 545 | - */ | |
| 546 | 384 | public static function get_meta( $entry ) { |
| 547 | 385 | if ( ! $entry ) { |
| 548 | 386 | return $entry; |
| 549 | 387 | } |
| @@ -554,21 +392,18 @@ | ||
| 554 | 392 | array( |
| 555 | 393 | 'item_id' => $entry->id, |
| 556 | 394 | 'field_id !' => 0, |
| 557 | 395 | ), |
| 558 | - 'field_id, meta_value, field_key, item_id, f.type' | |
| 396 | + 'field_id, meta_value, field_key, item_id' | |
| 559 | 397 | ); |
| 560 | 398 | |
| 561 | 399 | $entry->metas = array(); |
| 562 | 400 | |
| 563 | 401 | $include_key = apply_filters( 'frm_include_meta_keys', false, array( 'form_id' => $entry->form_id ) ); |
| 564 | - | |
| 565 | 402 | foreach ( $metas as $meta_val ) { |
| 566 | - FrmFieldsHelper::prepare_field_value( $meta_val->meta_value, $meta_val->type ); | |
| 567 | - | |
| 568 | - if ( (int) $meta_val->item_id === (int) $entry->id ) { | |
| 403 | + if ( $meta_val->item_id == $entry->id ) { | |
| 404 | + FrmAppHelper::unserialize_or_decode( $meta_val->meta_value ); | |
| 569 | 405 | $entry->metas[ $meta_val->field_id ] = $meta_val->meta_value; |
| 570 | - | |
| 571 | 406 | if ( $include_key ) { |
| 572 | 407 | $entry->metas[ $meta_val->field_key ] = $entry->metas[ $meta_val->field_id ]; |
| 573 | 408 | } |
| 574 | 409 | continue; |
| @@ -578,12 +413,13 @@ | ||
| 578 | 413 | if ( ! isset( $entry->metas[ $meta_val->field_id ] ) ) { |
| 579 | 414 | $entry->metas[ $meta_val->field_id ] = array(); |
| 580 | 415 | } |
| 581 | 416 | |
| 417 | + FrmAppHelper::unserialize_or_decode( $meta_val->meta_value ); | |
| 582 | 418 | $entry->metas[ $meta_val->field_id ][] = $meta_val->meta_value; |
| 583 | 419 | |
| 584 | 420 | unset( $meta_val ); |
| 585 | - }//end foreach | |
| 421 | + } | |
| 586 | 422 | unset( $metas ); |
| 587 | 423 | |
| 588 | 424 | FrmDb::set_cache( $entry->id, $entry, 'frm_entry' ); |
| 589 | 425 | |
| @@ -591,47 +427,43 @@ | ||
| 591 | 427 | } |
| 592 | 428 | |
| 593 | 429 | /** |
| 594 | 430 | * @param string $id |
| 595 | - * | |
| 596 | - * @return bool | |
| 597 | 431 | */ |
| 598 | 432 | public static function exists( $id ) { |
| 599 | 433 | global $wpdb; |
| 600 | 434 | |
| 601 | 435 | if ( FrmDb::check_cache( $id, 'frm_entry' ) ) { |
| 602 | - return true; | |
| 436 | + $exists = true; | |
| 437 | + | |
| 438 | + return $exists; | |
| 603 | 439 | } |
| 604 | 440 | |
| 605 | - $where = is_numeric( $id ) ? array( 'id' => $id ) : array( 'item_key' => $id ); | |
| 606 | - $id = FrmDb::get_var( $wpdb->prefix . 'frm_items', $where ); | |
| 441 | + if ( is_numeric( $id ) ) { | |
| 442 | + $where = array( 'id' => $id ); | |
| 443 | + } else { | |
| 444 | + $where = array( 'item_key' => $id ); | |
| 445 | + } | |
| 446 | + $id = FrmDb::get_var( $wpdb->prefix . 'frm_items', $where ); | |
| 607 | 447 | |
| 608 | - return $id && $id > 0; | |
| 448 | + return ( $id && $id > 0 ); | |
| 609 | 449 | } |
| 610 | 450 | |
| 611 | - /** | |
| 612 | - * @param array|string $where | |
| 613 | - * @param string $order_by | |
| 614 | - * @param string $limit | |
| 615 | - * @param bool $meta | |
| 616 | - * @param bool $inc_form | |
| 617 | - * | |
| 618 | - * @return array | |
| 619 | - */ | |
| 620 | 451 | public static function getAll( $where, $order_by = '', $limit = '', $meta = false, $inc_form = true ) { |
| 621 | 452 | global $wpdb; |
| 622 | 453 | |
| 623 | - $limit = FrmDb::esc_limit( $limit ); | |
| 454 | + $limit = FrmDb::esc_limit( $limit ); | |
| 455 | + | |
| 624 | 456 | $cache_key = FrmAppHelper::maybe_json_encode( $where ) . $order_by . $limit . $inc_form; |
| 625 | 457 | $entries = wp_cache_get( $cache_key, 'frm_entry' ); |
| 626 | 458 | |
| 627 | 459 | if ( false === $entries ) { |
| 628 | - $fields = 'it.id, it.item_key, it.name, it.ip, it.form_id, it.post_id, it.user_id, it.parent_item_id, it.updated_by, it.created_at, it.updated_at, it.is_draft, it.description'; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 460 | + $fields = 'it.id, it.item_key, it.name, it.ip, it.form_id, it.post_id, it.user_id, it.parent_item_id, it.updated_by, it.created_at, it.updated_at, it.is_draft'; | |
| 629 | 461 | $table = $wpdb->prefix . 'frm_items it '; |
| 630 | 462 | |
| 631 | 463 | if ( $inc_form ) { |
| 632 | 464 | $fields = 'it.*, fr.name as form_name,fr.form_key as form_key'; |
| 633 | - $table .= 'LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_forms fr ON it.form_id=fr.id '; | |
| 465 | + $table .= 'LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_forms fr ON it.form_id=fr.id '; | |
| 634 | 466 | } |
| 635 | 467 | |
| 636 | 468 | if ( preg_match( '/ meta_([0-9]+)/', $order_by, $order_matches ) ) { |
| 637 | 469 | $fields .= self::sort_by_field( $order_matches[1] ); |
| @@ -644,9 +476,9 @@ | ||
| 644 | 476 | $entries = $wpdb->get_results( $query, OBJECT_K ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 645 | 477 | unset( $query ); |
| 646 | 478 | |
| 647 | 479 | FrmDb::set_cache( $cache_key, $entries, 'frm_entry' ); |
| 648 | - }//end if | |
| 480 | + } | |
| 649 | 481 | |
| 650 | 482 | if ( ! $meta || ! $entries ) { |
| 651 | 483 | self::prepare_entries( $entries ); |
| 652 | 484 | return $entries; |
| @@ -657,21 +489,15 @@ | ||
| 657 | 489 | $where = array( 'it.form_id' => substr( $where, 11 ) ); |
| 658 | 490 | } |
| 659 | 491 | |
| 660 | 492 | $meta_where = array( 'field_id !' => 0 ); |
| 661 | - | |
| 662 | - // phpcs:ignore Universal.Operators.StrictComparisons | |
| 663 | - if ( $limit == '' && is_array( $where ) && count( $where ) === 1 && isset( $where['it.form_id'] ) ) { | |
| 493 | + if ( $limit == '' && is_array( $where ) && count( $where ) == 1 && isset( $where['it.form_id'] ) ) { | |
| 664 | 494 | $meta_where['fi.form_id'] = $where['it.form_id']; |
| 665 | 495 | } else { |
| 666 | 496 | $meta_where['item_id'] = array_keys( $entries ); |
| 667 | 497 | } |
| 668 | 498 | |
| 669 | - $metas = FrmDb::get_results( | |
| 670 | - $wpdb->prefix . 'frm_item_metas it LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id = fi.id', | |
| 671 | - $meta_where, | |
| 672 | - 'item_id, meta_value, field_id, field_key, form_id, fi.type' | |
| 673 | - ); | |
| 499 | + $metas = FrmDb::get_results( $wpdb->prefix . 'frm_item_metas it LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_fields fi ON (it.field_id = fi.id)', $meta_where, 'item_id, meta_value, field_id, field_key, form_id' ); | |
| 674 | 500 | |
| 675 | 501 | unset( $meta_where ); |
| 676 | 502 | |
| 677 | 503 | if ( ! $metas ) { |
| @@ -687,9 +513,9 @@ | ||
| 687 | 513 | if ( ! isset( $entries[ $meta_val->item_id ]->metas ) ) { |
| 688 | 514 | $entries[ $meta_val->item_id ]->metas = array(); |
| 689 | 515 | } |
| 690 | 516 | |
| 691 | - FrmFieldsHelper::prepare_field_value( $meta_val->meta_value, $meta_val->type ); | |
| 517 | + FrmAppHelper::unserialize_or_decode( $meta_val->meta_value ); | |
| 692 | 518 | $entries[ $meta_val->item_id ]->metas[ $meta_val->field_id ] = $meta_val->meta_value; |
| 693 | 519 | unset( $m_key, $meta_val ); |
| 694 | 520 | } |
| 695 | 521 | |
| @@ -704,15 +530,15 @@ | ||
| 704 | 530 | return $entries; |
| 705 | 531 | } |
| 706 | 532 | |
| 707 | 533 | /** |
| 708 | - * @param int|string $field_id | |
| 709 | - * | |
| 534 | + * @param int $field_id | |
| 710 | 535 | * @return string |
| 711 | 536 | */ |
| 712 | 537 | private static function sort_by_field( $field_id ) { |
| 713 | 538 | global $wpdb; |
| 714 | - $field_id = (int) $field_id; | |
| 539 | + $field_id = (int) $field_id; | |
| 540 | + | |
| 715 | 541 | $field_options = FrmDb::get_var( 'frm_fields', array( 'id' => $field_id ), 'field_options' ); |
| 716 | 542 | FrmAppHelper::unserialize_or_decode( $field_options ); |
| 717 | 543 | |
| 718 | 544 | if ( empty( $field_options['post_field'] ) ) { |
| @@ -725,15 +551,13 @@ | ||
| 725 | 551 | } |
| 726 | 552 | |
| 727 | 553 | // Pagination Methods |
| 728 | 554 | /** |
| 729 | - * @param array|int|string $where If int, use the form id. | |
| 730 | - * | |
| 731 | - * @return int|string | |
| 555 | + * @param int|array|string If int, use the form id. | |
| 732 | 556 | */ |
| 733 | 557 | public static function getRecordCount( $where = '' ) { |
| 734 | 558 | global $wpdb; |
| 735 | - $table_join = $wpdb->prefix . 'frm_items it JOIN ' . $wpdb->prefix . 'frm_forms fr ON it.form_id=fr.id'; | |
| 559 | + $table_join = $wpdb->prefix . 'frm_items it LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_forms fr ON it.form_id=fr.id'; | |
| 736 | 560 | |
| 737 | 561 | if ( is_numeric( $where ) ) { |
| 738 | 562 | $table_join = 'frm_items'; |
| 739 | 563 | $where = array( 'form_id' => $where ); |
| @@ -739,32 +563,25 @@ | ||
| 739 | 563 | $where = array( 'form_id' => $where ); |
| 740 | 564 | } |
| 741 | 565 | |
| 742 | 566 | if ( is_array( $where ) ) { |
| 743 | - return FrmDb::get_count( $table_join, $where ); | |
| 567 | + $count = FrmDb::get_count( $table_join, $where ); | |
| 568 | + } else { | |
| 569 | + $cache_key = 'count_' . FrmAppHelper::maybe_json_encode( $where ); | |
| 570 | + $query = 'SELECT COUNT(*) FROM ' . $table_join . FrmDb::prepend_and_or_where( ' WHERE ', $where ); | |
| 571 | + $count = FrmDb::check_cache( $cache_key, 'frm_entry', $query, 'get_var' ); | |
| 744 | 572 | } |
| 745 | 573 | |
| 746 | - $cache_key = 'count_' . FrmAppHelper::maybe_json_encode( $where ); | |
| 747 | - $query = 'SELECT COUNT(*) FROM ' . $table_join . FrmDb::prepend_and_or_where( ' WHERE ', $where ); | |
| 748 | - | |
| 749 | - return FrmDb::check_cache( $cache_key, 'frm_entry', $query, 'get_var' ); | |
| 574 | + return $count; | |
| 750 | 575 | } |
| 751 | 576 | |
| 752 | - /** | |
| 753 | - * @param int|string $p_size | |
| 754 | - * @param array|int|string $where | |
| 755 | - * | |
| 756 | - * @return int | |
| 757 | - */ | |
| 758 | 577 | public static function getPageCount( $p_size, $where = '' ) { |
| 759 | 578 | $p_size = (int) $p_size; |
| 760 | 579 | $count = 1; |
| 761 | - | |
| 762 | 580 | if ( $p_size ) { |
| 763 | 581 | if ( ! is_numeric( $where ) ) { |
| 764 | 582 | $where = self::getRecordCount( $where ); |
| 765 | 583 | } |
| 766 | - | |
| 767 | 584 | $count = ceil( (int) $where / $p_size ); |
| 768 | 585 | } |
| 769 | 586 | |
| 770 | 587 | return $count; |
| @@ -774,21 +591,24 @@ | ||
| 774 | 591 | * Prepare the data before inserting it into the database |
| 775 | 592 | * |
| 776 | 593 | * @since 2.0.16 |
| 777 | 594 | * |
| 778 | - * @param array $values | |
| 595 | + * @param array $values | |
| 779 | 596 | * @param string $type |
| 780 | 597 | * |
| 781 | 598 | * @return array $new_values |
| 782 | 599 | */ |
| 783 | 600 | private static function before_insert_entry_in_database( &$values, $type ) { |
| 601 | + | |
| 784 | 602 | self::sanitize_entry_post( $values ); |
| 785 | 603 | |
| 786 | - if ( $type !== 'xml' ) { | |
| 604 | + if ( $type != 'xml' ) { | |
| 787 | 605 | $values = apply_filters( 'frm_pre_create_entry', $values ); |
| 788 | 606 | } |
| 789 | 607 | |
| 790 | - return self::package_entry_data( $values ); | |
| 608 | + $new_values = self::package_entry_data( $values ); | |
| 609 | + | |
| 610 | + return $new_values; | |
| 791 | 611 | } |
| 792 | 612 | |
| 793 | 613 | /** |
| 794 | 614 | * Create an entry and perform after create actions |
| @@ -797,13 +617,12 @@ | ||
| 797 | 617 | * |
| 798 | 618 | * @param array $values |
| 799 | 619 | * @param array $new_values |
| 800 | 620 | * |
| 801 | - * @return bool|int $entry_id | |
| 621 | + * @return boolean|int $entry_id | |
| 802 | 622 | */ |
| 803 | 623 | private static function continue_to_create_entry( $values, $new_values ) { |
| 804 | 624 | $entry_id = self::insert_entry_into_database( $new_values ); |
| 805 | - | |
| 806 | 625 | if ( ! $entry_id ) { |
| 807 | 626 | return false; |
| 808 | 627 | } |
| 809 | 628 | |
| @@ -816,11 +635,9 @@ | ||
| 816 | 635 | * Sanitize the POST values before we use them |
| 817 | 636 | * |
| 818 | 637 | * @since 2.0 |
| 819 | 638 | * |
| 820 | - * @param array $values The POST values by reference. | |
| 821 | - * | |
| 822 | - * @return void | |
| 639 | + * @param array $values The POST values by reference | |
| 823 | 640 | */ |
| 824 | 641 | public static function sanitize_entry_post( &$values ) { |
| 825 | 642 | $sanitize_method = array( |
| 826 | 643 | 'form_id' => 'absint', |
| @@ -869,22 +686,15 @@ | ||
| 869 | 686 | 'description' => self::get_entry_description( $values ), |
| 870 | 687 | 'user_id' => self::get_entry_user_id( $values ), |
| 871 | 688 | ); |
| 872 | 689 | |
| 873 | - $new_values['updated_by'] = $values['updated_by'] ?? $new_values['user_id']; | |
| 690 | + $new_values['updated_by'] = isset( $values['updated_by'] ) ? $values['updated_by'] : $new_values['user_id']; | |
| 874 | 691 | |
| 875 | 692 | return $new_values; |
| 876 | 693 | } |
| 877 | 694 | |
| 878 | - /** | |
| 879 | - * @param array $values | |
| 880 | - * @param string $name | |
| 881 | - * @param mixed $default | |
| 882 | - * | |
| 883 | - * @return mixed | |
| 884 | - */ | |
| 885 | 695 | private static function get_entry_value( $values, $name, $default ) { |
| 886 | - return $values[ $name ] ?? $default; | |
| 696 | + return isset( $values[ $name ] ) ? $values[ $name ] : $default; | |
| 887 | 697 | } |
| 888 | 698 | |
| 889 | 699 | /** |
| 890 | 700 | * Get the ip for a new entry. |
| @@ -901,9 +711,8 @@ | ||
| 901 | 711 | return ''; |
| 902 | 712 | } |
| 903 | 713 | |
| 904 | 714 | $ip = FrmAppHelper::get_ip_address(); |
| 905 | - | |
| 906 | 715 | if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { |
| 907 | 716 | $ip = self::get_entry_value( $values, 'ip', $ip ); |
| 908 | 717 | } |
| 909 | 718 | |
| @@ -919,13 +728,9 @@ | ||
| 919 | 728 | * |
| 920 | 729 | * @return int |
| 921 | 730 | */ |
| 922 | 731 | private static function get_is_draft_value( $values ) { |
| 923 | - if ( isset( $values['frm_saving_draft'] ) && FrmEntriesHelper::DRAFT_ENTRY_STATUS === (int) $values['frm_saving_draft'] ) { | |
| 924 | - return FrmEntriesHelper::DRAFT_ENTRY_STATUS; | |
| 925 | - } | |
| 926 | - | |
| 927 | - return isset( $values['is_draft'] ) ? absint( $values['is_draft'] ) : FrmEntriesHelper::SUBMITTED_ENTRY_STATUS; | |
| 732 | + return ( ( isset( $values['frm_saving_draft'] ) && $values['frm_saving_draft'] == 1 ) || ( isset( $values['is_draft'] ) && $values['is_draft'] == 1 ) ) ? 1 : 0; | |
| 928 | 733 | } |
| 929 | 734 | |
| 930 | 735 | /** |
| 931 | 736 | * Get the created_at value for a new entry |
| @@ -949,9 +754,15 @@ | ||
| 949 | 754 | * |
| 950 | 755 | * @return string |
| 951 | 756 | */ |
| 952 | 757 | private static function get_updated_at( $values ) { |
| 953 | - return $values['updated_at'] ?? self::get_created_at( $values ); | |
| 758 | + if ( isset( $values['updated_at'] ) ) { | |
| 759 | + $updated_at = $values['updated_at']; | |
| 760 | + } else { | |
| 761 | + $updated_at = self::get_created_at( $values ); | |
| 762 | + } | |
| 763 | + | |
| 764 | + return $updated_at; | |
| 954 | 765 | } |
| 955 | 766 | |
| 956 | 767 | /** |
| 957 | 768 | * Get the description value for a new entry |
| @@ -962,18 +773,20 @@ | ||
| 962 | 773 | * |
| 963 | 774 | * @return string |
| 964 | 775 | */ |
| 965 | 776 | private static function get_entry_description( $values ) { |
| 966 | - if ( ! empty( $values['description'] ) ) { | |
| 967 | - return FrmAppHelper::maybe_json_encode( $values['description'] ); | |
| 777 | + if ( isset( $values['description'] ) && ! empty( $values['description'] ) ) { | |
| 778 | + $description = FrmAppHelper::maybe_json_encode( $values['description'] ); | |
| 779 | + } else { | |
| 780 | + $description = json_encode( | |
| 781 | + array( | |
| 782 | + 'browser' => FrmAppHelper::get_server_value( 'HTTP_USER_AGENT' ), | |
| 783 | + 'referrer' => FrmAppHelper::get_server_value( 'HTTP_REFERER' ), | |
| 784 | + ) | |
| 785 | + ); | |
| 968 | 786 | } |
| 969 | 787 | |
| 970 | - return json_encode( | |
| 971 | - array( | |
| 972 | - 'browser' => FrmAppHelper::get_server_value( 'HTTP_USER_AGENT' ), | |
| 973 | - 'referrer' => FrmAppHelper::get_server_value( 'HTTP_REFERER' ), | |
| 974 | - ) | |
| 975 | - ); | |
| 788 | + return $description; | |
| 976 | 789 | } |
| 977 | 790 | |
| 978 | 791 | /** |
| 979 | 792 | * Get the user_id value for a new entry |
| @@ -985,13 +798,15 @@ | ||
| 985 | 798 | * @return int |
| 986 | 799 | */ |
| 987 | 800 | private static function get_entry_user_id( $values ) { |
| 988 | 801 | if ( isset( $values['frm_user_id'] ) && ( is_numeric( $values['frm_user_id'] ) || FrmAppHelper::is_admin() ) ) { |
| 989 | - return $values['frm_user_id']; | |
| 802 | + $user_id = $values['frm_user_id']; | |
| 803 | + } else { | |
| 804 | + $current_user_id = get_current_user_id(); | |
| 805 | + $user_id = $current_user_id ? $current_user_id : 0; | |
| 990 | 806 | } |
| 991 | 807 | |
| 992 | - $current_user_id = get_current_user_id(); | |
| 993 | - return $current_user_id ? $current_user_id : 0; | |
| 808 | + return $user_id; | |
| 994 | 809 | } |
| 995 | 810 | |
| 996 | 811 | /** |
| 997 | 812 | * Insert new entry into the database |
| @@ -999,9 +814,9 @@ | ||
| 999 | 814 | * @since 2.0.16 |
| 1000 | 815 | * |
| 1001 | 816 | * @param array $new_values |
| 1002 | 817 | * |
| 1003 | - * @return bool|int $entry_id | |
| 818 | + * @return int | boolean $entry_id | |
| 1004 | 819 | */ |
| 1005 | 820 | private static function insert_entry_into_database( $new_values ) { |
| 1006 | 821 | global $wpdb; |
| 1007 | 822 | |
| @@ -1006,9 +821,15 @@ | ||
| 1006 | 821 | global $wpdb; |
| 1007 | 822 | |
| 1008 | 823 | $query_results = $wpdb->insert( $wpdb->prefix . 'frm_items', $new_values ); |
| 1009 | 824 | |
| 1010 | - return ! $query_results ? false : $wpdb->insert_id; | |
| 825 | + if ( ! $query_results ) { | |
| 826 | + $entry_id = false; | |
| 827 | + } else { | |
| 828 | + $entry_id = $wpdb->insert_id; | |
| 829 | + } | |
| 830 | + | |
| 831 | + return $entry_id; | |
| 1011 | 832 | } |
| 1012 | 833 | |
| 1013 | 834 | /** |
| 1014 | 835 | * Add the new entry to global $frm_vars |
| @@ -1014,11 +835,9 @@ | ||
| 1014 | 835 | * Add the new entry to global $frm_vars |
| 1015 | 836 | * |
| 1016 | 837 | * @since 2.0.16 |
| 1017 | 838 | * |
| 1018 | - * @param int|string $entry_id | |
| 1019 | - * | |
| 1020 | - * @return void | |
| 839 | + * @param int $entry_id | |
| 1021 | 840 | */ |
| 1022 | 841 | private static function add_new_entry_to_frm_vars( $entry_id ) { |
| 1023 | 842 | global $frm_vars; |
| 1024 | 843 | |
| @@ -1033,55 +852,28 @@ | ||
| 1033 | 852 | * Add entry metas, if there are any |
| 1034 | 853 | * |
| 1035 | 854 | * @since 2.0.16 |
| 1036 | 855 | * |
| 1037 | - * @param array $values | |
| 1038 | - * @param int|string $entry_id | |
| 1039 | - * | |
| 856 | + * @param array $values | |
| 857 | + * @param int $entry_id | |
| 1040 | 858 | * @return void |
| 1041 | 859 | */ |
| 1042 | 860 | private static function maybe_add_entry_metas( $values, $entry_id ) { |
| 1043 | 861 | if ( isset( $values['item_meta'] ) ) { |
| 1044 | 862 | FrmEntryMeta::update_entry_metas( $entry_id, $values['item_meta'] ); |
| 1045 | - self::maybe_add_unique_id_meta( $values, $entry_id ); | |
| 1046 | 863 | } |
| 1047 | 864 | self::maybe_add_captcha_meta( (int) $values['form_id'], (int) $entry_id ); |
| 1048 | 865 | } |
| 1049 | 866 | |
| 1050 | 867 | /** |
| 1051 | - * @since 6.16.3 | |
| 1052 | - * | |
| 1053 | - * @param array $values | |
| 1054 | - * @param int $entry_id | |
| 1055 | - * | |
| 1056 | - * @return void | |
| 1057 | - */ | |
| 1058 | - private static function maybe_add_unique_id_meta( $values, $entry_id ) { | |
| 1059 | - if ( ! empty( $values['parent_form_id'] ) || empty( $values['unique_id'] ) || ! self::should_check_for_unique_id_match() ) { | |
| 1060 | - return; | |
| 1061 | - } | |
| 1062 | - | |
| 1063 | - // This unique ID is inserted with JS on form submit. | |
| 1064 | - // It is used to check for duplicate entries. | |
| 1065 | - $unique_id = sanitize_key( $values['unique_id'] ); | |
| 1066 | - | |
| 1067 | - if ( $unique_id ) { | |
| 1068 | - FrmEntryMeta::add_entry_meta( $entry_id, 0, '', compact( 'unique_id' ) ); | |
| 1069 | - self::flag_new_unique_key( $unique_id ); | |
| 1070 | - } | |
| 1071 | - } | |
| 1072 | - | |
| 1073 | - /** | |
| 1074 | 868 | * @since 5.0.15 |
| 1075 | 869 | * |
| 1076 | 870 | * @param int $form_id |
| 1077 | 871 | * @param int $entry_id |
| 1078 | - * | |
| 1079 | 872 | * @return void |
| 1080 | 873 | */ |
| 1081 | 874 | private static function maybe_add_captcha_meta( $form_id, $entry_id ) { |
| 1082 | 875 | global $frm_vars; |
| 1083 | - | |
| 1084 | 876 | if ( array_key_exists( 'captcha_scores', $frm_vars ) && array_key_exists( $form_id, $frm_vars['captcha_scores'] ) ) { |
| 1085 | 877 | $captcha_score_meta = array( 'captcha_score' => $frm_vars['captcha_scores'][ $form_id ] ); |
| 1086 | 878 | FrmEntryMeta::add_entry_meta( $entry_id, 0, '', maybe_serialize( $captcha_score_meta ) ); |
| 1087 | 879 | } |
| @@ -1091,17 +883,14 @@ | ||
| 1091 | 883 | * Trigger frm_after_create_entry hooks |
| 1092 | 884 | * |
| 1093 | 885 | * @since 2.0.16 |
| 1094 | 886 | * |
| 1095 | - * @param int $entry_id | |
| 1096 | - * @param array $values | |
| 887 | + * @param int $entry_id | |
| 1097 | 888 | * @param array $new_values |
| 1098 | - * | |
| 1099 | - * @return void | |
| 1100 | 889 | */ |
| 1101 | 890 | private static function after_entry_created_actions( $entry_id, $values, $new_values ) { |
| 1102 | - // This is a child entry. | |
| 1103 | - $is_child = isset( $values['parent_nonce'] ) && ! empty( $values['parent_form_id'] ) && wp_verify_nonce( $values['parent_nonce'], 'parent' ); | |
| 891 | + // this is a child entry | |
| 892 | + $is_child = isset( $values['parent_form_id'] ) && isset( $values['parent_nonce'] ) && ! empty( $values['parent_form_id'] ) && wp_verify_nonce( $values['parent_nonce'], 'parent' ); | |
| 1104 | 893 | |
| 1105 | 894 | do_action( 'frm_after_create_entry', $entry_id, $new_values['form_id'], compact( 'is_child' ) ); |
| 1106 | 895 | do_action( 'frm_after_create_entry_' . $new_values['form_id'], $entry_id, compact( 'is_child' ) ); |
| 1107 | 896 | } |
| @@ -1112,13 +901,12 @@ | ||
| 1112 | 901 | * @since 2.0.16 |
| 1113 | 902 | * |
| 1114 | 903 | * @param array $values |
| 1115 | 904 | * @param array $new_values |
| 1116 | - * @param int $entry_id | |
| 1117 | - * | |
| 1118 | - * @return void | |
| 905 | + * @param int $entry_id | |
| 1119 | 906 | */ |
| 1120 | 907 | private static function after_insert_entry_in_database( $values, $new_values, $entry_id ) { |
| 908 | + | |
| 1121 | 909 | self::add_new_entry_to_frm_vars( $entry_id ); |
| 1122 | 910 | |
| 1123 | 911 | self::maybe_add_entry_metas( $values, $entry_id ); |
| 1124 | 912 | |
| @@ -1131,13 +919,13 @@ | ||
| 1131 | 919 | * Perform some actions right before updating an entry |
| 1132 | 920 | * |
| 1133 | 921 | * @since 2.0.16 |
| 1134 | 922 | * |
| 1135 | - * @param int|string $id | |
| 1136 | - * @param array $values | |
| 1137 | - * @param string $update_type | |
| 923 | + * @param int $id | |
| 924 | + * @param array $values | |
| 925 | + * @param string $update_type | |
| 1138 | 926 | * |
| 1139 | - * @return bool $update | |
| 927 | + * @return boolean $update | |
| 1140 | 928 | */ |
| 1141 | 929 | private static function before_update_entry( $id, &$values, $update_type ) { |
| 1142 | 930 | $update = true; |
| 1143 | 931 | |
| @@ -1142,14 +930,13 @@ | ||
| 1142 | 930 | $update = true; |
| 1143 | 931 | |
| 1144 | 932 | global $frm_vars; |
| 1145 | 933 | |
| 1146 | - // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict | |
| 1147 | - if ( isset( $frm_vars['saved_entries'] ) && is_array( $frm_vars['saved_entries'] ) && in_array( (int) $id, $frm_vars['saved_entries'] ) ) { | |
| 934 | + if ( isset( $frm_vars['saved_entries'] ) && is_array( $frm_vars['saved_entries'] ) && in_array( (int) $id, (array) $frm_vars['saved_entries'] ) ) { | |
| 1148 | 935 | $update = false; |
| 1149 | 936 | } |
| 1150 | 937 | |
| 1151 | - if ( $update && $update_type !== 'xml' ) { | |
| 938 | + if ( $update && $update_type != 'xml' ) { | |
| 1152 | 939 | $values = apply_filters( 'frm_pre_update_entry', $values, $id ); |
| 1153 | 940 | } |
| 1154 | 941 | |
| 1155 | 942 | return $update; |
| @@ -1159,9 +946,9 @@ | ||
| 1159 | 946 | * Package the entry data for updating |
| 1160 | 947 | * |
| 1161 | 948 | * @since 2.0.16 |
| 1162 | 949 | * |
| 1163 | - * @param int $id | |
| 950 | + * @param int $id | |
| 1164 | 951 | * @param array $values |
| 1165 | 952 | * |
| 1166 | 953 | * @return array $new_values |
| 1167 | 954 | */ |
| @@ -1172,9 +959,9 @@ | ||
| 1172 | 959 | 'name' => self::get_new_entry_name( $values ), |
| 1173 | 960 | 'form_id' => (int) self::get_entry_value( $values, 'form_id', null ), |
| 1174 | 961 | 'is_draft' => self::get_is_draft_value( $values ), |
| 1175 | 962 | 'updated_at' => current_time( 'mysql', 1 ), |
| 1176 | - 'updated_by' => $values['updated_by'] ?? get_current_user_id(), | |
| 963 | + 'updated_by' => isset( $values['updated_by'] ) ? $values['updated_by'] : get_current_user_id(), | |
| 1177 | 964 | ); |
| 1178 | 965 | |
| 1179 | 966 | if ( isset( $values['post_id'] ) ) { |
| 1180 | 967 | $new_values['post_id'] = (int) $values['post_id']; |
| @@ -1191,9 +978,11 @@ | ||
| 1191 | 978 | if ( isset( $values['frm_user_id'] ) && is_numeric( $values['frm_user_id'] ) ) { |
| 1192 | 979 | $new_values['user_id'] = $values['frm_user_id']; |
| 1193 | 980 | } |
| 1194 | 981 | |
| 1195 | - return apply_filters( 'frm_update_entry', $new_values, $id ); | |
| 982 | + $new_values = apply_filters( 'frm_update_entry', $new_values, $id ); | |
| 983 | + | |
| 984 | + return $new_values; | |
| 1196 | 985 | } |
| 1197 | 986 | |
| 1198 | 987 | /** |
| 1199 | 988 | * Perform some actions right after updating an entry |
| @@ -1199,14 +988,12 @@ | ||
| 1199 | 988 | * Perform some actions right after updating an entry |
| 1200 | 989 | * |
| 1201 | 990 | * @since 2.0.16 |
| 1202 | 991 | * |
| 1203 | - * @param bool|int $query_results | |
| 1204 | - * @param int|string $id | |
| 1205 | - * @param array $values | |
| 1206 | - * @param array $new_values | |
| 1207 | - * | |
| 1208 | - * @return void | |
| 992 | + * @param boolean|int $query_results | |
| 993 | + * @param int $id | |
| 994 | + * @param array $values | |
| 995 | + * @param array $new_values | |
| 1209 | 996 | */ |
| 1210 | 997 | private static function after_update_entry( $query_results, $id, $values, $new_values ) { |
| 1211 | 998 | if ( $query_results ) { |
| 1212 | 999 | self::clear_cache(); |
| @@ -1212,9 +999,8 @@ | ||
| 1212 | 999 | self::clear_cache(); |
| 1213 | 1000 | } |
| 1214 | 1001 | |
| 1215 | 1002 | global $frm_vars; |
| 1216 | - | |
| 1217 | 1003 | if ( ! isset( $frm_vars['saved_entries'] ) ) { |
| 1218 | 1004 | $frm_vars['saved_entries'] = array(); |
| 1219 | 1005 | } |
| 1220 | 1006 | |
| @@ -1235,12 +1021,14 @@ | ||
| 1235 | 1021 | * @since 2.0.16 |
| 1236 | 1022 | * |
| 1237 | 1023 | * @param array $values |
| 1238 | 1024 | * |
| 1239 | - * @return bool|int $entry_id | |
| 1025 | + * @return int | boolean $entry_id | |
| 1240 | 1026 | */ |
| 1241 | 1027 | public static function create_entry_from_xml( $values ) { |
| 1242 | - return self::create_entry( $values, 'xml' ); | |
| 1028 | + $entry_id = self::create_entry( $values, 'xml' ); | |
| 1029 | + | |
| 1030 | + return $entry_id; | |
| 1243 | 1031 | } |
| 1244 | 1032 | |
| 1245 | 1033 | /** |
| 1246 | 1034 | * Update entry from an XML import |
| @@ -1247,15 +1035,17 @@ | ||
| 1247 | 1035 | * Certain actions aren't necessary when importing (like saving sub entries and modifying other vals) |
| 1248 | 1036 | * |
| 1249 | 1037 | * @since 2.0.16 |
| 1250 | 1038 | * |
| 1251 | - * @param int $id | |
| 1039 | + * @param int $id | |
| 1252 | 1040 | * @param array $values |
| 1253 | 1041 | * |
| 1254 | - * @return bool|int $updated | |
| 1042 | + * @return int | boolean $updated | |
| 1255 | 1043 | */ |
| 1256 | 1044 | public static function update_entry_from_xml( $id, $values ) { |
| 1257 | - return self::update_entry( $id, $values, 'xml' ); | |
| 1045 | + $updated = self::update_entry( $id, $values, 'xml' ); | |
| 1046 | + | |
| 1047 | + return $updated; | |
| 1258 | 1048 | } |
| 1259 | 1049 | |
| 1260 | 1050 | /** |
| 1261 | 1051 | * @param string $key |
| @@ -1263,23 +1053,8 @@ | ||
| 1263 | 1053 | * @return int entry_id |
| 1264 | 1054 | */ |
| 1265 | 1055 | public static function get_id_by_key( $key ) { |
| 1266 | 1056 | $entry_id = FrmDb::get_var( 'frm_items', array( 'item_key' => sanitize_title( $key ) ) ); |
| 1057 | + | |
| 1267 | 1058 | return (int) $entry_id; |
| 1268 | - } | |
| 1269 | - | |
| 1270 | - /** | |
| 1271 | - * Get entries count. | |
| 1272 | - * | |
| 1273 | - * @since 6.8 | |
| 1274 | - * | |
| 1275 | - * @return int|string | |
| 1276 | - */ | |
| 1277 | - public static function get_entries_count() { | |
| 1278 | - $args = array( | |
| 1279 | - 'or' => 1, | |
| 1280 | - 'parent_form_id' => null, | |
| 1281 | - 'parent_form_id <' => 1, | |
| 1282 | - ); | |
| 1283 | - return self::getRecordCount( $args ); | |
| 1284 | 1059 | } |
| 1285 | 1060 | } |