Skip to content

Commit 5f8e029

Browse files
authored
Make static WordPress exports public (#11)
1 parent 1cbb22e commit 5f8e029

7 files changed

Lines changed: 57 additions & 12 deletions

File tree

‎README.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ Spacefast adds a delivery step after Simply Static has generated and delivered
3434
its files but before cleanup. The step hashes the generated bytes, declares a
3535
full export as an exact snapshot through Spacefast's normal version API,
3636
uploads only the files Spacefast requests, waits for activation, and records
37-
whether that version became live. Partial, update, build, and 404-only exports
37+
whether that version became live. Static WordPress exports are public by
38+
default; an explicit `sf.jsonc` in the export is preserved. Partial, update,
39+
build, and 404-only exports
3840
are additive so they cannot accidentally delete omitted site files. After a
3941
deletion, the plugin blocks partial publishing until a full export removes old
4042
files safely. Upload transfer is paged and processed one file

‎includes/class-spacefast-client.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,9 @@ public function upload_static_file( array $target, string $file_path ): array {
351351
}
352352

353353
public static function content_type_for_file( string $file_path ): string {
354+
if ( 'jsonc' === strtolower( (string) pathinfo( $file_path, PATHINFO_EXTENSION ) ) ) {
355+
return 'application/json';
356+
}
354357
$filetype = wp_check_filetype( $file_path );
355358
$type = is_array( $filetype ) ? (string) ( $filetype['type'] ?? '' ) : '';
356359
return '' !== $type ? $type : 'application/octet-stream';

‎includes/class-spacefast-static-publisher.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ public static function step(
123123
*/
124124
public static function manifest( string $archive_dir ): array {
125125
$root = self::archive_root( $archive_dir );
126+
self::ensure_public_config( $root );
126127
$files = array();
127128
$iterator = new RecursiveIteratorIterator(
128129
new RecursiveDirectoryIterator( $root, FilesystemIterator::SKIP_DOTS )
@@ -162,6 +163,20 @@ public static function reset(): void {
162163
delete_option( self::OPTION );
163164
}
164165

166+
private static function ensure_public_config( string $root ): void {
167+
$config = $root . DIRECTORY_SEPARATOR . 'sf.jsonc';
168+
if ( file_exists( $config ) || is_link( $config ) ) {
169+
if ( ! is_file( $config ) || is_link( $config ) || ! is_readable( $config ) ) {
170+
throw new RuntimeException( 'The generated Spacefast configuration is not a readable file.' );
171+
}
172+
return;
173+
}
174+
$written = file_put_contents( $config, "{\n \"access\": \"public\"\n}\n", LOCK_EX );
175+
if ( false === $written ) {
176+
throw new RuntimeException( 'The generated export could not be made public on Spacefast.' );
177+
}
178+
}
179+
165180
private static function archive_root( string $archive_dir ): string {
166181
$root = realpath( $archive_dir );
167182
if ( false === $root || ! is_dir( $root ) || ! is_readable( $root ) ) {

‎readme.txt‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Contributors: spacefast
33
Tags: static site, headless cms, simply static, deployment
44
Requires at least: 6.5
55
Requires PHP: 8.1
6-
Stable tag: 0.5.5
6+
Stable tag: 0.5.6
77
License: GPLv2 or later
88

99
Publish a Simply Static export to Spacefast, or rebuild a headless site when WordPress content changes.
@@ -31,6 +31,9 @@ OAuth access is limited to the Team you authorize and the mode you choose. WordP
3131

3232
== Changelog ==
3333

34+
= 0.5.6 =
35+
* Make static WordPress exports publicly readable by default while preserving an explicit sf.jsonc access policy.
36+
3437
= 0.5.5 =
3538
* Declare and send each exported file's media type so managed hosting accepts binary uploads.
3639

‎spacefast-wordpress.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Plugin Name: Spacefast
44
* Plugin URI: https://spacefast.com/
55
* Description: Publishes static WordPress exports or rebuilds a headless Spacefast site.
6-
* Version: 0.5.5
6+
* Version: 0.5.6
77
* Update URI: https://github.com/spacefast/wordpress
88
* Requires at least: 6.5
99
* Requires PHP: 8.1
@@ -14,7 +14,7 @@
1414

1515
defined( 'ABSPATH' ) || exit;
1616

17-
define( 'SPACEFAST_WORDPRESS_VERSION', '0.5.5' );
17+
define( 'SPACEFAST_WORDPRESS_VERSION', '0.5.6' );
1818
define( 'SPACEFAST_WORDPRESS_FILE', __FILE__ );
1919

2020
require_once __DIR__ . '/includes/class-spacefast-settings.php';

‎tests/acceptance/wordpress.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ function spacefast_accept( bool $condition, string $message ): void {
113113
);
114114

115115
$plugin = get_plugin_data( WP_PLUGIN_DIR . '/spacefast-wordpress/spacefast-wordpress.php', false, false );
116-
spacefast_accept( '0.5.5' === $plugin['Version'], 'Unexpected plugin version.' );
116+
spacefast_accept( '0.5.6' === $plugin['Version'], 'Unexpected plugin version.' );
117117
spacefast_accept( 'https://github.com/spacefast/wordpress' === $plugin['UpdateURI'], 'Update URI is missing.' );
118118

119119
Spacefast_Settings::disconnect();

‎tests/behavior.php‎

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -847,14 +847,28 @@ static function ( string $url ) use ( &$invalid_receipt_requests ): array {
847847
file_put_contents( $archive . '/assets/app.js', 'console.log("spacefast")' );
848848
$manifest = Spacefast_Static_Publisher::manifest( $archive );
849849
check(
850-
array( 'assets/app.js', 'index.html' ) === array_column( $manifest, 'path' ),
850+
array( 'assets/app.js', 'index.html', 'sf.jsonc' ) === array_column( $manifest, 'path' ),
851851
'builds a sorted snapshot manifest from generated files'
852852
);
853853
check(
854854
hash_file( 'sha256', $archive . '/index.html' ) === $manifest[1]['sha256'],
855855
'hashes the generated bytes instead of trusting exporter metadata'
856856
);
857857
check( 'text/javascript' === $manifest[0]['contentType'], 'declares generated asset media types in the publish manifest' );
858+
check( 'application/json' === $manifest[2]['contentType'], 'declares the generated Spacefast config as JSON' );
859+
check(
860+
'public' === ( json_decode( file_get_contents( $archive . '/sf.jsonc' ), true )['access'] ?? '' ),
861+
'makes a static WordPress export publicly readable by default'
862+
);
863+
$custom_archive = sys_get_temp_dir() . '/spacefast-wordpress-custom-config-' . uniqid();
864+
mkdir( $custom_archive );
865+
file_put_contents( $custom_archive . '/index.html', '<h1>Custom</h1>' );
866+
file_put_contents( $custom_archive . '/sf.jsonc', '{"access":{"public":["/docs/**"]}}' );
867+
Spacefast_Static_Publisher::manifest( $custom_archive );
868+
check(
869+
'{"access":{"public":["/docs/**"]}}' === file_get_contents( $custom_archive . '/sf.jsonc' ),
870+
'preserves an explicit Spacefast access configuration'
871+
);
858872
check( 'image/png' === Spacefast_Client::content_type_for_file( '/tmp/preview.png' ), 'sends binary uploads with their media type' );
859873
check(
860874
'application/octet-stream' === Spacefast_Client::content_type_for_file( '/tmp/unknown.custom' ),
@@ -883,7 +897,7 @@ static function ( string $url, array $args ) use ( &$static_requests ): array {
883897
'id' => 'upl_static',
884898
'mode' => 'dist',
885899
'expiresAt' => '2030-01-01T00:00:00.000Z',
886-
'summary' => array( 'upload' => 1, 'reused' => 0, 'ignored' => 0 ),
900+
'summary' => array( 'upload' => 1, 'reused' => 1, 'ignored' => 0 ),
887901
'targets' => array(
888902
array(
889903
'path' => 'assets/app.js',
@@ -905,10 +919,10 @@ static function ( string $url, array $args ) use ( &$static_requests ): array {
905919
'id' => 'upl_static',
906920
'mode' => 'dist',
907921
'expiresAt' => '2030-01-01T00:00:00.000Z',
908-
'summary' => array( 'upload' => 2, 'reused' => 0, 'ignored' => 0 ),
909-
'targets' => array(
910-
array(
911-
'path' => 'index.html',
922+
'summary' => array( 'upload' => 2, 'reused' => 1, 'ignored' => 0 ),
923+
'targets' => array(
924+
array(
925+
'path' => 'sf.jsonc',
912926
'method' => 'PUT',
913927
'url' => 'https://uploads.example.test/file',
914928
'headers' => array( 'X-Upload' => 'signed' ),
@@ -945,9 +959,13 @@ static function ( string $url, string $method, array $headers, string $file ) us
945959
'attributes the published version to its public WordPress site'
946960
);
947961
check(
948-
str_ends_with( $static_requests[1][3], '/index.html' ),
962+
str_ends_with( $static_requests[1][3], '/sf.jsonc' ),
949963
'uploads only the file named by the opaque server target'
950964
);
965+
check(
966+
'public' === ( json_decode( file_get_contents( $static_requests[1][3] ), true )['access'] ?? '' ),
967+
'uploads the generated public-access declaration with the export'
968+
);
951969
check(
952970
str_ends_with( $static_requests[3][3], '/assets/app.js' ),
953971
'uploads the next opaque target after resuming'
@@ -1034,7 +1052,11 @@ public function run_static_export() { self::$runs++; return true; }
10341052
unlink( $archive . '/assets/app.js' );
10351053
rmdir( $archive . '/assets' );
10361054
unlink( $archive . '/index.html' );
1055+
unlink( $archive . '/sf.jsonc' );
10371056
rmdir( $archive );
1057+
unlink( $custom_archive . '/index.html' );
1058+
unlink( $custom_archive . '/sf.jsonc' );
1059+
rmdir( $custom_archive );
10381060
Spacefast_Static_Publisher::reset();
10391061

10401062
fwrite( STDOUT, "Spacefast WordPress behavior tests: PASS\n" );

0 commit comments

Comments
 (0)