Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Wordpress Node): Excerpt and Featured Media support for Posts #14033

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions packages/nodes-base/nodes/Wordpress/PostDescription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,20 @@ export const postFields: INodeProperties[] = [
default: 'draft',
description: 'A named status for the post',
},
{
displayName: 'Excerpt',
name: 'excerpt',
type: 'string',
default: '',
description: 'The excerpt for the post',
},
{
displayName: 'Featured Media ID',
name: 'featuredMediaId',
type: 'number',
default: '',
description: 'The ID of the featured media for the post',
},
{
displayName: 'Comment Status',
name: 'commentStatus',
Expand Down Expand Up @@ -413,6 +427,20 @@ export const postFields: INodeProperties[] = [
default: 'draft',
description: 'A named status for the post',
},
{
displayName: 'Excerpt',
name: 'excerpt',
type: 'string',
default: '',
description: 'The excerpt for the post',
},
{
displayName: 'Featured Media ID',
name: 'featuredMediaId',
type: 'number',
default: '',
description: 'The ID of the featured media for the post',
},
{
displayName: 'Comment Status',
name: 'commentStatus',
Expand Down
2 changes: 2 additions & 0 deletions packages/nodes-base/nodes/Wordpress/PostInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export interface IPost {
slug?: string;
password?: string;
status?: string;
excerpt?: string;
featured_media?: number;
comment_status?: string;
ping_status?: string;
format?: string;
Expand Down
12 changes: 12 additions & 0 deletions packages/nodes-base/nodes/Wordpress/Wordpress.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ export class Wordpress implements INodeType {
if (additionalFields.status) {
body.status = additionalFields.status as string;
}
if (additionalFields.excerpt) {
body.excerpt = additionalFields.excerpt as string;
}
if (additionalFields.featuredMediaId) {
body.featured_media = additionalFields.featuredMediaId as number;
}
if (additionalFields.commentStatus) {
body.comment_status = additionalFields.commentStatus as string;
}
Expand Down Expand Up @@ -214,6 +220,12 @@ export class Wordpress implements INodeType {
if (updateFields.status) {
body.status = updateFields.status as string;
}
if (updateFields.excerpt) {
body.excerpt = updateFields.excerpt as string;
}
if (updateFields.featuredMediaId) {
body.featured_media = updateFields.featuredMediaId as number;
}
if (updateFields.commentStatus) {
body.comment_status = updateFields.commentStatus as string;
}
Expand Down
69 changes: 69 additions & 0 deletions packages/nodes-base/nodes/Wordpress/test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import type { IExecuteFunctions, ILoadOptionsFunctions } from 'n8n-workflow/src';

import { wordpressApiRequest } from '../GenericFunctions';

describe('wordpressApiRequest', () => {
const mockHttpRequestWithAuthentication = jest.fn();
const mockContext = {
helpers: {
requestWithAuthentication: mockHttpRequestWithAuthentication,
},
getCredentials: jest.fn().mockImplementation(async (type) => {
if (type === 'wordpressApi') {
return {
url: 'https://example.com',
username: 'testuser',
password: 'testpassword',
allowUnauthorizedCerts: false,
};
}
return {};
}),
} as unknown as IExecuteFunctions | ILoadOptionsFunctions;

beforeEach(() => {
jest.clearAllMocks();
});

it('should make a POST request with body and return data', async () => {
const mockResponse = {
id: 123,
date: '2023-10-15T14:30:45',
date_gmt: '2023-10-15T14:30:45',
guid: { rendered: 'https://example.com/test-post' },
modified: '2023-10-15T14:30:45',
modified_gmt: '2023-10-15T14:30:45',
slug: 'my-test-post-title',
status: 'publish',
type: 'post',
link: 'https://example.com/my-test-post-title',
title: { rendered: 'My Test Post Title' },
content: {
rendered: 'This is the full content of the blog post with <b>HTML</b> formatting.',
},
excerpt: { rendered: 'This is a short excerpt for the post' },
featured_media: 42,
};
mockHttpRequestWithAuthentication.mockResolvedValue(mockResponse);

const requestBody = {
title: 'My Test Post Title',
excerpt: 'This is a short excerpt for the post',
content: 'This is the full content of the blog post with <b>HTML</b> formatting.',
featured_media: 42,
};

const result = await wordpressApiRequest.call(mockContext, 'POST', '/posts', requestBody);

expect(mockHttpRequestWithAuthentication).toHaveBeenCalledWith(
'wordpressApi',
expect.objectContaining({
method: 'POST',
uri: 'https://example.com/wp-json/wp/v2/posts',
body: requestBody,
}),
);

expect(result).toEqual(mockResponse);
});
});