Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Data Liberation] Use WordPress for editing static files in a local directory #2098

Draft
wants to merge 15 commits into
base: add-epub-reader
Choose a base branch
from

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);
        }
    }
}

@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: In progress
Development

Successfully merging this pull request may close these issues.

1 participant