Skip to content

Conversation

@adamziel
Copy link
Collaborator

@adamziel adamziel commented Dec 17, 2024

Exploring a WordPress plugin that can import a tree of static files as WordPress posts and store any database updates back as static files on the disk.

<?php

require_once __DIR__ . '/../../bootstrap.php';
require_once __DIR__ . '/../../../data-liberation-markdown/src/bootstrap.php';

use WordPress\Zip\WP_Zip_Filesystem;
use WordPress\ByteReader\WP_File_Reader;
use WordPress\ByteReader\WP_Remote_File_Ranged_Reader;


$fs = new WP_Zip_Filesystem(
    WP_File_Reader::create(__DIR__.'/../docs.zip')
);


$hierarchy = WP_Filesystem_To_Post_Hierarchy::create(
    $fs,
    array (
        'root_dir' => '/',
        'first_post_id' => 2,
        'filter_pattern' => '#\.md$#',
        'index_file_pattern' => '#^index\.md$#',
    )
);

while($hierarchy->next_post()) {
    $post = $hierarchy->get_current_post();
    if($post['type'] !== 'file') {
        continue;
    }
    
    $converter = new WP_Markdown_To_Blocks( $fs->read_file($post['source_path']) );
    if( false === $converter->convert() ) {
        throw new Exception('Failed to convert Markdown to blocks');
    }
    $entities = new WP_Block_Markup_Entity_Reader(
        $converter->get_block_markup(),
        $converter->get_all_metadata(),
        $post['post_id']
    );
    while($entities->next_entity()) {
        // foreach($entities as $entity) {
        $entity = $entities->get_entity();
        $data = $entity->get_data();
        if( $entity->get_type() === 'post' ) {
            $data['id'] = $post['post_id'];
            $data['guid'] = $post['source_path'];
            $data['post_parent'] = $post['post_parent'];

            $data['post_title'] = $data['post_title'] ?? null;
            if ( ! $data['post_title'] ) {
                $data['post_title'] = WP_Import_Utils::slug_to_title( basename( $post['source_path'] ) );
            }

            var_dump($data);
        }
    }
}

Adds a basic WP_HTML_To_Blocks class that accepts HTML and outputs block markup.

It only considers the markup and won't consider any visual changes introduced via CSS or JavaScript.

A part of #1894

 ## Example

```html
$html = <<<HTML
<meta name="post_title" content="My first post">
<p>Hello <b>world</b>!</p>
HTML;

$converter = new WP_HTML_To_Blocks( $html );
$converter->convert();

var_dump( $converter->get_all_metadata() );
/*
 * array( 'post_title' => array( 'My first post' ) )
 */

var_dump( $converter->get_block_markup() );
/*
 * <!-- wp:paragraph -->
 * <p>Hello <b>world</b>!</p>
 * <!-- /wp:paragraph -->
 */
```

 ## Testing instructions

This PR mostly adds new code. Just confirm the unit tests pass in CI.
@adamziel adamziel changed the title [Data Liberation] Import static files from a local static-pages directory [Data Liberation] Use WordPress for editing static files in a local directory Dec 19, 2024
Base automatically changed from add-epub-reader to trunk January 9, 2025 08:41
@adamziel
Copy link
Collaborator Author

adamziel commented Jan 9, 2025

I'll close this intermediate stacked PR and split the larger work in #2109 into smaller chunks

@adamziel adamziel closed this Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

No open projects
Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants