Skip to content
This repository was archived by the owner on Feb 27, 2024. It is now read-only.

Commit 130ae11

Browse files
committed
Merge branch 'staging' into develop
2 parents 967e47b + 5d17780 commit 130ae11

File tree

82 files changed

+1741
-540
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+1741
-540
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import {gql} from '@apollo/client'
2+
import {wpDataEndpoints} from '@/api/wordpress/connector'
3+
import commentsFields from '@/api/wordpress/_partials/commentsFields'
4+
5+
// Mutation: Add a comment to the given post
6+
const mutationAddComment = gql`
7+
mutation ADD_COMMENT(
8+
$author: String
9+
$authorEmail: String
10+
$authorUrl: String
11+
$postId: Int!
12+
$content: String!
13+
) {
14+
postComment (
15+
author: $author,
16+
authorEmail: $authorEmail,
17+
authorUrl: $authorUrl,
18+
postId: $postId,
19+
content: $content
20+
) @rest(type: "Comments", path: "${wpDataEndpoints.postComment}?{args}") {
21+
comment {
22+
${commentsFields}
23+
}
24+
}
25+
}
26+
`
27+
28+
export default mutationAddComment
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import mutationAddComment from './mutationAddComment'
2+
import {initializeFeApollo} from '../../connector'
3+
4+
/**
5+
* Post a comment to the given post. Follows established WordPress
6+
* behavior for posting comments:
7+
*
8+
* If this is an authenticated request (i.e. "logged in"), the `author`
9+
* fields will be ignored in favor of the logged-in user's information.
10+
*
11+
* If comment moderation is turned on, the `data.createComment.comment`
12+
* object may be `null`. This does not necessarily indicate an error;
13+
* the comment may be held for moderation.
14+
*
15+
* If the comment does not need manual approval, it will be returned
16+
* with this query.
17+
*
18+
* @author WebDevStudios
19+
* @param {string} author Name of the comment author.
20+
* @param {string} authorEmail Email for the comment author.
21+
* @param {string} authorUrl URL/website for the comment author.
22+
* @param {number} postId Database ID for the post being commented on.
23+
* @param {string} content Content of the comment.
24+
* @return {object} Comment data or error object.
25+
*/
26+
export default async function postComment(
27+
author,
28+
authorEmail,
29+
authorUrl,
30+
postId,
31+
content
32+
) {
33+
const apolloClient = initializeFeApollo()
34+
35+
return apolloClient
36+
.mutate({
37+
mutation: mutationAddComment,
38+
variables: {
39+
author,
40+
authorEmail,
41+
authorUrl,
42+
postId,
43+
content
44+
}
45+
})
46+
.then(
47+
(response) =>
48+
response?.data?.postComment ?? {
49+
error: true,
50+
errorMessage: `An error occurred while trying to post the comment.`
51+
}
52+
)
53+
.catch((error) => {
54+
return {
55+
error: true,
56+
errorMessage: error.message
57+
}
58+
})
59+
}

api/wordpress/_global/getPostTypeArchive.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export const archiveSeo = {
5252
* @param {string} cursor Start/end cursor for pagination.
5353
* @param {boolean} getNext Whether to retrieve next set of posts (true) or previous set (false).
5454
* @param {number} perPage Number of posts per page.
55-
* @return {object} Object containing Apollo client instance and post archive data or error object.
55+
* @return {object} Object containing Apollo client instance and post archive data or error object.
5656
*/
5757
export default async function getPostTypeArchive(
5858
postType,

api/wordpress/_global/getPostTypeById.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import getMenus from '../menus/getMenus'
2020
* @param {number | string} id Post identifier.
2121
* @param {string} idType Type of ID.
2222
* @param {string} preview Whether query is for a regular post view (null), a preview check (basic), or full post preview (full).
23-
* @return {object} Object containing Apollo client instance and post data or error object.
23+
* @return {object} Object containing Apollo client instance and post data or error object.
2424
*/
2525
export default async function getPostTypeById(
2626
postType,

api/wordpress/_global/getPostTypeStaticPaths.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {initializeWpApollo} from '../connector'
77
*
88
* @author WebDevStudios
99
* @param {string} postType WP post type.
10-
* @return {object} Post type paths.
10+
* @return {object} Post type paths.
1111
*/
1212
export default async function getPostTypeStaticPaths(postType) {
1313
if (!postType || !isValidPostType(postType)) {

api/wordpress/_global/getPostTypeStaticProps.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import getFrontendPage, {frontendPageSeo} from './getFrontendPage'
1212
* @param {string} postType Post Type.
1313
* @param {boolean} preview Whether requesting preview of post.
1414
* @param {object} previewData Post preview data.
15-
* @return {object} Object containing post props and revalidate setting.
15+
* @return {object} Object containing post props and revalidate setting.
1616
*/
1717
export default async function getPostTypeStaticProps(
1818
params,
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import {initializeWpApollo} from '../connector'
2+
import mutationCommentToPost from '../comments/mutationCommentToPost'
3+
4+
/**
5+
* Post a comment to the given post. Follows established WordPress
6+
* behavior for posting comments:
7+
*
8+
* If this is an authenticated request (i.e. "logged in"), the `author`
9+
* fields will be ignored in favor of the logged-in user's information.
10+
*
11+
* If comment moderation is turned on, the `data.createComment.comment`
12+
* object may be `null`. This does not necessarily indicate an error;
13+
* the comment may be held for moderation.
14+
*
15+
* If the comment does not need manual approval, it will be returned
16+
* with this query.
17+
*
18+
* @author WebDevStudios
19+
* @param {string} author Name of the comment author.
20+
* @param {string} authorEmail Email for the comment author.
21+
* @param {string} authorUrl URL/website for the comment author.
22+
* @param {number} postId Database ID for the post being commented on.
23+
* @param {string} content Content of the comment.
24+
* @return {object} Comment data or error object.
25+
*/
26+
export default async function postCommentToPost(
27+
author,
28+
authorEmail,
29+
authorUrl,
30+
postId,
31+
content
32+
) {
33+
// Get/create Apollo instance.
34+
const apolloClient = initializeWpApollo()
35+
36+
// Set up return object.
37+
const response = {
38+
apolloClient,
39+
comment: null
40+
}
41+
42+
// Determine query variables.
43+
const variables = {
44+
author,
45+
authorEmail,
46+
authorUrl,
47+
postId: parseInt(postId),
48+
content
49+
}
50+
51+
// Execute query.
52+
await apolloClient
53+
.mutate({mutation: mutationCommentToPost, variables})
54+
.then((comment) => {
55+
const {data} = comment
56+
// Set error props if data not found.
57+
if (!data?.createComment) {
58+
response.error = true
59+
response.errorMessage = `An error occurred while trying to post the comment.`
60+
61+
return null
62+
}
63+
64+
response.comment = data.createComment.comment
65+
})
66+
.catch((error) => {
67+
response.error = true
68+
response.errorMessage = error.message
69+
})
70+
71+
return response
72+
}

api/wordpress/_global/postTypes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export const hierarchicalPostTypes = ['page']
5050
*
5151
* @author WebDevStudios
5252
* @param {string} postType WP post type.
53-
* @return {boolean} Whether provided post type is valid.
53+
* @return {boolean} Whether provided post type is valid.
5454
*/
5555
export function isValidPostType(postType) {
5656
return Object.keys(postTypes).includes(postType)
@@ -61,7 +61,7 @@ export function isValidPostType(postType) {
6161
*
6262
* @author WebDevStudios
6363
* @param {string} postType WP post type.
64-
* @return {boolean} Whether provided post type is hierarchical.
64+
* @return {boolean} Whether provided post type is hierarchical.
6565
*/
6666
export function isHierarchicalPostType(postType) {
6767
return hierarchicalPostTypes.includes(postType)
Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
// Query partial: retrieve comment fields.
22
const commentsFields = `
3-
edges {
4-
node {
5-
databaseId
6-
content(format: RENDERED)
7-
parentDatabaseId
8-
approved
9-
id
10-
date
11-
parentId
12-
type
3+
databaseId
4+
content(format: RENDERED)
5+
parentDatabaseId
6+
approved
7+
id
8+
date
9+
parentId
10+
type
11+
author {
12+
node {
13+
name
14+
url
15+
}
1316
}
14-
}
1517
`
1618
export default commentsFields

api/wordpress/_partials/commentsPostFields.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import commentsFields from './commentsFields'
33
// Query partial: retrieve comment post fields.
44
const commentsPostFields = `
55
comments(first: 10) {
6-
${commentsFields}
6+
edges {
7+
node {
8+
${commentsFields}
9+
}
10+
}
711
}
812
`
913
export default commentsPostFields

0 commit comments

Comments
 (0)