-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
create-new-post.js
64 lines (55 loc) · 1.69 KB
/
create-new-post.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
/**
* WordPress dependencies
*/
import { addQueryArgs } from '@wordpress/url';
/**
* Internal dependencies
*/
import { visitAdminPage } from './visit-admin-page';
/**
* Creates new post.
*
* @param {Object} object Object to create new post, along with tips enabling option.
* @param {string} [object.postType] Post type of the new post.
* @param {string} [object.title] Title of the new post.
* @param {string} [object.content] Content of the new post.
* @param {string} [object.excerpt] Excerpt of the new post.
* @param {boolean} [object.showWelcomeGuide] Whether to show the welcome guide.
*/
export async function createNewPost( {
postType,
title,
content,
excerpt,
showWelcomeGuide = false,
} = {} ) {
const query = addQueryArgs( '', {
post_type: postType,
post_title: title,
content,
excerpt,
} ).slice( 1 );
await visitAdminPage( 'post-new.php', query );
await page.waitForSelector( '.edit-post-layout' );
const isWelcomeGuideActive = await page.evaluate( () =>
wp.data.select( 'core/edit-post' ).isFeatureActive( 'welcomeGuide' )
);
const isFullscreenMode = await page.evaluate( () =>
wp.data.select( 'core/edit-post' ).isFeatureActive( 'fullscreenMode' )
);
if ( showWelcomeGuide !== isWelcomeGuideActive ) {
await page.evaluate( () =>
wp.data.dispatch( 'core/edit-post' ).toggleFeature( 'welcomeGuide' )
);
await page.reload();
await page.waitForSelector( '.edit-post-layout' );
}
if ( isFullscreenMode ) {
await page.evaluate( () =>
wp.data
.dispatch( 'core/edit-post' )
.toggleFeature( 'fullscreenMode' )
);
await page.waitForSelector( 'body:not(.is-fullscreen-mode)' );
}
}