-
Notifications
You must be signed in to change notification settings - Fork 2k
/
start-editing-post-copy.js
70 lines (61 loc) · 2.01 KB
/
start-editing-post-copy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* External dependencies
*/
import { includes, pick, reduce } from 'lodash';
/**
* Internal dependencies
*/
import { decodeEntities } from 'lib/formatting';
import wpcom from 'lib/wp';
import { getFeaturedImageId } from 'state/posts/utils';
import { editorSetLoadingError, startEditingNewPost, startEditingPost } from 'state/editor/actions';
import 'state/posts/init';
export const startEditingPostCopy = ( siteId, postToCopyId ) => ( dispatch ) => {
dispatch( startEditingPost( siteId, null ) );
wpcom
.site( siteId )
.post( postToCopyId )
.get( { context: 'edit' } )
.then( ( postToCopy ) => {
const postAttributes = pick( postToCopy, [
'canonical_image',
'content',
'excerpt',
'format',
'post_thumbnail',
'terms',
'type',
] );
postAttributes.title = decodeEntities( postToCopy.title );
postAttributes.featured_image = getFeaturedImageId( postToCopy );
/**
* A list of allowed post metadata for the `updatePostMetadata()` action.
*
* This is needed because blindly passing all post metadata to `editPost()`
* causes unforeseeable issues, such as Publicize not triggering on the copied post.
*
* @see https://github.com/Automattic/wp-calypso/issues/14840
*/
const allowedMetadata = [ 'geo_latitude', 'geo_longitude', 'geo_address', 'geo_public' ];
// Filter the post metadata to include only the ones we want to copy,
// use only the `key` and `value` properties (and, most importantly exclude `id`),
// and add an `operation` field to the copied values.
const copiedMetadata = reduce(
postToCopy.metadata,
( copiedMeta, { key, value } ) => {
if ( includes( allowedMetadata, key ) ) {
copiedMeta.push( { key, value, operation: 'update' } );
}
return copiedMeta;
},
[]
);
if ( copiedMetadata.length > 0 ) {
postAttributes.metadata = copiedMetadata;
}
dispatch( startEditingNewPost( siteId, postAttributes ) );
} )
.catch( ( error ) => {
dispatch( editorSetLoadingError( error ) );
} );
};