mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
✨ Wordpress node
This commit is contained in:
@@ -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)];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user