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