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