Skip to content

Commit

Permalink
Export and import menu order
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Apr 2, 2024
1 parent c21dd16 commit c05fe8c
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
<h1>Another Top-level page</h1>

<!-- wp:image {"id":11,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image size-full"><img src="https://playground.internal/wp-content/uploads/2024/04/L-Misc2.jpg" alt="" class="wp-image-11"/></figure>
<!-- wp:paragraph -->
<p>How-to Guides 123</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>An image</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":69,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image size-full"><img src="https://playground.internal/wp-content/uploads/2024/04/L-Misc2.jpg" alt="" class="wp-image-69"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>How-to Guides<br>In this article<br>Creating blocks<br>Extending blocks<br>Extending the Editor UI<br>Meta boxes<br>Theme support<br>Autocomplete<br>Block parsing and serialization<br>The new editor is highly flexible, like most of WordPress. You can build custom blocks, modify the editor’s appearance, add special plugins, and much more.</p>
<p><br>In this article<br>Creating blocks<br>Extending blocks<br>Extending the Editor UI<br>Meta boxes<br>Theme support<br>Autocomplete<br>Block parsing and serialization<br>The new editor is highly flexible, like most of WordPress. You can build custom blocks, modify the editor’s appearance, add special plugins, and much more.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<h1>Welcome to Playground</h1>
<h1>Welcome to Playground 1</h1>

<!-- wp:paragraph -->
<p>This is content of my page</p>
Expand Down
28 changes: 18 additions & 10 deletions wp-content/plugins/wp-docs-plugin/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,20 @@ function create_db_doc_page_from_html_file(SplFileInfo $file, $parent_id = 0) {
}

// Insert the content as a WordPress doc_page
return wp_insert_post(array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'publish',
'post_author' => get_current_user_id(),
'post_type' => 'doc_page',
'post_parent' => $parent_id,
));
$post_data = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'publish',
'post_author' => get_current_user_id(),
'post_type' => 'doc_page',
'post_parent' => $parent_id,
);

if(preg_match('/^(\d+)_/', $file->getFilename(), $matches)) {
$post_data['menu_order'] = $matches[1];
}

return wp_insert_post($post_data);
}

function delete_db_doc_pages() {
Expand Down Expand Up @@ -272,7 +278,9 @@ function save_db_doc_pages_as_html($path, $parent_id = 0) {
while ($pages->have_posts()) {
$pages->the_post();
$page_id = get_the_ID();
$page = get_post($page_id);
$title = sanitize_title(get_the_title());

$content = '<h1>' . esc_html(get_the_title()) . "</h1>\n\n" . get_the_content();
// Replace current site URL with a placeholder URL for the export.
// @TODO: This is very naive, let's actually parse the block
Expand All @@ -291,14 +299,14 @@ function save_db_doc_pages_as_html($path, $parent_id = 0) {
}

if (!empty($child_pages)) {
$new_parent = $path . '/' . $title;
$new_parent = $path . '/' . $page->menu_order . '_' . $title;
if (!file_exists($new_parent)) {
mkdir($new_parent, 0777, true);
}
file_put_contents($new_parent . '/index.html', $content);
save_db_doc_pages_as_html($new_parent, $page_id);
} else {
file_put_contents($path . '/' . $title . '.html', $content);
file_put_contents($path . '/' . $page->menu_order . '_' . $title . '.html', $content);
}
}
}
Expand Down

0 comments on commit c05fe8c

Please sign in to comment.