← All changes
|
jetpack_vendor/automattic/jetpack-sync/src/class-queue.php
+63
-16
12.5.2
→
16.3-a.1
View file →
| @@ -120,9 +120,9 @@ | ||
| 120 | 120 | return true; |
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | /** |
| 124 | - * Get the front-most item on the queue without checking it out. | |
| 124 | + * Get the front-most items on the queue without checking them out. | |
| 125 | 125 | * |
| 126 | 126 | * @param int $count Number of items to return when looking at the items. |
| 127 | 127 | * |
| 128 | 128 | * @return array |
| @@ -136,8 +136,24 @@ | ||
| 136 | 136 | return array(); |
| 137 | 137 | } |
| 138 | 138 | |
| 139 | 139 | /** |
| 140 | + * Get the last-added items on the queue without checking them out. | |
| 141 | + * | |
| 142 | + * @param int $count Number of items to return when looking at the items. | |
| 143 | + * | |
| 144 | + * @return array | |
| 145 | + */ | |
| 146 | + public function peek_newest( $count = 1 ) { | |
| 147 | + $items = $this->fetch_items( $count, 'DESC' ); | |
| 148 | + if ( $items ) { | |
| 149 | + return Utils::get_item_values( $items ); | |
| 150 | + } | |
| 151 | + | |
| 152 | + return array(); | |
| 153 | + } | |
| 154 | + | |
| 155 | + /** | |
| 140 | 156 | * Gets items with particular IDs. |
| 141 | 157 | * |
| 142 | 158 | * @param array $item_ids Array of item IDs to retrieve. |
| 143 | 159 | * |
| @@ -198,9 +214,9 @@ | ||
| 198 | 214 | * Used to checkout the queue. |
| 199 | 215 | * |
| 200 | 216 | * @param int $buffer_size Size of the buffer to checkout. |
| 201 | 217 | * |
| 202 | - * @return Automattic\Jetpack\Sync\Queue_Buffer|bool|int|\WP_Error | |
| 218 | + * @return \Automattic\Jetpack\Sync\Queue_Buffer|bool|int|\WP_Error | |
| 203 | 219 | */ |
| 204 | 220 | public function checkout( $buffer_size ) { |
| 205 | 221 | if ( $this->get_checkout_id() ) { |
| 206 | 222 | return new WP_Error( 'unclosed_buffer', 'There is an unclosed buffer' ); |
| @@ -244,11 +260,11 @@ | ||
| 244 | 260 | ); |
| 245 | 261 | } |
| 246 | 262 | |
| 247 | 263 | /** |
| 248 | - * Pop elements from the queue. | |
| 264 | + * Remove the oldest items from the queue. | |
| 249 | 265 | * |
| 250 | - * @param int $limit Number of items to pop from the queue. | |
| 266 | + * @param int $limit Number of items to remove from the queue. | |
| 251 | 267 | * |
| 252 | 268 | * @return array|object|null |
| 253 | 269 | */ |
| 254 | 270 | public function pop( $limit ) { |
| @@ -253,8 +269,12 @@ | ||
| 253 | 269 | */ |
| 254 | 270 | public function pop( $limit ) { |
| 255 | 271 | $items = $this->fetch_items( $limit ); |
| 256 | 272 | |
| 273 | + if ( ! $items ) { | |
| 274 | + return array(); | |
| 275 | + } | |
| 276 | + | |
| 257 | 277 | $ids = $this->get_ids( $items ); |
| 258 | 278 | |
| 259 | 279 | $this->delete( $ids ); |
| 260 | 280 | |
| @@ -261,8 +281,29 @@ | ||
| 261 | 281 | return $items; |
| 262 | 282 | } |
| 263 | 283 | |
| 264 | 284 | /** |
| 285 | + * Remove the newest items from the queue. | |
| 286 | + * | |
| 287 | + * @param int $limit Number of items to remove from the queue. | |
| 288 | + * | |
| 289 | + * @return array|object|null | |
| 290 | + */ | |
| 291 | + public function pop_newest( $limit ) { | |
| 292 | + $items = $this->fetch_items( $limit, 'DESC' ); | |
| 293 | + | |
| 294 | + if ( ! $items ) { | |
| 295 | + return array(); | |
| 296 | + } | |
| 297 | + | |
| 298 | + $ids = $this->get_ids( $items ); | |
| 299 | + | |
| 300 | + $this->delete( $ids ); | |
| 301 | + | |
| 302 | + return $items; | |
| 303 | + } | |
| 304 | + | |
| 305 | + /** | |
| 265 | 306 | * Get the items from the queue with a memory limit. |
| 266 | 307 | * |
| 267 | 308 | * This checks out rows until it either empties the queue or hits a certain memory limit |
| 268 | 309 | * it loads the sizes from the DB first so that it doesn't accidentally |
| @@ -325,14 +366,13 @@ | ||
| 325 | 366 | * unserializing all and then merging them with other items. |
| 326 | 367 | * |
| 327 | 368 | * PHPCS ignore is because this is the expected behavior - we're assigning a variable in the condition part of the loop. |
| 328 | 369 | */ |
| 329 | - // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition | |
| 370 | + // phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition | |
| 330 | 371 | while ( ( $current_item = array_shift( $current_items ) ) !== null ) { |
| 331 | 372 | // @codingStandardsIgnoreStart |
| 332 | - $current_item->value = unserialize( $current_item->value ); | |
| 373 | + $current_item->value = @unserialize( $current_item->value ); | |
| 333 | 374 | // @codingStandardsIgnoreEnd |
| 334 | - | |
| 335 | 375 | $items[] = $current_item; |
| 336 | 376 | } |
| 337 | 377 | } |
| 338 | 378 | |
| @@ -347,9 +387,9 @@ | ||
| 347 | 387 | |
| 348 | 388 | /** |
| 349 | 389 | * Check in the queue. |
| 350 | 390 | * |
| 351 | - * @param Automattic\Jetpack\Sync\Queue_Buffer $buffer Queue_Buffer object. | |
| 391 | + * @param \Automattic\Jetpack\Sync\Queue_Buffer $buffer Queue_Buffer object. | |
| 352 | 392 | * |
| 353 | 393 | * @return bool|\WP_Error |
| 354 | 394 | */ |
| 355 | 395 | public function checkin( $buffer ) { |
| @@ -366,10 +406,10 @@ | ||
| 366 | 406 | |
| 367 | 407 | /** |
| 368 | 408 | * Close the buffer. |
| 369 | 409 | * |
| 370 | - * @param Automattic\Jetpack\Sync\Queue_Buffer $buffer Queue_Buffer object. | |
| 371 | - * @param null|array $ids_to_remove Ids to remove from the queue. | |
| 410 | + * @param \Automattic\Jetpack\Sync\Queue_Buffer $buffer Queue_Buffer object. | |
| 411 | + * @param null|array $ids_to_remove Ids to remove from the queue. | |
| 372 | 412 | * |
| 373 | 413 | * @return bool|\WP_Error |
| 374 | 414 | */ |
| 375 | 415 | public function close( $buffer, $ids_to_remove = null ) { |
| @@ -520,11 +560,14 @@ | ||
| 520 | 560 | ) |
| 521 | 561 | ); |
| 522 | 562 | |
| 523 | 563 | if ( $checkout_value ) { |
| 524 | - list( $checkout_id, $timestamp ) = explode( ':', $checkout_value ); | |
| 525 | - if ( (int) $timestamp > time() ) { | |
| 526 | - return $checkout_id; | |
| 564 | + $parts = explode( ':', $checkout_value, 2 ); | |
| 565 | + if ( count( $parts ) === 2 ) { | |
| 566 | + list( $checkout_id, $timestamp ) = $parts; | |
| 567 | + if ( (int) $timestamp > time() ) { | |
| 568 | + return $checkout_id; | |
| 569 | + } | |
| 527 | 570 | } |
| 528 | 571 | } |
| 529 | 572 | |
| 530 | 573 | return false; |
| @@ -609,14 +652,18 @@ | ||
| 609 | 652 | /** |
| 610 | 653 | * Return the items in the queue. |
| 611 | 654 | * |
| 612 | 655 | * @param null|int $limit Limit to the number of items we fetch at once. |
| 656 | + * @param string $order Sort direction for the items. Accepts 'ASC' or 'DESC'. | |
| 657 | + * Any other value will be treated as 'ASC'. | |
| 613 | 658 | * |
| 614 | 659 | * @return array|object|null |
| 615 | 660 | */ |
| 616 | - private function fetch_items( $limit = null ) { | |
| 617 | - $items = $this->queue_storage->fetch_items( $limit ); | |
| 661 | + private function fetch_items( $limit = null, $order = 'ASC' ) { | |
| 662 | + $order = 'DESC' === $order ? 'DESC' : 'ASC'; | |
| 618 | 663 | |
| 664 | + $items = $this->queue_storage->fetch_items( $limit, $order ); | |
| 665 | + | |
| 619 | 666 | return $this->unserialize_values( $items ); |
| 620 | 667 | } |
| 621 | 668 | |
| 622 | 669 | /** |
| @@ -652,9 +699,9 @@ | ||
| 652 | 699 | |
| 653 | 700 | /** |
| 654 | 701 | * Return true if the buffer is still valid or an Error other wise. |
| 655 | 702 | * |
| 656 | - * @param Automattic\Jetpack\Sync\Queue_Buffer $buffer The Queue_Buffer. | |
| 703 | + * @param \Automattic\Jetpack\Sync\Queue_Buffer $buffer The Queue_Buffer. | |
| 657 | 704 | * |
| 658 | 705 | * @return bool|WP_Error |
| 659 | 706 | */ |
| 660 | 707 | private function validate_checkout( $buffer ) { |