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