Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 32 additions & 0 deletions includes/class-pattern-builder-cloud-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function register_routes() {
'/cloud/upload' => array( 'POST', 'upload' ),
'/cloud/download' => array( 'POST', 'download' ),
'/cloud/generate' => array( 'POST', 'generate' ),
'/cloud/tokens/check' => array( 'POST', 'tokens_check' ),
);

foreach ( $routes as $route => $handler ) {
Expand Down Expand Up @@ -294,6 +295,20 @@ public function download( $request ) {
return $pbp;
}

/*
* Design tokens the pattern references but this site doesn't define
* get written to the home the user chose (§4a). Definitions this
* site already has always win — those tokens are never touched.
*/
$tokens_written = array();
$token_destination = $request->get_param( 'tokenDestination' );
if ( ! empty( $pbp['tokens'] ) && in_array( $token_destination, array( 'user', 'theme' ), true ) ) {
$tokens_written = Pattern_Builder_Cloud_Tokens::apply( $pbp['tokens'], $token_destination );
if ( is_wp_error( $tokens_written ) ) {
return $tokens_written;
}
}

$porter = new Pattern_Builder_Cloud_Porter();
$result = $porter->import_pbp( $pbp, $destination );
if ( is_wp_error( $result ) ) {
Expand All @@ -303,9 +318,26 @@ public function download( $request ) {
// Remember the linkage so re-uploads offer "update".
Pattern_Builder_Cloud::set_link( Pattern_Builder_Cloud_Porter::local_key( $result['type'], $result['id'] ), $cloud_id );

$result['tokensWritten'] = $tokens_written;
return rest_ensure_response( $result );
}

/**
* POST /cloud/tokens/check — which of a pattern's tokens this site
* lacks, so the download flow knows whether to ask where to put them.
*
* @param WP_REST_Request $request Request.
* @return WP_REST_Response
*/
public function tokens_check( $request ) {
$tokens = $request->get_param( 'tokens' );
return rest_ensure_response(
array(
'missing' => Pattern_Builder_Cloud_Tokens::missing( is_array( $tokens ) ? $tokens : array() ),
)
);
}

/**
* DELETE /cloud/library/{id} — remove a cloud pattern (frees a slot).
*
Expand Down
1 change: 1 addition & 0 deletions includes/class-pattern-builder-cloud-porter.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public function export_local( $type, $id ) {
'templateTypes' => array_values( (array) $pattern->templateTypes ), // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
'content' => $content,
'assets' => array_values( $assets ),
'tokens' => Pattern_Builder_Cloud_Tokens::collect( (string) $pattern->content ),
'origin' => array(
'site' => home_url(),
'kind' => $type,
Expand Down
Loading
Loading