Skip to content

Commit 2b2d5d5

Browse files
authored
Fix (design library): Display placeholders in Stackable Patterns (#3553)
* fix placeholders * add file
1 parent 105b890 commit 2b2d5d5

File tree

4 files changed

+616
-2
lines changed

4 files changed

+616
-2
lines changed

gulpfile.js

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,81 @@ ${ blockDesignSystem });
411411
cb()
412412
} )
413413

414+
gulp.task( 'generate-design-library-default-placeholders-php', function( cb ) {
415+
const fs = require( 'fs' )
416+
417+
let defaultPlaceholders = 'array()'
418+
419+
const toAssocArray = ( key, value, cb, indent ) => {
420+
if ( typeof value === 'object' ) {
421+
const parsed = cb( value, indent + 1 )
422+
return `"${ key }" => ${ parsed }`
423+
}
424+
425+
return `"${ key }" => "${ value }"`
426+
}
427+
428+
const parsePlaceholders = ( obj, indent ) => {
429+
let content = ''
430+
const tab = '\t'.repeat( indent )
431+
432+
if ( typeof obj === 'object' ) {
433+
content += 'array(\n'
434+
435+
Object.entries( obj ).forEach( ( [ key, value ], index, bds ) => {
436+
content += tab + toAssocArray( key, value, parsePlaceholders, indent )
437+
438+
if ( index !== bds.length - 1 ) {
439+
content += ',\n'
440+
} else {
441+
content += '\n'
442+
}
443+
} )
444+
content += `\t`.repeat( indent - 1 ) + ')'
445+
}
446+
return content
447+
}
448+
449+
const jsonPath = path.resolve( __dirname, `src/components/design-library-list/default.json` )
450+
if ( fs.existsSync( jsonPath ) ) {
451+
const fileContent = fs.readFileSync( jsonPath, 'utf-8' )
452+
const raw = JSON.parse( fileContent )
453+
defaultPlaceholders = parsePlaceholders( raw, 4 )
454+
}
455+
456+
// Generate PHP variable string
457+
const script = `<?php
458+
// This is a generated file by gulp generate-design-library-default-placeholders-php
459+
// Use src/components/design-library-list/default.json if you want to edit this file.
460+
461+
// Exit if accessed directly.
462+
if ( ! defined( 'ABSPATH' ) ) {
463+
exit;
464+
}
465+
466+
if ( ! class_exists( 'Stackable_Design_Library_Placeholders' ) ) {
467+
class Stackable_Design_Library_Placeholders {
468+
469+
function __construct() {
470+
}
471+
472+
public static function get_default() {
473+
$default_placeholders = ${ defaultPlaceholders };
474+
475+
return $default_placeholders;
476+
}
477+
}
478+
479+
new Stackable_Design_Library_Placeholders();
480+
}
481+
?>
482+
`
483+
// Write PHP variable to file
484+
fs.writeFileSync( path.resolve( __dirname, 'src/design-library/default-placeholders.php' ), script )
485+
486+
cb()
487+
} )
488+
414489
gulp.task( 'generate-translations-js', gulp.series(
415490
// The collect function has an issue where it will not continue if the
416491
// folder will it writes to doesn't exist, create it to prevent an error.
@@ -703,7 +778,7 @@ gulp.task( 'style-deprecated', gulp.parallel(
703778
* END deprecated build styles, we still build these
704779
********************************************************************/
705780

706-
gulp.task( 'build-process', gulp.parallel( 'style', 'style-editor', 'welcome-styles', 'style-deprecated', 'generate-translations-js', 'generate-stk-block-typesphp' ) )
781+
gulp.task( 'build-process', gulp.parallel( 'style', 'style-editor', 'welcome-styles', 'style-deprecated', 'generate-translations-js', 'generate-stk-block-typesphp', 'generate-design-library-default-placeholders-php' ) )
707782

708783
gulp.task( 'build-block-design-system', gulp.parallel( 'generate-block-design-system-php', 'generate-block-design-system-scss' ) )
709784

@@ -740,6 +815,11 @@ const watchFuncs = ( basePath = '.' ) => {
740815
[ `${ basePath }/src/block/**/block.json` ],
741816
gulp.parallel( [ 'generate-stk-block-typesphp' ] )
742817
)
818+
819+
gulp.watch(
820+
[ `${ basePath }/src/components/design-library-list/default.json` ],
821+
gulp.parallel( [ 'generate-design-library-default-placeholders-php' ] )
822+
)
743823
}
744824

745825
gulp.task( 'watch', gulp.series( 'build-block-design-system', 'build-process', function watch( done ) {

plugin.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ function is_frontend() {
230230
require_once( plugin_dir_path( __FILE__ ) . 'src/kses.php' );
231231
require_once( plugin_dir_path( __FILE__ ) . 'src/dynamic-breakpoints.php' );
232232
require_once( plugin_dir_path( __FILE__ ) . 'src/design-library/init.php' );
233+
require_once( plugin_dir_path( __FILE__ ) . 'src/design-library/default-placeholders.php' );
233234
require_once( plugin_dir_path( __FILE__ ) . 'src/styles/block-design-system.php' );
234235
require_once( plugin_dir_path( __FILE__ ) . 'src/plugins/theme-block-style-inheritance/index.php' );
235236
require_once( plugin_dir_path( __FILE__ ) . 'src/global-settings.php' );

0 commit comments

Comments
 (0)