|
1 | 1 | <?php
|
2 | 2 |
|
| 3 | +/** |
| 4 | + * The Entity Reader ingests content from a source and breaks it down into |
| 5 | + * individual "entities" that WordPress understands - posts, comments, metadata, etc. |
| 6 | + * |
| 7 | + * The reader implements Iterator so you can easily loop through entities: |
| 8 | + * foreach ($reader as $entity) { ... } |
| 9 | + */ |
3 | 10 | abstract class WP_Entity_Reader implements \Iterator {
|
4 | 11 |
|
| 12 | + /** |
| 13 | + * Gets the current entity being processed. |
| 14 | + * |
| 15 | + * @return WP_Imported_Entity|false The current entity, or false if none available |
| 16 | + */ |
5 | 17 | abstract public function get_entity();
|
6 |
| - abstract public function next_entity(); |
7 | 18 |
|
8 | 19 | /**
|
9 |
| - * Checks if processing is finished. |
| 20 | + * Advances to the next entity in the source content. |
10 | 21 | *
|
11 |
| - * @since WP_VERSION |
| 22 | + * This is where each data source implements its own logic for parsing the bytes |
| 23 | + * and extracting the next meaningful piece of content. |
12 | 24 | *
|
13 |
| - * @return bool Whether processing is finished. |
| 25 | + * @return bool Whether we successfully moved to the next entity |
| 26 | + */ |
| 27 | + abstract public function next_entity(); |
| 28 | + |
| 29 | + /** |
| 30 | + * Checks if we've processed everything from the source. |
| 31 | + * |
| 32 | + * @return bool Whether we've processed everything from the source |
14 | 33 | */
|
15 | 34 | abstract public function is_finished(): bool;
|
16 | 35 |
|
17 | 36 | /**
|
18 |
| - * Gets the last error that occurred. |
| 37 | + * Gets any error that occurred during processing. |
19 | 38 | *
|
20 |
| - * @since WP_VERSION |
| 39 | + * Readers should use this to report issues like invalid source content |
| 40 | + * or parsing failures. |
21 | 41 | *
|
22 |
| - * @return string|null The error message, or null if no error occurred. |
| 42 | + * @since WP_VERSION |
| 43 | + * @return string|null Error message if something went wrong, null otherwise |
23 | 44 | */
|
24 | 45 | abstract public function get_last_error(): ?string;
|
25 | 46 |
|
26 | 47 | /**
|
27 |
| - * Returns a cursor that can be used to restore the reader's state. |
| 48 | + * Returns a cursor position that can be used to resume processing later. |
28 | 49 | *
|
29 |
| - * @TODO: Define a general interface for entity readers. |
| 50 | + * This allows for processing large imports in chunks without losing your place. |
| 51 | + * Not all readers support this yet. |
30 | 52 | *
|
31 |
| - * @return string |
| 53 | + * @TODO: Define a general interface for entity readers. |
| 54 | + * @return string Position marker for resuming later |
32 | 55 | */
|
33 | 56 | public function get_reentrancy_cursor() {
|
34 | 57 | return '';
|
35 | 58 | }
|
36 | 59 |
|
| 60 | + // The iterator interface: |
| 61 | + |
37 | 62 | public function current(): object {
|
38 | 63 | if ( null === $this->get_entity() && ! $this->is_finished() && ! $this->get_last_error() ) {
|
39 | 64 | $this->next();
|
|
0 commit comments