| @@ -9,16 +9,14 @@ | ||
| 9 | 9 | * @param int $entry_id |
| 10 | 10 | * @param int $field_id |
| 11 | 11 | * @param string $meta_key usually set to '' as this parameter is no longer used. |
| 12 | 12 | * @param mixed $meta_value |
| 13 | - * | |
| 14 | - * @return int | |
| 15 | 13 | */ |
| 16 | 14 | public static function add_entry_meta( $entry_id, $field_id, $meta_key, $meta_value ) { |
| 17 | 15 | global $wpdb; |
| 18 | 16 | |
| 19 | 17 | if ( FrmAppHelper::is_empty_value( $meta_value ) ) { |
| 20 | - // Don't save blank fields | |
| 18 | + // don't save blank fields | |
| 21 | 19 | return 0; |
| 22 | 20 | } |
| 23 | 21 | |
| 24 | 22 | $new_values = array( |
| @@ -28,27 +26,30 @@ | ||
| 28 | 26 | 'created_at' => current_time( 'mysql', 1 ), |
| 29 | 27 | ); |
| 30 | 28 | |
| 31 | 29 | self::set_value_before_save( $new_values ); |
| 32 | - $new_values = apply_filters( 'frm_add_entry_meta', $new_values ); | |
| 30 | + $new_values = apply_filters( 'frm_add_entry_meta', $new_values ); | |
| 31 | + | |
| 33 | 32 | $query_results = $wpdb->insert( $wpdb->prefix . 'frm_item_metas', $new_values ); |
| 34 | 33 | |
| 35 | 34 | if ( $query_results ) { |
| 36 | 35 | self::clear_cache(); |
| 37 | 36 | wp_cache_delete( $entry_id, 'frm_entry' ); |
| 38 | - return $wpdb->insert_id; | |
| 37 | + $id = $wpdb->insert_id; | |
| 38 | + } else { | |
| 39 | + $id = 0; | |
| 39 | 40 | } |
| 40 | 41 | |
| 41 | - return 0; | |
| 42 | + return $id; | |
| 42 | 43 | } |
| 43 | 44 | |
| 44 | 45 | /** |
| 45 | - * @param int $entry_id | |
| 46 | - * @param int $field_id | |
| 47 | - * @param string $meta_key Deprecated. | |
| 46 | + * @param int $entry_id | |
| 47 | + * @param int $field_id | |
| 48 | + * @param string $meta_key deprecated | |
| 48 | 49 | * @param array|string $meta_value |
| 49 | 50 | * |
| 50 | - * @return bool|int | |
| 51 | + * @return bool|false|int | |
| 51 | 52 | */ |
| 52 | 53 | public static function update_entry_meta( $entry_id, $field_id, $meta_key, $meta_value ) { |
| 53 | 54 | if ( ! $field_id ) { |
| 54 | 55 | return false; |
| @@ -67,9 +68,8 @@ | ||
| 67 | 68 | |
| 68 | 69 | if ( is_array( $values['meta_value'] ) ) { |
| 69 | 70 | $values['meta_value'] = array_filter( $values['meta_value'], 'FrmAppHelper::is_not_empty_value' ); |
| 70 | 71 | } |
| 71 | - | |
| 72 | 72 | $meta_value = maybe_serialize( $values['meta_value'] ); |
| 73 | 73 | |
| 74 | 74 | wp_cache_delete( $entry_id, 'frm_entry' ); |
| 75 | 75 | self::clear_cache(); |
| @@ -78,31 +78,20 @@ | ||
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | /** |
| 81 | 81 | * @since 3.0 |
| 82 | - * | |
| 83 | - * @param array $values | |
| 84 | - * | |
| 85 | - * @return void | |
| 86 | 82 | */ |
| 87 | 83 | private static function set_value_before_save( &$values ) { |
| 88 | 84 | $field = FrmField::getOne( $values['field_id'] ); |
| 85 | + if ( $field ) { | |
| 86 | + $field_obj = FrmFieldFactory::get_field_object( $field ); | |
| 89 | 87 | |
| 90 | - if ( ! $field ) { | |
| 91 | - return; | |
| 88 | + $values['meta_value'] = $field_obj->set_value_before_save( $values['meta_value'] ); | |
| 92 | 89 | } |
| 93 | - | |
| 94 | - $field_obj = FrmFieldFactory::get_field_object( $field ); | |
| 95 | - $values['meta_value'] = $field_obj->set_value_before_save( $values['meta_value'] ); | |
| 96 | 90 | } |
| 97 | 91 | |
| 98 | 92 | /** |
| 99 | 93 | * @since 3.0 |
| 100 | - * | |
| 101 | - * @param array $atts | |
| 102 | - * @param mixed $value | |
| 103 | - * | |
| 104 | - * @return void | |
| 105 | 94 | */ |
| 106 | 95 | private static function get_value_to_save( $atts, &$value ) { |
| 107 | 96 | if ( is_object( $atts['field'] ) ) { |
| 108 | 97 | $field_obj = FrmFieldFactory::get_field_object( $atts['field'] ); |
| @@ -117,19 +106,13 @@ | ||
| 117 | 106 | |
| 118 | 107 | $value = apply_filters( 'frm_prepare_data_before_db', $value, $atts['field_id'], $atts['entry_id'], array( 'field' => $atts['field'] ) ); |
| 119 | 108 | } |
| 120 | 109 | |
| 121 | - /** | |
| 122 | - * @param int|string $entry_id | |
| 123 | - * @param array $values Either indexed by field ID or field key. | |
| 124 | - * | |
| 125 | - * @return void | |
| 126 | - */ | |
| 127 | 110 | public static function update_entry_metas( $entry_id, $values ) { |
| 128 | 111 | global $wpdb; |
| 129 | 112 | |
| 130 | - $previous_field_ids = FrmDb::get_col( | |
| 131 | - 'frm_item_metas', | |
| 113 | + $prev_values = FrmDb::get_col( | |
| 114 | + $wpdb->prefix . 'frm_item_metas', | |
| 132 | 115 | array( |
| 133 | 116 | 'item_id' => $entry_id, |
| 134 | 117 | 'field_id !' => 0, |
| 135 | 118 | ), |
| @@ -135,69 +118,53 @@ | ||
| 135 | 118 | ), |
| 136 | 119 | 'field_id' |
| 137 | 120 | ); |
| 138 | 121 | |
| 139 | - $values_indexed_by_field_id = array(); | |
| 140 | - | |
| 141 | - foreach ( $values as $field_id_or_key => $meta_value ) { | |
| 142 | - $field_id = $field_id_or_key; | |
| 143 | - $field = null; | |
| 144 | - | |
| 145 | - if ( $field_id_or_key ) { | |
| 146 | - $field = FrmField::getOne( $field_id_or_key ); | |
| 147 | - | |
| 148 | - if ( is_object( $field ) ) { | |
| 149 | - $field_id = $field->id; | |
| 150 | - } | |
| 122 | + foreach ( $values as $field_id => $meta_value ) { | |
| 123 | + $field = false; | |
| 124 | + if ( ! empty( $field_id ) ) { | |
| 125 | + $field = FrmField::getOne( $field_id ); | |
| 151 | 126 | } |
| 152 | 127 | |
| 153 | - $values_indexed_by_field_id[ $field_id ] = $meta_value; | |
| 154 | - | |
| 155 | 128 | self::get_value_to_save( compact( 'field', 'field_id', 'entry_id' ), $meta_value ); |
| 156 | 129 | |
| 157 | - if ( ! $previous_field_ids || ! in_array( $field_id, $previous_field_ids, true ) ) { | |
| 158 | - // If value does not exist, then create it | |
| 159 | - self::add_entry_meta( $entry_id, $field_id, '', $meta_value ); | |
| 160 | - continue; | |
| 161 | - } | |
| 130 | + if ( $prev_values && in_array( $field_id, $prev_values ) ) { | |
| 162 | 131 | |
| 163 | - if ( $meta_value === array() || ( ! is_array( $meta_value ) && trim( $meta_value ) === '' ) ) { | |
| 164 | - // Remove blank fields. | |
| 165 | - unset( $values_indexed_by_field_id[ $field_id ] ); | |
| 132 | + if ( ( is_array( $meta_value ) && empty( $meta_value ) ) || ( ! is_array( $meta_value ) && trim( $meta_value ) == '' ) ) { | |
| 133 | + // remove blank fields | |
| 134 | + unset( $values[ $field_id ] ); | |
| 135 | + } else { | |
| 136 | + // if value exists, then update it | |
| 137 | + self::update_entry_meta( $entry_id, $field_id, '', $meta_value ); | |
| 138 | + } | |
| 166 | 139 | } else { |
| 167 | - // If value exists, then update it | |
| 168 | - self::update_entry_meta( $entry_id, $field_id, '', $meta_value ); | |
| 140 | + // if value does not exist, then create it | |
| 141 | + self::add_entry_meta( $entry_id, $field_id, '', $meta_value ); | |
| 169 | 142 | } |
| 170 | - }//end foreach | |
| 143 | + } | |
| 171 | 144 | |
| 172 | - if ( ! $previous_field_ids ) { | |
| 145 | + if ( empty( $prev_values ) ) { | |
| 173 | 146 | return; |
| 174 | 147 | } |
| 175 | 148 | |
| 176 | - $field_ids_to_remove = array_diff( $previous_field_ids, array_keys( $values_indexed_by_field_id ) ); | |
| 149 | + $prev_values = array_diff( $prev_values, array_keys( $values ) ); | |
| 177 | 150 | |
| 178 | - if ( ! $field_ids_to_remove ) { | |
| 151 | + if ( empty( $prev_values ) ) { | |
| 179 | 152 | return; |
| 180 | 153 | } |
| 181 | 154 | |
| 182 | - // Prepare the query | |
| 155 | + // prepare the query | |
| 183 | 156 | $where = array( |
| 184 | 157 | 'item_id' => $entry_id, |
| 185 | - 'field_id' => $field_ids_to_remove, | |
| 158 | + 'field_id' => $prev_values, | |
| 186 | 159 | ); |
| 187 | 160 | FrmDb::get_where_clause_and_values( $where ); |
| 188 | 161 | |
| 189 | 162 | // Delete any leftovers |
| 190 | - $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_item_metas ' . $where['where'], $where['values'] ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 163 | + $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_item_metas ' . $where['where'], $where['values'] ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 191 | 164 | self::clear_cache(); |
| 192 | 165 | } |
| 193 | 166 | |
| 194 | - /** | |
| 195 | - * @param int $old_id | |
| 196 | - * @param int $new_id | |
| 197 | - * | |
| 198 | - * @return void | |
| 199 | - */ | |
| 200 | 167 | public static function duplicate_entry_metas( $old_id, $new_id ) { |
| 201 | 168 | $metas = self::get_entry_meta_info( $old_id ); |
| 202 | 169 | |
| 203 | 170 | /** |
| @@ -207,9 +174,8 @@ | ||
| 207 | 174 | * |
| 208 | 175 | * @param array $metas The list of entry meta values. |
| 209 | 176 | */ |
| 210 | 177 | $metas = apply_filters( 'frm_before_duplicate_entry_values', $metas ); |
| 211 | - | |
| 212 | 178 | foreach ( $metas as $meta ) { |
| 213 | 179 | self::add_entry_meta( $new_id, $meta->field_id, '', $meta->meta_value ); |
| 214 | 180 | unset( $meta ); |
| 215 | 181 | } |
| @@ -215,14 +181,8 @@ | ||
| 215 | 181 | } |
| 216 | 182 | self::clear_cache(); |
| 217 | 183 | } |
| 218 | 184 | |
| 219 | - /** | |
| 220 | - * @param int $entry_id | |
| 221 | - * @param int $field_id | |
| 222 | - * | |
| 223 | - * @return false|int | |
| 224 | - */ | |
| 225 | 185 | public static function delete_entry_meta( $entry_id, $field_id ) { |
| 226 | 186 | global $wpdb; |
| 227 | 187 | self::clear_cache(); |
| 228 | 188 | |
| @@ -233,10 +193,8 @@ | ||
| 233 | 193 | * Clear entry meta caching |
| 234 | 194 | * Called when a meta is added or changed |
| 235 | 195 | * |
| 236 | 196 | * @since 2.0.5 |
| 237 | - * | |
| 238 | - * @return void | |
| 239 | 197 | */ |
| 240 | 198 | public static function clear_cache() { |
| 241 | 199 | FrmDb::cache_delete_group( 'frm_entry_meta' ); |
| 242 | 200 | FrmDb::cache_delete_group( 'frm_item_meta' ); |
| @@ -243,27 +201,17 @@ | ||
| 243 | 201 | } |
| 244 | 202 | |
| 245 | 203 | /** |
| 246 | 204 | * @since 2.0.9 |
| 247 | - * | |
| 248 | - * @param stdClass $entry | |
| 249 | - * @param int|string $field_id | |
| 250 | - * | |
| 251 | - * @return mixed | |
| 252 | 205 | */ |
| 253 | 206 | public static function get_meta_value( $entry, $field_id ) { |
| 254 | 207 | if ( isset( $entry->metas ) ) { |
| 255 | - return $entry->metas[ $field_id ] ?? false; | |
| 208 | + return isset( $entry->metas[ $field_id ] ) ? $entry->metas[ $field_id ] : false; | |
| 209 | + } else { | |
| 210 | + return self::get_entry_meta_by_field( $entry->id, $field_id ); | |
| 256 | 211 | } |
| 257 | - return self::get_entry_meta_by_field( $entry->id, $field_id ); | |
| 258 | 212 | } |
| 259 | 213 | |
| 260 | - /** | |
| 261 | - * @param int|object|string $entry_id | |
| 262 | - * @param int|string $field_id This function supports field keys as field id. | |
| 263 | - * | |
| 264 | - * @return mixed | |
| 265 | - */ | |
| 266 | 214 | public static function get_entry_meta_by_field( $entry_id, $field_id ) { |
| 267 | 215 | global $wpdb; |
| 268 | 216 | |
| 269 | 217 | if ( is_object( $entry_id ) ) { |
| @@ -276,38 +224,28 @@ | ||
| 276 | 224 | } |
| 277 | 225 | |
| 278 | 226 | if ( $cached && isset( $cached->metas ) && isset( $cached->metas[ $field_id ] ) ) { |
| 279 | 227 | $result = $cached->metas[ $field_id ]; |
| 228 | + | |
| 280 | 229 | return wp_unslash( $result ); |
| 281 | 230 | } |
| 282 | 231 | |
| 283 | 232 | $get_table = $wpdb->prefix . 'frm_item_metas'; |
| 284 | 233 | $query = array( 'item_id' => $entry_id ); |
| 285 | - | |
| 286 | 234 | if ( is_numeric( $field_id ) ) { |
| 287 | - // Query by field ID. | |
| 288 | 235 | $query['field_id'] = $field_id; |
| 289 | 236 | } else { |
| 290 | - // Query by field key. | |
| 291 | - $get_table .= ' it JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id=fi.id'; | |
| 237 | + $get_table .= ' it LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id=fi.id'; | |
| 292 | 238 | $query['fi.field_key'] = $field_id; |
| 293 | 239 | } |
| 294 | 240 | |
| 295 | - $result = FrmDb::get_var( $get_table, $query, 'meta_value' ); | |
| 296 | - $field_type = FrmField::get_type( $field_id ); | |
| 297 | - FrmFieldsHelper::prepare_field_value( $result, $field_type ); | |
| 241 | + $result = FrmDb::get_var( $get_table, $query, 'meta_value' ); | |
| 242 | + FrmAppHelper::unserialize_or_decode( $result ); | |
| 243 | + $result = wp_unslash( $result ); | |
| 298 | 244 | |
| 299 | - return wp_unslash( $result ); | |
| 245 | + return $result; | |
| 300 | 246 | } |
| 301 | 247 | |
| 302 | - /** | |
| 303 | - * @param int|string $field_id | |
| 304 | - * @param string $order | |
| 305 | - * @param string $limit | |
| 306 | - * @param array $args | |
| 307 | - * | |
| 308 | - * @return array | |
| 309 | - */ | |
| 310 | 248 | public static function get_entry_metas_for_field( $field_id, $order = '', $limit = '', $args = array() ) { |
| 311 | 249 | $defaults = array( |
| 312 | 250 | 'value' => false, |
| 313 | 251 | 'unique' => false, |
| @@ -314,11 +252,13 @@ | ||
| 314 | 252 | 'stripslashes' => true, |
| 315 | 253 | 'is_draft' => false, |
| 316 | 254 | ); |
| 317 | 255 | $args = wp_parse_args( $args, $defaults ); |
| 318 | - $query = array(); | |
| 256 | + | |
| 257 | + $query = array(); | |
| 319 | 258 | self::meta_field_query( $field_id, $order, $limit, $args, $query ); |
| 320 | - $query = implode( ' ', $query ); | |
| 259 | + $query = implode( ' ', $query ); | |
| 260 | + | |
| 321 | 261 | $cache_key = 'entry_metas_for_field_' . $field_id . $order . $limit . FrmAppHelper::maybe_json_encode( $args ); |
| 322 | 262 | $values = FrmDb::check_cache( $cache_key, 'frm_entry', $query, 'get_col' ); |
| 323 | 263 | |
| 324 | 264 | if ( ! $args['stripslashes'] ) { |
| @@ -334,15 +274,10 @@ | ||
| 334 | 274 | return wp_unslash( $values ); |
| 335 | 275 | } |
| 336 | 276 | |
| 337 | 277 | /** |
| 338 | - * @param int|string $field_id | |
| 339 | - * @param string $order | |
| 340 | - * @param string $limit | |
| 341 | - * @param array $args | |
| 342 | - * @param array $query | |
| 343 | - * | |
| 344 | - * @return void | |
| 278 | + * @param string $order | |
| 279 | + * @param string $limit | |
| 345 | 280 | */ |
| 346 | 281 | private static function meta_field_query( $field_id, $order, $limit, $args, array &$query ) { |
| 347 | 282 | global $wpdb; |
| 348 | 283 | $query[] = 'SELECT'; |
| @@ -365,40 +300,24 @@ | ||
| 365 | 300 | |
| 366 | 301 | if ( $args['value'] ) { |
| 367 | 302 | $query[] = $wpdb->prepare( ' AND meta_value=%s', $args['value'] ); |
| 368 | 303 | } |
| 369 | - | |
| 370 | 304 | $query[] = $order . $limit; |
| 371 | 305 | } |
| 372 | 306 | |
| 373 | - /** | |
| 374 | - * @param int|string $entry_id | |
| 375 | - * | |
| 376 | - * @return array | |
| 377 | - */ | |
| 378 | 307 | public static function get_entry_meta_info( $entry_id ) { |
| 379 | 308 | return FrmDb::get_results( 'frm_item_metas', array( 'item_id' => $entry_id ) ); |
| 380 | 309 | } |
| 381 | 310 | |
| 382 | - /** | |
| 383 | - * @param array $where | |
| 384 | - * @param string $order_by | |
| 385 | - * @param string $limit | |
| 386 | - * @param bool $stripslashes | |
| 387 | - * | |
| 388 | - * @return mixed | |
| 389 | - */ | |
| 390 | 311 | public static function getAll( $where = array(), $order_by = '', $limit = '', $stripslashes = false ) { |
| 391 | 312 | global $wpdb; |
| 392 | - // phpcs:disable Generic.WhiteSpace.ScopeIndent | |
| 393 | 313 | $query = 'SELECT it.*, fi.type as field_type, fi.field_key as field_key, |
| 394 | 314 | fi.required as required, fi.form_id as field_form_id, fi.name as field_name, fi.options as fi_options |
| 395 | 315 | FROM ' . $wpdb->prefix . 'frm_item_metas it LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id=fi.id' . |
| 396 | 316 | FrmDb::prepend_and_or_where( ' WHERE ', $where ) . $order_by . $limit; |
| 397 | - // phpcs:enable Generic.WhiteSpace.ScopeIndent | |
| 398 | 317 | |
| 399 | 318 | $cache_key = 'all_' . FrmAppHelper::maybe_json_encode( $where ) . $order_by . $limit; |
| 400 | - $results = FrmDb::check_cache( $cache_key, 'frm_entry', $query, $limit === ' LIMIT 1' ? 'get_row' : 'get_results' ); | |
| 319 | + $results = FrmDb::check_cache( $cache_key, 'frm_entry', $query, ( $limit == ' LIMIT 1' ? 'get_row' : 'get_results' ) ); | |
| 401 | 320 | |
| 402 | 321 | if ( ! $results || ! $stripslashes ) { |
| 403 | 322 | return $results; |
| 404 | 323 | } |
| @@ -411,17 +330,8 @@ | ||
| 411 | 330 | |
| 412 | 331 | return $results; |
| 413 | 332 | } |
| 414 | 333 | |
| 415 | - /** | |
| 416 | - * @param array|string $where | |
| 417 | - * @param string $order_by | |
| 418 | - * @param string $limit | |
| 419 | - * @param bool $unique | |
| 420 | - * @param array $args | |
| 421 | - * | |
| 422 | - * @return array|string|null | |
| 423 | - */ | |
| 424 | 334 | public static function getEntryIds( $where = array(), $order_by = '', $limit = '', $unique = true, $args = array() ) { |
| 425 | 335 | $defaults = array( |
| 426 | 336 | 'is_draft' => false, |
| 427 | 337 | 'user_id' => '', |
| @@ -427,11 +337,13 @@ | ||
| 427 | 337 | 'user_id' => '', |
| 428 | 338 | 'group_by' => '', |
| 429 | 339 | ); |
| 430 | 340 | $args = wp_parse_args( $args, $defaults ); |
| 431 | - $query = array(); | |
| 341 | + | |
| 342 | + $query = array(); | |
| 432 | 343 | self::get_ids_query( $where, $order_by, $limit, $unique, $args, $query ); |
| 433 | - $query = implode( ' ', $query ); | |
| 344 | + $query = implode( ' ', $query ); | |
| 345 | + | |
| 434 | 346 | $cache_key = 'ids_' . FrmAppHelper::maybe_json_encode( $where ) . $order_by . 'l' . $limit . 'u' . $unique . FrmAppHelper::maybe_json_encode( $args ); |
| 435 | 347 | $type = 'get_' . ( ' LIMIT 1' === $limit ? 'var' : 'col' ); |
| 436 | 348 | return FrmDb::check_cache( $cache_key, 'frm_entry', $query, $type ); |
| 437 | 349 | } |
| @@ -441,9 +353,8 @@ | ||
| 441 | 353 | * If a child entry id is matched, its parent will be returned in its place |
| 442 | 354 | * |
| 443 | 355 | * @param array $query |
| 444 | 356 | * @param array $args |
| 445 | - * | |
| 446 | 357 | * @return array |
| 447 | 358 | */ |
| 448 | 359 | public static function get_top_level_entry_ids( $query, $args ) { |
| 449 | 360 | $args['return_parent_id_if_0_return_id'] = true; |
| @@ -450,54 +361,13 @@ | ||
| 450 | 361 | return self::getEntryIds( $query, '', '', true, $args ); |
| 451 | 362 | } |
| 452 | 363 | |
| 453 | 364 | /** |
| 454 | - * Returns true if the where clause refers to a field table column that is not form_id. It also updates | |
| 455 | - * the where clause to refer to the entry table for form_id if fields table should not be joined. | |
| 456 | - * | |
| 457 | - * @since 6.16.1 | |
| 458 | - * | |
| 459 | - * @param array|string $where | |
| 460 | - * | |
| 461 | - * @return bool | |
| 365 | + * @param string|array $where | |
| 366 | + * @param string $order_by | |
| 367 | + * @param string $limit | |
| 462 | 368 | */ |
| 463 | - private static function should_join_fields_table( &$where ) { | |
| 464 | - if ( is_string( $where ) ) { | |
| 465 | - if ( preg_match( '/\bfi\.(?!form_id)\w+/i', $where ) ) { | |
| 466 | - return true; | |
| 467 | - } | |
| 468 | - | |
| 469 | - $where = str_ireplace( 'fi.form_id', 'e.form_id', $where ); | |
| 470 | - return false; | |
| 471 | - } | |
| 472 | - | |
| 473 | - $where_fields = array_keys( $where ); | |
| 474 | - | |
| 475 | - foreach ( $where_fields as $where_field ) { | |
| 476 | - if ( str_starts_with( $where_field, 'fi.' ) && 'fi.form_id' !== $where_field ) { | |
| 477 | - return true; | |
| 478 | - } | |
| 479 | - } | |
| 480 | - | |
| 481 | - if ( isset( $where['fi.form_id'] ) ) { | |
| 482 | - $where['e.form_id'] = $where['fi.form_id']; | |
| 483 | - unset( $where['fi.form_id'] ); | |
| 484 | - } | |
| 485 | - | |
| 486 | - return false; | |
| 487 | - } | |
| 488 | - | |
| 489 | - /** | |
| 490 | - * @param array|string $where | |
| 491 | - * @param string $order_by | |
| 492 | - * @param string $limit | |
| 493 | - * @param bool $unique | |
| 494 | - * @param array $args | |
| 495 | - * @param array $query | |
| 496 | - * | |
| 497 | - * @return void | |
| 498 | - */ | |
| 499 | - private static function get_ids_query( $where, $order_by, $limit, $unique, $args, array &$query ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh, SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 369 | + private static function get_ids_query( $where, $order_by, $limit, $unique, $args, array &$query ) { | |
| 500 | 370 | global $wpdb; |
| 501 | 371 | $query[] = 'SELECT'; |
| 502 | 372 | $defaults = array( |
| 503 | 373 | 'return_parent_id' => false, |
| @@ -516,45 +386,21 @@ | ||
| 516 | 386 | } else { |
| 517 | 387 | $query[] = 'it.item_id'; |
| 518 | 388 | } |
| 519 | 389 | |
| 520 | - $from = 'FROM ' . $wpdb->prefix . 'frm_item_metas it'; | |
| 390 | + $query[] = 'FROM ' . $wpdb->prefix . 'frm_item_metas it LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id=fi.id'; | |
| 521 | 391 | |
| 522 | - if ( self::should_join_fields_table( $where ) || self::should_join_fields_table( $order_by ) ) { | |
| 523 | - $from .= ' LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id=fi.id'; | |
| 524 | - } | |
| 525 | - | |
| 526 | - $query[] = $from; | |
| 527 | 392 | $query[] = 'INNER JOIN ' . $wpdb->prefix . 'frm_items e ON (e.id=it.item_id)'; |
| 528 | - | |
| 529 | 393 | if ( is_array( $where ) ) { |
| 530 | 394 | if ( ! $args['is_draft'] ) { |
| 531 | 395 | $where['e.is_draft'] = 0; |
| 532 | - } elseif ( is_numeric( $args['is_draft'] ) ) { | |
| 533 | - $where['e.is_draft'] = class_exists( 'FrmAbandonmentHooksController', false ) ? absint( $args['is_draft'] ) : 1; | |
| 534 | - } elseif ( 'both' === $args['is_draft'] && class_exists( 'FrmAbandonmentHooksController', false ) ) { | |
| 535 | - $where['e.is_draft'] = array( 0, 1 ); | |
| 536 | - } elseif ( str_contains( $args['is_draft'], ',' ) ) { | |
| 537 | - $is_draft = array_reduce( | |
| 538 | - explode( ',', $args['is_draft'] ), | |
| 539 | - function ( $total, $current ) { | |
| 540 | - if ( is_numeric( $current ) ) { | |
| 541 | - $total[] = absint( $current ); | |
| 542 | - } | |
| 543 | - return $total; | |
| 544 | - }, | |
| 545 | - array() | |
| 546 | - ); | |
| 396 | + } elseif ( $args['is_draft'] == 1 ) { | |
| 397 | + $where['e.is_draft'] = 1; | |
| 398 | + } | |
| 547 | 399 | |
| 548 | - if ( $is_draft ) { | |
| 549 | - $where['e.is_draft'] = $is_draft; | |
| 550 | - } | |
| 551 | - }//end if | |
| 552 | - | |
| 553 | 400 | if ( ! empty( $args['user_id'] ) ) { |
| 554 | 401 | $where['e.user_id'] = $args['user_id']; |
| 555 | 402 | } |
| 556 | - | |
| 557 | 403 | $query[] = FrmDb::prepend_and_or_where( ' WHERE ', $where ) . $order_by . $limit; |
| 558 | 404 | |
| 559 | 405 | if ( $args['group_by'] ) { |
| 560 | 406 | $query[] = ' GROUP BY ' . sanitize_text_field( $args['group_by'] ); |
| @@ -560,16 +406,15 @@ | ||
| 560 | 406 | $query[] = ' GROUP BY ' . sanitize_text_field( $args['group_by'] ); |
| 561 | 407 | } |
| 562 | 408 | |
| 563 | 409 | return; |
| 564 | - }//end if | |
| 410 | + } | |
| 565 | 411 | |
| 566 | 412 | $draft_where = ''; |
| 567 | 413 | $user_where = ''; |
| 568 | - | |
| 569 | 414 | if ( ! $args['is_draft'] ) { |
| 570 | 415 | $draft_where = $wpdb->prepare( ' AND e.is_draft=%d', 0 ); |
| 571 | - } elseif ( $args['is_draft'] == 1 ) { // phpcs:ignore Universal.Operators.StrictComparisons | |
| 416 | + } elseif ( $args['is_draft'] == 1 ) { | |
| 572 | 417 | $draft_where = $wpdb->prepare( ' AND e.is_draft=%d', 1 ); |
| 573 | 418 | } |
| 574 | 419 | |
| 575 | 420 | if ( ! empty( $args['user_id'] ) ) { |
| @@ -575,10 +420,10 @@ | ||
| 575 | 420 | if ( ! empty( $args['user_id'] ) ) { |
| 576 | 421 | $user_where = $wpdb->prepare( ' AND e.user_id=%d', $args['user_id'] ); |
| 577 | 422 | } |
| 578 | 423 | |
| 579 | - if ( str_contains( $where, ' GROUP BY ' ) ) { | |
| 580 | - // Don't inject WHERE filtering after GROUP BY | |
| 424 | + if ( strpos( $where, ' GROUP BY ' ) ) { | |
| 425 | + // don't inject WHERE filtering after GROUP BY | |
| 581 | 426 | $parts = explode( ' GROUP BY ', $where ); |
| 582 | 427 | $where = $parts[0]; |
| 583 | 428 | $where .= $draft_where . $user_where; |
| 584 | 429 | $where .= ' GROUP BY ' . $parts[1]; |
| @@ -589,30 +434,20 @@ | ||
| 589 | 434 | // The query has already been prepared |
| 590 | 435 | $query[] = FrmDb::prepend_and_or_where( ' WHERE ', $where ) . $order_by . $limit; |
| 591 | 436 | } |
| 592 | 437 | |
| 593 | - /** | |
| 594 | - * @param array|string $search | |
| 595 | - * @param int|string $field_id | |
| 596 | - * @param string $operator | |
| 597 | - * | |
| 598 | - * @return array | |
| 599 | - */ | |
| 600 | 438 | public static function search_entry_metas( $search, $field_id, $operator ) { |
| 601 | 439 | $cache_key = 'search_' . FrmAppHelper::maybe_json_encode( $search ) . $field_id . $operator; |
| 602 | 440 | $results = wp_cache_get( $cache_key, 'frm_entry' ); |
| 603 | - | |
| 604 | 441 | if ( false !== $results ) { |
| 605 | 442 | return $results; |
| 606 | 443 | } |
| 607 | 444 | |
| 608 | 445 | global $wpdb; |
| 609 | - | |
| 610 | 446 | if ( is_array( $search ) ) { |
| 611 | 447 | $where = ''; |
| 612 | - | |
| 613 | 448 | foreach ( $search as $field => $value ) { |
| 614 | - if ( $value <= 0 || ! in_array( $field, array( 'year', 'month', 'day' ), true ) ) { | |
| 449 | + if ( $value <= 0 || ! in_array( $field, array( 'year', 'month', 'day' ) ) ) { | |
| 615 | 450 | continue; |
| 616 | 451 | } |
| 617 | 452 | |
| 618 | 453 | switch ( $field ) { |
| @@ -624,21 +459,18 @@ | ||
| 624 | 459 | break; |
| 625 | 460 | case 'day': |
| 626 | 461 | $value = '%' . $value . '%'; |
| 627 | 462 | } |
| 628 | - | |
| 629 | 463 | $where .= $wpdb->prepare( ' meta_value ' . $operator . ' %s and', $value ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 630 | 464 | } |
| 631 | - | |
| 632 | 465 | $where .= $wpdb->prepare( ' field_id=%d', $field_id ); |
| 633 | - $query = 'SELECT DISTINCT item_id FROM ' . $wpdb->prefix . 'frm_item_metas' . FrmDb::prepend_and_or_where( ' WHERE ', $where ); | |
| 466 | + $query = 'SELECT DISTINCT item_id FROM ' . $wpdb->prefix . 'frm_item_metas' . FrmDb::prepend_and_or_where( ' WHERE ', $where ); | |
| 634 | 467 | } else { |
| 635 | - if ( $operator === 'LIKE' ) { | |
| 468 | + if ( $operator == 'LIKE' ) { | |
| 636 | 469 | $search = '%' . $search . '%'; |
| 637 | 470 | } |
| 638 | - | |
| 639 | - $query = $wpdb->prepare( "SELECT DISTINCT item_id FROM {$wpdb->prefix}frm_item_metas WHERE meta_value {$operator} %s and field_id = %d", $search, $field_id ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 640 | - }//end if | |
| 471 | + $query = $wpdb->prepare( "SELECT DISTINCT item_id FROM {$wpdb->prefix}frm_item_metas WHERE meta_value {$operator} %s and field_id = %d", $search, $field_id ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared | |
| 472 | + } | |
| 641 | 473 | |
| 642 | 474 | $results = $wpdb->get_col( $query, 0 ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 643 | 475 | FrmDb::set_cache( $cache_key, $results, 'frm_entry' ); |
| 644 | 476 | |