Skip to content

Commit

Permalink
WIP: Saving custom variations
Browse files Browse the repository at this point in the history
  • Loading branch information
ribaricplusplus committed Jan 8, 2023
1 parent 0b6a6be commit 15c9e21
Show file tree
Hide file tree
Showing 15 changed files with 918 additions and 58 deletions.
146 changes: 134 additions & 12 deletions lib/class-wp-theme-json-resolver-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ public static function get_user_data_from_wp_global_styles( $theme, $create_post
$args = array(
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'desc',
'order' => 'asc',
'post_type' => $post_type_filter,
'post_status' => $post_status_filter,
'ignore_sticky_posts' => true,
Expand All @@ -453,19 +453,14 @@ public static function get_user_data_from_wp_global_styles( $theme, $create_post
if ( count( $recent_posts ) === 1 ) {
$user_cpt = get_object_vars( $recent_posts[0] );
} elseif ( $create_post ) {
$cpt_post_id = wp_insert_post(
$cpt_post_id = self::add_user_global_styles_variation(
array(
'post_content' => '{"version": ' . WP_Theme_JSON_Gutenberg::LATEST_SCHEMA . ', "isGlobalStylesUserThemeJSON": true }',
'post_status' => 'publish',
'post_title' => 'Custom Styles', // Do not make string translatable, see https://core.trac.wordpress.org/ticket/54518.
'post_type' => $post_type_filter,
'post_name' => sprintf( 'wp-global-styles-%s', urlencode( $stylesheet ) ),
'tax_input' => array(
'wp_theme' => array( $stylesheet ),
),
),
true
'title' => 'Custom Styles', // Do not make string translatable, see https://core.trac.wordpress.org/ticket/54518.
'global_styles' => '{"version": ' . WP_Theme_JSON_Gutenberg::LATEST_SCHEMA . ', "isGlobalStylesUserThemeJSON": true }',
'stylesheet' => $stylesheet,
)
);

if ( ! is_wp_error( $cpt_post_id ) ) {
$user_cpt = get_object_vars( get_post( $cpt_post_id ) );
}
Expand All @@ -474,6 +469,87 @@ public static function get_user_data_from_wp_global_styles( $theme, $create_post
return $user_cpt;
}

/**
* Saves a new user variation into the database.
*
* @param array $args Arguments. All are required.
* {
* @type string title Global styles variation name.
* @type string global_styles Global styles settings as a JSON string.
* @type string $stylesheet Slug of the theme associated with these global styles.
* }
*
* @return int|WP_Error Post ID of the new variation or error if insertion failed.
*/
public static function add_user_global_styles_variation( $args ) {
$theme = wp_get_theme();

/*
* Bail early if the theme does not support a theme.json.
*
* Since wp_theme_has_theme_json only supports the active
* theme, the extra condition for whether $theme is the active theme is
* present here.
*/
if ( $theme->get_stylesheet() === $args['stylesheet'] && ! wp_theme_has_theme_json() ) {
return new WP_Error( __( 'Theme does not have theme.json', 'gutenberg' ) );
}

$post_id = wp_insert_post(
array(
'post_content' => $args['global_styles'],
'post_status' => 'publish',
'post_title' => $args['title'],
'post_type' => 'wp_global_styles',
'post_name' => sprintf( 'wp-global-styles-%s', urlencode( $args['stylesheet'] ) ),
'tax_input' => array(
'wp_theme' => array( $args['stylesheet'] ),
),
),
true
);

return $post_id;
}

/**
* Make an association between post $id and post containing current user
* global styles
*
* @since 6.2
*
* @param int|null $id ID of the associated post. Null to delete the asociation.
* @return int|false Meta ID on success, false on failure.
*/
public static function associate_user_variation_with_global_styles_post( $id ) {
$current_gs_id = static::get_user_global_styles_post_id();

if ( $id === $current_gs_id ) {
return false;
}

$prev_id = get_post_meta( $current_gs_id, 'associated_user_variation', true );

if ( empty( $prev_id ) ) {
return add_post_meta( $current_gs_id, 'associated_user_variation', $id, true );
}

return update_post_meta( $current_gs_id, 'associated_user_variation', $id );
}

/**
* Get associated variation ID.
*
* @since 6.2
*
* @return int|null|false Meta ID or null on success, false on failure.
*/
public static function get_associated_user_variation_id() {
$current_gs_id = static::get_user_global_styles_post_id();

return get_post_meta( $current_gs_id, 'associated_user_variation', true );
}

/**
* Returns the user's origin config.
*
Expand Down Expand Up @@ -698,4 +774,50 @@ public static function get_style_variations() {
return $variations;
}

/**
* Returns the style variations defined by the user.
*
* @since 6.2.0
*
* @return array
*/
public static function get_user_style_variations() {
$stylesheet = get_stylesheet();

$initial_variation = (int) static::get_user_global_styles_post_id();

$args = array(
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'desc',
'post_type' => 'wp_global_styles',
'post_status' => 'publish',
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'post__not_in' => array( $initial_variation ),
'tax_query' => array(
array(
'taxonomy' => 'wp_theme',
'field' => 'name',
'terms' => $stylesheet,
),
),
);

$global_style_query = new WP_Query();
$variation_posts = $global_style_query->query( $args );
$variations = array();

foreach ( $variation_posts as $variation_post ) {
$decoded = json_decode( $variation_post->post_content, true );
$variation = ( new WP_Theme_JSON_Gutenberg( $decoded ) )->get_raw_data();
$variation['title'] = $variation_post->post_title;
$variation['id'] = $variation_post->ID;
$variations[] = $variation;
}

return $variations;
}
}
Loading

0 comments on commit 15c9e21

Please sign in to comment.