This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix corrupt Classic Template placeholders for specific products. (#7033)
* If user has created a template for a specific product, find the closest matching template details * Move Classic Template functions into Utils file and refactor function for specific template names * Classic Template utils refactor and unit tests
- Loading branch information
1 parent
788e8a5
commit ed3abbe
Showing
5 changed files
with
129 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getTemplateDetailsBySlug } from '../utils'; | ||
|
||
const TEMPLATES = { | ||
'single-product': { | ||
title: 'Single Product Title', | ||
placeholder: 'Single Product Placeholder', | ||
}, | ||
'archive-product': { | ||
title: 'Product Archive Title', | ||
placeholder: 'Product Archive Placeholder', | ||
}, | ||
'archive-product': { | ||
title: 'Product Archive Title', | ||
placeholder: 'Product Archive Placeholder', | ||
}, | ||
'taxonomy-product_cat': { | ||
title: 'Product Taxonomy Title', | ||
placeholder: 'Product Taxonomy Placeholder', | ||
}, | ||
}; | ||
|
||
describe( 'getTemplateDetailsBySlug', function () { | ||
it( 'should return single-product object when given an exact match', () => { | ||
expect( | ||
getTemplateDetailsBySlug( 'single-product', TEMPLATES ) | ||
).toBeTruthy(); | ||
expect( | ||
getTemplateDetailsBySlug( 'single-product', TEMPLATES ) | ||
).toStrictEqual( TEMPLATES[ 'single-product' ] ); | ||
} ); | ||
|
||
it( 'should return single-product object when given a partial match', () => { | ||
expect( | ||
getTemplateDetailsBySlug( 'single-product-hoodie', TEMPLATES ) | ||
).toBeTruthy(); | ||
expect( | ||
getTemplateDetailsBySlug( 'single-product-hoodie', TEMPLATES ) | ||
).toStrictEqual( TEMPLATES[ 'single-product' ] ); | ||
} ); | ||
|
||
it( 'should return null object when given an incorrect match', () => { | ||
expect( getTemplateDetailsBySlug( 'void', TEMPLATES ) ).toBeNull(); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type TemplateDetails = Record< string, Record< string, string > >; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { Block } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { TEMPLATES } from './constants'; | ||
import { TemplateDetails } from './types'; | ||
|
||
// Finds the most appropriate template details object for specific template keys such as single-product-hoodie. | ||
export function getTemplateDetailsBySlug( | ||
parsedTemplate: string, | ||
templates: TemplateDetails | ||
) { | ||
const templateKeys = Object.keys( templates ); | ||
let templateDetails = null; | ||
|
||
for ( let i = 0; templateKeys.length > i; i++ ) { | ||
const keyToMatch = parsedTemplate.substr( 0, templateKeys[ i ].length ); | ||
const maybeTemplate = templates[ keyToMatch ]; | ||
if ( maybeTemplate ) { | ||
templateDetails = maybeTemplate; | ||
break; | ||
} | ||
} | ||
|
||
return templateDetails; | ||
} | ||
|
||
export function isClassicTemplateBlockRegisteredWithAnotherTitle( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
block: Block< any > | undefined, | ||
parsedTemplate: string | ||
) { | ||
const templateDetails = getTemplateDetailsBySlug( | ||
parsedTemplate, | ||
TEMPLATES | ||
); | ||
return block?.title !== templateDetails?.title; | ||
} | ||
|
||
export function hasTemplateSupportForClassicTemplateBlock( | ||
parsedTemplate: string, | ||
templates: TemplateDetails | ||
): boolean { | ||
return getTemplateDetailsBySlug( parsedTemplate, templates ) ? true : false; | ||
} |