@wordpress/e2e-test-utils-playwright 1.48.0 → 1.48.2-next.v.202606191442.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/admin/create-new-post.js +31 -0
- package/build/admin/create-new-post.js.map +1 -0
- package/build/admin/edit-post.js +20 -0
- package/build/admin/edit-post.js.map +1 -0
- package/{src/admin/get-page-error.ts → build/admin/get-page-error.js} +9 -12
- package/build/admin/get-page-error.js.map +1 -0
- package/build/admin/index.js +40 -0
- package/build/admin/index.js.map +1 -0
- package/build/admin/visit-admin-page.js +32 -0
- package/build/admin/visit-admin-page.js.map +1 -0
- package/build/admin/visit-site-editor.js +45 -0
- package/build/admin/visit-site-editor.js.map +1 -0
- package/build/admin/wait-for-site-editor.js +41 -0
- package/build/admin/wait-for-site-editor.js.map +1 -0
- package/build/config.js +13 -0
- package/build/config.js.map +1 -0
- package/build/editor/click-block-options-menu-item.js +17 -0
- package/build/editor/click-block-options-menu-item.js.map +1 -0
- package/build/editor/click-block-toolbar-button.js +16 -0
- package/build/editor/click-block-toolbar-button.js.map +1 -0
- package/build/editor/get-blocks.js +45 -0
- package/build/editor/get-blocks.js.map +1 -0
- package/build/editor/get-edited-post-content.js +15 -0
- package/build/editor/get-edited-post-content.js.map +1 -0
- package/build/editor/index.js +75 -0
- package/build/editor/index.js.map +1 -0
- package/build/editor/insert-block.js +23 -0
- package/build/editor/insert-block.js.map +1 -0
- package/build/editor/open-document-settings-sidebar.js +27 -0
- package/build/editor/open-document-settings-sidebar.js.map +1 -0
- package/build/editor/preview.js +21 -0
- package/build/editor/preview.js.map +1 -0
- package/build/editor/publish-post.js +45 -0
- package/build/editor/publish-post.js.map +1 -0
- package/build/editor/save-draft.js +18 -0
- package/build/editor/save-draft.js.map +1 -0
- package/build/editor/select-blocks.js +28 -0
- package/build/editor/select-blocks.js.map +1 -0
- package/build/editor/set-content.js +17 -0
- package/build/editor/set-content.js.map +1 -0
- package/build/editor/set-is-fixed-toolbar.js +18 -0
- package/build/editor/set-is-fixed-toolbar.js.map +1 -0
- package/build/editor/set-preferences.js +21 -0
- package/build/editor/set-preferences.js.map +1 -0
- package/build/editor/show-block-toolbar.js +19 -0
- package/build/editor/show-block-toolbar.js.map +1 -0
- package/build/editor/site-editor.js +44 -0
- package/build/editor/site-editor.js.map +1 -0
- package/build/editor/switch-editor-tool.js +33 -0
- package/build/editor/switch-editor-tool.js.map +1 -0
- package/build/editor/switch-to-legacy-canvas.js +18 -0
- package/build/editor/switch-to-legacy-canvas.js.map +1 -0
- package/build/editor/transform-block-to.js +24 -0
- package/build/editor/transform-block-to.js.map +1 -0
- package/build/index.js +19 -0
- package/build/index.js.map +1 -0
- package/build/lighthouse/index.js +61 -0
- package/build/lighthouse/index.js.map +1 -0
- package/build/metrics/index.js +314 -0
- package/build/metrics/index.js.map +1 -0
- package/build/page-utils/drag-files.js +119 -0
- package/build/page-utils/drag-files.js.map +1 -0
- package/build/page-utils/emulate-network-conditions.js +71 -0
- package/build/page-utils/emulate-network-conditions.js.map +1 -0
- package/build/page-utils/index.js +37 -0
- package/build/page-utils/index.js.map +1 -0
- package/build/page-utils/is-current-url.js +21 -0
- package/build/page-utils/is-current-url.js.map +1 -0
- package/build/page-utils/keycodes.js +41 -0
- package/build/page-utils/keycodes.js.map +1 -0
- package/build/page-utils/press-keys.js +141 -0
- package/build/page-utils/press-keys.js.map +1 -0
- package/{src → build}/page-utils/set-browser-viewport.js +12 -14
- package/build/page-utils/set-browser-viewport.js.map +1 -0
- package/build/request-utils/blocks.js +40 -0
- package/build/request-utils/blocks.js.map +1 -0
- package/build/request-utils/comments.js +52 -0
- package/build/request-utils/comments.js.map +1 -0
- package/build/request-utils/gutenberg-experiments.js +47 -0
- package/build/request-utils/gutenberg-experiments.js.map +1 -0
- package/build/request-utils/index.js +196 -0
- package/build/request-utils/index.js.map +1 -0
- package/build/request-utils/login.js +21 -0
- package/build/request-utils/login.js.map +1 -0
- package/build/request-utils/media.js +106 -0
- package/build/request-utils/media.js.map +1 -0
- package/build/request-utils/menus.js +116 -0
- package/build/request-utils/menus.js.map +1 -0
- package/build/request-utils/pages.js +53 -0
- package/build/request-utils/pages.js.map +1 -0
- package/build/request-utils/patterns.js +27 -0
- package/build/request-utils/patterns.js.map +1 -0
- package/build/request-utils/plugins.js +85 -0
- package/build/request-utils/plugins.js.map +1 -0
- package/build/request-utils/posts.js +42 -0
- package/build/request-utils/posts.js.map +1 -0
- package/build/request-utils/preferences.js +54 -0
- package/build/request-utils/preferences.js.map +1 -0
- package/{src/request-utils/records.ts → build/request-utils/records.js} +10 -15
- package/build/request-utils/records.js.map +1 -0
- package/build/request-utils/rest.js +181 -0
- package/build/request-utils/rest.js.map +1 -0
- package/build/request-utils/site-settings.js +33 -0
- package/build/request-utils/site-settings.js.map +1 -0
- package/build/request-utils/templates.js +54 -0
- package/build/request-utils/templates.js.map +1 -0
- package/build/request-utils/themes.js +69 -0
- package/build/request-utils/themes.js.map +1 -0
- package/build/request-utils/users.js +81 -0
- package/build/request-utils/users.js.map +1 -0
- package/build/request-utils/widgets.js +61 -0
- package/build/request-utils/widgets.js.map +1 -0
- package/build/test.js +189 -0
- package/build/test.js.map +1 -0
- package/build/types.js +3 -0
- package/build/types.js.map +1 -0
- package/package.json +8 -5
- package/src/admin/create-new-post.ts +0 -47
- package/src/admin/edit-post.ts +0 -24
- package/src/admin/index.ts +0 -52
- package/src/admin/visit-admin-page.ts +0 -43
- package/src/admin/visit-site-editor.ts +0 -62
- package/src/admin/wait-for-site-editor.ts +0 -52
- package/src/config.ts +0 -12
- package/src/editor/click-block-options-menu-item.ts +0 -18
- package/src/editor/click-block-toolbar-button.ts +0 -21
- package/src/editor/get-blocks.ts +0 -75
- package/src/editor/get-edited-post-content.ts +0 -19
- package/src/editor/index.ts +0 -95
- package/src/editor/insert-block.ts +0 -54
- package/src/editor/open-document-settings-sidebar.ts +0 -31
- package/src/editor/preview.ts +0 -32
- package/src/editor/publish-post.ts +0 -51
- package/src/editor/save-draft.ts +0 -20
- package/src/editor/select-blocks.ts +0 -48
- package/src/editor/set-content.ts +0 -24
- package/src/editor/set-is-fixed-toolbar.ts +0 -20
- package/src/editor/set-preferences.ts +0 -37
- package/src/editor/show-block-toolbar.ts +0 -20
- package/src/editor/site-editor.ts +0 -55
- package/src/editor/switch-editor-tool.ts +0 -34
- package/src/editor/switch-to-legacy-canvas.ts +0 -20
- package/src/editor/transform-block-to.ts +0 -34
- package/src/index.ts +0 -8
- package/src/lighthouse/index.ts +0 -75
- package/src/metrics/index.ts +0 -441
- package/src/page-utils/drag-files.ts +0 -173
- package/src/page-utils/emulate-network-conditions.ts +0 -101
- package/src/page-utils/index.ts +0 -54
- package/src/page-utils/is-current-url.ts +0 -20
- package/src/page-utils/keycodes.ts +0 -48
- package/src/page-utils/press-keys.ts +0 -195
- package/src/request-utils/blocks.ts +0 -58
- package/src/request-utils/comments.ts +0 -80
- package/src/request-utils/gutenberg-experiments.ts +0 -62
- package/src/request-utils/index.ts +0 -222
- package/src/request-utils/login.ts +0 -34
- package/src/request-utils/media.ts +0 -102
- package/src/request-utils/menus.ts +0 -145
- package/src/request-utils/pages.ts +0 -75
- package/src/request-utils/patterns.ts +0 -31
- package/src/request-utils/plugins.ts +0 -103
- package/src/request-utils/posts.ts +0 -69
- package/src/request-utils/preferences.ts +0 -63
- package/src/request-utils/rest.ts +0 -222
- package/src/request-utils/site-settings.ts +0 -58
- package/src/request-utils/templates.ts +0 -83
- package/src/request-utils/themes.ts +0 -110
- package/src/request-utils/users.ts +0 -125
- package/src/request-utils/widgets.js +0 -68
- package/src/test.ts +0 -204
- package/src/types.ts +0 -13
|
@@ -1,222 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* External dependencies
|
|
3
|
-
*/
|
|
4
|
-
import * as fs from 'fs/promises';
|
|
5
|
-
import * as path from 'path';
|
|
6
|
-
import { request } from '@playwright/test';
|
|
7
|
-
import type { APIRequestContext, Cookie } from '@playwright/test';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Internal dependencies
|
|
11
|
-
*/
|
|
12
|
-
import { WP_ADMIN_USER, WP_BASE_URL } from '../config';
|
|
13
|
-
import type { User } from './login';
|
|
14
|
-
import { login } from './login';
|
|
15
|
-
import { listMedia, uploadMedia, deleteMedia, deleteAllMedia } from './media';
|
|
16
|
-
import { createUser, deleteAllUsers } from './users';
|
|
17
|
-
import { setupRest, rest, getMaxBatchSize, batchRest } from './rest';
|
|
18
|
-
import { getPluginsMap, activatePlugin, deactivatePlugin } from './plugins';
|
|
19
|
-
import { deleteAllTemplates, createTemplate } from './templates';
|
|
20
|
-
import {
|
|
21
|
-
activateTheme,
|
|
22
|
-
getCurrentThemeGlobalStylesPostId,
|
|
23
|
-
getThemeGlobalStylesRevisions,
|
|
24
|
-
} from './themes';
|
|
25
|
-
import { createBlock, deleteAllBlocks } from './blocks';
|
|
26
|
-
import { createComment, deleteAllComments } from './comments';
|
|
27
|
-
import { createPost, deleteAllPosts } from './posts';
|
|
28
|
-
import {
|
|
29
|
-
createClassicMenu,
|
|
30
|
-
createNavigationMenu,
|
|
31
|
-
deleteAllMenus,
|
|
32
|
-
getNavigationMenus,
|
|
33
|
-
} from './menus';
|
|
34
|
-
import { deleteAllPages, createPage } from './pages';
|
|
35
|
-
import { createRecord } from './records';
|
|
36
|
-
import { resetPreferences, setPreferences } from './preferences';
|
|
37
|
-
import { getSiteSettings, updateSiteSettings } from './site-settings';
|
|
38
|
-
import { deleteAllWidgets, addWidgetBlock } from './widgets';
|
|
39
|
-
import { deleteAllPatternCategories } from './patterns';
|
|
40
|
-
import { setGutenbergExperiments } from './gutenberg-experiments';
|
|
41
|
-
|
|
42
|
-
interface StorageState {
|
|
43
|
-
cookies: Cookie[];
|
|
44
|
-
nonce: string;
|
|
45
|
-
rootURL: string;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
class RequestUtils {
|
|
49
|
-
request: APIRequestContext;
|
|
50
|
-
user: User;
|
|
51
|
-
maxBatchSize?: number;
|
|
52
|
-
storageState?: StorageState;
|
|
53
|
-
storageStatePath?: string;
|
|
54
|
-
baseURL?: string;
|
|
55
|
-
|
|
56
|
-
pluginsMap: Record< string, string > | null = null;
|
|
57
|
-
|
|
58
|
-
static async setup( {
|
|
59
|
-
user,
|
|
60
|
-
storageStatePath,
|
|
61
|
-
baseURL = WP_BASE_URL,
|
|
62
|
-
}: {
|
|
63
|
-
user?: User;
|
|
64
|
-
storageStatePath?: string;
|
|
65
|
-
baseURL?: string;
|
|
66
|
-
} ) {
|
|
67
|
-
let storageState: StorageState | undefined;
|
|
68
|
-
if ( storageStatePath ) {
|
|
69
|
-
await fs.mkdir( path.dirname( storageStatePath ), {
|
|
70
|
-
recursive: true,
|
|
71
|
-
} );
|
|
72
|
-
|
|
73
|
-
try {
|
|
74
|
-
storageState = JSON.parse(
|
|
75
|
-
await fs.readFile( storageStatePath, 'utf-8' )
|
|
76
|
-
);
|
|
77
|
-
} catch ( error ) {
|
|
78
|
-
if (
|
|
79
|
-
error instanceof Error &&
|
|
80
|
-
( error as NodeJS.ErrnoException ).code === 'ENOENT'
|
|
81
|
-
) {
|
|
82
|
-
// Ignore errors if the state is not found.
|
|
83
|
-
} else {
|
|
84
|
-
throw error;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const requestContext = await request.newContext( {
|
|
90
|
-
baseURL,
|
|
91
|
-
storageState: storageState && {
|
|
92
|
-
cookies: storageState.cookies,
|
|
93
|
-
origins: [],
|
|
94
|
-
},
|
|
95
|
-
} );
|
|
96
|
-
|
|
97
|
-
const requestUtils = new this( requestContext, {
|
|
98
|
-
user,
|
|
99
|
-
storageState,
|
|
100
|
-
storageStatePath,
|
|
101
|
-
baseURL,
|
|
102
|
-
} );
|
|
103
|
-
|
|
104
|
-
return requestUtils;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
constructor(
|
|
108
|
-
requestContext: APIRequestContext,
|
|
109
|
-
{
|
|
110
|
-
user = WP_ADMIN_USER,
|
|
111
|
-
storageState,
|
|
112
|
-
storageStatePath,
|
|
113
|
-
baseURL = WP_BASE_URL,
|
|
114
|
-
}: {
|
|
115
|
-
user?: User;
|
|
116
|
-
storageState?: StorageState;
|
|
117
|
-
storageStatePath?: string;
|
|
118
|
-
baseURL?: string;
|
|
119
|
-
} = {}
|
|
120
|
-
) {
|
|
121
|
-
this.user = user;
|
|
122
|
-
this.request = requestContext;
|
|
123
|
-
this.storageStatePath = storageStatePath;
|
|
124
|
-
this.storageState = storageState;
|
|
125
|
-
this.baseURL = baseURL;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/** @borrows login as this.login */
|
|
129
|
-
login: typeof login = login.bind( this );
|
|
130
|
-
/** @borrows setupRest as this.setupRest */
|
|
131
|
-
setupRest: typeof setupRest = setupRest.bind( this );
|
|
132
|
-
// .bind() drops the generic types. Re-casting it to keep the type signature.
|
|
133
|
-
rest: typeof rest = rest.bind( this ) as typeof rest;
|
|
134
|
-
/** @borrows getMaxBatchSize as this.getMaxBatchSize */
|
|
135
|
-
getMaxBatchSize: typeof getMaxBatchSize = getMaxBatchSize.bind( this );
|
|
136
|
-
// .bind() drops the generic types. Re-casting it to keep the type signature.
|
|
137
|
-
batchRest: typeof batchRest = batchRest.bind( this ) as typeof batchRest;
|
|
138
|
-
// .bind() drops the generic types. Re-casting it to keep the type signature.
|
|
139
|
-
createRecord: typeof createRecord = createRecord.bind(
|
|
140
|
-
this
|
|
141
|
-
) as typeof createRecord;
|
|
142
|
-
/** @borrows getPluginsMap as this.getPluginsMap */
|
|
143
|
-
getPluginsMap: typeof getPluginsMap = getPluginsMap.bind( this );
|
|
144
|
-
/** @borrows activatePlugin as this.activatePlugin */
|
|
145
|
-
activatePlugin: typeof activatePlugin = activatePlugin.bind( this );
|
|
146
|
-
/** @borrows deactivatePlugin as this.deactivatePlugin */
|
|
147
|
-
deactivatePlugin: typeof deactivatePlugin = deactivatePlugin.bind( this );
|
|
148
|
-
/** @borrows activateTheme as this.activateTheme */
|
|
149
|
-
activateTheme: typeof activateTheme = activateTheme.bind( this );
|
|
150
|
-
/** @borrows createBlock as this.createBlock */
|
|
151
|
-
createBlock: typeof createBlock = createBlock.bind( this );
|
|
152
|
-
/** @borrows deleteAllBlocks as this.deleteAllBlocks */
|
|
153
|
-
deleteAllBlocks = deleteAllBlocks.bind( this );
|
|
154
|
-
/** @borrows createPost as this.createPost */
|
|
155
|
-
createPost: typeof createPost = createPost.bind( this );
|
|
156
|
-
/** @borrows deleteAllPosts as this.deleteAllPosts */
|
|
157
|
-
deleteAllPosts: typeof deleteAllPosts = deleteAllPosts.bind( this );
|
|
158
|
-
/** @borrows createClassicMenu as this.createClassicMenu */
|
|
159
|
-
createClassicMenu: typeof createClassicMenu =
|
|
160
|
-
createClassicMenu.bind( this );
|
|
161
|
-
/** @borrows createNavigationMenu as this.createNavigationMenu */
|
|
162
|
-
createNavigationMenu: typeof createNavigationMenu =
|
|
163
|
-
createNavigationMenu.bind( this );
|
|
164
|
-
/** @borrows deleteAllMenus as this.deleteAllMenus */
|
|
165
|
-
deleteAllMenus: typeof deleteAllMenus = deleteAllMenus.bind( this );
|
|
166
|
-
/** @borrows getNavigationMenus as this.getNavigationMenus */
|
|
167
|
-
getNavigationMenus: typeof getNavigationMenus =
|
|
168
|
-
getNavigationMenus.bind( this );
|
|
169
|
-
/** @borrows createComment as this.createComment */
|
|
170
|
-
createComment: typeof createComment = createComment.bind( this );
|
|
171
|
-
/** @borrows deleteAllComments as this.deleteAllComments */
|
|
172
|
-
deleteAllComments: typeof deleteAllComments =
|
|
173
|
-
deleteAllComments.bind( this );
|
|
174
|
-
/** @borrows deleteAllWidgets as this.deleteAllWidgets */
|
|
175
|
-
deleteAllWidgets: typeof deleteAllWidgets = deleteAllWidgets.bind( this );
|
|
176
|
-
/** @borrows addWidgetBlock as this.addWidgetBlock */
|
|
177
|
-
addWidgetBlock: typeof addWidgetBlock = addWidgetBlock.bind( this );
|
|
178
|
-
/** @borrows deleteAllTemplates as this.deleteAllTemplates */
|
|
179
|
-
deleteAllTemplates: typeof deleteAllTemplates =
|
|
180
|
-
deleteAllTemplates.bind( this );
|
|
181
|
-
/** @borrows createTemplate as this.createTemplate */
|
|
182
|
-
createTemplate: typeof createTemplate = createTemplate.bind( this );
|
|
183
|
-
/** @borrows resetPreferences as this.resetPreferences */
|
|
184
|
-
resetPreferences: typeof resetPreferences = resetPreferences.bind( this );
|
|
185
|
-
/** @borrows setPreferences as this.setPreferences */
|
|
186
|
-
setPreferences: typeof setPreferences = setPreferences.bind( this );
|
|
187
|
-
/** @borrows listMedia as this.listMedia */
|
|
188
|
-
listMedia: typeof listMedia = listMedia.bind( this );
|
|
189
|
-
/** @borrows uploadMedia as this.uploadMedia */
|
|
190
|
-
uploadMedia: typeof uploadMedia = uploadMedia.bind( this );
|
|
191
|
-
/** @borrows deleteMedia as this.deleteMedia */
|
|
192
|
-
deleteMedia: typeof deleteMedia = deleteMedia.bind( this );
|
|
193
|
-
/** @borrows deleteAllMedia as this.deleteAllMedia */
|
|
194
|
-
deleteAllMedia: typeof deleteAllMedia = deleteAllMedia.bind( this );
|
|
195
|
-
/** @borrows createUser as this.createUser */
|
|
196
|
-
createUser: typeof createUser = createUser.bind( this );
|
|
197
|
-
/** @borrows deleteAllUsers as this.deleteAllUsers */
|
|
198
|
-
deleteAllUsers: typeof deleteAllUsers = deleteAllUsers.bind( this );
|
|
199
|
-
/** @borrows getSiteSettings as this.getSiteSettings */
|
|
200
|
-
getSiteSettings: typeof getSiteSettings = getSiteSettings.bind( this );
|
|
201
|
-
/** @borrows updateSiteSettings as this.updateSiteSettings */
|
|
202
|
-
updateSiteSettings: typeof updateSiteSettings =
|
|
203
|
-
updateSiteSettings.bind( this );
|
|
204
|
-
/** @borrows deleteAllPages as this.deleteAllPages */
|
|
205
|
-
deleteAllPages: typeof deleteAllPages = deleteAllPages.bind( this );
|
|
206
|
-
/** @borrows createPage as this.createPage */
|
|
207
|
-
createPage: typeof createPage = createPage.bind( this );
|
|
208
|
-
/** @borrows getCurrentThemeGlobalStylesPostId as this.getCurrentThemeGlobalStylesPostId */
|
|
209
|
-
getCurrentThemeGlobalStylesPostId: typeof getCurrentThemeGlobalStylesPostId =
|
|
210
|
-
getCurrentThemeGlobalStylesPostId.bind( this );
|
|
211
|
-
/** @borrows getThemeGlobalStylesRevisions as this.getThemeGlobalStylesRevisions */
|
|
212
|
-
getThemeGlobalStylesRevisions: typeof getThemeGlobalStylesRevisions =
|
|
213
|
-
getThemeGlobalStylesRevisions.bind( this );
|
|
214
|
-
/** @borrows deleteAllPatternCategories as this.deleteAllPatternCategories */
|
|
215
|
-
deleteAllPatternCategories = deleteAllPatternCategories.bind( this );
|
|
216
|
-
/** @borrows setGutenbergExperiments as this.setGutenbergExperiments */
|
|
217
|
-
setGutenbergExperiments: typeof setGutenbergExperiments =
|
|
218
|
-
setGutenbergExperiments.bind( this );
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
export type { StorageState };
|
|
222
|
-
export { RequestUtils };
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Internal dependencies
|
|
3
|
-
*/
|
|
4
|
-
import type { RequestUtils } from './index';
|
|
5
|
-
|
|
6
|
-
export interface User {
|
|
7
|
-
username: string;
|
|
8
|
-
password: string;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
async function login( this: RequestUtils, user: User = this.user ) {
|
|
12
|
-
// Login to admin using request context.
|
|
13
|
-
let response = await this.request.post( 'wp-login.php', {
|
|
14
|
-
failOnStatusCode: true,
|
|
15
|
-
form: {
|
|
16
|
-
log: user.username,
|
|
17
|
-
pwd: user.password,
|
|
18
|
-
},
|
|
19
|
-
} );
|
|
20
|
-
await response.dispose();
|
|
21
|
-
|
|
22
|
-
// Get the nonce.
|
|
23
|
-
response = await this.request.get(
|
|
24
|
-
'wp-admin/admin-ajax.php?action=rest-nonce',
|
|
25
|
-
{
|
|
26
|
-
failOnStatusCode: true,
|
|
27
|
-
}
|
|
28
|
-
);
|
|
29
|
-
const nonce = await response.text();
|
|
30
|
-
|
|
31
|
-
return nonce;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export { login };
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* External dependencies
|
|
3
|
-
*/
|
|
4
|
-
import * as fs from 'fs';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Internal dependencies
|
|
8
|
-
*/
|
|
9
|
-
import type { RequestUtils } from './index';
|
|
10
|
-
|
|
11
|
-
export interface Media {
|
|
12
|
-
id: number;
|
|
13
|
-
title: {
|
|
14
|
-
raw: string;
|
|
15
|
-
rendered: string;
|
|
16
|
-
};
|
|
17
|
-
source_url: string;
|
|
18
|
-
slug: string;
|
|
19
|
-
alt_text: string;
|
|
20
|
-
caption: { rendered: string };
|
|
21
|
-
link: string;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* List all media files.
|
|
26
|
-
*
|
|
27
|
-
* @see https://developer.wordpress.org/rest-api/reference/media/#list-media
|
|
28
|
-
* @param this
|
|
29
|
-
*/
|
|
30
|
-
async function listMedia( this: RequestUtils ) {
|
|
31
|
-
const response = await this.rest< Media[] >( {
|
|
32
|
-
method: 'GET',
|
|
33
|
-
path: '/wp/v2/media',
|
|
34
|
-
params: {
|
|
35
|
-
per_page: 100,
|
|
36
|
-
},
|
|
37
|
-
} );
|
|
38
|
-
|
|
39
|
-
return response;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Upload a media file.
|
|
44
|
-
*
|
|
45
|
-
* @see https://developer.wordpress.org/rest-api/reference/media/#create-a-media-item
|
|
46
|
-
* @param this
|
|
47
|
-
* @param filePathOrData The path or data of the file being uploaded.
|
|
48
|
-
*/
|
|
49
|
-
async function uploadMedia(
|
|
50
|
-
this: RequestUtils,
|
|
51
|
-
filePathOrData: string | fs.ReadStream
|
|
52
|
-
) {
|
|
53
|
-
const file =
|
|
54
|
-
typeof filePathOrData === 'string'
|
|
55
|
-
? fs.createReadStream( filePathOrData )
|
|
56
|
-
: filePathOrData;
|
|
57
|
-
|
|
58
|
-
const response = await this.rest< Media >( {
|
|
59
|
-
method: 'POST',
|
|
60
|
-
path: '/wp/v2/media',
|
|
61
|
-
multipart: {
|
|
62
|
-
file,
|
|
63
|
-
},
|
|
64
|
-
} );
|
|
65
|
-
|
|
66
|
-
return response;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* delete a media file.
|
|
71
|
-
*
|
|
72
|
-
* @see https://developer.wordpress.org/rest-api/reference/media/#delete-a-media-item
|
|
73
|
-
* @param this
|
|
74
|
-
* @param mediaId The ID of the media file.
|
|
75
|
-
*/
|
|
76
|
-
async function deleteMedia( this: RequestUtils, mediaId: number ) {
|
|
77
|
-
const response = await this.rest( {
|
|
78
|
-
method: 'DELETE',
|
|
79
|
-
path: `/wp/v2/media/${ mediaId }`,
|
|
80
|
-
params: { force: true },
|
|
81
|
-
} );
|
|
82
|
-
|
|
83
|
-
return response;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* delete all media files.
|
|
88
|
-
*
|
|
89
|
-
* @param this
|
|
90
|
-
*/
|
|
91
|
-
async function deleteAllMedia( this: RequestUtils ) {
|
|
92
|
-
const files = await this.listMedia();
|
|
93
|
-
|
|
94
|
-
// The media endpoint doesn't support batch request yet.
|
|
95
|
-
const responses = await Promise.all(
|
|
96
|
-
files.map( ( media ) => this.deleteMedia( media.id ) )
|
|
97
|
-
);
|
|
98
|
-
|
|
99
|
-
return responses;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
export { listMedia, uploadMedia, deleteMedia, deleteAllMedia };
|
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Internal dependencies
|
|
3
|
-
*/
|
|
4
|
-
import type { RequestUtils } from './index';
|
|
5
|
-
|
|
6
|
-
export interface MenuData {
|
|
7
|
-
title: string;
|
|
8
|
-
content: string;
|
|
9
|
-
}
|
|
10
|
-
export interface NavigationMenu {
|
|
11
|
-
id: number;
|
|
12
|
-
content: string;
|
|
13
|
-
status: 'publish' | 'future' | 'draft' | 'pending' | 'private';
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Create a classic menu
|
|
18
|
-
*
|
|
19
|
-
* @param name Menu name.
|
|
20
|
-
* @return Menu content.
|
|
21
|
-
*/
|
|
22
|
-
export async function createClassicMenu( this: RequestUtils, name: string ) {
|
|
23
|
-
const menuItems = [
|
|
24
|
-
{
|
|
25
|
-
title: 'Custom link',
|
|
26
|
-
url: 'http://localhost:8889/',
|
|
27
|
-
type: 'custom',
|
|
28
|
-
menu_order: 1,
|
|
29
|
-
},
|
|
30
|
-
];
|
|
31
|
-
|
|
32
|
-
const menu = await this.rest< NavigationMenu >( {
|
|
33
|
-
method: 'POST',
|
|
34
|
-
path: `/wp/v2/menus/`,
|
|
35
|
-
data: {
|
|
36
|
-
name,
|
|
37
|
-
},
|
|
38
|
-
} );
|
|
39
|
-
|
|
40
|
-
await this.batchRest(
|
|
41
|
-
menuItems.map( ( menuItem ) => ( {
|
|
42
|
-
method: 'POST',
|
|
43
|
-
path: `/wp/v2/menu-items`,
|
|
44
|
-
body: {
|
|
45
|
-
menus: menu.id,
|
|
46
|
-
object_id: undefined,
|
|
47
|
-
...menuItem,
|
|
48
|
-
parent: undefined,
|
|
49
|
-
},
|
|
50
|
-
} ) )
|
|
51
|
-
);
|
|
52
|
-
|
|
53
|
-
return menu;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Create a navigation menu
|
|
58
|
-
*
|
|
59
|
-
* @param menuData navigation menu post data.
|
|
60
|
-
* @return Menu content.
|
|
61
|
-
*/
|
|
62
|
-
export async function createNavigationMenu(
|
|
63
|
-
this: RequestUtils,
|
|
64
|
-
menuData: MenuData
|
|
65
|
-
) {
|
|
66
|
-
return this.createRecord< NavigationMenu >( 'navigation', {
|
|
67
|
-
status: 'publish',
|
|
68
|
-
...menuData,
|
|
69
|
-
} );
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Delete all navigation and classic menus
|
|
74
|
-
*
|
|
75
|
-
*/
|
|
76
|
-
export async function deleteAllMenus( this: RequestUtils ) {
|
|
77
|
-
const navMenus = await this.rest< NavigationMenu[] >( {
|
|
78
|
-
path: `/wp/v2/navigation/`,
|
|
79
|
-
data: {
|
|
80
|
-
status: [
|
|
81
|
-
'publish',
|
|
82
|
-
'pending',
|
|
83
|
-
'draft',
|
|
84
|
-
'auto-draft',
|
|
85
|
-
'future',
|
|
86
|
-
'private',
|
|
87
|
-
'inherit',
|
|
88
|
-
'trash',
|
|
89
|
-
],
|
|
90
|
-
},
|
|
91
|
-
} );
|
|
92
|
-
|
|
93
|
-
if ( navMenus.length ) {
|
|
94
|
-
await this.batchRest(
|
|
95
|
-
navMenus.map( ( menu ) => ( {
|
|
96
|
-
method: 'DELETE',
|
|
97
|
-
path: `/wp/v2/navigation/${ menu.id }?force=true`,
|
|
98
|
-
} ) )
|
|
99
|
-
);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
const classicMenus = await this.rest< NavigationMenu[] >( {
|
|
103
|
-
path: `/wp/v2/menus/`,
|
|
104
|
-
data: {
|
|
105
|
-
status: [
|
|
106
|
-
'publish',
|
|
107
|
-
'pending',
|
|
108
|
-
'draft',
|
|
109
|
-
'auto-draft',
|
|
110
|
-
'future',
|
|
111
|
-
'private',
|
|
112
|
-
'inherit',
|
|
113
|
-
'trash',
|
|
114
|
-
],
|
|
115
|
-
},
|
|
116
|
-
} );
|
|
117
|
-
|
|
118
|
-
if ( classicMenus.length ) {
|
|
119
|
-
await this.batchRest(
|
|
120
|
-
classicMenus.map( ( menu ) => ( {
|
|
121
|
-
method: 'DELETE',
|
|
122
|
-
path: `/wp/v2/menus/${ menu.id }?force=true`,
|
|
123
|
-
} ) )
|
|
124
|
-
);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Get latest navigation menus
|
|
130
|
-
*
|
|
131
|
-
* @param args
|
|
132
|
-
* @param args.status
|
|
133
|
-
* @return {string} Menu content.
|
|
134
|
-
*/
|
|
135
|
-
export async function getNavigationMenus(
|
|
136
|
-
this: RequestUtils,
|
|
137
|
-
args: { status: 'publish' }
|
|
138
|
-
) {
|
|
139
|
-
const navigationMenus = await this.rest< NavigationMenu[] >( {
|
|
140
|
-
method: 'GET',
|
|
141
|
-
path: `/wp/v2/navigation/`,
|
|
142
|
-
data: args,
|
|
143
|
-
} );
|
|
144
|
-
return navigationMenus;
|
|
145
|
-
}
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Internal dependencies
|
|
3
|
-
*/
|
|
4
|
-
import type { RequestUtils } from './index';
|
|
5
|
-
|
|
6
|
-
const PAGE_STATUS = [
|
|
7
|
-
'publish',
|
|
8
|
-
'future',
|
|
9
|
-
'draft',
|
|
10
|
-
'pending',
|
|
11
|
-
'private',
|
|
12
|
-
'trash',
|
|
13
|
-
] as const;
|
|
14
|
-
|
|
15
|
-
export type Page = {
|
|
16
|
-
id: number;
|
|
17
|
-
status: ( typeof PAGE_STATUS )[ number ];
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export type CreatePagePayload = {
|
|
21
|
-
title?: string;
|
|
22
|
-
content?: string;
|
|
23
|
-
status: ( typeof PAGE_STATUS )[ number ];
|
|
24
|
-
date?: string;
|
|
25
|
-
date_gmt?: string;
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
export async function deletePage( this: RequestUtils, id: number ) {
|
|
29
|
-
// https://developer.wordpress.org/rest-api/reference/pages/#delete-a-page
|
|
30
|
-
return await this.rest( {
|
|
31
|
-
method: 'DELETE',
|
|
32
|
-
path: `/wp/v2/pages/${ id }`,
|
|
33
|
-
params: {
|
|
34
|
-
force: true,
|
|
35
|
-
},
|
|
36
|
-
} );
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Delete all pages using REST API.
|
|
41
|
-
*
|
|
42
|
-
* @param this
|
|
43
|
-
*/
|
|
44
|
-
export async function deleteAllPages( this: RequestUtils ) {
|
|
45
|
-
// List all pages.
|
|
46
|
-
// https://developer.wordpress.org/rest-api/reference/pages/#list-pages
|
|
47
|
-
const pages = await this.rest< Page[] >( {
|
|
48
|
-
path: '/wp/v2/pages',
|
|
49
|
-
params: {
|
|
50
|
-
per_page: 100,
|
|
51
|
-
|
|
52
|
-
status: PAGE_STATUS.join( ',' ),
|
|
53
|
-
},
|
|
54
|
-
} );
|
|
55
|
-
|
|
56
|
-
// Delete all pages one by one.
|
|
57
|
-
// "/wp/v2/pages" not yet supports batch requests.
|
|
58
|
-
await Promise.all(
|
|
59
|
-
pages.map( ( page ) => deletePage.call( this, page.id ) )
|
|
60
|
-
);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Create a new page.
|
|
65
|
-
*
|
|
66
|
-
* @param this
|
|
67
|
-
* @param payload The page payload.
|
|
68
|
-
*/
|
|
69
|
-
export async function createPage(
|
|
70
|
-
this: RequestUtils,
|
|
71
|
-
payload: CreatePagePayload
|
|
72
|
-
) {
|
|
73
|
-
// https://developer.wordpress.org/rest-api/reference/pages/#create-a-page
|
|
74
|
-
return this.createRecord< Page >( 'pages', { ...payload } );
|
|
75
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Internal dependencies
|
|
3
|
-
*/
|
|
4
|
-
import type { RequestUtils } from './index';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Delete all pattern categories using REST API.
|
|
8
|
-
*
|
|
9
|
-
* @see https://developer.wordpress.org/rest-api/reference/categories/#list-categories
|
|
10
|
-
* @param this
|
|
11
|
-
*/
|
|
12
|
-
export async function deleteAllPatternCategories( this: RequestUtils ) {
|
|
13
|
-
// List all pattern categories.
|
|
14
|
-
// https://developer.wordpress.org/rest-api/reference/categories/#list-categories
|
|
15
|
-
const categories = await this.rest( {
|
|
16
|
-
path: '/wp/v2/wp_pattern_category',
|
|
17
|
-
params: {
|
|
18
|
-
per_page: 100,
|
|
19
|
-
},
|
|
20
|
-
} );
|
|
21
|
-
|
|
22
|
-
// Delete pattern categories.
|
|
23
|
-
// https://developer.wordpress.org/rest-api/reference/categories/#delete-a-category
|
|
24
|
-
// "/wp/v2/category" does not yet supports batch requests.
|
|
25
|
-
await this.batchRest(
|
|
26
|
-
categories.map( ( category: { id: number } ) => ( {
|
|
27
|
-
method: 'DELETE',
|
|
28
|
-
path: `/wp/v2/wp_pattern_category/${ category.id }?force=true`,
|
|
29
|
-
} ) )
|
|
30
|
-
);
|
|
31
|
-
}
|
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* External dependencies
|
|
3
|
-
*/
|
|
4
|
-
import { paramCase as kebabCase } from 'change-case';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Internal dependencies
|
|
8
|
-
*/
|
|
9
|
-
import type { RequestUtils } from './index';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Fetch the plugins from API and cache them in memory,
|
|
13
|
-
* since they are unlikely to change during testing.
|
|
14
|
-
*
|
|
15
|
-
* @param this
|
|
16
|
-
* @param forceRefetch Force refetch the installed plugins to update the cache.
|
|
17
|
-
*/
|
|
18
|
-
async function getPluginsMap( this: RequestUtils, forceRefetch = false ) {
|
|
19
|
-
if ( ! forceRefetch && this.pluginsMap ) {
|
|
20
|
-
return this.pluginsMap;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const plugins = await this.rest( {
|
|
24
|
-
path: '/wp/v2/plugins',
|
|
25
|
-
} );
|
|
26
|
-
this.pluginsMap = {};
|
|
27
|
-
for ( const plugin of plugins ) {
|
|
28
|
-
// Ideally, we should be using sanitize_title() in PHP rather than kebabCase(),
|
|
29
|
-
// but we don't have the exact port of it in JS.
|
|
30
|
-
// This is a good approximation though.
|
|
31
|
-
const slug = kebabCase( plugin.name.toLowerCase() );
|
|
32
|
-
this.pluginsMap[ slug ] = plugin.plugin;
|
|
33
|
-
}
|
|
34
|
-
return this.pluginsMap;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Finds a plugin in the plugin map.
|
|
39
|
-
*
|
|
40
|
-
* Attempts to provide a helpful error message if not found.
|
|
41
|
-
*
|
|
42
|
-
* @param slug Plugin slug.
|
|
43
|
-
* @param pluginsMap Plugins map.
|
|
44
|
-
*/
|
|
45
|
-
function getPluginFromMap(
|
|
46
|
-
slug: string,
|
|
47
|
-
pluginsMap: Record< string, string >
|
|
48
|
-
) {
|
|
49
|
-
const plugin = pluginsMap[ slug ];
|
|
50
|
-
|
|
51
|
-
if ( ! plugin ) {
|
|
52
|
-
for ( const key of Object.keys( pluginsMap ) ) {
|
|
53
|
-
if (
|
|
54
|
-
key.toLowerCase().replace( /-/g, '' ) ===
|
|
55
|
-
slug.toLowerCase().replace( /-/g, '' )
|
|
56
|
-
) {
|
|
57
|
-
throw new Error(
|
|
58
|
-
`The plugin "${ slug }" isn't installed. Did you perhaps mean "${ key }"?`
|
|
59
|
-
);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
throw new Error( `The plugin "${ slug }" isn't installed` );
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
return plugin;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Activates an installed plugin.
|
|
71
|
-
*
|
|
72
|
-
* @param this RequestUtils.
|
|
73
|
-
* @param slug Plugin slug.
|
|
74
|
-
*/
|
|
75
|
-
async function activatePlugin( this: RequestUtils, slug: string ) {
|
|
76
|
-
const pluginsMap = await this.getPluginsMap();
|
|
77
|
-
const plugin = getPluginFromMap( slug, pluginsMap );
|
|
78
|
-
|
|
79
|
-
await this.rest( {
|
|
80
|
-
method: 'PUT',
|
|
81
|
-
path: `/wp/v2/plugins/${ plugin }`,
|
|
82
|
-
data: { status: 'active' },
|
|
83
|
-
} );
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Deactivates an active plugin.
|
|
88
|
-
*
|
|
89
|
-
* @param this RequestUtils.
|
|
90
|
-
* @param slug Plugin slug.
|
|
91
|
-
*/
|
|
92
|
-
async function deactivatePlugin( this: RequestUtils, slug: string ) {
|
|
93
|
-
const pluginsMap = await this.getPluginsMap();
|
|
94
|
-
const plugin = getPluginFromMap( slug, pluginsMap );
|
|
95
|
-
|
|
96
|
-
await this.rest( {
|
|
97
|
-
method: 'PUT',
|
|
98
|
-
path: `/wp/v2/plugins/${ plugin }`,
|
|
99
|
-
data: { status: 'inactive' },
|
|
100
|
-
} );
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
export { getPluginsMap, activatePlugin, deactivatePlugin };
|