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

Generated slug is "auto-draft" when a new post is created #8770

Closed
abotteram opened this issue Aug 9, 2018 · 1 comment
Closed

Generated slug is "auto-draft" when a new post is created #8770

abotteram opened this issue Aug 9, 2018 · 1 comment
Labels
[Feature] Saving Related to saving functionality [Package] Editor /packages/editor [Type] Bug An existing feature does not function as intended

Comments

@abotteram
Copy link
Contributor

abotteram commented Aug 9, 2018

Describe the bug
The generated_slug attribute in the core/editor store is set to auto-draft by default.

To Reproduce
Steps to reproduce the behavior:

  1. Create a new post in Gutenberg.
  2. In the console execute the following line:
wp.data.select( 'core/editor' ).getEditedPostAttribute( 'generated_slug' );

Expected behavior
I expect generated_slug to be empty.

Additional note on autosaving
When I look at the slug that is generated when the autosave is triggered before a draft is saved manually, I see that the url is: http://example.com/?p=123 (you can view the url when hovering over the title). So either the generated_slug should be ?p=123 or an empty string, after an autosave.

Why auto-draft (which is a post status) is in the generated_slug field in the first place seems unintentional?

Screenshots
If applicable, add screenshots to help explain your problem.

Additional context
Gutenberg version: 3.4

Currently we use the following logic to get the slug from the core/editor store:

/**
 * Get the post's slug.
 *
 * @returns {string} The post's slug.
 */
getSlug() {
	const getPostAttribute = wp.data.select( "core/editor" ).getPostAttribute;
	/**
	 * Before the post has been saved for the first time, the generated_slug is "auto-draft".
	 *
	 * Before the post is saved the post status is "auto-draft", so when this is the case the slug
	 * should be empty.
	 */
	if ( getPostAttribute( "status" ) === "auto-draft" ) {
		return "";
	}
	let generatedSlug = getPostAttribute( "generated_slug" );

	if ( generatedSlug === "auto-draft" ) {
		generatedSlug = "";
	}
	// When no custom slug is provided we should use the generated_slug attribute.
	return getPostAttribute( "slug" ) || generatedSlug;
}
@abotteram abotteram added [Type] Bug An existing feature does not function as intended [Package] Editor /packages/editor labels Aug 9, 2018
@designsimply designsimply added the [Feature] Saving Related to saving functionality label Aug 9, 2018
@aduth
Copy link
Member

aduth commented May 31, 2019

In debugging this, I've found that while this is perhaps a not-desirable result, it is expected by the current logic of the REST API and permalink-generating. I'm not familiar enough with the context for why it is implemented the way it is, but you can follow it back from the logic:

$ wp shell
wp> $default_post_to_edit = get_default_post_to_edit( 'post', true );
wp> $post = get_post( $default_post_to_edit->ID );
wp> get_sample_permalink( $post->ID, $post->post_title, '' );
=> array(2) {
  [0]=>
  string(26) "http://editor.test/?p=5362"
  [1]=>
  string(10) "auto-draft"
}

The second member of the array is what's returned as the generated_slug (source). The arguments in this example are pulled directly from how the posts REST API controller operates (source).

There is a peculiar though: The result of a WP_Post instance differs between get_default_post_to_edit and what would be produced by get_post when using the ID from get_default_post_to_edit (which is what the REST API is operating with). In the former case, you would get an empty generated_slug when fed directly into get_sample_permalink. This is because get_default_post_to_edit explicitly overrides the post object's title (source), presumably to be able to display it correctly in the Classic Editor interface with an empty title field.

With that in mind, there could be possible actions to produce an empty string, if that's the desired result:

  • get_sample_permalink should be more aware of the post status to assign the slug to always be an empty string
  • The arguments passed to get_sample_permalink in the posts controller should account for the auto-draft status

Since this is not a problem caused by Gutenberg, I'm going to close the issue. Depending on expected behaviors and possible resolution paths, I'd encourage you to consider opening a Trac ticket instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Saving Related to saving functionality [Package] Editor /packages/editor [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

No branches or pull requests

3 participants