| @@ -195,8 +195,9 @@ | ||
| 195 | 195 | |
| 196 | 196 | $args = array( |
| 197 | 197 | 'per_page' => $per_page, |
| 198 | 198 | 'paged' => $paged, |
| 199 | + // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Meta query is necessary for filtering services by meta fields | |
| 199 | 200 | 'meta_query' => array(), |
| 200 | 201 | ); |
| 201 | 202 | |
| 202 | 203 | if ( ! empty( $request['team_member_id'] ) ) { |
| @@ -216,8 +217,28 @@ | ||
| 216 | 217 | } |
| 217 | 218 | |
| 218 | 219 | $args = apply_filters( 'booktics_service_args', $args, $request ); |
| 219 | 220 | |
| 221 | + // Get service IDs for category BEFORE pagination to ensure correct total count | |
| 222 | + $service_ids_for_category = null; | |
| 223 | + if ( ! empty( $request['category_id'] ) ) { | |
| 224 | + $service_ids_for_category = get_term_meta( absint( $request['category_id'] ), 'booktics_category_services', true ); | |
| 225 | + | |
| 226 | + // If category filter is applied, add post__in to args to filter BEFORE pagination | |
| 227 | + if ( ! empty( $service_ids_for_category ) && is_array( $service_ids_for_category ) ) { | |
| 228 | + $args['post__in'] = $service_ids_for_category; | |
| 229 | + } elseif ( ! empty( $service_ids_for_category ) ) { | |
| 230 | + // If category has no services, return early | |
| 231 | + return $this->response( array( | |
| 232 | + 'items' => array(), | |
| 233 | + 'total' => 0, | |
| 234 | + 'per_page' => $per_page, | |
| 235 | + 'current_page' => $paged, | |
| 236 | + 'last_page' => 0, | |
| 237 | + ) ); | |
| 238 | + } | |
| 239 | + } | |
| 240 | + | |
| 220 | 241 | $data = Service_Model::paginate( $per_page, $paged, $args ); |
| 221 | 242 | |
| 222 | 243 | if ( ! $data ) { |
| 223 | 244 | return $this->error( __( 'No service found', 'booktics' ), 404 ); |
| @@ -222,20 +243,12 @@ | ||
| 222 | 243 | if ( ! $data ) { |
| 223 | 244 | return $this->error( __( 'No service found', 'booktics' ), 404 ); |
| 224 | 245 | } |
| 225 | 246 | |
| 226 | - if ( ! empty( $request['category_id'] ) ) { | |
| 227 | - $service_ids_for_category = get_term_meta( $request['category_id'], 'booktics_category_services', true); | |
| 228 | - } | |
| 229 | - | |
| 230 | 247 | if ( ! empty( $data['items'] ) ) { |
| 231 | 248 | $enhanced_items = array(); |
| 232 | 249 | foreach ( $data['items'] as $service ) { |
| 233 | 250 | $service_array = $service->to_array(); |
| 234 | - | |
| 235 | - if ( isset( $service_ids_for_category ) && ! in_array( $service_array['id'], $service_ids_for_category ) ) { | |
| 236 | - continue; | |
| 237 | - } | |
| 238 | 251 | |
| 239 | 252 | if ( ! empty( $service_array['team_member'] ) ) { |
| 240 | 253 | $team_member_ids = is_array( $service->team_member ) ? array_unique( $service->team_member ) : array( $service->team_member ); |
| 241 | 254 | $service->team_member = $team_member_ids; |
| @@ -265,8 +278,9 @@ | ||
| 265 | 278 | private function get_service_category_ids( int $service_id ): array { |
| 266 | 279 | $args = array( |
| 267 | 280 | 'taxonomy' => 'booktics_service_category', // Category taxonomy |
| 268 | 281 | 'hide_empty' => false, |
| 282 | + // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Meta query is necessary for services by category ID | |
| 269 | 283 | 'meta_query' => array( |
| 270 | 284 | array( |
| 271 | 285 | 'key' => 'booktics_category_services', |
| 272 | 286 | 'value' => sprintf( 'i:%d;', $service_id ), |
| @@ -511,9 +525,9 @@ | ||
| 511 | 525 | * |
| 512 | 526 | * @return WP_HTTP_Response |
| 513 | 527 | */ |
| 514 | 528 | public function delete_items( $request ) { |
| 515 | - $ids = $request['ids']; | |
| 529 | + $ids = isset( $request['ids'] ) ? $request['ids'] : array(); | |
| 516 | 530 | |
| 517 | 531 | if ( ! is_array( $ids ) ) { |
| 518 | 532 | return $this->error( __( 'Ids should be an array of items ids', 'booktics' ) ); |
| 519 | 533 | } |
| @@ -529,13 +543,13 @@ | ||
| 529 | 543 | ++$count; |
| 530 | 544 | } |
| 531 | 545 | } |
| 532 | 546 | |
| 533 | - if ( $count === 0 ) { | |
| 547 | + if ( 0 === $count ) { | |
| 534 | 548 | return $this->error( __( 'No item deleted.', 'booktics' ) ); |
| 535 | 549 | } |
| 536 | - | |
| 537 | - $message = sprintf( 'Deleted %d items of %d', $count, $total ); | |
| 550 | + /* translators: 1: deleted count, 2: total count */ | |
| 551 | + $message = sprintf( __( 'Deleted %1$d items of %2$d', 'booktics' ), $count, $total ); | |
| 538 | 552 | |
| 539 | 553 | return $this->response( array(), $message ); |
| 540 | 554 | } |
| 541 | 555 | |