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