Wordpress node

This commit is contained in:
Ricardo Espinoza 2020-01-02 17:34:48 -05:00
parent c490d4bc84
commit f16b8a57a0
4 changed files with 862 additions and 372 deletions

View file

@ -40,24 +40,24 @@ export async function wordpressApiRequest(this: IExecuteFunctions | IExecuteSing
}
}
export async function intercomApiRequestAllItems(this: IExecuteFunctions, propertyName: string, method: string, endpoint: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
export async function wordpressApiRequestAllItems(this: IExecuteFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const returnData: IDataObject[] = [];
let responseData;
query.per_page = 10;
query.page = 1;
query.page = 0;
let uri: string | undefined;
do {
responseData = await wordpressApiRequest.call(this, method, endpoint, body, query, uri);
uri = responseData.pages.next;
query.page++;
responseData = await wordpressApiRequest.call(this, method, endpoint, body, query, uri, { resolveWithFullResponse: true });
returnData.push.apply(returnData, responseData.body);
} while (
responseData.pages !== undefined &&
responseData.pages.next !== undefined &&
responseData.pages.next !== null
responseData.headers['x-wp-totalpages'] !== undefined &&
parseInt(responseData.headers['x-wp-totalpages'], 10) < query.page
);
return returnData;

View file

@ -16,7 +16,27 @@ export const postOperations = [
{
name: 'Create',
value: 'create',
description: ``,
description: 'Create a post',
},
{
name: 'Update',
value: 'update',
description: 'Update a post',
},
{
name: 'Get',
value: 'get',
description: 'Get a post',
},
{
name: 'Get All',
value: 'getAll',
description: 'Get all posts',
},
{
name: 'Delete',
value: 'delete',
description: 'Delete a post',
},
],
default: 'create',
@ -29,7 +49,6 @@ export const postFields = [
/* -------------------------------------------------------------------------- */
/* post:create */
/* -------------------------------------------------------------------------- */
{
displayName: 'Title',
name: 'title',
@ -69,6 +88,9 @@ export const postFields = [
displayName: 'Content',
name: 'content',
type: 'string',
typeOptions: {
alwaysOpenEditWindow: true,
},
default: '',
description: 'The content for the post',
},
@ -116,8 +138,8 @@ export const postFields = [
description: 'A named status for the post.',
},
{
displayName: 'Content Status',
name: 'contentStatus',
displayName: 'Comment Status',
name: 'commentStatus',
type: 'options',
options: [
{
@ -149,11 +171,60 @@ export const postFields = [
default: '',
description: 'Whether or not comments are open on the post.',
},
{
displayName: 'Format',
name: 'format',
type: 'options',
options: [
{
name: 'Standard',
value: 'standard'
},
{
name: 'Aside',
value: 'aside'
},
{
name: 'Chat',
value: 'chat'
},
{
name: 'Gallery',
value: 'gallery'
},
{
name: 'Link',
value: 'link'
},
{
name: 'Image',
value: 'image'
},
{
name: 'Quote',
value: 'quote'
},
{
name: 'Status',
value: 'status'
},
{
name: 'Video',
value: 'video'
},
{
name: 'Audio',
value: 'audio'
},
],
default: '',
description: 'Whether or not comments are open on the post.',
},
{
displayName: 'Sticky',
name: 'sticky',
type: 'boolean',
default: '',
default: false,
description: 'Whether or not the object should be treated as sticky.',
},
{
@ -166,187 +237,574 @@ export const postFields = [
default: [],
description: 'The terms assigned to the object in the category taxonomy.',
},
{
displayName: 'Tags',
name: 'tags',
type: 'multiOptions',
typeOptions: {
loadOptionsMethod: 'getTags',
},
default: [],
description: 'The terms assigned to the object in the post_tag taxonomy.',
},
]
},
/* -------------------------------------------------------------------------- */
/* post:update */
/* -------------------------------------------------------------------------- */
{
displayName: 'Post ID',
name: 'postId',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'post',
],
operation: [
'update',
]
},
},
description: 'Unique identifier for the object.',
},
{
displayName: 'Update Fields',
name: 'updateFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
resource: [
'post',
],
operation: [
'update',
],
},
},
options: [
{
displayName: 'Title',
name: 'title',
type: 'string',
default: '',
description: 'The title for the post',
},
{
displayName: 'Content',
name: 'content',
type: 'string',
typeOptions: {
alwaysOpenEditWindow: true,
},
default: '',
description: 'The content for the post',
},
{
displayName: 'Slug',
name: 'slug',
type: 'string',
default: '',
description: 'An alphanumeric identifier for the object unique to its type.',
},
{
displayName: 'Password',
name: 'password',
type: 'string',
default: '',
description: 'A password to protect access to the content and excerpt.',
},
{
displayName: 'Status',
name: 'status',
type: 'options',
options: [
{
name: 'Publish',
value: 'publish'
},
{
name: 'Future',
value: 'future'
},
{
name: 'Draft',
value: 'draft'
},
{
name: 'Pending',
value: 'pending'
},
{
name: 'Private',
value: 'private'
},
],
default: '',
description: 'A named status for the post.',
},
{
displayName: 'Comment Status',
name: 'commentStatus',
type: 'options',
options: [
{
name: 'Open',
value: 'open'
},
{
name: 'Close',
value: 'closed'
},
],
default: '',
description: 'Whether or not comments are open on the post.',
},
{
displayName: 'Ping Status',
name: 'pingStatus',
type: 'options',
options: [
{
name: 'Open',
value: 'open'
},
{
name: 'Close',
value: 'closed'
},
],
default: '',
description: 'Whether or not comments are open on the post.',
},
{
displayName: 'Format',
name: 'format',
type: 'options',
options: [
{
name: 'Standard',
value: 'standard'
},
{
name: 'Aside',
value: 'aside'
},
{
name: 'Chat',
value: 'chat'
},
{
name: 'Gallery',
value: 'gallery'
},
{
name: 'Link',
value: 'link'
},
{
name: 'Image',
value: 'image'
},
{
name: 'Quote',
value: 'quote'
},
{
name: 'Status',
value: 'status'
},
{
name: 'Video',
value: 'video'
},
{
name: 'Audio',
value: 'audio'
},
],
default: '',
description: 'Whether or not comments are open on the post.',
},
{
displayName: 'Sticky',
name: 'sticky',
type: 'boolean',
default: false,
description: 'Whether or not the object should be treated as sticky.',
},
{
displayName: 'Categories',
name: 'categories',
type: 'multiOptions',
typeOptions: {
loadOptionsMethod: 'getCategories',
},
default: [],
description: 'The terms assigned to the object in the category taxonomy.',
},
{
displayName: 'Tags',
name: 'tags',
type: 'multiOptions',
typeOptions: {
loadOptionsMethod: 'getTags',
},
default: [],
description: 'The terms assigned to the object in the post_tag taxonomy.',
},
]
},
/* -------------------------------------------------------------------------- */
/* post:get */
/* -------------------------------------------------------------------------- */
{
displayName: 'Post ID',
name: 'postId',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'post',
],
operation: [
'get',
]
},
},
description: 'Unique identifier for the object.',
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add Option',
default: {},
displayOptions: {
show: {
resource: [
'post',
],
operation: [
'get',
],
},
},
options: [
{
displayName: 'Password',
name: 'password',
type: 'string',
default: '',
description: 'The password for the post if it is password protected.',
},
{
displayName: 'Context',
name: 'context',
type: 'options',
options: [
{
name: 'View',
value: 'view',
},
{
name: 'Embed',
value: 'embed',
},
{
name: 'Edit',
value: 'edit',
},
],
default: 'view',
description: 'Scope under which the request is made; determines fields present in response.',
},
]
},
/* -------------------------------------------------------------------------- */
/* post:getAll */
/* -------------------------------------------------------------------------- */
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
displayOptions: {
show: {
resource: [
'post',
],
operation: [
'getAll',
],
},
},
default: false,
description: 'If all results should be returned or only up to a given limit.',
},
{
displayName: 'Limit',
name: 'limit',
type: 'number',
displayOptions: {
show: {
resource: [
'post',
],
operation: [
'getAll',
],
returnAll: [
false,
],
},
},
typeOptions: {
minValue: 1,
maxValue: 10,
},
default: 5,
description: 'How many results to return.',
},
{
displayName: 'Filters',
name: 'filters',
type: 'collection',
placeholder: 'Add Filter',
default: {},
displayOptions: {
show: {
resource: [
'post',
],
operation: [
'getAll',
],
},
},
options: [
{
displayName: 'Context',
name: 'context',
type: 'options',
options: [
{
name: 'View',
value: 'view',
},
{
name: 'Embed',
value: 'embed',
},
{
name: 'Edit',
value: 'edit',
},
],
default: [],
description: 'Scope under which the request is made; determines fields present in response.',
},
{
displayName: 'Order By',
name: 'orderBy',
type: 'options',
options: [
{
name: 'Author',
value: 'author',
},
{
name: 'Date',
value: 'date',
},
{
name: 'ID',
value: 'id',
},
{
name: 'ID',
value: 'id',
},
{
name: 'Include',
value: 'include',
},
{
name: 'Modified',
value: 'modified',
},
{
name: 'Parent',
value: 'parent',
},
{
name: 'Relevance',
value: 'relevance',
},
{
name: 'Slug',
value: 'slug',
},
{
name: 'Include Slugs',
value: 'includeSlugs',
},
{
name: 'Title',
value: 'title',
},
],
default: [],
description: 'Sort collection by object attribute.',
},
{
displayName: 'Order',
name: 'order',
type: 'options',
options: [
{
name: 'Desc',
value: 'desc',
},
{
name: 'Asc',
value: 'asc',
},
],
default: 'desc',
description: 'Order sort attribute ascending or descending.',
},
{
displayName: 'Search',
name: 'search',
type: 'string',
default: '',
description: 'Limit results to those matching a string.',
},
{
displayName: 'After',
name: 'after',
type: 'dateTime',
default: '',
description: 'Limit response to posts published after a given ISO8601 compliant date.',
},
{
displayName: 'Before',
name: 'before',
type: 'dateTime',
default: '',
description: 'Limit response to posts published before a given ISO8601 compliant date.',
},
{
displayName: 'Author',
name: 'author',
type: 'multiOptions',
default: [],
typeOptions: {
loadOptionsMethod: 'getAuthors',
},
description: 'Limit result set to posts assigned to specific authors.',
},
{
displayName: 'Categories',
name: 'categories',
type: 'multiOptions',
default: [],
typeOptions: {
loadOptionsMethod: 'getCategories',
},
description: 'Limit result set to all items that have the specified term assigned in the categories taxonomy.',
},
{
displayName: 'Exclude Categories',
name: 'excludedCategories',
type: 'multiOptions',
default: [],
typeOptions: {
loadOptionsMethod: 'getCategories',
},
description: 'Limit result set to all items except those that have the specified term assigned in the categories taxonomy.',
},
{
displayName: 'Tags',
name: 'tags',
type: 'multiOptions',
default: [],
typeOptions: {
loadOptionsMethod: 'getTags',
},
description: 'Limit result set to all items that have the specified term assigned in the tags taxonomy.',
},
{
displayName: 'Exclude Tags',
name: 'excludedTags',
type: 'multiOptions',
default: [],
typeOptions: {
loadOptionsMethod: 'getTags',
},
description: 'Limit result set to all items except those that have the specified term assigned in the tags taxonomy.',
},
{
displayName: 'Sticky',
name: 'sticky',
type: 'boolean',
default: false,
description: 'Limit result set to items that are sticky.',
},
/* -------------------------------------------------------------------------- */
/* user:alias */
/* -------------------------------------------------------------------------- */
{
displayName: 'ID',
name: 'id',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'user',
],
operation: [
'alias',
]
},
},
description: 'The old unique identifier of the user',
},
{
displayName: 'New ID',
name: 'newId',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'user',
],
operation: [
'alias',
]
},
},
description: 'The new unique identifier of the user',
]
},
/* -------------------------------------------------------------------------- */
/* user:unsubscribe */
/* post:delete */
/* -------------------------------------------------------------------------- */
{
displayName: 'ID',
name: 'id',
displayName: 'Post ID',
name: 'postId',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'user',
],
operation: [
'unsubscribe',
]
},
},
description: 'The unique identifier of the user',
},
/* -------------------------------------------------------------------------- */
/* user:resubscribe */
/* -------------------------------------------------------------------------- */
{
displayName: 'ID',
name: 'id',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'user',
],
operation: [
'resubscribe',
]
},
},
description: 'The unique identifier of the user',
},
/* -------------------------------------------------------------------------- */
/* user:delete */
/* -------------------------------------------------------------------------- */
{
displayName: 'ID',
name: 'id',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'user',
'post',
],
operation: [
'delete',
]
},
},
description: 'The unique identifier of the user',
description: 'Unique identifier for the object.',
},
/* -------------------------------------------------------------------------- */
/* user:addTags */
/* -------------------------------------------------------------------------- */
{
displayName: 'ID',
name: 'id',
type: 'string',
required: true,
default: '',
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add Option',
default: {},
displayOptions: {
show: {
resource: [
'user',
'post',
],
operation: [
'addTags',
]
},
},
description: 'The unique identifier of the user',
},
{
displayName: 'Tags',
name: 'tags',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'user',
'delete',
],
operation: [
'addTags',
]
},
},
description: 'Tags to add separated by ","',
},
/* -------------------------------------------------------------------------- */
/* user:removeTags */
/* -------------------------------------------------------------------------- */
{
displayName: 'ID',
name: 'id',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'user',
],
operation: [
'removeTags',
]
options: [
{
displayName: 'Force',
name: 'force',
type: 'boolean',
default: false,
description: 'Whether to bypass trash and force deletion.',
},
},
description: 'The unique identifier of the user',
},
{
displayName: 'Tags',
name: 'tags',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'user',
],
operation: [
'removeTags',
]
},
},
description: 'Tags to remove separated by ","',
]
},
] as INodeProperties[];

View file

@ -0,0 +1,15 @@
export interface IPost {
id?: number;
title?: string;
content?: string;
slug?: string;
password?: string;
status?: string;
comment_status?: string;
ping_status?: string;
format?: string;
sticky?: boolean;
categories?: number[];
tags?: number[];
}

View file

@ -10,12 +10,15 @@ import {
INodePropertyOptions,
} from 'n8n-workflow';
import {
wordpressApiRequest,
wordpressApiRequest, wordpressApiRequestAllItems,
} from './GenericFunctions';
import {
postOperations,
postFields,
} from './PostDescription';
import {
IPost,
} from './PostInterface';
export class Wordpress implements INodeType {
description: INodeTypeDescription = {
@ -66,9 +69,9 @@ export class Wordpress implements INodeType {
const returnData: INodePropertyOptions[] = [];
let categories;
try {
categories = await wordpressApiRequest.call(this, 'GET', '/categories', {});
categories = await wordpressApiRequestAllItems.call(this, 'GET', '/categories', {});
} catch (err) {
throw new Error(`Mandrill Error: ${err}`);
throw new Error(`Wordpress Error: ${err}`);
}
for (const category of categories) {
const categoryName = category.name;
@ -79,19 +82,54 @@ export class Wordpress implements INodeType {
value: categoryId,
});
}
return returnData;
}
},
// Get all the available tags to display them to user so that he can
// select them easily
async getTags(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
let tags;
try {
tags = await wordpressApiRequestAllItems.call(this, 'GET', '/tags', {});
} catch (err) {
throw new Error(`Wordpress Error: ${err}`);
}
for (const tag of tags) {
const tagName = tag.name;
const tagId = tag.id;
returnData.push({
name: tagName,
value: tagId,
});
}
return returnData;
},
// Get all the available authors to display them to user so that he can
// select them easily
async getAuthors(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
let authors;
try {
authors = await wordpressApiRequestAllItems.call(this, 'GET', '/users', {}, { who: 'authors' });
} catch (err) {
throw new Error(`Wordpress Error: ${err}`);
}
for (const author of authors) {
const authorName = author.name;
const authorId = author.id;
returnData.push({
name: authorName,
value: authorId,
});
}
return returnData;
},
},
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const credentials = this.getCredentials('flowApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
const items = this.getInputData();
const returnData: IDataObject[] = [];
const length = items.length as unknown as number;
@ -100,203 +138,182 @@ export class Wordpress implements INodeType {
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
// for (let i = 0; i < length; i++) {
// if (resource === 'task') {
// //https://developer.getflow.com/api/#tasks_create-task
// if (operation === 'create') {
// const workspaceId = this.getNodeParameter('workspaceId', i) as string;
// const name = this.getNodeParameter('name', i) as string;
// const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
// const body: ITask = {
// organization_id: credentials.organizationId as number,
// };
// const task: TaskInfo = {
// name,
// workspace_id: parseInt(workspaceId, 10)
// };
// if (additionalFields.ownerId) {
// task.owner_id = parseInt(additionalFields.ownerId as string, 10);
// }
// if (additionalFields.listId) {
// task.list_id = parseInt(additionalFields.listId as string, 10);
// }
// if (additionalFields.startsOn) {
// task.starts_on = additionalFields.startsOn as string;
// }
// if (additionalFields.dueOn) {
// task.due_on = additionalFields.dueOn as string;
// }
// if (additionalFields.mirrorParentSubscribers) {
// task.mirror_parent_subscribers = additionalFields.mirrorParentSubscribers as boolean;
// }
// if (additionalFields.mirrorParentTags) {
// task.mirror_parent_tags = additionalFields.mirrorParentTags as boolean;
// }
// if (additionalFields.noteContent) {
// task.note_content = additionalFields.noteContent as string;
// }
// if (additionalFields.noteMimeType) {
// task.note_mime_type = additionalFields.noteMimeType as string;
// }
// if (additionalFields.parentId) {
// task.parent_id = parseInt(additionalFields.parentId as string, 10);
// }
// if (additionalFields.positionList) {
// task.position_list = additionalFields.positionList as number;
// }
// if (additionalFields.positionUpcoming) {
// task.position_upcoming = additionalFields.positionUpcoming as number;
// }
// if (additionalFields.position) {
// task.position = additionalFields.position as number;
// }
// if (additionalFields.sectionId) {
// task.section_id = additionalFields.sectionId as number;
// }
// if (additionalFields.tags) {
// task.tags = (additionalFields.tags as string).split(',');
// }
// body.task = task;
// try {
// responseData = await flowApiRequest.call(this, 'POST', '/tasks', body);
// responseData = responseData.task;
// } catch (err) {
// throw new Error(`Flow Error: ${err.message}`);
// }
// }
// //https://developer.getflow.com/api/#tasks_update-a-task
// if (operation === 'update') {
// const workspaceId = this.getNodeParameter('workspaceId', i) as string;
// const taskId = this.getNodeParameter('taskId', i) as string;
// const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
// const body: ITask = {
// organization_id: credentials.organizationId as number,
// };
// const task: TaskInfo = {
// workspace_id: parseInt(workspaceId, 10),
// id: parseInt(taskId, 10),
// };
// if (updateFields.name) {
// task.name = updateFields.name as string;
// }
// if (updateFields.ownerId) {
// task.owner_id = parseInt(updateFields.ownerId as string, 10);
// }
// if (updateFields.listId) {
// task.list_id = parseInt(updateFields.listId as string, 10);
// }
// if (updateFields.startsOn) {
// task.starts_on = updateFields.startsOn as string;
// }
// if (updateFields.dueOn) {
// task.due_on = updateFields.dueOn as string;
// }
// if (updateFields.mirrorParentSubscribers) {
// task.mirror_parent_subscribers = updateFields.mirrorParentSubscribers as boolean;
// }
// if (updateFields.mirrorParentTags) {
// task.mirror_parent_tags = updateFields.mirrorParentTags as boolean;
// }
// if (updateFields.noteContent) {
// task.note_content = updateFields.noteContent as string;
// }
// if (updateFields.noteMimeType) {
// task.note_mime_type = updateFields.noteMimeType as string;
// }
// if (updateFields.parentId) {
// task.parent_id = parseInt(updateFields.parentId as string, 10);
// }
// if (updateFields.positionList) {
// task.position_list = updateFields.positionList as number;
// }
// if (updateFields.positionUpcoming) {
// task.position_upcoming = updateFields.positionUpcoming as number;
// }
// if (updateFields.position) {
// task.position = updateFields.position as number;
// }
// if (updateFields.sectionId) {
// task.section_id = updateFields.sectionId as number;
// }
// if (updateFields.tags) {
// task.tags = (updateFields.tags as string).split(',');
// }
// if (updateFields.completed) {
// task.completed = updateFields.completed as boolean;
// }
// body.task = task;
// try {
// responseData = await flowApiRequest.call(this, 'PUT', `/tasks/${taskId}`, body);
// responseData = responseData.task;
// } catch (err) {
// throw new Error(`Flow Error: ${err.message}`);
// }
// }
// //https://developer.getflow.com/api/#tasks_get-task
// if (operation === 'get') {
// const taskId = this.getNodeParameter('taskId', i) as string;
// const filters = this.getNodeParameter('filters', i) as IDataObject;
// qs.organization_id = credentials.organizationId as number;
// if (filters.include) {
// qs.include = (filters.include as string[]).join(',');
// }
// try {
// responseData = await flowApiRequest.call(this,'GET', `/tasks/${taskId}`, {}, qs);
// } catch (err) {
// throw new Error(`Flow Error: ${err.message}`);
// }
// }
// //https://developer.getflow.com/api/#tasks_get-tasks
// if (operation === 'getAll') {
// const returnAll = this.getNodeParameter('returnAll', i) as boolean;
// const filters = this.getNodeParameter('filters', i) as IDataObject;
// qs.organization_id = credentials.organizationId as number;
// if (filters.include) {
// qs.include = (filters.include as string[]).join(',');
// }
// if (filters.order) {
// qs.order = filters.order as string;
// }
// if (filters.workspaceId) {
// qs.workspace_id = filters.workspaceId as string;
// }
// if (filters.createdBefore) {
// qs.created_before = filters.createdBefore as string;
// }
// if (filters.createdAfter) {
// qs.created_after = filters.createdAfter as string;
// }
// if (filters.updateBefore) {
// qs.updated_before = filters.updateBefore as string;
// }
// if (filters.updateAfter) {
// qs.updated_after = filters.updateAfter as string;
// }
// if (filters.deleted) {
// qs.deleted = filters.deleted as boolean;
// }
// if (filters.cleared) {
// qs.cleared = filters.cleared as boolean;
// }
// try {
// if (returnAll === true) {
// responseData = await FlowApiRequestAllItems.call(this, 'tasks', 'GET', '/tasks', {}, qs);
// } else {
// qs.limit = this.getNodeParameter('limit', i) as number;
// responseData = await flowApiRequest.call(this, 'GET', '/tasks', {}, qs);
// responseData = responseData.tasks;
// }
// } catch (err) {
// throw new Error(`Flow Error: ${err.message}`);
// }
// }
// }
// if (Array.isArray(responseData)) {
// returnData.push.apply(returnData, responseData as IDataObject[]);
// } else {
// returnData.push(responseData as IDataObject);
// }
// }
for (let i = 0; i < length; i++) {
if (resource === 'post') {
//https://developer.wordpress.org/rest-api/reference/posts/#create-a-post
if (operation === 'create') {
const title = this.getNodeParameter('title', i) as string;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
const body: IPost = {
title,
};
if (additionalFields.content) {
body.content = additionalFields.content as string;
}
if (additionalFields.slug) {
body.slug = additionalFields.slug as string;
}
if (additionalFields.password) {
body.password = additionalFields.password as string;
}
if (additionalFields.status) {
body.status = additionalFields.status as string;
}
if (additionalFields.commentStatus) {
body.comment_status = additionalFields.commentStatus as string;
}
if (additionalFields.pingStatus) {
body.ping_status = additionalFields.pingStatus as string;
}
if (additionalFields.sticky) {
body.sticky = additionalFields.sticky as boolean;
}
if (additionalFields.categories) {
body.categories = additionalFields.categories as number[];
}
if (additionalFields.tags) {
body.tags = additionalFields.tags as number[];
}
if (additionalFields.format) {
body.format = additionalFields.format as string;
}
try{
responseData = await wordpressApiRequest.call(this, 'POST', '/posts', body);
} catch (err) {
throw new Error(`Wordpress Error: ${err.message}`);
}
}
//https://developer.wordpress.org/rest-api/reference/posts/#update-a-post
if (operation === 'update') {
const postId = this.getNodeParameter('postId', i) as string;
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
const body: IPost = {
id: parseInt(postId, 10),
};
if (updateFields.title) {
body.title = updateFields.title as string;
}
if (updateFields.content) {
body.content = updateFields.content as string;
}
if (updateFields.slug) {
body.slug = updateFields.slug as string;
}
if (updateFields.password) {
body.password = updateFields.password as string;
}
if (updateFields.status) {
body.status = updateFields.status as string;
}
if (updateFields.commentStatus) {
body.comment_status = updateFields.commentStatus as string;
}
if (updateFields.pingStatus) {
body.ping_status = updateFields.pingStatus as string;
}
if (updateFields.sticky) {
body.sticky = updateFields.sticky as boolean;
}
if (updateFields.categories) {
body.categories = updateFields.categories as number[];
}
if (updateFields.tags) {
body.tags = updateFields.tags as number[];
}
if (updateFields.format) {
body.format = updateFields.format as string;
}
try {
responseData = await wordpressApiRequest.call(this, 'POST', `/posts/${postId}`, body);
} catch (err) {
throw new Error(`Wordpress Error: ${err.message}`);
}
}
//https://developer.wordpress.org/rest-api/reference/posts/#retrieve-a-post
if (operation === 'get') {
const postId = this.getNodeParameter('postId', i) as string;
const options = this.getNodeParameter('options', i) as IDataObject;
options.id = postId;
if (options.password) {
qs.password = options.password as string;
}
if (options.context) {
qs.context = options.context as string;
}
try {
responseData = await wordpressApiRequest.call(this,'GET', `/posts/${postId}`, {}, qs);
} catch (err) {
throw new Error(`Wordpress Error: ${err.message}`);
}
}
//https://developer.wordpress.org/rest-api/reference/posts/#list-posts
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
const filters = this.getNodeParameter('filters', i) as IDataObject;
if (filters.context) {
qs.context = filters.context as string;
}
if (filters.orderBy) {
qs.orderby = filters.orderBy as string;
}
if (filters.order) {
qs.order = filters.order as string;
}
if (filters.search) {
qs.search = filters.search as string;
}
if (filters.after) {
qs.after = filters.after as string;
}
if (filters.author) {
qs.author = filters.author as number[];
}
if (filters.categories) {
qs.categories = filters.categories as number[];
}
if (filters.excludedCategories) {
qs.categories_exclude = filters.excludedCategories as number[];
}
if (filters.tags) {
qs.tags = filters.tags as number[];
}
if (filters.excludedTags) {
qs.tags_exclude = filters.excludedTags as number[];
}
if (filters.sticky) {
qs.sticky = filters.sticky as boolean;
}
try {
if (returnAll === true) {
responseData = await wordpressApiRequestAllItems.call(this, 'GET', '/posts', {}, qs);
} else {
qs.per_page = this.getNodeParameter('limit', i) as number;
responseData = await wordpressApiRequest.call(this, 'GET', '/posts', {}, qs);
}
} catch (err) {
throw new Error(`Wordpress Error: ${err.message}`);
}
}
//https://developer.wordpress.org/rest-api/reference/posts/#delete-a-post
if (operation === 'delete') {
const postId = this.getNodeParameter('postId', i) as string;
const options = this.getNodeParameter('options', i) as IDataObject;
if (options.force) {
qs.force = options.force as boolean;
}
try {
responseData = await wordpressApiRequest.call(this, 'DELETE', `/posts/${postId}`, {}, qs);
} catch (err) {
throw new Error(`Wordpress Error: ${err.message}`);
}
}
}
if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]);
} else {
returnData.push(responseData as IDataObject);
}
}
return [this.helpers.returnJsonArray(returnData)];
}
}