Skip to content

Commit

Permalink
Use the Simply Static plugin for static site generation
Browse files Browse the repository at this point in the history
Playground now supports a loopback request and libcurl, which means we
can use Simply Static to generate a static site.
  • Loading branch information
adamziel committed Apr 29, 2024
1 parent eaa8cfb commit 24bf941
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 193 deletions.
15 changes: 15 additions & 0 deletions build-static-site.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

bun \
--config=/Users/cloudnik/.bunfig.toml \
../playground/packages/playground/cli/src/cli.ts \
run-blueprint \
--mount=./wp-content/plugins/wp-docs-plugin:/wordpress/wp-content/plugins/wp-docs-plugin \
--mount=./wp-content/plugins/export-static-site:/wordpress/wp-content/plugins/export-static-site \
--mount=./wp-content/html-pages:/wordpress/wp-content/html-pages \
--mount=./wp-content/uploads:/wordpress/wp-content/uploads \
--mount=./wp-content/themes/playground-docs:/wordpress/wp-content/themes/playground-docs \
--mount=./output:/output \
--blueprint=./wp-content/blueprint-static-site.json \
--wp=6.5 \
--php=8.0 \
108 changes: 0 additions & 108 deletions generate-static-site.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,24 @@
"slug": "gutenberg"
}
},
{
"step": "installPlugin",
"pluginZipFile": {
"resource": "wordpress.org/plugins",
"slug": "simply-static"
}
},
{
"step": "activatePlugin",
"pluginPath": "wp-docs-plugin/plugin.php"
}
{
"step": "installPlugin",
"pluginZipFile": {
"resource": "wordpress.org/plugins",
"slug": "simply-static"
}
},
{
"step": "activatePlugin",
"pluginPath": "wp-docs-plugin/plugin.php"
},
{
"step": "activatePlugin",
"pluginPath": "export-static-site/export-static-site.php"
},
{
"step": "runPHP",
"code": "<?php require '/wordpress/wp-load.php'; export_static_site( '/output/export.zip' );"
}
]
}
74 changes: 0 additions & 74 deletions wp-content/plugins/wp-docs-plugin/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,6 @@
return array ();
});


/**
* Static site generation endpoints
*/
add_action('parse_request', function ($wp) {
if (str_starts_with($_SERVER['REQUEST_URI'], '/sitemap/')) {
header('Content-Type: application/json');
echo json_encode(get_index_of_all_pages());
exit;
}
if (str_starts_with($_SERVER['REQUEST_URI'], '/zip-wp-files/')) {
zip_wp_files(ABSPATH.'/wp.zip');
rename(ABSPATH.'/wp.zip', ABSPATH.'/wp-content/wp.zip');
header('Content-Type: application/json');
echo json_encode(array('ok'));
exit;
}
if (str_starts_with($_SERVER['REQUEST_URI'], '/logout/')) {
wp_logout();
header('Location: /');
exit;
}
});

function initialize_docs_plugin() {
if(get_option('docs_populated')) {
// Prevent collisions between the initial create_db_pages_from_html_files call
Expand Down Expand Up @@ -203,56 +179,6 @@ function download_docs_callback() {
rename($tmpPath, HTML_PAGES_PATH);
});

function get_index_of_all_pages() {
$frontpage_id = get_option('page_on_front');
$pages = [];
foreach (array('page', 'page') as $page_type) {
$pages = array_merge($pages, get_pages(
array(
'post_status' => 'publish',
'posts_per_page' => -1,
'post_type' => $page_type
)
));
}

$sitemap = [];
// Loop through pages and print URLs
foreach($pages as $page) {
$sitemap[] = [
'url' => get_permalink($page->ID),
'title' => $page->post_title,
'isFrontPage' => $page->ID == $frontpage_id,
];
}
return $sitemap;
}

function zip_wp_files($target_path) {
// Zip wp-content and wp-includes
$zip = new ZipArchive();
if ($zip->open($target_path, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) {
echo 'Failed to create zip file';
exit(1);
}

foreach(['wp-content', 'wp-includes'] as $dir) {
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(ABSPATH . '/' . $dir),
RecursiveIteratorIterator::LEAVES_ONLY
);

foreach ($files as $name => $file) {
if (!$file->isDir() && pathinfo($file->getRealPath(), PATHINFO_EXTENSION) !== 'php') {
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen(ABSPATH));
$zip->addFile($filePath, $relativePath);
}
}
}

$zip->close();
}

function create_db_pages_from_html_files($dir, $parent_id = 0) {
$indexFilePath = $dir . '/index.html';
Expand Down

0 comments on commit 24bf941

Please sign in to comment.