PluginProbe
Code Snippets / 3.10.0-beta.1
Code Snippets v3.10.0-beta.1
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.10.0-beta.1, at js/utils/snippets/snippets.ts

115 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { __, sprintf } from '@wordpress/i18n'
2 import { isLicensed, isNetworkAdmin } from '../screen'
3 import { buildUrl } from '../urls'
4 import { parseSnippetObject } from './objects'
5 import type { SnippetSchema } from '../../types/schema/SnippetSchema'
6 import type { Snippet, SnippetScope, SnippetType } from '../../types/Snippet'
7
8 const PRO_TYPES = new Set<SnippetType>(['css', 'js', 'cond'])
9
10 export const SNIPPET_TYPE_LABELS: Record<SnippetType, string> = {
11 php: __('Functions', 'code-snippets'),
12 html: __('Content', 'code-snippets'),
13 css: __('Styles', 'code-snippets'),
14 js: __('Scripts', 'code-snippets'),
15 cond: __('Conditions', 'code-snippets')
16 }
17
18 export const createSnippetObject = (fields?: Partial<Snippet> | Partial<SnippetSchema>): Snippet =>
19 parseSnippetObject(fields)
20
21 export const cloneSnippetObject = (snippet: Snippet): Snippet =>
22 createSnippetObject({
23 ...snippet,
24 id: 0,
25 active: false,
26 // translators: %s: snippet title.
27 name: sprintf(__('%s [CLONE]', 'code-snippets'), snippet.name)
28 })
29
30 export const getSnippetType = ({ scope }: Pick<Snippet, 'scope'>): SnippetType => {
31 switch (true) {
32 case scope.endsWith('-css'):
33 return 'css'
34
35 case scope.endsWith('-js'):
36 return 'js'
37
38 case scope.endsWith('content'):
39 return 'html'
40
41 case 'condition' === scope:
42 return 'cond'
43
44 default:
45 return 'php'
46 }
47 }
48
49 export const getSnippetEditUrl = (snippet?: Pick<Snippet, 'id'>): string | undefined =>
50 snippet?.id
51 ? buildUrl(window.CODE_SNIPPETS?.urls.edit, { id: snippet.id })
52 : window.CODE_SNIPPETS?.urls.addNew
53
54 export const getSnippetAddNewUrl = (type?: SnippetType): string | undefined =>
55 type && (isLicensed() || !isProType(type))
56 ? buildUrl(window.CODE_SNIPPETS?.urls.addNew, { type })
57 : window.CODE_SNIPPETS?.urls.addNew
58
59 export const getSnippetDisplayName = (snippet: Pick<Snippet, 'name' | 'id' | 'scope'>): string =>
60 '' === snippet.name.trim()
61 // translators: %s: snippet identifier.
62 ? sprintf(isCondition(snippet) ? __('Condition #%d', 'code-snippets') : __('Snippet #%d', 'code-snippets'), snippet.id)
63 : snippet.name
64
65 export const validateSnippet = (snippet: Snippet): undefined | string => {
66 const missingTitle = '' === snippet.name.trim()
67 const missingCode = '' === snippet.code.trim()
68
69 switch (true) {
70 case missingCode && missingTitle:
71 return __('This snippet has no code or title.', 'code-snippets')
72
73 case missingCode:
74 return __('This snippet has no snippet code.', 'code-snippets')
75
76 case missingTitle:
77 return __('This snippet has no title.', 'code-snippets')
78
79 default:
80 return undefined
81 }
82 }
83
84 export const isCondition = (snippet: { scope?: SnippetScope }): boolean =>
85 'condition' === snippet.scope
86
87 export const isProSnippet = (snippet: Pick<Snippet, 'scope'>): boolean =>
88 PRO_TYPES.has(getSnippetType(snippet))
89
90 export const isProType = (type: SnippetType): boolean =>
91 PRO_TYPES.has(type)
92
93 export const isSnippetActive = (
94 snippet: Snippet,
95 activeByCondition: Map<Snippet['id'], Snippet[]>
96 ): boolean =>
97 'cond' === getSnippetType(snippet)
98 ? 0 < (activeByCondition.get(snippet.id)?.length ?? 0)
99 : snippet.active
100
101 /**
102 * Whether the snippet belongs to the network and is not shared with subsites,
103 * making it read-only outside of the network admin.
104 */
105 export const isNetworkOnlySnippet = (snippet: Snippet): boolean =>
106 !isNetworkAdmin() && snippet.network && !snippet.shared_network
107
108 /**
109 * Whether the current user is able to modify the snippet, either directly or
110 * through actions such as cloning and trashing.
111 */
112 export const canModifySnippet = (snippet: Snippet): boolean =>
113 !isNetworkOnlySnippet(snippet) &&
114 !(snippet.shared_network && !window.CODE_SNIPPETS_MANAGE?.hasNetworkCap)
115