Skip to content

Commit

Permalink
Fix exception when editing non-public CPTs (#12201)
Browse files Browse the repository at this point in the history
* Fix exception when editing non-public CPTs

- Changes the posts API endpoint to only set `permalink_template` and
 `generated_slug` when the post is public and queryable. This matches the
 behaviour in Core.
- Changes the `getPermalink()` and `getPermalinkParts()` selectors to
 return null when there is no `permalink_template`. This fixes an
 exception on non-public CPTs caused by attempting to parse an undefined
 `permalink_template`.

* Generate docs
  • Loading branch information
noisysocks authored Nov 22, 2018
1 parent 89f8897 commit b7bfcd1
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
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;
}

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

0 comments on commit b7bfcd1

Please sign in to comment.