Initial commit: WordPress wp-content (themes, plugins, languages)

- Theme: momentry (custom theme with REST API routes)
- Plugins: code-snippets (contains all API proxies)
- Languages: zh_TW translations
- Excludes: cache, backups, uploads, logs
This commit is contained in:
OpenCode
2026-05-29 19:07:56 +08:00
commit 09ef1f000f
6521 changed files with 867163 additions and 0 deletions

View File

@@ -0,0 +1,92 @@
import { addQueryArgs } from '@wordpress/url'
import { REST_SNIPPETS_BASE } from '../restAPI'
import { createSnippetObject } from './snippets'
import type { RestAPI } from '../../hooks/useRestAPI'
import type { SnippetSchema, WritableSnippetSchema } from '../../types/schema/SnippetSchema'
import type { Snippet } from '../../types/Snippet'
import type { SnippetsExport } from '../../types/schema/SnippetsExport'
export interface SnippetsAPI {
fetchAll: (network?: boolean | null) => Promise<Snippet[]>
fetch: (snippetId: number, network?: boolean | null) => Promise<Snippet>
create: (snippet: Snippet) => Promise<Snippet>
update: (snippet: Pick<Snippet, 'id' | 'network'> & Partial<Snippet>) => Promise<Snippet>
delete: (snippet: Pick<Snippet, 'id' | 'network'>) => Promise<void>
activate: (snippet: Pick<Snippet, 'id' | 'network'>) => Promise<Snippet>
deactivate: (snippet: Pick<Snippet, 'id' | 'network'>) => Promise<Snippet>
export: (snippet: Pick<Snippet, 'id' | 'network'>) => Promise<SnippetsExport>
exportCode: (snippet: Pick<Snippet, 'id' | 'network'>) => Promise<string>
attach: (snippet: Pick<Snippet, 'id' | 'network' | 'conditionId'>) => Promise<void>
detach: (snippet: Pick<Snippet, 'id' | 'network'>) => Promise<void>
}
const buildURL = ({ id, network }: Pick<Snippet, 'id' | 'network'>, action?: string) =>
addQueryArgs(
[REST_SNIPPETS_BASE, id, action].filter(Boolean).join('/'),
{ network: network ? true : undefined }
)
const mapToSchema = ({
name,
desc,
code,
tags,
scope,
priority,
active,
network,
shared_network,
conditionId
}: Partial<Snippet>): WritableSnippetSchema => ({
name,
desc,
code,
tags,
scope,
priority,
active,
network,
shared_network,
condition_id: conditionId
})
export const buildSnippetsAPI = ({ get, post, del, put }: RestAPI): SnippetsAPI => ({
fetchAll: network =>
get<SnippetSchema[]>(addQueryArgs(REST_SNIPPETS_BASE, { network }))
.then(response => response.map(createSnippetObject)),
fetch: (snippetId, network) =>
get<SnippetSchema>(addQueryArgs(`${REST_SNIPPETS_BASE}/${snippetId}`, { network }))
.then(createSnippetObject),
create: snippet =>
post<SnippetSchema>(REST_SNIPPETS_BASE, mapToSchema(snippet))
.then(createSnippetObject),
update: snippet =>
post<SnippetSchema>(snippet.id ? buildURL(snippet) : REST_SNIPPETS_BASE, mapToSchema(snippet))
.then(createSnippetObject),
delete: snippet =>
del(buildURL(snippet)),
activate: snippet =>
post<SnippetSchema>(buildURL(snippet, 'activate'))
.then(createSnippetObject),
deactivate: snippet =>
post<SnippetSchema>(buildURL(snippet, 'deactivate'))
.then(createSnippetObject),
export: snippet =>
get<SnippetsExport>(buildURL(snippet, 'export')),
exportCode: snippet =>
get<string>(buildURL(snippet, 'export-code')),
attach: snippet =>
put(buildURL(snippet, 'attach'), { condition_id: snippet.conditionId }),
detach: snippet =>
put(buildURL(snippet, 'detach'))
})

View File

@@ -0,0 +1,51 @@
import { SNIPPET_TYPE_SCOPES } from '../../types/Snippet'
import { isNetworkAdmin } from '../screen'
import type { Snippet, SnippetScope } from '../../types/Snippet'
const defaults: Omit<Snippet, 'tags'> = {
id: 0,
name: '',
code: '',
desc: '',
scope: 'global',
modified: '',
active: false,
network: isNetworkAdmin(),
shared_network: null,
priority: 10,
conditionId: 0
}
const isAbsInt = (value: unknown): value is number =>
'number' === typeof value && 0 < value
const parseStringArray = (value: unknown): string[] | undefined =>
Array.isArray(value) ? value.filter(entry => 'string' === typeof entry) : undefined
export const isValidScope = (scope: unknown): scope is SnippetScope =>
'string' === typeof scope && Object.values(SNIPPET_TYPE_SCOPES).some(typeScopes =>
typeScopes.some(typeScope => typeScope === scope))
export const parseSnippetObject = (fields: unknown): Snippet => {
const result: { -readonly [F in keyof Snippet]: Snippet[F] } = { ...defaults, tags: [] }
if ('object' !== typeof fields || null === fields) {
return result
}
return {
...result,
...'id' in fields && isAbsInt(fields.id) && { id: fields.id },
...'name' in fields && 'string' === typeof fields.name && { name: fields.name },
...'desc' in fields && 'string' === typeof fields.desc && { desc: fields.desc },
...'code' in fields && 'string' === typeof fields.code && { code: fields.code },
...'tags' in fields && { tags: parseStringArray(fields.tags) ?? result.tags },
...'scope' in fields && isValidScope(fields.scope) && { scope: fields.scope },
...'modified' in fields && 'string' === typeof fields.modified && { modified: fields.modified },
...'active' in fields && 'boolean' === typeof fields.active && { active: fields.active },
...'network' in fields && 'boolean' === typeof fields.network && { network: fields.network },
...'shared_network' in fields && 'boolean' === typeof fields.shared_network && { shared_network: fields.shared_network },
...'priority' in fields && 'number' === typeof fields.priority && { priority: fields.priority },
...'condition_id' in fields && isAbsInt(fields.condition_id) && { conditionId: fields.condition_id }
}
}

View File

@@ -0,0 +1,55 @@
import { __ } from '@wordpress/i18n'
import { parseSnippetObject } from './objects'
import type { Snippet, SnippetType } from '../../types/Snippet'
const PRO_TYPES = new Set<SnippetType>(['css', 'js', 'cond'])
export const createSnippetObject = (fields: unknown): Snippet =>
parseSnippetObject(fields)
export const getSnippetType = ({ scope }: Pick<Snippet, 'scope'>): SnippetType => {
switch (true) {
case scope.endsWith('-css'):
return 'css'
case scope.endsWith('-js'):
return 'js'
case scope.endsWith('content'):
return 'html'
case 'condition' === scope:
return 'cond'
default:
return 'php'
}
}
export const validateSnippet = (snippet: Snippet): undefined | string => {
const missingTitle = '' === snippet.name.trim()
const missingCode = '' === snippet.code.trim()
switch (true) {
case missingCode && missingTitle:
return __('This snippet has no code or title.', 'code-snippets')
case missingCode:
return __('This snippet has no snippet code.', 'code-snippets')
case missingTitle:
return __('This snippet has no title.', 'code-snippets')
default:
return undefined
}
}
export const isCondition = (snippet: Pick<Snippet, 'scope'>): boolean =>
'condition' === snippet.scope
export const isProSnippet = (snippet: Pick<Snippet, 'scope'>): boolean =>
PRO_TYPES.has(getSnippetType(snippet))
export const isProType = (type: SnippetType): boolean =>
PRO_TYPES.has(type)