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-importutils.php
124 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WordPress\DataLiberation\Importer; |
| 4 | |
| 5 | use WordPress\DataLiberation\BlockMarkup\BlockMarkupProcessor; |
| 6 | use WordPress\DataLiberation\DataLiberationHTMLProcessor; |
| 7 | |
| 8 | /** |
| 9 | * A collection of utility functions for importing data into WordPress. |
| 10 | * |
| 11 | * This is a stopgap solution until we have an expressive enough API |
| 12 | * to co-locate these tactical functions with their domain-specific code. |
| 13 | */ |
| 14 | class ImportUtils { |
| 15 | |
| 16 | /** |
| 17 | * Generates a block opener comment with given attributes. |
| 18 | * |
| 19 | * @param string $block_name The name of the block. |
| 20 | * @param array $attrs The attributes of the block. |
| 21 | * |
| 22 | * @return string The block opener. |
| 23 | */ |
| 24 | public static function block_opener( $block_name, $attrs = array() ) { |
| 25 | $template = "<!-- wp:{$block_name} -->"; |
| 26 | $processor = new BlockMarkupProcessor( $template ); |
| 27 | $processor->next_token(); |
| 28 | $processor->set_block_attributes( $attrs ); |
| 29 | |
| 30 | return $processor->get_updated_html(); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Generates a block closer comment. |
| 35 | * |
| 36 | * @param string $block_name The name of the block. |
| 37 | * |
| 38 | * @return string The block closer. |
| 39 | */ |
| 40 | public static function block_closer( $block_name ) { |
| 41 | return "<!-- /wp:{$block_name} -->"; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Convert an array of WP_Block_Object objects to HTML markup. |
| 46 | * |
| 47 | * @param array $blocks The blocks to convert to markup. |
| 48 | * |
| 49 | * @return string The HTML markup. |
| 50 | */ |
| 51 | public static function convert_blocks_to_markup( $blocks ) { |
| 52 | $block_markup = ''; |
| 53 | |
| 54 | foreach ( $blocks as $block ) { |
| 55 | // Allow mixing of inner blocks and content strings. |
| 56 | if ( is_string( $block ) ) { |
| 57 | $block_markup .= $block; |
| 58 | continue; |
| 59 | } |
| 60 | // Start of block comment. |
| 61 | $block_markup .= self::block_opener( $block->block_name, $block->attrs ); |
| 62 | $block_markup .= $block->attrs['content'] ?? ''; |
| 63 | $block_markup .= self::convert_blocks_to_markup( $block->inner_blocks ); |
| 64 | $block_markup .= self::block_closer( $block->block_name ); |
| 65 | } |
| 66 | |
| 67 | return $block_markup; |
| 68 | } |
| 69 | |
| 70 | public static function slug_to_title( $filename ) { |
| 71 | $name = pathinfo( $filename, PATHINFO_FILENAME ); |
| 72 | $name = preg_replace( '/^\d+/', '', $name ); |
| 73 | $name = str_replace( |
| 74 | array( '-', '_' ), |
| 75 | ' ', |
| 76 | $name |
| 77 | ); |
| 78 | $name = ucwords( $name ); |
| 79 | |
| 80 | return $name; |
| 81 | } |
| 82 | |
| 83 | public static function remove_first_h1_block_from_block_markup( $html ) { |
| 84 | $p = DataLiberationHTMLProcessor::create_fragment( $html ); |
| 85 | if ( false === $p->next_tag() ) { |
| 86 | return false; |
| 87 | } |
| 88 | if ( 'H1' !== $p->get_tag() ) { |
| 89 | return false; |
| 90 | } |
| 91 | $depth = $p->get_current_depth(); |
| 92 | $title = ''; |
| 93 | do { |
| 94 | if ( false === $p->next_token() ) { |
| 95 | break; |
| 96 | } |
| 97 | if ( '#text' === $p->get_token_type() ) { |
| 98 | $title .= $p->get_modifiable_text() . ' '; |
| 99 | } |
| 100 | } while ( $p->get_current_depth() > $depth ); |
| 101 | |
| 102 | if ( ! $title ) { |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | // Move past the closing comment. |
| 107 | $p->next_token(); |
| 108 | if ( '#text' === $p->get_token_type() ) { |
| 109 | $p->next_token(); |
| 110 | } |
| 111 | if ( '#comment' !== $p->get_token_type() ) { |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | return array( |
| 116 | 'h1_content' => trim( $title ), |
| 117 | 'remaining_html' => substr( |
| 118 | $html, |
| 119 | $p->get_string_index_after_current_token() |
| 120 | ), |
| 121 | ); |
| 122 | } |
| 123 | } |
| 124 |