PluginProbe
Code Snippets / 3.9.6
Code Snippets v3.9.6
4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 All 65 releases
code-snippets / js / utils / snippets / snippets.ts

snippets.ts in Code Snippets 3.9.6, at js/utils/snippets/snippets.ts

56 lines 1.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { __ } from '@wordpress/i18n'
2 import { parseSnippetObject } from './objects'
3 import type { Snippet, SnippetType } from '../../types/Snippet'
4
5 const PRO_TYPES = new Set<SnippetType>(['css', 'js', 'cond'])
6
7 export const createSnippetObject = (fields: unknown): Snippet =>
8 parseSnippetObject(fields)
9
10 export const getSnippetType = ({ scope }: Pick<Snippet, 'scope'>): SnippetType => {
11 switch (true) {
12 case scope.endsWith('-css'):
13 return 'css'
14
15 case scope.endsWith('-js'):
16 return 'js'
17
18 case scope.endsWith('content'):
19 return 'html'
20
21 case 'condition' === scope:
22 return 'cond'
23
24 default:
25 return 'php'
26 }
27 }
28
29 export const validateSnippet = (snippet: Snippet): undefined | string => {
30 const missingTitle = '' === snippet.name.trim()
31 const missingCode = '' === snippet.code.trim()
32
33 switch (true) {
34 case missingCode && missingTitle:
35 return __('This snippet has no code or title.', 'code-snippets')
36
37 case missingCode:
38 return __('This snippet has no snippet code.', 'code-snippets')
39
40 case missingTitle:
41 return __('This snippet has no title.', 'code-snippets')
42
43 default:
44 return undefined
45 }
46 }
47
48 export const isCondition = (snippet: Pick<Snippet, 'scope'>): boolean =>
49 'condition' === snippet.scope
50
51 export const isProSnippet = (snippet: Pick<Snippet, 'scope'>): boolean =>
52 PRO_TYPES.has(getSnippetType(snippet))
53
54 export const isProType = (type: SnippetType): boolean =>
55 PRO_TYPES.has(type)
56