-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme-utils.mjs
More file actions
589 lines (510 loc) · 16.6 KB
/
theme-utils.mjs
File metadata and controls
589 lines (510 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/**
* theme-utils.mjs
*
* Validation and utility script for the LightSpeed WordPress block theme starter.
*
* Usage:
* node theme-utils.mjs <command>
*
* Commands:
* validate-schema Validate theme.json and style variation JSON files against the WordPress schema.
* validate-theme Validate theme consistency: slugs, required files, placeholder usage, etc.
* escape-patterns Scan PHP pattern files for escaping and translation issues.
* security-scan Scan PHP files for common security issues.
* help Show this help message.
*/
import { readFileSync, existsSync, readdirSync, statSync } from 'fs';
import { resolve, join, relative } from 'path';
import { createRequire } from 'module';
const require = createRequire( import.meta.url );
// Root of the theme repository.
const ROOT = resolve( '.' );
// Colours for terminal output.
const RESET = '\x1b[0m';
const RED = '\x1b[31m';
const YELLOW = '\x1b[33m';
const GREEN = '\x1b[32m';
const CYAN = '\x1b[36m';
const BOLD = '\x1b[1m';
function log( message ) {
console.log( message );
}
function success( message ) {
console.log( `${ GREEN }✔ ${ message }${ RESET }` );
}
function warn( message ) {
console.warn( `${ YELLOW }⚠ ${ message }${ RESET }` );
}
function error( message ) {
console.error( `${ RED }✖ ${ message }${ RESET }` );
}
function info( message ) {
console.log( `${ CYAN }ℹ ${ message }${ RESET }` );
}
function heading( message ) {
console.log( `\n${ BOLD }${ message }${ RESET }` );
}
/**
* Read and parse a JSON file. Returns null and logs an error on failure.
*
* @param {string} filePath Absolute path to the JSON file.
* @returns {object|null}
*/
function readJSON( filePath ) {
try {
const content = readFileSync( filePath, 'utf8' );
return JSON.parse( content );
} catch ( err ) {
error( `Failed to parse JSON at ${ relative( ROOT, filePath ) }: ${ err.message }` );
return null;
}
}
/**
* Recursively collect files matching an extension in a directory.
*
* @param {string} dir Directory to search.
* @param {string} extension File extension (e.g. '.json', '.php').
* @returns {string[]} Array of absolute file paths.
*/
function collectFiles( dir, extension ) {
const results = [];
if ( ! existsSync( dir ) ) {
return results;
}
for ( const name of readdirSync( dir ) ) {
const fullPath = join( dir, name );
const stat = statSync( fullPath );
if ( stat.isDirectory() ) {
results.push( ...collectFiles( fullPath, extension ) );
} else if ( name.endsWith( extension ) ) {
results.push( fullPath );
}
}
return results;
}
// ---------------------------------------------------------------------------
// Command: validate-schema
// ---------------------------------------------------------------------------
/**
* Validate theme.json and all styles/*.json files against the WordPress theme.json schema.
* Also validates any JSON files found in styles/blocks/ and styles/sections/.
*/
async function validateSchema() {
heading( 'Validating JSON schemas…' );
let Ajv, addFormats;
try {
const ajvModule = await import( 'ajv' );
Ajv = ajvModule.default;
} catch {
error( 'ajv is not installed. Run: npm install' );
process.exit( 1 );
}
// Fetch the WordPress theme.json schema.
const schemaUrl = 'https://schemas.wp.org/trunk/theme.json';
let schema;
try {
const response = await fetch( schemaUrl );
if ( ! response.ok ) {
throw new Error( `HTTP ${ response.status }` );
}
schema = await response.json();
} catch ( fetchError ) {
warn( `Could not fetch schema from ${ schemaUrl }: ${ fetchError.message }` );
warn( 'Falling back to structural JSON parse check only.' );
schema = null;
}
// Collect JSON files to validate.
const filesToValidate = [
join( ROOT, 'theme.json' ),
...collectFiles( join( ROOT, 'styles' ), '.json' ),
];
// Note: assets/fonts/ contains binary font assets — NOT JSON.
// We intentionally do not validate anything under assets/fonts/.
if ( filesToValidate.length === 0 ) {
warn( 'No JSON files found to validate.' );
return;
}
let hasErrors = false;
if ( schema ) {
const ajv = new Ajv( { allErrors: true, strict: false } );
const validate = ajv.compile( schema );
for ( const file of filesToValidate ) {
const rel = relative( ROOT, file );
const data = readJSON( file );
if ( data === null ) {
hasErrors = true;
continue;
}
const valid = validate( data );
if ( ! valid ) {
error( `Schema validation failed: ${ rel }` );
for ( const validationError of ( validate.errors || [] ) ) {
error( ` ${ validationError.instancePath || '(root)' } ${ validationError.message }` );
}
hasErrors = true;
} else {
success( `${ rel }` );
}
}
} else {
// Fallback: just check JSON is parseable.
for ( const file of filesToValidate ) {
const rel = relative( ROOT, file );
const data = readJSON( file );
if ( data === null ) {
hasErrors = true;
} else {
success( `${ rel } (JSON parse OK — schema unavailable)` );
}
}
}
if ( hasErrors ) {
error( '\nSchema validation completed with errors.' );
process.exit( 1 );
} else {
success( '\nAll JSON files passed schema validation.' );
}
}
// ---------------------------------------------------------------------------
// Command: validate-theme
// ---------------------------------------------------------------------------
/**
* Validate theme consistency:
* - Required files exist.
* - No unreplaced placeholder tokens.
* - Text domain consistency.
* - Style variations light.json and dark.json exist.
*/
async function validateTheme() {
heading( 'Validating theme consistency…' );
let hasErrors = false;
let hasWarnings = false;
// Required files.
const required = [
'style.css',
'theme.json',
'functions.php',
'templates/index.html',
'parts/header.html',
'parts/footer.html',
'styles/light.json',
'styles/dark.json',
];
info( 'Checking required files…' );
for ( const file of required ) {
const fullPath = join( ROOT, file );
if ( existsSync( fullPath ) ) {
success( file );
} else {
error( `Missing required file: ${ file }` );
hasErrors = true;
}
}
// Check for unreplaced placeholder tokens.
info( '\nChecking for unreplaced placeholder tokens…' );
// Exclude generated lock files and dependency directories from placeholder scan.
const excludeDirs = [ 'node_modules', 'vendor' ];
const excludeFiles = [ 'package-lock.json', 'composer.lock' ];
const isExcluded = ( f ) =>
excludeDirs.some( d => f.includes( `/${ d }/` ) || f.includes( `\\${ d }\\` ) ) ||
excludeFiles.some( name => f.endsWith( `/${ name }` ) || f.endsWith( `\\${ name }` ) );
const textFiles = [
...collectFiles( ROOT, '.css' ).filter( f => ! isExcluded( f ) ),
...collectFiles( ROOT, '.php' ).filter( f => ! isExcluded( f ) ),
...collectFiles( ROOT, '.json' ).filter( f => ! isExcluded( f ) ),
...collectFiles( ROOT, '.html' ).filter( f => ! isExcluded( f ) ),
...collectFiles( ROOT, '.md' ).filter( f => ! isExcluded( f ) ),
...collectFiles( ROOT, '.txt' ).filter( f => ! isExcluded( f ) ),
];
const placeholderRegex = /\{\{[A-Z_]+\}\}/g;
const foundPlaceholders = {};
for ( const file of textFiles ) {
const rel = relative( ROOT, file );
try {
const content = readFileSync( file, 'utf8' );
const matches = content.match( placeholderRegex );
if ( matches ) {
foundPlaceholders[ rel ] = [ ...new Set( matches ) ];
}
} catch {
// Skip unreadable files.
}
}
if ( Object.keys( foundPlaceholders ).length > 0 ) {
warn( 'Unreplaced placeholder tokens found:' );
for ( const [ file, tokens ] of Object.entries( foundPlaceholders ) ) {
warn( ` ${ file }: ${ tokens.join( ', ' ) }` );
}
hasWarnings = true;
} else {
success( 'No unreplaced placeholder tokens found.' );
}
// Check text domain consistency in style.css vs functions.php.
info( '\nChecking text domain consistency…' );
const styleCssPath = join( ROOT, 'style.css' );
const functionsPHPPath = join( ROOT, 'functions.php' );
if ( existsSync( styleCssPath ) && existsSync( functionsPHPPath ) ) {
const styleContent = readFileSync( styleCssPath, 'utf8' );
const functionsContent = readFileSync( functionsPHPPath, 'utf8' );
const textDomainMatch = styleContent.match( /Text Domain:\s*(.+)/i );
const textDomainInCSS = textDomainMatch ? textDomainMatch[ 1 ].trim() : null;
if ( textDomainInCSS && ! textDomainInCSS.startsWith( '{{' ) ) {
const textDomainInFunctions = functionsContent.includes( `'${ textDomainInCSS }'` );
if ( textDomainInFunctions ) {
success( `Text domain '${ textDomainInCSS }' found consistently in style.css and functions.php.` );
} else {
warn( `Text domain '${ textDomainInCSS }' found in style.css but not in functions.php.` );
hasWarnings = true;
}
} else {
info( 'Text domain placeholder not yet replaced — skipping consistency check.' );
}
}
// Report outcome.
if ( hasErrors ) {
error( '\nTheme validation completed with errors. Please fix the issues above.' );
process.exit( 1 );
} else if ( hasWarnings ) {
warn( '\nTheme validation completed with warnings. Review the issues above.' );
} else {
success( '\nTheme validation passed.' );
}
}
// ---------------------------------------------------------------------------
// Command: escape-patterns
// ---------------------------------------------------------------------------
/**
* Scan PHP pattern files for potential escaping and translation issues.
* This is advisory — it flags likely issues but does not auto-fix them.
*/
async function escapePatterns() {
heading( 'Scanning PHP patterns for escaping issues…' );
const patternsDir = join( ROOT, 'patterns' );
const incDir = join( ROOT, 'inc' );
const functionsPath = join( ROOT, 'functions.php' );
const phpFiles = [
...collectFiles( patternsDir, '.php' ),
...collectFiles( incDir, '.php' ),
];
if ( existsSync( functionsPath ) ) {
phpFiles.push( functionsPath );
}
if ( phpFiles.length === 0 ) {
info( 'No PHP files found to scan.' );
return;
}
let issueCount = 0;
// Patterns that suggest unescaped output.
const echoPatterns = [
{
// echo without escaping function wrapping the value
regex: /\becho\s+(?!\s*(esc_html|esc_attr|esc_url|esc_js|wp_kses|wp_kses_post|absint|intval)\s*\()(.+?);/g,
label: 'Possible unescaped echo',
},
{
// print without escaping
regex: /\bprint\s+(?!\s*(esc_html|esc_attr|esc_url|esc_js|wp_kses|wp_kses_post|absint|intval)\s*\()(.+?);/g,
label: 'Possible unescaped print',
},
{
// Direct superglobal output
regex: /echo\s+\$_(GET|POST|REQUEST|SERVER|COOKIE)\[/g,
label: 'Direct superglobal output — critical',
},
];
// Translation function patterns to flag issues.
const translationPatterns = [
{
// __() or _e() without text domain
regex: /__\s*\(\s*['"][^'"]+['"]\s*\)/g,
label: '__() called without text domain',
},
{
// _e() without text domain
regex: /_e\s*\(\s*['"][^'"]+['"]\s*\)/g,
label: '_e() called without text domain',
},
];
for ( const file of phpFiles ) {
const rel = relative( ROOT, file );
const content = readFileSync( file, 'utf8' );
const lines = content.split( '\n' );
const fileIssues = [];
// Check line by line for suspicious patterns.
for ( let i = 0; i < lines.length; i++ ) {
const line = lines[ i ];
const lineNum = i + 1;
// Skip comment lines.
if ( line.trimStart().startsWith( '//' ) || line.trimStart().startsWith( '*' ) || line.trimStart().startsWith( '/*' ) ) {
continue;
}
// Check echo patterns.
for ( const pattern of echoPatterns ) {
pattern.regex.lastIndex = 0;
if ( pattern.regex.test( line ) ) {
// Exclude lines that do have escaping in them.
if ( ! /esc_html|esc_attr|esc_url|esc_js|wp_kses|wp_kses_post|absint|intval/.test( line ) ) {
fileIssues.push( ` Line ${ lineNum }: [${ pattern.label }] ${ line.trim() }` );
}
}
}
// Check translation patterns.
for ( const pattern of translationPatterns ) {
pattern.regex.lastIndex = 0;
if ( pattern.regex.test( line ) ) {
fileIssues.push( ` Line ${ lineNum }: [${ pattern.label }] ${ line.trim() }` );
}
}
}
if ( fileIssues.length > 0 ) {
warn( `\n${ rel }:` );
for ( const issue of fileIssues ) {
warn( issue );
}
issueCount += fileIssues.length;
} else {
success( rel );
}
}
if ( issueCount > 0 ) {
warn( `\nEscape scan completed. ${ issueCount } potential issue(s) found. Review manually.` );
} else {
success( '\nNo escaping issues found.' );
}
}
// ---------------------------------------------------------------------------
// Command: security-scan
// ---------------------------------------------------------------------------
/**
* Scan PHP files for common security issues.
* This is advisory — it flags likely risks but is not a replacement for a full audit.
*/
async function securityScan() {
heading( 'Running security scan on PHP files…' );
const phpFiles = [
...collectFiles( join( ROOT, 'patterns' ), '.php' ),
...collectFiles( join( ROOT, 'inc' ), '.php' ),
];
const functionsPath = join( ROOT, 'functions.php' );
if ( existsSync( functionsPath ) ) {
phpFiles.push( functionsPath );
}
if ( phpFiles.length === 0 ) {
info( 'No PHP files found to scan.' );
return;
}
// High-risk patterns.
const securityPatterns = [
{
regex: /\beval\s*\(/g,
label: 'CRITICAL: eval() detected',
},
{
regex: /echo\s+\$_(GET|POST|REQUEST|SERVER|COOKIE)\[/g,
label: 'CRITICAL: Direct superglobal output',
},
{
regex: /\$wpdb->query\s*\(\s*['"]/g,
label: 'WARNING: Possible unprepared database query',
},
{
regex: /\$wpdb->prepare\s*\(\s*\$[^,)]+\)/g,
label: 'WARNING: $wpdb->prepare() may be called without placeholders',
},
{
regex: /file_get_contents\s*\(\s*\$_(GET|POST|REQUEST)/g,
label: 'CRITICAL: file_get_contents with user input',
},
{
regex: /include\s*\(\s*\$_(GET|POST|REQUEST)/g,
label: 'CRITICAL: Dynamic include with user input',
},
{
regex: /require\s*\(\s*\$_(GET|POST|REQUEST)/g,
label: 'CRITICAL: Dynamic require with user input',
},
{
regex: /\$_(GET|POST|REQUEST)\[.+\]\s*(?!.*sanitize)/g,
label: 'WARNING: Unsanitised superglobal usage',
},
];
let issueCount = 0;
for ( const file of phpFiles ) {
const rel = relative( ROOT, file );
const content = readFileSync( file, 'utf8' );
const lines = content.split( '\n' );
const fileIssues = [];
for ( let i = 0; i < lines.length; i++ ) {
const line = lines[ i ];
const lineNum = i + 1;
if ( line.trimStart().startsWith( '//' ) || line.trimStart().startsWith( '*' ) ) {
continue;
}
for ( const pattern of securityPatterns ) {
pattern.regex.lastIndex = 0;
if ( pattern.regex.test( line ) ) {
fileIssues.push( ` Line ${ lineNum }: [${ pattern.label }] ${ line.trim() }` );
}
}
}
if ( fileIssues.length > 0 ) {
error( `\n${ rel }:` );
for ( const issue of fileIssues ) {
error( issue );
}
issueCount += fileIssues.length;
} else {
success( rel );
}
}
if ( issueCount > 0 ) {
error( `\nSecurity scan completed. ${ issueCount } potential issue(s) found. Review manually.` );
process.exit( 1 );
} else {
success( '\nSecurity scan passed.' );
}
}
// ---------------------------------------------------------------------------
// Command: help
// ---------------------------------------------------------------------------
function showHelp() {
log( `
${ BOLD }theme-utils.mjs — LightSpeed WordPress Block Theme Utility${ RESET }
Usage:
node theme-utils.mjs <command>
Commands:
${ CYAN }validate-schema${ RESET }
Validate theme.json and all styles/*.json files against the WordPress schema.
Also validates JSON files in styles/blocks/ and styles/sections/ if present.
${ CYAN }validate-theme${ RESET }
Cross-file checks: required files, placeholder tokens, text domain consistency.
${ CYAN }escape-patterns${ RESET }
Scan PHP pattern files and inc/ for likely escaping and translation issues.
${ CYAN }security-scan${ RESET }
Scan PHP files for common security risks (eval, direct superglobal output, etc).
${ CYAN }help${ RESET }
Show this help message.
` );
}
// ---------------------------------------------------------------------------
// Entry point
// ---------------------------------------------------------------------------
const command = process.argv[ 2 ];
switch ( command ) {
case 'validate-schema':
await validateSchema();
break;
case 'validate-theme':
await validateTheme();
break;
case 'escape-patterns':
await escapePatterns();
break;
case 'security-scan':
await securityScan();
break;
case 'help':
default:
showHelp();
break;
}