Skip to content

Commit a13787d

Browse files
authored
[N/A] Installer updates (#198)
* [N/A] Various file and branding updates * [N/A] Option to disable Twig during setup
1 parent 697c942 commit a13787d

File tree

1 file changed

+74
-8
lines changed

1 file changed

+74
-8
lines changed

bin/composer-scripts/ProjectEvents/PostCreateProjectScript.php

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ class PostCreateProjectScript extends ComposerScript {
2020
const BRANDING_CUSTOM = 2;
2121
const BRANDING_NONE = 3;
2222

23+
/**
24+
* Twig Options
25+
*/
26+
const TWIG_ENABLED = 1;
27+
const TWIG_DISABLED = 2;
28+
2329
/**
2430
* @var array
2531
*/
@@ -29,15 +35,15 @@ class PostCreateProjectScript extends ComposerScript {
2935
* @var array
3036
*/
3137
private static array $defaults = [
32-
'project-slug' => 'wordpress-site-starter',
33-
'project-name' => 'WP Site Starter',
38+
'project-slug' => 'wordpress-site-starter',
39+
'project-name' => 'WP Site Starter',
3440
'alt-project-name' => 'WordPress Site Starter',
35-
'host-name' => 'wpstarter',
36-
'theme-name' => 'WP Starter',
37-
'theme-slug' => 'wp-starter',
38-
'package-name' => 'WPStarter',
39-
'function-prefix' => 'wpstarter_',
40-
'text-domain' => 'wp-starter',
41+
'host-name' => 'wpstarter',
42+
'theme-name' => 'WP Starter',
43+
'theme-slug' => 'wp-starter',
44+
'package-name' => 'WPStarter',
45+
'function-prefix' => 'wpstarter_',
46+
'text-domain' => 'wp-starter',
4147
];
4248

4349
/**
@@ -85,6 +91,9 @@ public static function execute( Event $event ): void {
8591
// Perform project string replacements
8692
self::updateProjectFiles();
8793

94+
// Remove Twig support if disabled.
95+
self::maybeDisableTwig();
96+
8897
// Modify the description in the composer.json file.
8998
self::updateComposerDescription();
9099

@@ -158,6 +167,11 @@ public static function getProjectInfo(): void {
158167
self::$info['function'] = str_replace( '-', '_', self::$info['slug'] ) . '_';
159168
self::$info['function'] = self::ask( 'Do you want to customize the function prefix?', self::$info['function'] );
160169

170+
// Twig.
171+
$twigOptions = self::getTwigOptions();
172+
$twigPreference = empty( self::$info['twig'] ) ? self::TWIG_ENABLED : self::$info['twig'];
173+
self::$info['twig'] = intval( self::select( 'Use Twig Templates?', $twigOptions, $twigPreference ) );
174+
161175
// Proxy Domain.
162176
$proxyDomain = empty( self::$info['proxy-domain'] ) ? '' : self::$info['proxy-domain'];
163177
self::$info['proxy-domain'] = self::ask( 'Would you like to proxy media (uploads) from another domain? (leave blank to skip)', $proxyDomain );
@@ -197,13 +211,15 @@ public static function getProjectInfo(): void {
197211
}
198212

199213
$brandingText = $brandingOptions[ self::$info['branding'] ];
214+
$twigText = self::$info['twig'] === self::TWIG_ENABLED ? 'Enabled' : 'Disabled';
200215

201216
// Summary
202217
$summary = PHP_EOL . ' - Name: ' . self::$info['name'];
203218
$summary .= PHP_EOL . ' - Slug: ' . self::$info['slug'];
204219
$summary .= PHP_EOL . ' - Text Domain: ' . self::$info['text-domain'];
205220
$summary .= PHP_EOL . ' - Package: ' . self::$info['package'];
206221
$summary .= PHP_EOL . ' - Function Prefix: ' . self::$info['function'];
222+
$summary .= PHP_EOL . ' - Twig: ' . $twigText;
207223
if ( ! empty( self::$info['proxy-domain'] ) ) {
208224
$summary .= PHP_EOL . ' - Proxy Domain: ' . self::$info['proxy-domain'];
209225
}
@@ -231,6 +247,7 @@ public static function getProjectInfo(): void {
231247
private static function storeProjectInfo(): void {
232248
$brandingOptions = self::getBrandingOptions();
233249
$branding = strtolower( $brandingOptions[ self::$info['branding'] ] );
250+
$twig = self::$info['twig'] === self::TWIG_ENABLED ? 'enabled' : 'disabled';
234251

235252
$envPath = self::translatePath( '.ddev/.env' );
236253
$envData = file_get_contents( $envPath );
@@ -241,6 +258,7 @@ private static function storeProjectInfo(): void {
241258
$envData .= PHP_EOL . 'PROJECT_TEXT_DOMAIN="' . self::escapeQuotes( self::$info['text-domain'] ) . '"';
242259
$envData .= PHP_EOL . 'PROJECT_PACKAGE="' . self::escapeQuotes( self::$info['package'] ) . '"';
243260
$envData .= PHP_EOL . 'PROJECT_FUNCTION_PREFIX="' . self::escapeQuotes( self::$info['function'] ) . '"';
261+
$envData .= PHP_EOL . 'PROJECT_TWIG="' . self::escapeQuotes( $twig ) . '"';
244262
$envData .= PHP_EOL . 'PROJECT_BRANDING="' . self::escapeQuotes( $branding ) . '"';
245263
if ( ! empty( self::$info['branding-name'] ) ) {
246264
$envData .= PHP_EOL . 'PROJECT_BRANDING_NAME="' . self::escapeQuotes( self::$info['branding-name'] ) . '"';
@@ -387,6 +405,36 @@ public static function updateProjectFiles(): void {
387405
self::writeInfo( 'Project files updated!' );
388406
}
389407

408+
/**
409+
* Remove Twig support if disabled.
410+
*
411+
* @return void
412+
*/
413+
private static function maybeDisableTwig(): void {
414+
if ( self::$info['twig'] !== self::TWIG_DISABLED ) {
415+
return;
416+
}
417+
418+
$themeDir = self::translatePath( 'wp-content/themes/' . self::$info['slug'] );
419+
420+
self::writeLine( 'Removing Twig support...' );
421+
422+
// Remove from Composer
423+
$composerData = self::getComposerData( $themeDir );
424+
unset( $composerData['require']['twig/twig'] );
425+
self::updateComposerData( $composerData, $themeDir );
426+
427+
// Remove Twig files from the theme directory
428+
$blockFiles = glob( $themeDir . '/blocks/**/*.twig', GLOB_BRACE );
429+
$plopTemplates = glob( $themeDir . '/src/plop-templates/**/*.twig.hbs', GLOB_BRACE );
430+
431+
$twigFiles = array_merge( $blockFiles, $plopTemplates );
432+
433+
foreach ( $twigFiles as $twigFile ) {
434+
unlink( $twigFile );
435+
}
436+
}
437+
390438
/**
391439
* Require ACF if auth.json file is present.
392440
*
@@ -522,11 +570,13 @@ private static function getFilesToChange( string $themeDir ): array {
522570
self::translatePath( '.ddev/.env' ),
523571
self::translatePath( '.ddev/config.yaml' ),
524572
self::translatePath( 'README.md' ),
573+
self::translatePath( '.github/workflows/build.yaml' ),
525574
$themeDir . '/.phpcs.xml',
526575
$themeDir . '/readme.txt',
527576
$themeDir . '/README.md',
528577
$themeDir . '/style.css',
529578
$themeDir . '/vite.config.js',
579+
$themeDir . '/src/plop-templates/render.php.hbs',
530580
];
531581

532582
// TODO: Search theme directory recursively.
@@ -669,6 +719,7 @@ private static function updateBranding(): void {
669719

670720
$footerFile = self::translatePath( 'wp-content/mu-plugins/viget-wp/src/classes/Admin/Footer.php' );
671721
$loginScreenFile = self::translatePath( 'wp-content/mu-plugins/viget-wp/src/classes/Admin/LoginScreen.php' );
722+
$stylesheetFile = self::translatePath( 'wp-content/themes/wp-starter/style.css' );
672723

673724
// Modify the login screen branding.
674725
if ( self::BRANDING_VIGET !== self::$info['branding'] && file_exists( $loginScreenFile ) ) {
@@ -682,6 +733,7 @@ private static function updateBranding(): void {
682733
if ( self::BRANDING_NONE === self::$info['branding'] ) {
683734
self::searchReplaceFile( '$this->modify_footer_text();', '// $this->modify_footer_text();', $footerFile );
684735
self::searchReplaceFile( '$this->modify_footer_text();', '// $this->modify_footer_text();', $footerFile );
736+
self::searchReplaceFile( 'Viget, ', '', $stylesheetFile );
685737
return;
686738
}
687739

@@ -691,10 +743,12 @@ private static function updateBranding(): void {
691743

692744
if ( ! empty( self::$info['branding-website'] ) ) {
693745
self::searchReplaceFile( 'https://www.viget.com/', self::$info['branding-website'], $footerFile );
746+
self::searchReplaceFile( 'https://viget.com', self::$info['branding-website'], $stylesheetFile );
694747
}
695748

696749
if ( ! empty( self::$info['branding-name'] ) ) {
697750
self::searchReplaceFile( 'esc_html__( \'Viget\', \'viget-wp\' )', 'esc_html__( \'' . addslashes( self::$info['branding-name'] ) . '\', \'viget-wp\' )', $footerFile );
751+
self::searchReplaceFile( 'Viget,', self::$info['branding-name'] . ',', $stylesheetFile );
698752
}
699753
}
700754

@@ -710,4 +764,16 @@ private static function getBrandingOptions(): array {
710764
self::BRANDING_NONE => 'None',
711765
];
712766
}
767+
768+
/**
769+
* Get the Twig options.
770+
*
771+
* @return array
772+
*/
773+
private static function getTwigOptions(): array {
774+
return [
775+
self::TWIG_ENABLED => 'Yes',
776+
self::TWIG_DISABLED => 'No, use PHP templates',
777+
];
778+
}
713779
}

0 commit comments

Comments
 (0)