PluginProbe
Code Snippets / 3.10.2
Code Snippets v3.10.2
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.2, at js/utils/snippets/snippets.ts

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