Skip to content

Commit

Permalink
Add lookup for post type model and revisions collection in wp-api
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Jun 20, 2017
1 parent bb1d0ca commit 2dbda35
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
6 changes: 4 additions & 2 deletions editor/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ export default {
edits: toSend,
optimist: { id: transactionId },
} );
new wp.api.models.Post( toSend ).save().done( ( newPost ) => {
const Model = wp.api.getPostTypeModel( state.currentPost.type );
new Model( toSend ).save().done( ( newPost ) => {
dispatch( {
type: 'REQUEST_POST_UPDATE_SUCCESS',
post: newPost,
Expand Down Expand Up @@ -79,7 +80,8 @@ export default {
TRASH_POST( action, store ) {
const { dispatch } = store;
const { postId } = action;
new wp.api.models.Post( { id: postId } ).destroy().done( () => {
const Model = wp.api.getPostTypeModel( store.getState().currentPost.type );
new Model( { id: postId } ).destroy().done( () => {
dispatch( {
...action,
type: 'TRASH_POST_SUCCESS',
Expand Down
4 changes: 3 additions & 1 deletion editor/sidebar/last-revision/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ class LastRevision extends Component {
}
this.setState( { loading: true } );
const postIdToLoad = this.props.postId;
this.fetchRevisionsRequest = new wp.api.collections.PostRevisions( {}, { parent: postIdToLoad } ).fetch()
const Collection = wp.api.getPostTypeRevisionsCollection( this.props.postType || 'post' );
this.fetchRevisionsRequest = new Collection( {}, { parent: postIdToLoad } ).fetch()
.done( ( revisions ) => {
if ( this.props.postId !== postIdToLoad ) {
return;
Expand Down Expand Up @@ -104,6 +105,7 @@ export default connect(
( state ) => {
return {
postId: getCurrentPost( state ).id,
postType: getCurrentPost( state ).type,
isSaving: isSavingPost( state ),
};
}
Expand Down
28 changes: 24 additions & 4 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,29 @@ function gutenberg_scripts_and_styles( $hook ) {
*/
wp_enqueue_media();

// Export REST bases for all post types.
$post_type_rest_base_mapping = array();
foreach ( get_post_types( array(), 'objects' ) as $post_type_object ) {
$rest_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name;
$post_type_rest_base_mapping[ $post_type_object->name ] = $rest_base;
}
$script = sprintf( 'wp.api.postTypeRestBaseMapping = %s;', wp_json_encode( $post_type_rest_base_mapping ) );
$script .= <<<JS
wp.api.getPostTypeModel = function( postType ) {
var route = '/' + wpApiSettings.versionString + this.postTypeRestBaseMapping[ postType ] + '/(?P<id>[\\\\d]+)';
return _.first( _.filter( wp.api.models, function( model ) {
return model.prototype.route && route === model.prototype.route.index;
} ) );
};
wp.api.getPostTypeRevisionsCollection = function( postType ) {
var route = '/' + wpApiSettings.versionString + this.postTypeRestBaseMapping[ postType ] + '/(?P<parent>[\\\\d]+)/revisions';
return _.first( _.filter( wp.api.collections, function( model ) {
return model.prototype.route && route === model.prototype.route.index;
} ) );
};
JS;
wp_add_inline_script( 'wp-api', $script );

// The editor code itself.
wp_enqueue_script(
'wp-editor',
Expand Down Expand Up @@ -341,14 +364,11 @@ function gutenberg_scripts_and_styles( $hook ) {
wp_die( __( 'Unauthorized to edit post.', 'gutenberg' ) );
}

// @todo Export to JS.
$rest_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name;

$post_to_edit = null;
if ( $post ) {
$request = new WP_REST_Request(
'GET',
sprintf( '/wp/v2/%s/%d', $rest_base, $post->ID )
sprintf( '/wp/v2/%s/%d', $post_type_rest_base_mapping[ $post->post_type ], $post->ID )
);
$request->set_param( 'context', 'edit' );
$response = rest_do_request( $request );
Expand Down

0 comments on commit 2dbda35

Please sign in to comment.