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