jetpack
/
vendor
/
wp-php-toolkit
/
data-liberation
/
Importer
/
class-retryfrontloadingiterator.php
class-attachmentdownloader.php
1 week ago
class-attachmentdownloaderevent.php
1 week ago
class-entityimporter.php
1 week ago
class-entityiteratorchain.php
1 week ago
class-filevisitorevent.php
1 week ago
class-importsession.php
1 week ago
class-importutils.php
1 week ago
class-retryfrontloadingiterator.php
1 week ago
class-streamimporter.php
1 week ago
class-retryfrontloadingiterator.php
113 lines
| 1 | <?php |
| 2 | // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 3 | |
| 4 | namespace WordPress\DataLiberation\Importer; |
| 5 | |
| 6 | use Iterator; |
| 7 | use WordPress\DataLiberation\ImportEntity; |
| 8 | |
| 9 | class RetryFrontloadingIterator implements Iterator { |
| 10 | private $import_post_id; |
| 11 | private $last_id_on_page = null; |
| 12 | private $placeholders = array(); |
| 13 | private $current; |
| 14 | private $rewound = true; |
| 15 | |
| 16 | public function __construct( $import_post_id ) { |
| 17 | $this->import_post_id = $import_post_id; |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * @return mixed |
| 22 | */ |
| 23 | public function current() { |
| 24 | if ( ! $this->current ) { |
| 25 | return null; |
| 26 | } |
| 27 | |
| 28 | return new ImportEntity( |
| 29 | 'asset_retry', |
| 30 | array( |
| 31 | 'current_url' => $this->current->meta['current_url'], |
| 32 | 'original_url' => $this->current->meta['original_url'], |
| 33 | ) |
| 34 | ); |
| 35 | } |
| 36 | |
| 37 | public function next(): void { |
| 38 | if ( empty( $this->placeholders ) ) { |
| 39 | $this->query_next_page(); |
| 40 | } |
| 41 | $this->current = array_shift( $this->placeholders ); |
| 42 | $this->rewound = false; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @return mixed |
| 47 | */ |
| 48 | public function key() { |
| 49 | if ( ! $this->current ) { |
| 50 | return null; |
| 51 | } |
| 52 | |
| 53 | return $this->current->id; |
| 54 | } |
| 55 | |
| 56 | public function valid(): bool { |
| 57 | if ( $this->rewound ) { |
| 58 | $this->next(); |
| 59 | } |
| 60 | |
| 61 | return null !== $this->current; |
| 62 | } |
| 63 | |
| 64 | public function rewind(): void { |
| 65 | $this->placeholders = array(); |
| 66 | $this->last_id_on_page = null; |
| 67 | $this->rewound = true; |
| 68 | } |
| 69 | |
| 70 | private function query_next_page() { |
| 71 | global $wpdb; |
| 72 | |
| 73 | $where_clauses = array( |
| 74 | $wpdb->prepare( 'post_type = %s', 'frontloading_stub' ), |
| 75 | $wpdb->prepare( 'post_parent = %d', $this->import_post_id ), |
| 76 | $wpdb->prepare( 'post_status = %s', ImportSession::FRONTLOAD_STATUS_ERROR ), |
| 77 | "pm.meta_key = 'attempts'", |
| 78 | 'pm.meta_value < 3', |
| 79 | ); |
| 80 | |
| 81 | if ( null !== $this->last_id_on_page ) { |
| 82 | $where_clauses[] = $wpdb->prepare( 'p.ID > %d', $this->last_id_on_page ); |
| 83 | } |
| 84 | |
| 85 | $where = implode( ' AND ', $where_clauses ); |
| 86 | $this->placeholders = $wpdb->get_results( |
| 87 | "SELECT p.* FROM {$wpdb->posts} p |
| 88 | INNER JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id |
| 89 | WHERE {$where} |
| 90 | ORDER BY p.ID ASC |
| 91 | LIMIT 100" |
| 92 | ); |
| 93 | $last_placeholder = end( $this->placeholders ); |
| 94 | if ( $last_placeholder ) { |
| 95 | $this->last_id_on_page = $last_placeholder->id; |
| 96 | } |
| 97 | |
| 98 | update_meta_cache( |
| 99 | 'post', |
| 100 | array_map( |
| 101 | function ( $placeholder ) { |
| 102 | return $placeholder->id; |
| 103 | }, |
| 104 | $this->placeholders |
| 105 | ) |
| 106 | ); |
| 107 | |
| 108 | foreach ( $this->placeholders as $placeholder ) { |
| 109 | $placeholder->meta = get_all_post_meta_flat( $placeholder->id ); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 |