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

Fix exception when editing non-public CPTs #12201

Merged
merged 2 commits into from
Nov 22, 2018
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
8 changes: 5 additions & 3 deletions docs/designers-developers/developers/data/data-core-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -1292,19 +1292,21 @@ Returns the permalink for the post.

*Returns*

The permalink.
The permalink, or null if the post is not viewable.

### getPermalinkParts

Returns the permalink for a post, split into it's three parts: the prefix, the postName, and the suffix.
Returns the permalink for a post, split into it's three parts: the prefix,
the postName, and the suffix.

*Parameters*

* state: Editor state.

*Returns*

The prefix, postName, and suffix for the permalink.
An object containing the prefix, postName, and suffix for
the permalink, or null if the post is not viewable.

### inSomeHistory

Expand Down
5 changes: 5 additions & 0 deletions lib/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ function gutenberg_add_permalink_template_to_posts( $response, $post, $request )
return $response;
}

$post_type_obj = get_post_type_object( $post->post_type );
if ( ! is_post_type_viewable( $post_type_obj ) || ! $post_type_obj->public ) {
return $response;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pulled this logic over from the 5.0 branch.


if ( ! function_exists( 'get_sample_permalink' ) ) {
require_once ABSPATH . '/wp-admin/includes/post.php';
}
Expand Down
19 changes: 15 additions & 4 deletions packages/editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2119,10 +2119,15 @@ export function isPermalinkEditable( state ) {
*
* @param {Object} state Editor state.
*
* @return {string} The permalink.
* @return {?string} The permalink, or null if the post is not viewable.
*/
export function getPermalink( state ) {
const { prefix, postName, suffix } = getPermalinkParts( state );
const permalinkParts = getPermalinkParts( state );
if ( ! permalinkParts ) {
return null;
}

const { prefix, postName, suffix } = permalinkParts;

if ( isPermalinkEditable( state ) ) {
return prefix + postName + suffix;
Expand All @@ -2132,14 +2137,20 @@ export function getPermalink( state ) {
}

/**
* Returns the permalink for a post, split into it's three parts: the prefix, the postName, and the suffix.
* Returns the permalink for a post, split into it's three parts: the prefix,
* the postName, and the suffix.
*
* @param {Object} state Editor state.
*
* @return {Object} The prefix, postName, and suffix for the permalink.
* @return {Object} An object containing the prefix, postName, and suffix for
* the permalink, or null if the post is not viewable.
*/
export function getPermalinkParts( state ) {
const permalinkTemplate = getEditedPostAttribute( state, 'permalink_template' );
if ( ! permalinkTemplate ) {
return null;
}

const postName = getEditedPostAttribute( state, 'slug' ) || getEditedPostAttribute( state, 'generated_slug' );

const [ prefix, suffix ] = permalinkTemplate.split( PERMALINK_POSTNAME_REGEX );
Expand Down
22 changes: 22 additions & 0 deletions packages/editor/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -5042,6 +5042,17 @@ describe( 'selectors', () => {

expect( getPermalink( state ) ).toBe( 'http://foo.test/bar/baz/' );
} );

it( 'should return null if the post has no permalink template', () => {
const state = {
currentPost: {},
editor: {
present: {},
},
};

expect( getPermalink( state ) ).toBeNull();
} );
} );

describe( 'getPermalinkParts', () => {
Expand Down Expand Up @@ -5087,6 +5098,17 @@ describe( 'selectors', () => {

expect( getPermalinkParts( state ) ).toEqual( parts );
} );

it( 'should return null if the post has no permalink template', () => {
const state = {
currentPost: {},
editor: {
present: {},
},
};

expect( getPermalinkParts( state ) ).toBeNull();
} );
} );

describe( 'getBlockListSettings', () => {
Expand Down