Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions includes/class-spacefast-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ public function list_spaces(): array {
return $this->list_all( '/v1/spaces' );
}

/** @return array{ok:bool,retryable:bool,code:string,message:string,data:array<string,mixed>} */
public function create_space( string $team_id, string $title ): array {
return $this->request(
'POST',
'/v1/spaces',
array(
'teamId' => $team_id,
'title' => $title,
)
);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

/** @return array{ok:bool,retryable:bool,code:string,message:string,data:array<string,mixed>} */
private function list_all( string $path ): array {
$items = array();
Expand Down
9 changes: 3 additions & 6 deletions includes/class-spacefast-oauth.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ public function __construct( ?callable $transport = null ) {

/** @return array<int,string> */
public static function scopes( string $mode ): array {
return array(
'teams:read',
'spaces:read',
Spacefast_Settings::MODE_STATIC === $mode ? 'spaces:publish' : 'builds:trigger',
'offline_access',
);
return Spacefast_Settings::MODE_STATIC === $mode
? array( 'teams:read', 'spaces:read', 'spaces:write', 'spaces:publish', 'offline_access' )
Comment thread
batuhan marked this conversation as resolved.
Resolved
: array( 'teams:read', 'spaces:read', 'builds:trigger', 'offline_access' );
}

public static function callback_url(): string {
Expand Down
70 changes: 63 additions & 7 deletions includes/class-spacefast-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public static function boot(): void {
add_action( 'admin_init', array( __CLASS__, 'oauth_callback' ) );
add_action( 'admin_post_spacefast_wordpress_oauth_start', array( __CLASS__, 'oauth_start' ) );
add_action( 'admin_post_spacefast_wordpress_select_space', array( __CLASS__, 'select_space' ) );
add_action( 'admin_post_spacefast_wordpress_create_space', array( __CLASS__, 'create_space' ) );
add_action( 'admin_post_spacefast_wordpress_change_space', array( __CLASS__, 'change_space' ) );
add_action( 'admin_post_spacefast_wordpress_disconnect', array( __CLASS__, 'disconnect' ) );
add_action( 'admin_post_spacefast_wordpress_test', array( __CLASS__, 'test_connection' ) );
Expand Down Expand Up @@ -149,9 +150,57 @@ public static function select_space(): void {
foreach ( $teams as $candidate ) {
if ( is_array( $candidate ) && hash_equals( (string) ( $candidate['id'] ?? '' ), $team_id ) ) $team = $candidate;
}
$result = self::connect_space( $space, is_array( $team ) ? $team : array() );
self::notice(
$result['ok'] ? 'success' : 'error',
$result['ok'] ? __( 'Spacefast is connected.', 'spacefast-wordpress' ) : $result['message']
);
self::redirect();
}

public static function create_space(): void {
self::assert_admin( 'spacefast_wordpress_create_space' );
if ( Spacefast_Settings::MODE_STATIC !== Spacefast_Settings::mode() ) {
self::notice( 'error', __( 'New headless Spaces need a connected repository. Create one in Spacefast, then choose it here.', 'spacefast-wordpress' ) );
self::redirect();
}
$choices = get_option( Spacefast_OAuth::CHOICES_OPTION, array() );
$teams = is_array( $choices['teams'] ?? null ) ? array_values( $choices['teams'] ) : array();
if ( time() > (int) ( $choices['expires_at'] ?? 0 ) || 1 !== count( $teams ) || ! is_array( $teams[0] ) ) {
self::notice( 'error', __( 'Team authorization expired. Start again.', 'spacefast-wordpress' ) );
self::redirect();
}
$team = $teams[0];
$team_id = (string) ( $team['id'] ?? '' );
$title = sanitize_text_field( wp_unslash( $_POST['space_title'] ?? '' ) );
if ( '' === $title ) $title = sanitize_text_field( (string) get_bloginfo( 'name' ) );
if ( ! preg_match( '/^team_[A-Za-z0-9_-]+$/', $team_id ) || '' === $title ) {
self::notice( 'error', __( 'Spacefast could not determine the Team or Space name.', 'spacefast-wordpress' ) );
self::redirect();
}
$created = ( new Spacefast_Client() )->create_space( $team_id, $title );
$space = $created['data']['space'] ?? null;
if ( ! $created['ok'] || ! is_array( $space ) || ! preg_match( '/^spc_[A-Za-z0-9_-]+$/', (string) ( $space['id'] ?? '' ) ) ) {
self::notice( 'error', $created['ok'] ? __( 'Spacefast returned an invalid Space.', 'spacefast-wordpress' ) : $created['message'] );
self::redirect();
}
$result = self::connect_space( $space, $team );
Comment thread
batuhan marked this conversation as resolved.
Resolved
self::notice(
$result['ok'] ? 'success' : 'error',
$result['ok'] ? __( 'Space created and connected.', 'spacefast-wordpress' ) : $result['message']
);
self::redirect();
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

/**
* @param array<string,mixed> $space Spacefast Space.
* @param array<string,mixed> $team Authorized Team.
* @return array{ok:bool,retryable:bool,code:string,message:string,data:array<string,mixed>}
*/
private static function connect_space( array $space, array $team ): array {
Spacefast_Settings::merge(
array(
'team_id' => $team_id,
'team_id' => (string) ( $space['teamId'] ?? $team['id'] ?? '' ),
'team_name' => (string) ( $team['name'] ?? $space['teamSlug'] ?? '' ),
'team_slug' => (string) ( $team['slug'] ?? $space['teamSlug'] ?? '' ),
'space_id' => (string) $space['id'],
Expand All @@ -167,11 +216,7 @@ public static function select_space(): void {
delete_option( Spacefast_OAuth::CHOICES_OPTION );
self::configure_simply_static();
}
self::notice(
$result['ok'] ? 'success' : 'error',
$result['ok'] ? __( 'Spacefast is connected.', 'spacefast-wordpress' ) : $result['message']
);
self::redirect();
return $result;
}

public static function change_space(): void {
Expand Down Expand Up @@ -647,7 +692,18 @@ public static function render_admin(): void {
<?php submit_button( __( 'Use this Space', 'spacefast-wordpress' ) ); ?>
</form>
<?php else : ?>
<div class="notice notice-error inline"><p><?php esc_html_e( 'No available Spaces were returned. Create a Space in the authorized Team, then reconnect.', 'spacefast-wordpress' ); ?></p></div>
<?php if ( ! $static_mode ) : ?><div class="notice notice-error inline"><p><?php esc_html_e( 'No repository-backed Spaces are available. Create one in Spacefast, connect its repository, then reconnect.', 'spacefast-wordpress' ); ?></p></div><?php endif; ?>
<?php endif; ?>
<?php if ( $static_mode ) : ?>
<h3><?php echo esc_html( $spaces ? __( 'Or create a new Space', 'spacefast-wordpress' ) : __( 'Create your first Space', 'spacefast-wordpress' ) ); ?></h3>
<p><?php esc_html_e( 'Spacefast will create it in the authorized Team and connect this WordPress site immediately.', 'spacefast-wordpress' ); ?></p>
<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" style="max-width:720px">
<input type="hidden" name="action" value="spacefast_wordpress_create_space">
<?php wp_nonce_field( 'spacefast_wordpress_create_space' ); ?>
<label for="spacefast-space-title"><strong><?php esc_html_e( 'Space name', 'spacefast-wordpress' ); ?></strong></label><br>
<input id="spacefast-space-title" name="space_title" type="text" class="regular-text" maxlength="255" required value="<?php echo esc_attr( (string) get_bloginfo( 'name' ) ); ?>">
<?php submit_button( __( 'Create and connect', 'spacefast-wordpress' ) ); ?>
</form>
<?php endif; ?>
<?php self::action_form( 'spacefast_wordpress_disconnect', __( 'Start over', 'spacefast-wordpress' ), 'secondary' ); ?>
<?php else : ?>
Expand Down
6 changes: 5 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: spacefast
Tags: static site, headless cms, simply static, deployment
Requires at least: 6.5
Requires PHP: 8.1
Stable tag: 0.3.2
Stable tag: 0.4.0
License: GPLv2 or later

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

== Changelog ==

= 0.4.0 =
* Create and connect a new Space directly from the Static WordPress setup flow.
* Request Space management permission so the authorized Team can receive the new Space.

= 0.3.2 =
* Preserve the complete WordPress admin callback URL during Spacefast authorization.

Expand Down
4 changes: 2 additions & 2 deletions spacefast-wordpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Spacefast
* Plugin URI: https://spacefast.com/
* Description: Publishes static WordPress exports or rebuilds a headless Spacefast site.
* Version: 0.3.2
* Version: 0.4.0
* Update URI: https://github.com/spacefast/wordpress
* Requires at least: 6.5
* Requires PHP: 8.1
Expand All @@ -14,7 +14,7 @@

defined( 'ABSPATH' ) || exit;

define( 'SPACEFAST_WORDPRESS_VERSION', '0.3.2' );
define( 'SPACEFAST_WORDPRESS_VERSION', '0.4.0' );
define( 'SPACEFAST_WORDPRESS_FILE', __FILE__ );

require_once __DIR__ . '/includes/class-spacefast-settings.php';
Expand Down
8 changes: 6 additions & 2 deletions tests/acceptance/wordpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ function spacefast_accept( bool $condition, string $message ): void {
10 === has_action( 'admin_post_spacefast_wordpress_select_space', array( 'Spacefast_Plugin', 'select_space' ) ),
'Space selection handler is not registered.'
);
spacefast_accept(
10 === has_action( 'admin_post_spacefast_wordpress_create_space', array( 'Spacefast_Plugin', 'create_space' ) ),
'Space creation handler is not registered.'
);
spacefast_accept(
10 === has_action( 'admin_post_spacefast_wordpress_change_space', array( 'Spacefast_Plugin', 'change_space' ) ),
'Space change handler is not registered.'
);
spacefast_accept(
array( 'teams:read', 'spaces:read', 'spaces:publish', 'offline_access' ) === Spacefast_OAuth::scopes( Spacefast_Settings::MODE_STATIC ),
array( 'teams:read', 'spaces:read', 'spaces:write', 'spaces:publish', 'offline_access' ) === Spacefast_OAuth::scopes( Spacefast_Settings::MODE_STATIC ),
'Static mode requested unexpected OAuth scopes.'
);
spacefast_accept(
Expand Down Expand Up @@ -71,7 +75,7 @@ function spacefast_accept( bool $condition, string $message ): void {
);

$plugin = get_plugin_data( WP_PLUGIN_DIR . '/spacefast-wordpress/spacefast-wordpress.php', false, false );
spacefast_accept( '0.3.2' === $plugin['Version'], 'Unexpected plugin version.' );
spacefast_accept( '0.4.0' === $plugin['Version'], 'Unexpected plugin version.' );
spacefast_accept( 'https://github.com/spacefast/wordpress' === $plugin['UpdateURI'], 'Update URI is missing.' );

Spacefast_Settings::disconnect();
Expand Down
30 changes: 28 additions & 2 deletions tests/behavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ function raw_response( int $status, array $data = array() ): array {
}

check(
array( 'teams:read', 'spaces:read', 'spaces:publish', 'offline_access' ) === Spacefast_OAuth::scopes( Spacefast_Settings::MODE_STATIC ),
'static OAuth asks only for Team discovery, Space discovery, publishing, and refresh access'
array( 'teams:read', 'spaces:read', 'spaces:write', 'spaces:publish', 'offline_access' ) === Spacefast_OAuth::scopes( Spacefast_Settings::MODE_STATIC ),
'static OAuth asks for Team discovery, Space creation and discovery, publishing, and refresh access'
);
check(
array( 'teams:read', 'spaces:read', 'builds:trigger', 'offline_access' ) === Spacefast_OAuth::scopes( Spacefast_Settings::MODE_HEADLESS ),
Expand Down Expand Up @@ -272,6 +272,32 @@ function raw_response( int $status, array $data = array() ): array {
);
check( ! Spacefast_Settings::configured(), 'authorization alone is not presented as Connected' );

$create_requests = array();
$creating_client = new Spacefast_Client(
static function ( string $url, array $args ) use ( &$create_requests ): array {
$create_requests[] = array( $url, $args );
return response(
201,
array(
'space' => array(
'id' => 'spc_created',
'teamId' => 'team_demo',
'slug' => 'spacefast-launchpad',
'title' => 'Spacefast Launchpad',
'liveUrl' => 'https://spacefast-launchpad.view.fast',
),
)
);
}
);
$created_space = $creating_client->create_space( 'team_demo', 'Spacefast Launchpad' );
check( true === $created_space['ok'], 'creates a Space with the authorized OAuth token' );
check( '/v1/spaces' === parse_url( $create_requests[0][0], PHP_URL_PATH ), 'uses the canonical Space creation endpoint' );
check(
array( 'teamId' => 'team_demo', 'title' => 'Spacefast Launchpad' ) === json_decode( $create_requests[0][1]['body'], true ),
'creates the Space in the authorized Team with the WordPress site name'
);

$reauthorization = $oauth->begin( Spacefast_Settings::MODE_HEADLESS );
$reauthorization_pending = get_option( Spacefast_OAuth::PENDING_OPTION );
$reauthorized = $oauth->finish( 'replacement-code', (string) $reauthorization_pending['state'] );
Expand Down
Loading