diff --git a/packages/backend/src/apps/wordpress/actions/index.js b/packages/backend/src/apps/wordpress/actions/index.js index 6f717545..d197c720 100644 --- a/packages/backend/src/apps/wordpress/actions/index.js +++ b/packages/backend/src/apps/wordpress/actions/index.js @@ -1,3 +1,4 @@ import createPost from './create-post/index.js'; +import updatePost from './update-post/index.js'; -export default [createPost]; +export default [createPost, updatePost]; diff --git a/packages/backend/src/apps/wordpress/actions/update-post/index.js b/packages/backend/src/apps/wordpress/actions/update-post/index.js new file mode 100644 index 00000000..ae149a98 --- /dev/null +++ b/packages/backend/src/apps/wordpress/actions/update-post/index.js @@ -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 }); + }, +}); diff --git a/packages/backend/src/apps/wordpress/dynamic-data/index.js b/packages/backend/src/apps/wordpress/dynamic-data/index.js index 9069e4a0..f48c0a0a 100644 --- a/packages/backend/src/apps/wordpress/dynamic-data/index.js +++ b/packages/backend/src/apps/wordpress/dynamic-data/index.js @@ -1,7 +1,15 @@ import listCategories from './list-categories/index.js'; import listMedia from './list-media/index.js'; +import listPosts from './list-posts/index.js'; import listStatuses from './list-statuses/index.js'; import listTags from './list-tags/index.js'; import listUsers from './list-users/index.js'; -export default [listCategories, listMedia, listStatuses, listTags, listUsers]; +export default [ + listCategories, + listMedia, + listPosts, + listStatuses, + listTags, + listUsers, +]; diff --git a/packages/backend/src/apps/wordpress/dynamic-data/list-posts/index.js b/packages/backend/src/apps/wordpress/dynamic-data/list-posts/index.js new file mode 100644 index 00000000..464a5469 --- /dev/null +++ b/packages/backend/src/apps/wordpress/dynamic-data/list-posts/index.js @@ -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; + }, +}; diff --git a/packages/docs/pages/apps/wordpress/actions.md b/packages/docs/pages/apps/wordpress/actions.md index 9a5d2b59..8565accc 100644 --- a/packages/docs/pages/apps/wordpress/actions.md +++ b/packages/docs/pages/apps/wordpress/actions.md @@ -3,6 +3,8 @@ favicon: /favicons/wordpress.svg items: - name: Create post desc: Creates a new post. + - name: Update post + desc: Updates a post. ---