PluginProbe
Code Snippets / 3.6.6
Code Snippets v3.6.6
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 3.0.1 All 64 releases
code-snippets / js / components / SnippetForm / fields / ScopeInput.tsx

ScopeInput.tsx in Code Snippets 3.6.6, at js/components/SnippetForm/fields/ScopeInput.tsx

173 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { ExternalLink } from '@wordpress/components'
2 import { __ } from '@wordpress/i18n'
3 import React, { useState } from 'react'
4 import { SNIPPET_TYPES, SNIPPET_TYPE_SCOPES } from '../../../types/Snippet'
5 import { isNetworkAdmin } from '../../../utils/general'
6 import { buildShortcodeTag } from '../../../utils/shortcodes'
7 import { getSnippetType } from '../../../utils/snippets'
8 import { CopyToClipboardButton } from '../../common/CopyToClipboardButton'
9 import { useSnippetForm } from '../../../hooks/useSnippetForm'
10 import type { ShortcodeAtts } from '../../../utils/shortcodes'
11 import type { SnippetScope } from '../../../types/Snippet'
12 import type { Dispatch, SetStateAction} from 'react'
13
14 const SHORTCODE_TAG = 'code_snippet'
15
16 const SCOPE_ICONS: Record<SnippetScope, string> = {
17 'global': 'admin-site',
18 'admin': 'admin-tools',
19 'front-end': 'admin-appearance',
20 'single-use': 'clock',
21 'content': 'shortcode',
22 'head-content': 'editor-code',
23 'footer-content': 'editor-code',
24 'admin-css': 'dashboard',
25 'site-css': 'admin-customizer',
26 'site-head-js': 'media-code',
27 'site-footer-js': 'media-code'
28 }
29
30 const SCOPE_DESCRIPTIONS: Record<SnippetScope, string> = {
31 'global': __('Run snippet everywhere', 'code-snippets'),
32 'admin': __('Only run in administration area', 'code-snippets'),
33 'front-end': __('Only run on site front-end', 'code-snippets'),
34 'single-use': __('Only run once', 'code-snippets'),
35 'content': __('Only display when inserted into a post or page.', 'code-snippets'),
36 'head-content': __('Display in site <head> section.', 'code-snippets'),
37 'footer-content': __('Display at the end of the <body> section, in the footer.', 'code-snippets'),
38 'site-css': __('Site front-end styles', 'code-snippets'),
39 'admin-css': __('Administration area styles', 'code-snippets'),
40 'site-footer-js': __('Load JS at the end of the <body> section', 'code-snippets'),
41 'site-head-js': __('Load JS in the <head> section', 'code-snippets')
42 }
43
44 interface ShortcodeOptions {
45 php: boolean
46 format: boolean
47 shortcodes: boolean
48 }
49
50 const ShortcodeTag: React.FC<{ atts: ShortcodeAtts }> = ({ atts }) =>
51 <p>
52 <code className="shortcode-tag">{buildShortcodeTag(SHORTCODE_TAG, atts)}</code>
53
54 <CopyToClipboardButton
55 title={__('Copy shortcode to clipboard', 'code-snippets')}
56 text={buildShortcodeTag(SHORTCODE_TAG, atts)}
57 />
58 </p>
59
60 interface ShortcodeOptionsProps {
61 optionLabels: [keyof ShortcodeOptions, string][]
62 options: ShortcodeOptions
63 setOptions: Dispatch<SetStateAction<ShortcodeOptions>>
64 isReadOnly: boolean
65 }
66
67 const ShortcodeOptions: React.FC<ShortcodeOptionsProps> = ({
68 optionLabels,
69 options,
70 setOptions,
71 isReadOnly
72 }) =>
73 <p className="html-shortcode-options">
74 <strong>{__('Shortcode Options: ', 'code-snippets')}</strong>
75 {optionLabels.map(([option, label]) =>
76 <label key={option}>
77 <input
78 type="checkbox"
79 value={option}
80 checked={options[option]}
81 disabled={isReadOnly}
82 onChange={event =>
83 setOptions(previous => ({ ...previous, [option]: event.target.checked }))}
84 />
85 {` ${label}`}
86 </label>
87 )}
88 </p>
89
90 const ShortcodeInfo: React.FC = () => {
91 const { snippet, isReadOnly } = useSnippetForm()
92 const [options, setOptions] = useState<ShortcodeOptions>(() => ({
93 php: snippet.code.includes('<?'),
94 format: true,
95 shortcodes: false
96 }))
97
98 return 'content' === snippet.scope ?
99 <>
100 <p className="description">
101 {__('There are multiple options for inserting this snippet into a post, page or other content.', 'code-snippets')}
102 {' '}
103 {snippet.id ?
104 // eslint-disable-next-line @stylistic/max-len
105 __('You can copy the below shortcode, or use the Classic Editor button, Block editor (Pro) or Elementor widget (Pro).', 'code-snippets') :
106 // eslint-disable-next-line @stylistic/max-len
107 __('After saving, you can copy a shortcode, or use the Classic Editor button, Block editor (Pro) or Elementor widget (Pro).', 'code-snippets')}
108 {' '}
109 <ExternalLink
110 href={__('https://help.codesnippets.pro/article/50-inserting-snippets', 'code-snippets')}
111 >
112 {__('Learn more', 'code-snippets')}
113 </ExternalLink>
114 </p>
115
116 {snippet.id ?
117 <>
118 <ShortcodeTag atts={{
119 id: snippet.id,
120 network: snippet.network ?? isNetworkAdmin(),
121 ...options
122 }} />
123
124 <ShortcodeOptions
125 options={options}
126 setOptions={setOptions}
127 isReadOnly={isReadOnly}
128 optionLabels={[
129 ['php', __('Evaluate PHP code', 'code-snippets')],
130 ['format', __('Add paragraphs and formatting', 'code-snippets')],
131 ['shortcodes', __('Evaluate additional shortcode tags', 'code-snippets')]
132 ]}
133 />
134 </> : null}
135 </> : null
136 }
137
138 export const ScopeInput: React.FC = () => {
139 const { snippet, setSnippet, isReadOnly } = useSnippetForm()
140
141 return <>
142 <h2 className="screen-reader-text">{__('Scope', 'code-snippets')}</h2>
143
144 {SNIPPET_TYPES
145 .filter(type => !snippet.id || type === getSnippetType(snippet))
146 .map(type =>
147 <p key={type} className={`snippet-scope ${type}-scopes-list`}>
148 {SNIPPET_TYPE_SCOPES[type].map(scope =>
149 <label key={scope}>
150 <input
151 type="radio"
152 name="snippet_scope"
153 value={scope}
154 checked={scope === snippet.scope}
155 onChange={event =>
156 event.target.checked && setSnippet(previous => ({
157 ...previous,
158 scope
159 }))
160 }
161 disabled={isReadOnly}
162 />
163 {' '}
164 <span className={`dashicons dashicons-${SCOPE_ICONS[scope]}`}></span>
165 {` ${SCOPE_DESCRIPTIONS[scope]}`}
166 </label>)}
167
168 {'html' === type ? <ShortcodeInfo /> : null}
169 </p>
170 )}
171 </>
172 }
173