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

Commit

Permalink
Merge pull request #371 from WebDevStudios/feature/369-code-reorganiz…
Browse files Browse the repository at this point in the history
…ation-wp-functions

Feature/369 code reorganization wp functions
  • Loading branch information
Greg Rickaby authored Apr 20, 2021
2 parents df1783a + eec2ecd commit 32fb2e2
Show file tree
Hide file tree
Showing 95 changed files with 286 additions and 259 deletions.
2 changes: 1 addition & 1 deletion components/molecules/Blocks/Blocks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import displayBlock from '@/functions/wordpress/blocks/displayBlock'
import PropTypes from 'prop-types'
import React from 'react'
import displayBlock from '@/functions/displayBlock'

/**
* Render the Blocks component.
Expand Down
2 changes: 1 addition & 1 deletion components/molecules/Comments/Comments.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Text from '@/components/atoms/Inputs/Text'
import Form from '@/components/molecules/Form'
import processPostComment from '@/lib/next-api/wordpress/comments/processPostComment'
import processPostComment from '@/functions/next-api/wordpress/comments/processPostComment'
import {useSession} from 'next-auth/client'
import PropTypes from 'prop-types'
import React, {useState} from 'react'
Expand Down
9 changes: 6 additions & 3 deletions components/molecules/GravityForm/Fields/Checkbox.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import PropTypes from 'prop-types'
import {getGfFieldId, getGfHiddenClassName} from '@/functions/gravityForms'
import cn from 'classnames'
import * as Input from '@/components/atoms/Inputs'
import {
getGfFieldId,
getGfHiddenClassName
} from '@/functions/wordpress/gravityForms'
import cn from 'classnames'
import PropTypes from 'prop-types'

/**
* Merge GravityForm checkbox field data into single Array.
Expand Down
9 changes: 6 additions & 3 deletions components/molecules/GravityForm/Fields/File.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import PropTypes from 'prop-types'
import {getGfFieldId, getGfHiddenClassName} from '@/functions/gravityForms'
import {Field} from 'formik'
import InputError from '@/components/atoms/Inputs/InputError'
import styles from '@/components/atoms/Inputs/Text/Text.module.css'
import {
getGfFieldId,
getGfHiddenClassName
} from '@/functions/wordpress/gravityForms'
import cn from 'classnames'
import {Field} from 'formik'
import PropTypes from 'prop-types'

/**
* Render GravityForms File field component.
Expand Down
7 changes: 5 additions & 2 deletions components/molecules/GravityForm/Fields/Select.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import PropTypes from 'prop-types'
import {getGfFieldId, getGfHiddenClassName} from '@/functions/gravityForms'
import * as Input from '@/components/atoms/Inputs'
import {
getGfFieldId,
getGfHiddenClassName
} from '@/functions/wordpress/gravityForms'
import cn from 'classnames'
import PropTypes from 'prop-types'

/**
* Render GravityForms Select field component.
Expand Down
7 changes: 5 additions & 2 deletions components/molecules/GravityForm/Fields/Text.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import PropTypes from 'prop-types'
import * as Input from '@/components/atoms/Inputs'
import {getGfFieldId, getGfHiddenClassName} from '@/functions/gravityForms'
import {
getGfFieldId,
getGfHiddenClassName
} from '@/functions/wordpress/gravityForms'
import cn from 'classnames'
import PropTypes from 'prop-types'

/**
* Render the GravityForm Text component.
Expand Down
9 changes: 6 additions & 3 deletions components/molecules/GravityForm/Fields/Textarea.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import PropTypes from 'prop-types'
import {getGfFieldId, getGfHiddenClassName} from '@/functions/gravityForms'
import {Field} from 'formik'
import InputError from '@/components/atoms/Inputs/InputError'
import styles from '@/components/atoms/Inputs/Text/Text.module.css'
import {
getGfFieldId,
getGfHiddenClassName
} from '@/functions/wordpress/gravityForms'
import cn from 'classnames'
import {Field} from 'formik'
import PropTypes from 'prop-types'

/**
* Render GravityForms Textarea field component.
Expand Down
4 changes: 2 additions & 2 deletions components/molecules/GravityForm/GravityForm.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Form from '@/components/molecules/Form'
import getGfFormDefaults from '@/functions/gravityForms/getGfFormDefaults'
import getGfFormValidationSchema from '@/functions/gravityForms/getGfFormValidationSchema'
import getGfFormDefaults from '@/functions/wordpress/gravityForms/getGfFormDefaults'
import getGfFormValidationSchema from '@/functions/wordpress/gravityForms/getGfFormValidationSchema'
import cn from 'classnames'
import PropTypes from 'prop-types'
import React, {useState} from 'react'
Expand Down
8 changes: 3 additions & 5 deletions functions/getPagePropTypes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {
isHierarchicalPostType,
isValidPostType
} from '@/lib/wordpress/_global/postTypes'
import isHierarchicalPostType from '@/functions/wordpress/postTypes/isHierarchicalPostType'
import isValidPostType from '@/functions/wordpress/postTypes/isValidPostType'
import PropTypes from 'prop-types'

// Yoast SEO social prop types.
Expand Down Expand Up @@ -74,7 +72,7 @@ export const archivePropTypes = {
*
* @author WebDevStudios
* @param {string} postType WP post type.
* @return {object} Page prop types.
* @return {object} Page prop types.
*/
export default function getPagePropTypes(postType) {
// Check if post type is valid.
Expand Down
5 changes: 5 additions & 0 deletions functions/next-api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# /functions/next-api/

Functions for interacting with our <a href="https://nextjs.org/docs/api-routes/introduction">Next.js API Routes</a>, defined in `pages/api/`.

Subfolders should correspond to API routes, e.g., `/functions/next-api/wordpress/` matches with `/pages/api/wordpress/`.
3 changes: 3 additions & 0 deletions functions/next-api/wordpress/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# /functions/next-api/wordpress/

Functions for interacting with our WordPress-specific <a href="https://nextjs.org/docs/api-routes/introduction">Next.js API Routes</a>, defined in `/pages/api/wordpress/`.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {initializeNextApiApollo} from '@/lib/next-api/connector'
import queryArchivePosts from './queryArchivePosts'
import queryArchivePosts from '@/lib/next-api/wordpress/archive/queryArchivePosts'

/**
* Retrieve next page of posts for post type archive.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {initializeNextApiApollo} from '@/lib/next-api/connector'
import mutationAddComment from './mutationAddComment'
import mutationAddComment from '@/lib/next-api/wordpress/comments/mutationAddComment'

/**
* Add a comment to the given post. Follows established WordPress
Expand Down
3 changes: 3 additions & 0 deletions functions/wordpress/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# /functions/wordpress/

Functions for interacting with the <a href="https://www.wpgraphql.com/docs/introduction/">WordPress GraphQL API</a> or processing WordPress data.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import mutationLoginUser from '@/lib/wordpress/auth/mutationLoginUser'
import {initializeWpApollo} from '@/lib/wordpress/connector'
import mutationLoginUser from './mutationLoginUser'

/**
* Log user into WP.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import mutationRefreshAuthToken from '@/lib/wordpress/auth/mutationRefreshAuthToken'
import {initializeWpApollo} from '@/lib/wordpress/connector'
import mutationRefreshAuthToken from './mutationRefreshAuthToken'

/**
* Get a new auth token using refresh token.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import mutationRegisterUser from '@/lib/wordpress/auth/mutationRegisterUser'
import {initializeWpApollo} from '@/lib/wordpress/connector'
import mutationRegisterUser from './mutationRegisterUser'

/**
* Register a user in WP.
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import getFormById from '@/lib/wordpress/gravityForms/getFormById'
import getMediaByID from '@/lib/wordpress/media/getMediaByID'
import getGfFormById from '@/functions/wordpress/gravityForms/getGfFormById'
import getMediaByID from '@/functions/wordpress/media/getMediaByID'

/**
* Format and retrieve expanded block data.
Expand All @@ -23,7 +23,7 @@ export default async function formatBlockData(blocks) {
break
case 'gravityforms/form':
// Retrieve form data.
attributes.formData = await getFormById(attributes?.formId)
attributes.formData = await getGfFormById(attributes?.formId)
break
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import mutationInsertComment from '@/lib/wordpress/comments/mutationInsertComment'
import {initializeWpApollo} from '@/lib/wordpress/connector'
import mutationInsertComment from './mutationInsertComment'

/**
* Add a comment to the given post. Follows established WordPress
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {initializeWpApollo} from '@/lib/wordpress/connector'
import queryFormById from './queryFormById'
import queryFormById from '@/lib/wordpress/gravityForms/queryFormById'

/**
* Retrieve single form by ID.
Expand All @@ -8,7 +8,7 @@ import queryFormById from './queryFormById'
* @param {string} id Form ID.
* @return {object} Post data or error object.
*/
export default async function getFormById(id) {
export default async function getGfFormById(id) {
// Determine form global ID.
const formId = Buffer.from(`GravityFormsForm:${id}`).toString('base64')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import getGfFieldId from '@/functions/gravityForms/getGfFieldId'
import getGfFieldId from '@/functions/wordpress/gravityForms/getGfFieldId'

/**
* Assign default values for GravityForm Checkboxes.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import getGfFieldId from '@/functions/wordpress/gravityForms/getGfFieldId'
import ArraySchemaFactory from '@/functions/wordpress/gravityForms/yupSchema/ArraySchemaFactory'
import StringSchemaFactory from '@/functions/wordpress/gravityForms/yupSchema/StringSchemaFactory'
import * as Yup from 'yup'
import getGfFieldId from '@/functions/gravityForms/getGfFieldId'
import StringSchemaFactory from '@/functions/gravityForms/yupSchema/StringSchemaFactory'
import ArraySchemaFactory from '@/functions/gravityForms/yupSchema/ArraySchemaFactory'

/**
* Match field type with Yup schema object.
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {initializeWpApollo} from '@/lib/wordpress/connector'
import queryMediaAttributes from './queryMediaAttributes'
import queryMediaAttributes from '@/lib/wordpress/media/queryMediaAttributes'

/**
* Retrieve media details by ID.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import filterMenusByLocation from '@/functions/filterMenusByLocation'

// Define default menu locations.
export const menuLocations = ['primary-menu', 'footer-menu', 'mobile-menu']
import filterMenusByLocation from '@/functions/wordpress/menus/filterMenusByLocation'
import menuLocations from '@/lib/wordpress/_config/menuLocations'

/**
* Get menu data from WPGraphQL.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,8 @@
import formatDefaultSeoData from '@/functions/formatDefaultSeoData'
import getMenus from '@/functions/wordpress/menus/getMenus'
import formatDefaultSeoData from '@/functions/wordpress/seo/formatDefaultSeoData'
import {initializeWpApollo} from '@/lib/wordpress/connector'
import getMenus from '@/lib/wordpress/menus/getMenus'
import queryDefaultPageData from './queryDefaultPageData'

// Define SEO for Frontend routes.
export const frontendPageSeo = {
search: {
title: 'Search',
description: 'Search page'
},
login: {
title: 'Login',
description: 'Login page'
},
register: {
title: 'Register',
description: 'Registration page'
},
profile: {
title: 'Profile',
description: 'Profile page'
}
}
import queryDefaultPageData from '@/lib/wordpress/pages/queryDefaultPageData'
import frontendPageSeo from '@/lib/wordpress/_config/frontendPageSeo'

/**
* Retrieve data for Frontend-only route (i.e., page does not exist in WordPress).
Expand Down
32 changes: 32 additions & 0 deletions functions/wordpress/postTypes/getHeadlessConfigPage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import processPostTypeQuery from '@/functions/wordpress/postTypes/processPostTypeQuery'
import headlessConfigPageQuerySeo from '@/lib/wordpress/_config/headlessConfigPageQuerySeo'

/**
* Retrieve single page set via Headless Config.
*
* @author WebDevStudios
* @param {string} page Custom page name in config.
* @return {object} Object containing Apollo client instance and post data or error object.
*/
export default async function getHeadlessConfigPage(page) {
// Retrieve page query.
const query = headlessConfigPageQuerySeo?.[page]?.query ?? null

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

// Add custom SEO if missing.
if (!data?.post?.seo) {
data.post = {
...data?.post,
seo: {
title: `${headlessConfigPageQuerySeo[page]?.title ?? ''} - ${
data.defaultSeo?.openGraph?.siteName ?? ''
}`,
description: headlessConfigPageQuerySeo[page]?.description ?? '',
canonical: `${data.defaultSeo?.openGraph?.url ?? ''}/${page}`
}
}
}

return data
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
import formatArchiveSeoData from '@/functions/formatArchiveSeoData'
import formatDefaultSeoData from '@/functions/formatDefaultSeoData'
import getMenus from '@/functions/wordpress/menus/getMenus'
import formatArchiveSeoData from '@/functions/wordpress/seo/formatArchiveSeoData'
import formatDefaultSeoData from '@/functions/wordpress/seo/formatDefaultSeoData'
import {initializeWpApollo} from '@/lib/wordpress/connector'
import getMenus from '@/lib/wordpress/menus/getMenus'
import queryPostsArchive from '@/lib/wordpress/posts/queryPostsArchive'
import queryTeamsArchive from '@/lib/wordpress/teams/queryTeamsArchive'
import {postTypes} from './postTypes'

// Define SEO for archives.
export const archiveQuerySeo = {
post: {
query: queryPostsArchive,
title: 'Blog',
description: ''
},
team: {
query: queryTeamsArchive,
title: 'Team Members',
description: ''
}
}
import archiveQuerySeo from '@/lib/wordpress/_config/archiveQuerySeo'
import {postTypes} from '@/lib/wordpress/_config/postTypes'

/**
* Retrieve post archive.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import isHierarchicalPostType from '@/functions/wordpress/postTypes/isHierarchicalPostType'
import processPostTypeQuery from '@/functions/wordpress/postTypes/processPostTypeQuery'
import queryPageById from '@/lib/wordpress/pages/queryPageById'
import queryPostById from '@/lib/wordpress/posts/queryPostById'
import queryTeamById from '@/lib/wordpress/teams/queryTeamById'
import {isHierarchicalPostType} from './postTypes'
import processPostTypeQuery from './processPostTypeQuery'

/**
* Retrieve single post by specified identifier.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import isHierarchicalPostType from '@/functions/wordpress/postTypes/isHierarchicalPostType'
import isValidPostType from '@/functions/wordpress/postTypes/isValidPostType'
import {initializeWpApollo} from '@/lib/wordpress/connector'
import {postTypes} from '@/lib/wordpress/_config/postTypes'
import {gql} from '@apollo/client'
import {isHierarchicalPostType, isValidPostType, postTypes} from './postTypes'

/**
* Retrieve static paths by post type.
*
* @author WebDevStudios
* @param {string} postType WP post type.
* @return {object} Post type paths.
* @return {object} Post type paths.
*/
export default async function getPostTypeStaticPaths(postType) {
if (!postType || !isValidPostType(postType)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import getFrontendPage from '@/functions/wordpress/postTypes/getFrontendPage'
import getHeadlessConfigPage from '@/functions/wordpress/postTypes/getHeadlessConfigPage'
import getPostTypeArchive from '@/functions/wordpress/postTypes/getPostTypeArchive'
import getPostTypeById from '@/functions/wordpress/postTypes/getPostTypeById'
import getPostTypeTaxonomyArchive from '@/functions/wordpress/postTypes/getPostTypeTaxonomyArchive'
import {algoliaIndexName} from '@/lib/algolia/connector'
import {addApolloState} from '@/lib/apolloConfig'
import getFrontendPage, {frontendPageSeo} from './getFrontendPage'
import getPostTypeArchive, {archiveQuerySeo} from './getPostTypeArchive'
import getPostTypeById from './getPostTypeById'
import getPostTypeTaxonomyArchive from './getPostTypeTaxonomyArchive'
import getSettingsCustomPage, {
customPageQuerySeo
} from './getSettingsCustomPage'
import archiveQuerySeo from '@/lib/wordpress/_config/archiveQuerySeo'
import frontendPageSeo from '@/lib/wordpress/_config/frontendPageSeo'
import headlessConfigPageQuerySeo from '@/lib/wordpress/_config/headlessConfigPageQuerySeo'

/**
* Retrieve static props by post type.
Expand Down Expand Up @@ -106,8 +107,8 @@ export default async function getPostTypeStaticProps(
const slug = Array.isArray(params.slug) ? params.slug.join('/') : params.slug

/* -- Handle pages set via Additional Settings. -- */
if (Object.keys(customPageQuerySeo).includes(slug)) {
const {apolloClient, ...pageData} = await getSettingsCustomPage(slug)
if (Object.keys(headlessConfigPageQuerySeo).includes(slug)) {
const {apolloClient, ...pageData} = await getHeadlessConfigPage(slug)

// Display 404 error page if error encountered.
if (pageData.error && '404' !== slug) {
Expand Down
Loading

1 comment on commit 32fb2e2

@vercel
Copy link

@vercel vercel bot commented on 32fb2e2 Apr 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.