Skip to content

Commit

Permalink
Add option to toggle post fetching in WP API tools
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulAdamDavis committed Nov 9, 2023
1 parent 4ada1e2 commit 4a730ba
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
3 changes: 3 additions & 0 deletions packages/mg-wp-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ It's possible to pass more options, in order to achieve a better migration file
- Provide a JSON file with users. Contents should be a JSON array of objects that match
the format returned by the Wordpress API, including the following keys:
"id", "slug", "name", "description", "email" and "url".
- **`--posts**`**
- boolean - default: `true`
- Import posts
- **`--pages**`**
- boolean - default: `true`
- Import pages
Expand Down
14 changes: 8 additions & 6 deletions packages/mg-wp-api/lib/fetch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import WPAPI from 'wpapi';

const discover = async (url, {apiUser, usersJSON, pages, limit, cpt}) => {
const discover = async (url, {apiUser, usersJSON, posts, pages, limit, cpt}) => {
const requestOptions = {endpoint: `${url}/wp-json`};

if (apiUser && apiUser.username && apiUser.password) {
Expand Down Expand Up @@ -29,9 +29,11 @@ const discover = async (url, {apiUser, usersJSON, pages, limit, cpt}) => {

values.site = site;

const postsData = await site.posts().perPage(limit);
values.totals.posts = postsData._paging && postsData._paging.total ? postsData._paging.total : 0;
values.batches.posts = postsData._paging && postsData._paging.totalPages ? postsData._paging.totalPages : 0;
if (posts) {
const postsData = await site.posts().perPage(limit);
values.totals.posts = postsData._paging && postsData._paging.total ? postsData._paging.total : 0;
values.batches.posts = postsData._paging && postsData._paging.totalPages ? postsData._paging.totalPages : 0;
}

if (pages) {
const pageData = await site.pages().perPage(limit);
Expand Down Expand Up @@ -87,7 +89,7 @@ const buildTasks = (fileCache, tasks, api, type, limit, isAuthRequest, logger) =
const tasks = async (url, ctx) => {
const {logger} = ctx;
const {apiUser} = ctx || {};
const {pages, limit, cpt} = ctx.options;
const {pages, posts, limit, cpt} = ctx.options;
const {usersJSON} = ctx || null;
let isAuthRequest = false;

Expand All @@ -99,7 +101,7 @@ const tasks = async (url, ctx) => {
isAuthRequest = true;
}

const api = await discover(url, {apiUser, usersJSON, limit, cpt, pages});
const api = await discover(url, {apiUser, usersJSON, posts, pages, limit, cpt});

const theTasks = [];

Expand Down
4 changes: 4 additions & 0 deletions packages/migrate/commands/wp-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ const setup = (sywac) => {
defaultValue: null,
desc: 'Provide a JSON file with users'
});
sywac.boolean('--posts', {
defaultValue: true,
desc: 'Import posts'
});
sywac.boolean('--pages', {
defaultValue: true,
desc: 'Import pages'
Expand Down

0 comments on commit 4a730ba

Please sign in to comment.