← All changes
|
jetpack_vendor/automattic/jetpack-sync/src/sync-queue/class-queue-storage-table.php
+24
-41
13.4.5
→
16.3-a.5
View file →
| @@ -227,57 +227,40 @@ | ||
| 227 | 227 | /** |
| 228 | 228 | * Fetch items from the queue. |
| 229 | 229 | * |
| 230 | 230 | * @param int|null $item_count How many items to fetch from the queue. |
| 231 | - * The parameter is null-able, if no limit on the amount of items. | |
| 231 | + * Null for no limit. | |
| 232 | + * @param string $order Sort direction for the items. Accepts 'ASC' or 'DESC'. | |
| 233 | + * Any other value will be treated as 'ASC'. | |
| 232 | 234 | * |
| 233 | - * @return object[]|null | |
| 235 | + * @return array|object|null Array of result objects on success, or null on failure. | |
| 234 | 236 | */ |
| 235 | - public function fetch_items( $item_count ) { | |
| 237 | + public function fetch_items( $item_count, $order = 'ASC' ) { | |
| 236 | 238 | global $wpdb; |
| 237 | 239 | |
| 238 | - /** | |
| 239 | - * Ignoring the linting warning, as there's still no placeholder replacement for DB field name, | |
| 240 | - * in this case this is `$this->table_name` | |
| 241 | - */ | |
| 242 | - // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared | |
| 240 | + $order = ( 'DESC' === $order ) ? 'DESC' : 'ASC'; | |
| 241 | + $sql_order = "ORDER BY event_id {$order}"; | |
| 243 | 242 | |
| 244 | - // TODO make it more simple for the $item_count | |
| 243 | + $sql = " | |
| 244 | + SELECT | |
| 245 | + event_id AS id, | |
| 246 | + event_payload AS value | |
| 247 | + FROM {$this->table_name} | |
| 248 | + WHERE queue_id LIKE %s | |
| 249 | + {$sql_order} | |
| 250 | + "; | |
| 251 | + | |
| 252 | + $params = array( $this->queue_id ); | |
| 253 | + | |
| 245 | 254 | if ( $item_count ) { |
| 246 | - // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.InterpolatedNotPrepared | |
| 247 | - $items = $wpdb->get_results( | |
| 248 | - $wpdb->prepare( | |
| 249 | - " | |
| 250 | - SELECT | |
| 251 | - event_id AS id, | |
| 252 | - event_payload AS value | |
| 253 | - FROM {$this->table_name} | |
| 254 | - WHERE queue_id LIKE %s | |
| 255 | - ORDER BY event_id ASC | |
| 256 | - LIMIT %d | |
| 257 | - ", | |
| 258 | - $this->queue_id, | |
| 259 | - $item_count | |
| 260 | - ) | |
| 261 | - ); | |
| 262 | - } else { | |
| 263 | - // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.InterpolatedNotPrepared | |
| 264 | - $items = $wpdb->get_results( | |
| 265 | - $wpdb->prepare( | |
| 266 | - " | |
| 267 | - SELECT | |
| 268 | - event_id AS id, | |
| 269 | - event_payload AS value | |
| 270 | - FROM {$this->table_name} | |
| 271 | - WHERE queue_id LIKE %s | |
| 272 | - ORDER BY event_id ASC | |
| 273 | - ", | |
| 274 | - $this->queue_id | |
| 275 | - ) | |
| 276 | - ); | |
| 255 | + $sql .= ' LIMIT %d'; | |
| 256 | + $params[] = $item_count; | |
| 277 | 257 | } |
| 278 | 258 | |
| 279 | - // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared | |
| 259 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 260 | + $items = $wpdb->get_results( | |
| 261 | + $wpdb->prepare( $sql, $params ) // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 262 | + ); | |
| 280 | 263 | |
| 281 | 264 | return $items; |
| 282 | 265 | } |
| 283 | 266 | |