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