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

Commit fd15c3b

Browse files
committed
Merge branch 'staging' into feature/114-file-upload-field
2 parents 1728226 + 47803c0 commit fd15c3b

17 files changed

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

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/_global/postTypes.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
// Define valid WP post types (singular and plural GraphQL names).
22
export const postTypes = {
3-
comment: {
4-
pluralName: 'comments',
5-
route: null
6-
},
73
mediaItem: {
84
pluralName: 'mediaItems',
95
route: null

api/wordpress/_partials/seoPostFields.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// Query partial: retrieve SEO post fields.
22
const seoPostFields = `
33
seo {
4+
breadcrumbs {
5+
text
6+
url
7+
}
48
canonical
59
title
610
metaDesc
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 category archive.
7+
const queryPostsByCategory = gql`
8+
query GET_POSTS_BY_CATEGORY(
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: CategoryIdType = SLUG
18+
) {
19+
${defaultPageData}
20+
category(id: $id, idType: $idType) {
21+
${seoPostFields}
22+
${archivePosts}
23+
}
24+
}
25+
${archivePostFragment}
26+
`
27+
28+
export default queryPostsByCategory

0 commit comments

Comments
 (0)