Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for custom variations #161

Closed
wants to merge 5 commits into from
Closed
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
40 changes: 39 additions & 1 deletion functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ function extendable_support() {
* @return void
*/
function extendable_styles() {

// Register theme stylesheet.
$theme_version = wp_get_theme()->get( 'Version' );

Expand Down Expand Up @@ -149,4 +148,43 @@ function extendable_register_pattern_categories() {
}
}
}

add_action( 'init', 'extendable_register_pattern_categories', 9 );

function extendable_replace_custom_variation( $response, $_server, $request ) {
$extendable_variations_route = '/wp/v2/global-styles/themes/extendable/variations';

// Here we make sure the code only runs when we are asking specifically
// for Extendable's variations.
if ( $request->get_route() !== $extendable_variations_route ) {
return $response;
}

// We get the Custom variation saved in the database.
$extendable_custom_variation_string = get_option( 'extendable_custom_variation', null );

// If no Custom variation is stored in the database, we return the response as it is.
if ( $extendable_custom_variation_string === null ) {
return $response;
}

$extendable_custom_variation_json = json_decode( $extendable_custom_variation_string, true );

// If the content from the database is not valid JSON, we return the response as it is.
if ( ! $extendable_custom_variation_json ) {
return $response;
}

// We parse the the content from the custom variation and transform it into the shape
// that is expected by WordPress.
$extendable_custom_variation = ( new WP_Theme_JSON_Data( $extendable_custom_variation_json, 'theme' ) )->get_data();

// We append our custom variation at the start of the variations array.
$data = $response->get_data();
array_unshift( $data, $extendable_custom_variation );
$response->set_data( $data );

return $response;
}

add_filter( 'rest_post_dispatch', 'extendable_replace_custom_variation', 10, 3 );