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