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

Commit 6ddc0f6

Browse files
committed
Merge branch 'staging' into develop
2 parents 35ec8f2 + d5fa0e5 commit 6ddc0f6

File tree

12 files changed

+290
-57
lines changed

12 files changed

+290
-57
lines changed

.eslintrc.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ module.exports = {
3333
rules: {
3434
'func-style': ['error', 'declaration'],
3535
'jsdoc/check-indentation': 'warn',
36-
'jsdoc/check-line-alignment': ['warn', 'always'],
36+
'jsdoc/check-line-alignment': [
37+
'warn',
38+
'always',
39+
{
40+
tags: ['author', 'param', 'see']
41+
}
42+
],
3743
'jsdoc/require-param': [
3844
'warn',
3945
{

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ yarn-error.log*
3030
.env.development.local
3131
.env.test.local
3232
.env.production.local
33+
wpe.json
3334

3435
# storybook
3536
build-storybook.log

api/wordpress/_global/getPostTypeArchive.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import formatDefaultSeoData from '@/functions/formatDefaultSeoData'
66
import getMenus from '../menus/getMenus'
77

88
// Define SEO for archives.
9-
export const archiveSeo = {
9+
export const archiveQuerySeo = {
1010
post: {
11+
query: queryPostsArchive,
1112
title: 'Blog',
1213
description: ''
1314
},
1415
team: {
16+
query: queryTeamsArchive,
1517
title: 'Team Members',
1618
description: ''
1719
}
@@ -37,14 +39,8 @@ export default async function getPostTypeArchive(
3739
getNext = true,
3840
perPage = 10
3941
) {
40-
// Define single post query based on post type.
41-
const postTypeQuery = {
42-
post: queryPostsArchive,
43-
team: queryTeamsArchive
44-
}
45-
4642
// Retrieve post type query.
47-
const query = postTypeQuery?.[postType] ?? null
43+
const query = archiveQuerySeo?.[postType]?.query ?? null
4844

4945
// Get/create Apollo instance.
5046
const apolloClient = initializeWpApollo()
@@ -114,10 +110,10 @@ export default async function getPostTypeArchive(
114110
}`
115111
}
116112
: {
117-
title: `${archiveSeo?.[postType]?.title} - ${
113+
title: `${archiveQuerySeo?.[postType]?.title} - ${
118114
response.defaultSeo?.openGraph?.siteName ?? ''
119115
}`,
120-
metaDesc: archiveSeo?.[postType]?.description,
116+
metaDesc: archiveQuerySeo?.[postType]?.description,
121117
canonical: `${response.defaultSeo?.openGraph?.url ?? ''}/${
122118
postTypes?.[postType]?.route
123119
}`,

api/wordpress/_global/getPostTypeStaticProps.js

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import {algoliaIndexName} from '@/api/algolia/connector'
22
import getPostTypeById from './getPostTypeById'
3-
import getPostTypeArchive from './getPostTypeArchive'
3+
import getPostTypeArchive, {archiveQuerySeo} from './getPostTypeArchive'
44
import {addApolloState} from '@/api/apolloConfig'
55
import getFrontendPage, {frontendPageSeo} from './getFrontendPage'
6-
import getSettingsCustomPage, {customPageQuery} from './getSettingsCustomPage'
6+
import getSettingsCustomPage, {
7+
customPageQuerySeo
8+
} from './getSettingsCustomPage'
9+
import getPostTypeTaxonomyArchive from './getPostTypeTaxonomyArchive'
710

811
/**
912
* Retrieve static props by post type.
@@ -13,7 +16,7 @@ import getSettingsCustomPage, {customPageQuery} from './getSettingsCustomPage'
1316
* @param {string} postType Post Type.
1417
* @param {boolean} preview Whether requesting preview of post.
1518
* @param {object} previewData Post preview data.
16-
* @return {object} Object containing post props and revalidate setting.
19+
* @return {object} Object containing post props and revalidate setting.
1720
*/
1821
export default async function getPostTypeStaticProps(
1922
params,
@@ -73,13 +76,37 @@ export default async function getPostTypeStaticProps(
7376
})
7477
}
7578

79+
/* -- Handle taxonomy archives. -- */
80+
if (
81+
Object.keys(archiveQuerySeo).includes(postType) &&
82+
params.slug.length > 1
83+
) {
84+
const taxonomy = params.slug.shift()
85+
const taxonomySlug = params.slug.join('/')
86+
87+
const {apolloClient, ...archiveData} = await getPostTypeTaxonomyArchive(
88+
taxonomy,
89+
taxonomySlug
90+
)
91+
92+
// Merge in query results as Apollo state.
93+
return addApolloState(apolloClient, {
94+
props: {
95+
...archiveData,
96+
...sharedProps,
97+
archive: true
98+
},
99+
revalidate
100+
})
101+
}
102+
76103
/* -- Handle individual posts. -- */
77104

78105
// Handle catch-all routes.
79106
const slug = Array.isArray(params.slug) ? params.slug.join('/') : params.slug
80107

81108
/* -- Handle pages set via Additional Settings. -- */
82-
if (Object.keys(customPageQuery).includes(slug)) {
109+
if (Object.keys(customPageQuerySeo).includes(slug)) {
83110
const {apolloClient, ...pageData} = await getSettingsCustomPage(slug)
84111

85112
// Display 404 error page if error encountered.
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import {initializeWpApollo} from '../connector'
2+
import {postTypes} from './postTypes'
3+
import formatDefaultSeoData from '@/functions/formatDefaultSeoData'
4+
import getMenus from '../menus/getMenus'
5+
import queryPostsByTag from '../tags/queryPostsByTag'
6+
7+
/**
8+
* Retrieve post taxnomy archive.
9+
*
10+
* @author WebDevStudios
11+
* @param {string} taxonomy WP taxonomy type slug.
12+
* @param {string} taxonomyId WP taxonomy term slug.
13+
* @param {string} postType WP post type.
14+
* @param {string} orderBy Order by: field.
15+
* @param {string} order Order by: direction.
16+
* @param {string} cursor Start/end cursor for pagination.
17+
* @param {boolean} getNext Whether to retrieve next set of posts (true) or previous set (false).
18+
* @param {number} perPage Number of posts per page.
19+
* @return {object} Object containing Apollo client instance and post archive data or error object.
20+
*/
21+
export default async function getPostTypeTaxonomyArchive(
22+
taxonomy,
23+
taxonomyId,
24+
postType = 'post',
25+
orderBy = 'DATE',
26+
order = 'DESC',
27+
cursor = null,
28+
getNext = true,
29+
perPage = 10
30+
) {
31+
// Define single post query based on taxonomy.
32+
const postTypeQuery = {
33+
tag: queryPostsByTag
34+
}
35+
36+
// Retrieve taxonomy query.
37+
const query = postTypeQuery?.[taxonomy] ?? null
38+
39+
// Get/create Apollo instance.
40+
const apolloClient = initializeWpApollo()
41+
42+
// Set up return object.
43+
const response = {
44+
apolloClient,
45+
posts: null,
46+
pagination: null,
47+
error: false,
48+
errorMessage: null
49+
}
50+
51+
// If no query is set for given taxonomy, return error message.
52+
if (!query) {
53+
return {
54+
apolloClient,
55+
error: true,
56+
errorMessage: `Taxonomy \`${taxonomy}\` archives are not supported.`
57+
}
58+
}
59+
60+
// Determine query variables.
61+
const variables = {
62+
id: taxonomyId,
63+
first: getNext ? perPage : null, // Only used for retrieving next set.
64+
last: getNext ? null : perPage, // Only used for retrieving previous set.
65+
after: getNext ? cursor : null, // Only used for retrieving next set.
66+
before: getNext ? null : cursor, // Only used for retrieving previous set.
67+
orderBy,
68+
order
69+
}
70+
71+
// Execute query.
72+
await apolloClient
73+
.query({query, variables})
74+
.then((archive) => {
75+
const {homepageSettings, siteSeo, menus, ...archiveData} = archive.data
76+
77+
// Retrieve menus.
78+
response.menus = getMenus(menus)
79+
80+
// Retrieve default SEO data.
81+
response.defaultSeo = formatDefaultSeoData({homepageSettings, siteSeo})
82+
83+
const data = archiveData?.[taxonomy] ?? null
84+
85+
// Get post type plural name.
86+
const pluralName = postTypes?.[postType]?.pluralName
87+
88+
// Retrieve archive SEO.
89+
const archiveSeo = data?.seo
90+
91+
// Retrieve posts by post type.
92+
const {pageInfo, edges: posts} = data?.[pluralName] ?? {}
93+
94+
// Set error props if data not found.
95+
if (!posts || !pageInfo) {
96+
response.error = true
97+
response.errorMessage = `An error occurred while trying to retrieve data for ${taxonomyId} ${taxonomy} archive.`
98+
99+
return null
100+
}
101+
102+
// Flatten posts array to include inner node post data.
103+
response.posts = posts.map((post) => post.node)
104+
105+
// Structure archive SEO.
106+
response.post = {
107+
seo: {
108+
title:
109+
archiveSeo?.title ??
110+
`${taxonomyId} - ${response.defaultSeo?.openGraph?.siteName ?? ''}`,
111+
metaDesc: archiveSeo?.metaDesc ?? '',
112+
canonical:
113+
archiveSeo?.canonical ??
114+
`${response.defaultSeo?.openGraph?.url ?? ''}/${
115+
postTypes?.[postType]?.route
116+
}/${taxonomy}/${taxonomyId}`,
117+
metaRobotsNofollow: archiveSeo?.metaRobotsNofollow ?? 'follow',
118+
metaRobotsNoindex: archiveSeo?.metaRobotsNoindex ?? 'index'
119+
}
120+
}
121+
122+
// Extract pagination data.
123+
response.pagination = pageInfo
124+
})
125+
.catch((error) => {
126+
response.error = true
127+
response.errorMessage = error.message
128+
})
129+
130+
return response
131+
}

api/wordpress/_global/getSettingsCustomPage.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import queryError404Page from '../pages/queryError404Page'
22
import processPostTypeQuery from './processPostTypeQuery'
33

44
// Define single page query based on page name.
5-
export const customPageQuery = {
5+
export const customPageQuerySeo = {
66
404: {
77
query: queryError404Page,
88
title: '404 Not Found',
@@ -19,7 +19,7 @@ export const customPageQuery = {
1919
*/
2020
export default async function getSettingsCustomPage(page) {
2121
// Retrieve page query.
22-
const query = customPageQuery?.[page]?.query ?? null
22+
const query = customPageQuerySeo?.[page]?.query ?? null
2323

2424
const data = await processPostTypeQuery('page', page, query)
2525

@@ -28,10 +28,10 @@ export default async function getSettingsCustomPage(page) {
2828
data.post = {
2929
...data?.post,
3030
seo: {
31-
title: `${customPageQuery[page]?.title ?? ''} - ${
31+
title: `${customPageQuerySeo[page]?.title ?? ''} - ${
3232
data.defaultSeo?.openGraph?.siteName ?? ''
3333
}`,
34-
description: customPageQuery[page]?.description ?? '',
34+
description: customPageQuerySeo[page]?.description ?? '',
3535
canonical: `${data.defaultSeo?.openGraph?.url ?? ''}/${page}`
3636
}
3737
}

api/wordpress/posts/queryPostsArchive.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,32 @@ import seoPostFields from '../_partials/seoPostFields'
66
import defaultPageData from '../_partials/defaultPageData'
77

88
// Fragment: retrieve archive post fields.
9-
const archivePostFragment = gql`
9+
export const archivePostFragment = gql`
1010
fragment ArchivePostFields on Post {
1111
${globalPostFields}
1212
excerpt
1313
${featuredImagePostFields}
1414
}
1515
`
1616

17+
// Query partial: retrieve archive fields.
18+
export const archivePosts = `
19+
posts(
20+
first: $first
21+
last: $last
22+
after: $after
23+
before: $before
24+
where: {orderby: {field: $orderBy, order: $order}}
25+
) {
26+
${archivePageInfo}
27+
edges {
28+
node {
29+
...ArchivePostFields
30+
}
31+
}
32+
}
33+
`
34+
1735
// Query: retrieve posts archive.
1836
const queryPostsArchive = gql`
1937
query GET_POSTS_ARCHIVE(
@@ -31,20 +49,7 @@ const queryPostsArchive = gql`
3149
${seoPostFields}
3250
}
3351
}
34-
posts(
35-
first: $first
36-
last: $last
37-
after: $after
38-
before: $before
39-
where: {orderby: {field: $orderBy, order: $order}}
40-
) {
41-
${archivePageInfo}
42-
edges {
43-
node {
44-
...ArchivePostFields
45-
}
46-
}
47-
}
52+
${archivePosts}
4853
}
4954
${archivePostFragment}
5055
`

api/wordpress/tags/queryPostsByTag.js

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 defaultPageData from '../_partials/defaultPageData'
3+
import {archivePostFragment, archivePosts} from '../posts/queryPostsArchive'
4+
import seoPostFields from '../_partials/seoPostFields'
5+
6+
// Query: retrieve posts tag archive.
7+
const queryPostsByTag = gql`
8+
query GET_POSTS_BY_TAG(
9+
$first: Int
10+
$last: Int
11+
$after: String
12+
$before: String
13+
$orderBy: PostObjectsConnectionOrderbyEnum = DATE
14+
$order: OrderEnum = DESC
15+
$imageSize: MediaItemSizeEnum = THUMBNAIL
16+
$id: ID!
17+
$idType: TagIdType = SLUG
18+
) {
19+
${defaultPageData}
20+
tag(id: $id, idType: $idType) {
21+
${seoPostFields}
22+
${archivePosts}
23+
}
24+
}
25+
${archivePostFragment}
26+
`
27+
28+
export default queryPostsByTag

0 commit comments

Comments
 (0)