|
11 | 11 |
|
12 | 12 | const fs = require( 'fs' ); |
13 | 13 | const path = require( 'path' ); |
| 14 | +const json2php = require( 'json2php' ); |
14 | 15 |
|
15 | 16 | // Paths |
16 | 17 | const rootDir = path.resolve( __dirname, '../..' ); |
@@ -281,11 +282,18 @@ function generateScriptModulesPackages() { |
281 | 282 |
|
282 | 283 | processDirectory( modulesDir, modulesDir ); |
283 | 284 |
|
284 | | - // Generate both minified and non-minified PHP files |
285 | | - const phpContentMin = `<?php return ${ phpEncode( assetsMin ) }; |
286 | | -`; |
287 | | - const phpContentRegular = `<?php return ${ phpEncode( assetsRegular ) }; |
288 | | -`; |
| 285 | + // Generate both minified and non-minified PHP files using json2php |
| 286 | + const phpContentMin = '<?php return ' + json2php.make( { |
| 287 | + linebreak: '\n', |
| 288 | + indent: ' ', |
| 289 | + shortArraySyntax: false |
| 290 | + } )( assetsMin ) + ';'; |
| 291 | + |
| 292 | + const phpContentRegular = '<?php return ' + json2php.make( { |
| 293 | + linebreak: '\n', |
| 294 | + indent: ' ', |
| 295 | + shortArraySyntax: false |
| 296 | + } )( assetsRegular ) + ';'; |
289 | 297 |
|
290 | 298 | const outputPathMin = path.join( wpIncludesDir, 'assets/script-modules-packages.min.php' ); |
291 | 299 | const outputPathRegular = path.join( wpIncludesDir, 'assets/script-modules-packages.php' ); |
@@ -354,11 +362,18 @@ function generateScriptLoaderPackages() { |
354 | 362 | } |
355 | 363 | } |
356 | 364 |
|
357 | | - // Generate both minified and non-minified PHP files |
358 | | - const phpContentMin = `<?php return ${ phpEncode( assetsMin ) }; |
359 | | -`; |
360 | | - const phpContentRegular = `<?php return ${ phpEncode( assetsRegular ) }; |
361 | | -`; |
| 365 | + // Generate both minified and non-minified PHP files using json2php |
| 366 | + const phpContentMin = '<?php return ' + json2php.make( { |
| 367 | + linebreak: '\n', |
| 368 | + indent: ' ', |
| 369 | + shortArraySyntax: false |
| 370 | + } )( assetsMin ) + ';'; |
| 371 | + |
| 372 | + const phpContentRegular = '<?php return ' + json2php.make( { |
| 373 | + linebreak: '\n', |
| 374 | + indent: ' ', |
| 375 | + shortArraySyntax: false |
| 376 | + } )( assetsRegular ) + ';'; |
362 | 377 |
|
363 | 378 | const outputPathMin = path.join( wpIncludesDir, 'assets/script-loader-packages.min.php' ); |
364 | 379 | const outputPathRegular = path.join( wpIncludesDir, 'assets/script-loader-packages.php' ); |
@@ -460,6 +475,7 @@ ${ staticBlocks.map( name => `\t'${ name }',` ).join( '\n' ) } |
460 | 475 | /** |
461 | 476 | * Generate blocks-json.php from all block.json files. |
462 | 477 | * Reads all block.json files and combines them into a single PHP array. |
| 478 | + * Uses json2php to maintain consistency with Core's formatting. |
463 | 479 | */ |
464 | 480 | function generateBlocksJson() { |
465 | 481 | const blocksDir = path.join( wpIncludesDir, 'blocks' ); |
@@ -489,9 +505,12 @@ function generateBlocksJson() { |
489 | 505 | } |
490 | 506 | } |
491 | 507 |
|
492 | | - // Generate the PHP file content |
493 | | - const phpContent = `<?php return ${ phpEncode( blocks ) }; |
494 | | -`; |
| 508 | + // Generate the PHP file content using json2php for consistent formatting |
| 509 | + const phpContent = '<?php return ' + json2php.make( { |
| 510 | + linebreak: '\n', |
| 511 | + indent: ' ', |
| 512 | + shortArraySyntax: false |
| 513 | + } )( blocks ) + ';'; |
495 | 514 |
|
496 | 515 | fs.writeFileSync( |
497 | 516 | path.join( wpIncludesDir, 'blocks/blocks-json.php' ), |
@@ -664,65 +683,6 @@ function parsePHPArray( phpArrayContent ) { |
664 | 683 | } |
665 | 684 | } |
666 | 685 |
|
667 | | -/** |
668 | | - * Convert JavaScript value to PHP representation. |
669 | | - * Similar to json2php used in Gruntfile. |
670 | | - * |
671 | | - * @param {*} value - Value to encode. |
672 | | - * @param {number} indent - Indentation level. |
673 | | - * @return {string} PHP representation. |
674 | | - */ |
675 | | -function phpEncode( value, indent = 0 ) { |
676 | | - const indentStr = ' '.repeat( indent ); |
677 | | - const nextIndentStr = ' '.repeat( indent + 1 ); |
678 | | - |
679 | | - if ( value === null ) { |
680 | | - return 'null'; |
681 | | - } |
682 | | - |
683 | | - if ( typeof value === 'boolean' ) { |
684 | | - return value ? 'true' : 'false'; |
685 | | - } |
686 | | - |
687 | | - if ( typeof value === 'number' ) { |
688 | | - return String( value ); |
689 | | - } |
690 | | - |
691 | | - if ( typeof value === 'string' ) { |
692 | | - // Escape single quotes and backslashes |
693 | | - const escaped = value |
694 | | - .replace( /\\/g, '\\\\' ) |
695 | | - .replace( /'/g, "\\'" ); |
696 | | - return `'${ escaped }'`; |
697 | | - } |
698 | | - |
699 | | - if ( Array.isArray( value ) ) { |
700 | | - if ( value.length === 0 ) { |
701 | | - return 'array()'; |
702 | | - } |
703 | | - |
704 | | - const items = value.map( item => `${ nextIndentStr }${ phpEncode( item, indent + 1 ) }` ); |
705 | | - return `array(\n${ items.join( ',\n' ) }\n${ indentStr })`; |
706 | | - } |
707 | | - |
708 | | - if ( typeof value === 'object' ) { |
709 | | - const keys = Object.keys( value ); |
710 | | - |
711 | | - if ( keys.length === 0 ) { |
712 | | - return 'array()'; |
713 | | - } |
714 | | - |
715 | | - const items = keys.map( key => { |
716 | | - const phpKey = phpEncode( key, indent + 1 ); |
717 | | - const phpValue = phpEncode( value[ key ], indent + 1 ); |
718 | | - return `${ nextIndentStr }${ phpKey } => ${ phpValue }`; |
719 | | - } ); |
720 | | - |
721 | | - return `array(\n${ items.join( ',\n' ) }\n${ indentStr })`; |
722 | | - } |
723 | | - |
724 | | - return 'null'; |
725 | | -} |
726 | 686 |
|
727 | 687 | /** |
728 | 688 | * Transform PHP file contents to work in Core. |
|
0 commit comments