Skip to content

Commit

Permalink
Change post name
Browse files Browse the repository at this point in the history
  • Loading branch information
ribaricplusplus committed Jan 21, 2023
1 parent 44140a8 commit d9ed682
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
58 changes: 43 additions & 15 deletions lib/class-wp-theme-json-resolver-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,14 +429,14 @@ public static function get_user_data_from_wp_global_styles( $theme, $create_post
$user_cpt = array();
$post_type_filter = 'wp_global_styles';
$stylesheet = $theme->get_stylesheet();
$post_name = "wp-global-styles-{urlencode($stylesheet)}";
$post_name = "wp-global-styles-{urlencode($stylesheet)}";
$args = array(
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'asc',
'post_type' => $post_type_filter,
'post_status' => $post_status_filter,
'post_name' => $post_name,
'post_name' => $post_name,
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'update_post_meta_cache' => false,
Expand All @@ -460,7 +460,7 @@ public static function get_user_data_from_wp_global_styles( $theme, $create_post
'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,
'post_name'
'post_name' => $post_name,
)
);

Expand All @@ -475,12 +475,13 @@ public static function get_user_data_from_wp_global_styles( $theme, $create_post
/**
* Saves a new user variation into the database.
*
*
* @param array $args {
* Arguments to add a style variation.
*
* @type string title Required. Global styles variation name.
* @type string global_styles Required. Global styles settings as a JSON string.
* @type string $stylesheet Required. Slug of the theme associated with these global styles.
* @type string $post_name Required. Post name.
* @type string $post_name Optional. Post name.
* }
*
* @return int|WP_Error Post ID of the new variation or error if insertion failed.
Expand All @@ -499,23 +500,50 @@ public static function add_user_global_styles_variation( $args ) {
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' => $args['post_name'],
'tax_input' => array(
'wp_theme' => array( $args['stylesheet'] ),
),
$post_args = array(
'post_content' => $args['global_styles'],
'post_status' => 'publish',
'post_title' => $args['title'],
'post_type' => 'wp_global_styles',
'tax_input' => array(
'wp_theme' => array( $args['stylesheet'] ),
),
);

if ( ! empty( $args['post_name'] ) ) {
$post_args['post_name'] = $args['post_name'];
}

$post_id = wp_insert_post(
$post_args,
true
);

if ( empty( $args['post_name'] ) ) {
$post_id = wp_update_post(
array(
'ID' => $post_id,
'post_name' => self::generate_user_style_variation_post_name( $post_id ),
)
);
}

return $post_id;
}

/**
* Generate a post name for a variation with the given ID.
*
* @since 6.2
*
* @param int|null $variation_id Variation ID.
* @return string Post name.
*/
public static function generate_user_style_variation_post_name( $variation_id ) {
$stylesheet = get_stylesheet();
return "wp-global-styles-{$stylesheet}-variation-{$variation_id}";
}

/**
* Make an association between post $id and post containing current user
* global styles
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,7 @@ public function get_item_schema() {
return $this->add_additional_fields_schema( $this->schema );
}

/**
* Returns the given theme global styles variations.
*
* @since 6.0.0
Expand Down

0 comments on commit d9ed682

Please sign in to comment.