feat(wordpress): add update post action
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
import createPost from './create-post/index.js';
|
import createPost from './create-post/index.js';
|
||||||
|
import updatePost from './update-post/index.js';
|
||||||
|
|
||||||
export default [createPost];
|
export default [createPost, updatePost];
|
||||||
|
284
packages/backend/src/apps/wordpress/actions/update-post/index.js
Normal file
284
packages/backend/src/apps/wordpress/actions/update-post/index.js
Normal file
@@ -0,0 +1,284 @@
|
|||||||
|
import defineAction from '../../../../helpers/define-action.js';
|
||||||
|
import isEmpty from 'lodash/isEmpty.js';
|
||||||
|
import omitBy from 'lodash/omitBy.js';
|
||||||
|
|
||||||
|
export default defineAction({
|
||||||
|
name: 'Update post',
|
||||||
|
key: 'updatePost',
|
||||||
|
description: 'Updates a post.',
|
||||||
|
arguments: [
|
||||||
|
{
|
||||||
|
label: 'Post',
|
||||||
|
key: 'postId',
|
||||||
|
type: 'dropdown',
|
||||||
|
required: false,
|
||||||
|
description: 'Choose a post to update.',
|
||||||
|
variables: true,
|
||||||
|
source: {
|
||||||
|
type: 'query',
|
||||||
|
name: 'getDynamicData',
|
||||||
|
arguments: [
|
||||||
|
{
|
||||||
|
name: 'key',
|
||||||
|
value: 'listPosts',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Title',
|
||||||
|
key: 'title',
|
||||||
|
type: 'string',
|
||||||
|
required: true,
|
||||||
|
description: '',
|
||||||
|
variables: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Content',
|
||||||
|
key: 'content',
|
||||||
|
type: 'string',
|
||||||
|
required: false,
|
||||||
|
description: '',
|
||||||
|
variables: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Excerpt',
|
||||||
|
key: 'excerpt',
|
||||||
|
type: 'string',
|
||||||
|
required: false,
|
||||||
|
description: '',
|
||||||
|
variables: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Password',
|
||||||
|
key: 'password',
|
||||||
|
type: 'string',
|
||||||
|
required: false,
|
||||||
|
description: 'A password to protect access to the content and excerpt.',
|
||||||
|
variables: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Author',
|
||||||
|
key: 'author',
|
||||||
|
type: 'dropdown',
|
||||||
|
required: false,
|
||||||
|
description: '',
|
||||||
|
variables: true,
|
||||||
|
source: {
|
||||||
|
type: 'query',
|
||||||
|
name: 'getDynamicData',
|
||||||
|
arguments: [
|
||||||
|
{
|
||||||
|
name: 'key',
|
||||||
|
value: 'listUsers',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Featured Media',
|
||||||
|
key: 'featuredMedia',
|
||||||
|
type: 'dropdown',
|
||||||
|
required: false,
|
||||||
|
description: '',
|
||||||
|
variables: true,
|
||||||
|
source: {
|
||||||
|
type: 'query',
|
||||||
|
name: 'getDynamicData',
|
||||||
|
arguments: [
|
||||||
|
{
|
||||||
|
name: 'key',
|
||||||
|
value: 'listMedia',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Comment Status',
|
||||||
|
key: 'commentStatus',
|
||||||
|
type: 'dropdown',
|
||||||
|
required: false,
|
||||||
|
description: '',
|
||||||
|
variables: true,
|
||||||
|
options: [
|
||||||
|
{ label: 'Open', value: 'open' },
|
||||||
|
{ label: 'Closed', value: 'closed' },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Ping Status',
|
||||||
|
key: 'pingStatus',
|
||||||
|
type: 'dropdown',
|
||||||
|
required: false,
|
||||||
|
description: '',
|
||||||
|
variables: true,
|
||||||
|
options: [
|
||||||
|
{ label: 'Open', value: 'open' },
|
||||||
|
{ label: 'Closed', value: 'closed' },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Format',
|
||||||
|
key: 'format',
|
||||||
|
type: 'dropdown',
|
||||||
|
required: false,
|
||||||
|
description: '',
|
||||||
|
variables: true,
|
||||||
|
options: [
|
||||||
|
{ label: 'Standard', value: 'standard' },
|
||||||
|
{ label: 'Aside', value: 'aside' },
|
||||||
|
{ label: 'Chat', value: 'chat' },
|
||||||
|
{ label: 'Gallery', value: 'gallery' },
|
||||||
|
{ label: 'Link', value: 'link' },
|
||||||
|
{ label: 'Image', value: 'image' },
|
||||||
|
{ label: 'Quote', value: 'quote' },
|
||||||
|
{ label: 'Status', value: 'status' },
|
||||||
|
{ label: 'Status', value: 'status' },
|
||||||
|
{ label: 'Video', value: 'video' },
|
||||||
|
{ label: 'Audio', value: 'audio' },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Sticky',
|
||||||
|
key: 'sticky',
|
||||||
|
type: 'dropdown',
|
||||||
|
required: false,
|
||||||
|
description: '',
|
||||||
|
variables: true,
|
||||||
|
options: [
|
||||||
|
{ label: 'False', value: 'false' },
|
||||||
|
{ label: 'True', value: 'true' },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Categories',
|
||||||
|
key: 'categoryIds',
|
||||||
|
type: 'dynamic',
|
||||||
|
required: false,
|
||||||
|
description: '',
|
||||||
|
fields: [
|
||||||
|
{
|
||||||
|
label: 'Category',
|
||||||
|
key: 'categoryId',
|
||||||
|
type: 'dropdown',
|
||||||
|
required: false,
|
||||||
|
variables: true,
|
||||||
|
source: {
|
||||||
|
type: 'query',
|
||||||
|
name: 'getDynamicData',
|
||||||
|
arguments: [
|
||||||
|
{
|
||||||
|
name: 'key',
|
||||||
|
value: 'listCategories',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Tags',
|
||||||
|
key: 'tagIds',
|
||||||
|
type: 'dynamic',
|
||||||
|
required: false,
|
||||||
|
description: '',
|
||||||
|
fields: [
|
||||||
|
{
|
||||||
|
label: 'Tag',
|
||||||
|
key: 'tagId',
|
||||||
|
type: 'dropdown',
|
||||||
|
required: false,
|
||||||
|
variables: true,
|
||||||
|
source: {
|
||||||
|
type: 'query',
|
||||||
|
name: 'getDynamicData',
|
||||||
|
arguments: [
|
||||||
|
{
|
||||||
|
name: 'key',
|
||||||
|
value: 'listTags',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Status',
|
||||||
|
key: 'status',
|
||||||
|
type: 'dropdown',
|
||||||
|
required: false,
|
||||||
|
description: '',
|
||||||
|
variables: true,
|
||||||
|
source: {
|
||||||
|
type: 'query',
|
||||||
|
name: 'getDynamicData',
|
||||||
|
arguments: [
|
||||||
|
{
|
||||||
|
name: 'key',
|
||||||
|
value: 'listStatuses',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Date',
|
||||||
|
key: 'date',
|
||||||
|
type: 'string',
|
||||||
|
required: false,
|
||||||
|
description: "Post publish date in the site's timezone",
|
||||||
|
variables: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
|
||||||
|
async run($) {
|
||||||
|
const {
|
||||||
|
postId,
|
||||||
|
title,
|
||||||
|
content,
|
||||||
|
excerpt,
|
||||||
|
password,
|
||||||
|
author,
|
||||||
|
featuredMedia,
|
||||||
|
commentStatus,
|
||||||
|
pingStatus,
|
||||||
|
format,
|
||||||
|
sticky,
|
||||||
|
categoryIds,
|
||||||
|
tagIds,
|
||||||
|
status,
|
||||||
|
date,
|
||||||
|
} = $.step.parameters;
|
||||||
|
|
||||||
|
const allCategoryIds = categoryIds
|
||||||
|
?.map((categoryId) => categoryId.categoryId)
|
||||||
|
.filter(Boolean);
|
||||||
|
|
||||||
|
const allTagIds = tagIds?.map((tagId) => tagId.tagId).filter(Boolean);
|
||||||
|
|
||||||
|
let body = {
|
||||||
|
title,
|
||||||
|
content,
|
||||||
|
excerpt,
|
||||||
|
password,
|
||||||
|
author,
|
||||||
|
featured_media: featuredMedia,
|
||||||
|
comment_status: commentStatus,
|
||||||
|
ping_status: pingStatus,
|
||||||
|
format,
|
||||||
|
sticky,
|
||||||
|
categories: allCategoryIds,
|
||||||
|
tags: allTagIds,
|
||||||
|
status,
|
||||||
|
date,
|
||||||
|
};
|
||||||
|
|
||||||
|
body = omitBy(body, isEmpty);
|
||||||
|
|
||||||
|
const response = await $.http.post(
|
||||||
|
`?rest_route=/wp/v2/posts/${postId}`,
|
||||||
|
body
|
||||||
|
);
|
||||||
|
|
||||||
|
$.setActionItem({ raw: response.data });
|
||||||
|
},
|
||||||
|
});
|
@@ -1,7 +1,15 @@
|
|||||||
import listCategories from './list-categories/index.js';
|
import listCategories from './list-categories/index.js';
|
||||||
import listMedia from './list-media/index.js';
|
import listMedia from './list-media/index.js';
|
||||||
|
import listPosts from './list-posts/index.js';
|
||||||
import listStatuses from './list-statuses/index.js';
|
import listStatuses from './list-statuses/index.js';
|
||||||
import listTags from './list-tags/index.js';
|
import listTags from './list-tags/index.js';
|
||||||
import listUsers from './list-users/index.js';
|
import listUsers from './list-users/index.js';
|
||||||
|
|
||||||
export default [listCategories, listMedia, listStatuses, listTags, listUsers];
|
export default [
|
||||||
|
listCategories,
|
||||||
|
listMedia,
|
||||||
|
listPosts,
|
||||||
|
listStatuses,
|
||||||
|
listTags,
|
||||||
|
listUsers,
|
||||||
|
];
|
||||||
|
@@ -0,0 +1,37 @@
|
|||||||
|
export default {
|
||||||
|
name: 'List posts',
|
||||||
|
key: 'listPosts',
|
||||||
|
|
||||||
|
async run($) {
|
||||||
|
const posts = {
|
||||||
|
data: [],
|
||||||
|
};
|
||||||
|
|
||||||
|
const params = {
|
||||||
|
page: 1,
|
||||||
|
per_page: 100,
|
||||||
|
order: 'desc',
|
||||||
|
};
|
||||||
|
|
||||||
|
let totalPages = 1;
|
||||||
|
do {
|
||||||
|
const { data, headers } = await $.http.get('?rest_route=/wp/v2/posts', {
|
||||||
|
params,
|
||||||
|
});
|
||||||
|
|
||||||
|
params.page = params.page + 1;
|
||||||
|
totalPages = Number(headers['x-wp-totalpages']);
|
||||||
|
|
||||||
|
if (data) {
|
||||||
|
for (const post of data) {
|
||||||
|
posts.data.push({
|
||||||
|
value: post.id,
|
||||||
|
name: post.title.rendered,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while (params.page <= totalPages);
|
||||||
|
|
||||||
|
return posts;
|
||||||
|
},
|
||||||
|
};
|
@@ -3,6 +3,8 @@ favicon: /favicons/wordpress.svg
|
|||||||
items:
|
items:
|
||||||
- name: Create post
|
- name: Create post
|
||||||
desc: Creates a new post.
|
desc: Creates a new post.
|
||||||
|
- name: Update post
|
||||||
|
desc: Updates a post.
|
||||||
---
|
---
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
|
Reference in New Issue
Block a user