jetpack
/
vendor
/
wp-php-toolkit
/
data-liberation
/
EntityReader
/
class-blockswithmetadataentityreader.php
class-blockswithmetadataentityreader.php
6 days ago
class-databasecontententityreader.php
6 days ago
class-databaserowsentityreader.php
6 days ago
class-entityreaderiterator.php
6 days ago
class-epubentityreader.php
6 days ago
class-filesystementityreader.php
6 days ago
class-htmlentityreader.php
6 days ago
class-wxrentityreader.php
6 days ago
interface-entity-reader.php
6 days ago
class-blockswithmetadataentityreader.php
116 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WordPress\DataLiberation\EntityReader; |
| 4 | |
| 5 | use WordPress\DataLiberation\DataFormatConsumer\BlocksWithMetadata; |
| 6 | use WordPress\DataLiberation\ImportEntity; |
| 7 | use WordPress\DataLiberation\Importer\ImportUtils; |
| 8 | |
| 9 | /** |
| 10 | * Converts a WP_Blocks_With_Metadata object into a stream of WordPress post and post meta entities. |
| 11 | * |
| 12 | * Outputs a single post and a number of post meta entities. |
| 13 | */ |
| 14 | class BlocksWithMetadataEntityReader implements EntityReader { |
| 15 | |
| 16 | protected $block_markup; |
| 17 | protected $metadata; |
| 18 | protected $enqueued_entities = null; |
| 19 | protected $current_entity; |
| 20 | protected $finished = false; |
| 21 | protected $post_id; |
| 22 | |
| 23 | public function __construct( BlocksWithMetadata $blocks_with_meta, $post_id ) { |
| 24 | $this->block_markup = $blocks_with_meta->get_block_markup(); |
| 25 | $this->metadata = $blocks_with_meta->get_all_metadata(); |
| 26 | $this->post_id = $post_id; |
| 27 | } |
| 28 | |
| 29 | public function next_entity() { |
| 30 | if ( $this->finished ) { |
| 31 | return false; |
| 32 | } |
| 33 | |
| 34 | $this->current_entity = null; |
| 35 | |
| 36 | if ( null !== $this->enqueued_entities ) { |
| 37 | if ( 0 === count( $this->enqueued_entities ) ) { |
| 38 | $this->finished = true; |
| 39 | |
| 40 | return false; |
| 41 | } else { |
| 42 | $this->current_entity = array_shift( $this->enqueued_entities ); |
| 43 | |
| 44 | return true; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | $all_metadata = $this->metadata; |
| 49 | $post_fields = array(); |
| 50 | $other_metadata = array(); |
| 51 | |
| 52 | foreach ( $all_metadata as $key => $values ) { |
| 53 | if ( in_array( $key, ImportEntity::POST_FIELDS, true ) ) { |
| 54 | $post_fields[ $key ] = $values[0]; |
| 55 | } else { |
| 56 | $other_metadata[ $key ] = $values[0]; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | $post_fields['post_id'] = $this->post_id; |
| 61 | $post_fields['post_content'] = $this->block_markup; |
| 62 | $post_fields['parsed_metadata'] = $all_metadata; |
| 63 | |
| 64 | // In Markdown, the frontmatter title can be a worse title candidate than |
| 65 | // the first H1 block. In block markup exports, it will be the opposite. |
| 66 | // |
| 67 | // @TODO: Enable the API consumer to customize the title resolution. |
| 68 | if ( ! isset( $post_fields['post_title'] ) ) { |
| 69 | $removed_title = ImportUtils::remove_first_h1_block_from_block_markup( $post_fields['post_content'] ); |
| 70 | if ( false !== $removed_title ) { |
| 71 | $post_fields['post_title'] = $removed_title['h1_content']; |
| 72 | $post_fields['post_content'] = $removed_title['remaining_html']; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // Yield the post entity. |
| 77 | $this->enqueued_entities[] = new ImportEntity( 'post', $post_fields ); |
| 78 | |
| 79 | // Yield all the metadata that don't belong to the post entity. |
| 80 | foreach ( $other_metadata as $key => $value ) { |
| 81 | $this->enqueued_entities[] = new ImportEntity( |
| 82 | 'post_meta', |
| 83 | array( |
| 84 | 'post_id' => $this->post_id, |
| 85 | 'meta_key' => $key, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key |
| 86 | 'meta_value' => $value, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value |
| 87 | ) |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | $this->current_entity = array_shift( $this->enqueued_entities ); |
| 92 | |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | public function get_entity() { |
| 97 | if ( $this->is_finished() ) { |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | return $this->current_entity; |
| 102 | } |
| 103 | |
| 104 | public function is_finished(): bool { |
| 105 | return $this->finished; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @TODO: Implement this |
| 110 | * @return string |
| 111 | */ |
| 112 | public function get_reentrancy_cursor() { |
| 113 | return ''; |
| 114 | } |
| 115 | } |
| 116 |