From e92e0db802d76680a3c3e37197ff5e3ba38a7715 Mon Sep 17 00:00:00 2001
From: thelovekesh
Date: Wed, 13 Apr 2022 14:08:54 +0530
Subject: [PATCH 01/12] Add init script
---
bin/init.js | 227 ++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 227 insertions(+)
create mode 100644 bin/init.js
diff --git a/bin/init.js b/bin/init.js
new file mode 100644
index 00000000..cf435db5
--- /dev/null
+++ b/bin/init.js
@@ -0,0 +1,227 @@
+#! /usr/bin/env node
+
+/* eslint no-console: 0 */
+
+/**
+ * External dependencies
+ */
+const fs = require( 'fs' );
+const path = require( 'path' );
+const readline = require( 'readline' );
+
+/**
+ * Define Constants
+ */
+const rl = readline.createInterface( {
+ input: process.stdin,
+ output: process.stdout,
+} );
+const info = {
+ error: ( message ) => {
+ return `\x1b[31m${ message }\x1b[0m`;
+ },
+ success: ( message ) => {
+ return `\x1b[32m${ message }\x1b[0m`;
+ },
+ warning: ( message ) => {
+ return `\x1b[33m${ message }\x1b[0m`;
+ },
+ message: ( message ) => {
+ return `\x1b[34m${ message }\x1b[0m`;
+ },
+};
+
+// Start with a prompt.
+rl.question( 'Would you like to setup the theme? (Y/n) ', ( answer ) => {
+ if ( 'n' === answer.toLowerCase() ) {
+ console.log( info.warning( '\nTheme Setup Cancelled.\n' ) );
+ process.exit( 0 );
+ }
+ rl.question( 'Enter theme name (shown in WordPress admin)*: ', ( themeName ) => {
+ const themeInfo = setupTheme( themeName );
+ rl.question( 'Confirm the Theme Details (Y/n) ', ( confirm ) => {
+ if ( 'n' === confirm.toLowerCase() ) {
+ console.log( info.warning( '\nTheme Setup Cancelled.\n' ) );
+ process.exit( 0 );
+ }
+ initTheme( themeInfo );
+ rl.close();
+ } );
+ } );
+} );
+
+rl.on( 'close', () => {
+ process.exit( 0 );
+} );
+
+/**
+ * Theme Setup
+ *
+ * @param {string} themeName
+ *
+ * @return {Object} themeInfo
+ */
+const setupTheme = ( themeName ) => {
+ console.log( info.success( '\nFiring up the theme setup...' ) );
+
+ // Ask theme name.
+ if ( ! themeName ) {
+ console.log( info.error( '\nTheme name is required.\n' ) );
+ process.exit( 0 );
+ }
+
+ // Generate theme info.
+ const themeInfo = generateThemeInfo( themeName );
+
+ const themeDetails = {
+ 'Theme Name: ': `${ themeInfo.themeName }`,
+ 'Theme Version: ': `1.0.0`,
+ 'Text Domain: ': `${ themeInfo.kebabCase }`,
+ 'Package: ': `${ themeInfo.kebabCase }`,
+ 'Namespace: ': `${ themeInfo.pascalSnakeCase }`,
+ 'Function Prefix: ': `${ themeInfo.snakeCaseWithUnderscoreSuffix }`,
+ 'CSS Class Prefix: ': `${ themeInfo.kebabCaseWithHyphenSuffix }`,
+ 'PHP Variable Prefix: ': `${ themeInfo.snakeCaseWithUnderscoreSuffix }`,
+ 'Version Constant: ': `${ themeInfo.macroCase }_VERSION`,
+ 'Theme Directory Constant: ': `${ themeInfo.macroCase }_TEMP_DIR`,
+ 'Theme Build Directory Constant: ': `${ themeInfo.macroCase }_BUILD_DIR`,
+ 'Theme Build Directory URI Constant: ': `${ themeInfo.macroCase }_BUILD_URI`,
+ };
+
+ const biggestStringLength = themeDetails[ 'Theme Build Directory URI Constant: ' ].length + 'Theme Build Directory URI Constant: '.length;
+
+ console.log( info.success( '\nTheme Details:' ) );
+ console.log(
+ info.warning( '┌' + '─'.repeat( biggestStringLength + 2 ) + '┐' ),
+ );
+ Object.keys( themeDetails ).forEach( ( key ) => {
+ console.log(
+ info.warning( '│' + ' ' + info.success( key ) + info.message( themeDetails[ key ] ) + ' ' + ' '.repeat( biggestStringLength - ( themeDetails[ key ].length + key.length ) ) + info.warning( '│' ) ),
+ );
+ } );
+ console.log(
+ info.warning( '└' + '─'.repeat( biggestStringLength + 2 ) + '┘' ),
+ );
+
+ return themeInfo;
+};
+
+/**
+ * Initialize new theme
+ *
+ * @param {Object} themeInfo
+ */
+const initTheme = ( themeInfo ) => {
+ const chunksToReplace = {
+ 'elementary theme': themeInfo.themeNameLowerCase,
+ 'Elementary Theme': themeInfo.themeName,
+ 'ELEMENTARY THEME': themeInfo.themeNameCobolCase,
+ 'elementary-theme': themeInfo.kebabCase,
+ 'Elementary-Theme': themeInfo.trainCase,
+ 'ELEMENTARY-THEME': themeInfo.cobolCase,
+ elementary_theme: themeInfo.snakeCase,
+ Elementary_Theme: themeInfo.pascalSnakeCase,
+ ELEMENTARY_THEME: themeInfo.macroCase,
+ 'elementary-theme-': themeInfo.kebabCaseWithHyphenSuffix,
+ 'Elementary-Theme-': themeInfo.trainCaseWithHyphenSuffix,
+ 'ELEMENTARY-THEME-': themeInfo.cobolCaseWithHyphenSuffix,
+ elementary_theme_: themeInfo.snakeCaseWithUnderscoreSuffix,
+ Elementary_Theme_: themeInfo.pascalSnakeCaseWithUnderscoreSuffix,
+ ELEMENTARY_THEME_: themeInfo.macroCaseWithUnderscoreSuffix,
+ };
+
+ Object.keys( chunksToReplace ).forEach( ( key ) => {
+ replaceContentInFile( key, chunksToReplace[ key ] );
+ } );
+};
+
+/**
+ * Replace content in file
+ *
+ * @param {string} chunksToReplace
+ * @param {Object} newChunk
+ */
+const replaceContentInFile = ( chunksToReplace, newChunk ) => {
+ const files = [
+ 'composer.json',
+ 'functions.php',
+ 'index.php',
+ 'package.json',
+ 'package-lock.json',
+ 'phpcs.xml.dist',
+ 'README.md',
+ 'style.css',
+ ];
+
+ files.forEach( ( file ) => {
+ const filePath = path.resolve( getRoot(), file );
+
+ try {
+ let content = fs.readFileSync( filePath, 'utf8' );
+ const regex = new RegExp( chunksToReplace, 'g' );
+ content = content.replace( regex, newChunk );
+ if ( content !== fs.readFileSync( filePath, 'utf8' ) ) {
+ fs.writeFileSync( filePath, content, 'utf8' );
+ console.log( info.success( `Updated ${ file }` ) );
+ }
+ } catch ( err ) {
+ console.log( info.error( `\nError: ${ err }` ) );
+ }
+ } );
+};
+
+/**
+ * Generate Theme Info from Theme Name
+ *
+ * @param {string} themeName
+ */
+const generateThemeInfo = ( themeName ) => {
+ const themeNameLowerCase = themeName.toLowerCase();
+
+ const kebabCase = themeName.replace( /\s+/g, '-' ).toLowerCase();
+ const snakeCase = kebabCase.replace( /\-/g, '_' );
+ const kebabCaseWithHyphenSuffix = kebabCase + '-';
+ const snakeCaseWithUnderscoreSuffix = snakeCase + '_';
+
+ const trainCase = kebabCase.replace( /\b\w/g, ( l ) => {
+ return l.toUpperCase();
+ } );
+ const themeNameTrainCase = trainCase.replace( /\-/g, ' ' );
+ const pascalSnakeCase = trainCase.replace( /\-/g, '_' );
+ const trainCaseWithHyphenSuffix = trainCase + '-';
+ const pascalSnakeCaseWithUnderscoreSuffix = pascalSnakeCase + '_';
+
+ const cobolCase = kebabCase.toUpperCase();
+ const themeNameCobolCase = themeNameTrainCase.toUpperCase();
+ const macroCase = snakeCase.toUpperCase();
+ const cobolCaseWithHyphenSuffix = cobolCase + '-';
+ const macroCaseWithUnderscoreSuffix = macroCase + '_';
+
+ return {
+ themeName,
+ themeNameLowerCase,
+ kebabCase,
+ snakeCase,
+ kebabCaseWithHyphenSuffix,
+ snakeCaseWithUnderscoreSuffix,
+ trainCase,
+ themeNameTrainCase,
+ pascalSnakeCase,
+ trainCaseWithHyphenSuffix,
+ pascalSnakeCaseWithUnderscoreSuffix,
+ cobolCase,
+ themeNameCobolCase,
+ macroCase,
+ cobolCaseWithHyphenSuffix,
+ macroCaseWithUnderscoreSuffix,
+ };
+};
+
+/**
+ * Return root directory
+ *
+ * @return {string} root directory
+ */
+const getRoot = () => {
+ return path.resolve( __dirname, '../' );
+};
From a3c056c2df0fb025b0376af6930e5b54debb4133 Mon Sep 17 00:00:00 2001
From: thelovekesh
Date: Wed, 13 Apr 2022 16:09:52 +0530
Subject: [PATCH 02/12] Add functions to get all files ina given dir
---
bin/init.js | 62 ++++++++++++++++++++++++++++++++++++++++++-----------
1 file changed, 49 insertions(+), 13 deletions(-)
diff --git a/bin/init.js b/bin/init.js
index cf435db5..43c228a8 100644
--- a/bin/init.js
+++ b/bin/init.js
@@ -30,6 +30,7 @@ const info = {
return `\x1b[34m${ message }\x1b[0m`;
},
};
+let isSomethingChanged = false;
// Start with a prompt.
rl.question( 'Would you like to setup the theme? (Y/n) ', ( answer ) => {
@@ -130,18 +131,6 @@ const initTheme = ( themeInfo ) => {
ELEMENTARY_THEME_: themeInfo.macroCaseWithUnderscoreSuffix,
};
- Object.keys( chunksToReplace ).forEach( ( key ) => {
- replaceContentInFile( key, chunksToReplace[ key ] );
- } );
-};
-
-/**
- * Replace content in file
- *
- * @param {string} chunksToReplace
- * @param {Object} newChunk
- */
-const replaceContentInFile = ( chunksToReplace, newChunk ) => {
const files = [
'composer.json',
'functions.php',
@@ -152,7 +141,53 @@ const replaceContentInFile = ( chunksToReplace, newChunk ) => {
'README.md',
'style.css',
];
+ const incDirFiles = getAllFiles( getRoot() + '/inc' );
+ const templatesDirFiles = getAllFiles( getRoot() + '/templates' );
+
+ // Concat all files.
+ const allFiles = files.concat( incDirFiles ).concat( templatesDirFiles );
+
+ console.log( info.success( '\nReplacing theme details in files...' ) );
+ Object.keys( chunksToReplace ).forEach( ( key ) => {
+ replaceContentInFile( allFiles, key, chunksToReplace[ key ] );
+ } );
+ if ( isSomethingChanged ) {
+ console.log( info.success( '\nYour new theme is ready to go!' ), '✨' );
+ // Docs link
+ console.log( info.success( '\nFor more information on how to use this theme, please visit the following link: ' + info.warning( 'https://github.com/rtCamp/theme-elementary/blob/main/README.md' ) ) );
+ } else {
+ console.log( info.warning( '\nNo changes were made to your theme.\n' ) );
+ }
+};
+/**
+ * Get all files in a directory
+ *
+ * @param {Array} dir - Directory to search
+ */
+const getAllFiles = ( dir ) => {
+ const files = fs.readdirSync( dir );
+ const allFiles = [];
+ files.forEach( ( file ) => {
+ const filePath = path.join( dir, file );
+ const stat = fs.statSync( filePath );
+ if ( stat.isDirectory() ) {
+ allFiles.push( ...getAllFiles( filePath ) );
+ } else {
+ allFiles.push( filePath );
+ }
+ } );
+ return allFiles;
+};
+
+/**
+ * Replace content in file
+ *
+ * @param {Array} files Files to search
+ * @param {string} chunksToReplace String to replace
+ * @param {Object} newChunk New string to replace with
+ */
+const replaceContentInFile = ( files, chunksToReplace, newChunk ) => {
files.forEach( ( file ) => {
const filePath = path.resolve( getRoot(), file );
@@ -162,7 +197,8 @@ const replaceContentInFile = ( chunksToReplace, newChunk ) => {
content = content.replace( regex, newChunk );
if ( content !== fs.readFileSync( filePath, 'utf8' ) ) {
fs.writeFileSync( filePath, content, 'utf8' );
- console.log( info.success( `Updated ${ file }` ) );
+ console.log( info.success( `Updated file is ${ info.message( path.basename( filePath ) ) }` ) );
+ isSomethingChanged = true;
}
} catch ( err ) {
console.log( info.error( `\nError: ${ err }` ) );
From ae783fc7afb5b4e6ec53f45bbe5380e10d80c924 Mon Sep 17 00:00:00 2001
From: thelovekesh
Date: Wed, 13 Apr 2022 17:32:40 +0530
Subject: [PATCH 03/12] Add function to rename file name
---
bin/init.js | 60 ++++++++++++++++++++++++++++++++++++++++++++++-------
1 file changed, 52 insertions(+), 8 deletions(-)
diff --git a/bin/init.js b/bin/init.js
index 43c228a8..924ef310 100644
--- a/bin/init.js
+++ b/bin/init.js
@@ -30,7 +30,8 @@ const info = {
return `\x1b[34m${ message }\x1b[0m`;
},
};
-let isSomethingChanged = false;
+let fileContentUpdated = false;
+let fileNameUpdated = false;
// Start with a prompt.
rl.question( 'Would you like to setup the theme? (Y/n) ', ( answer ) => {
@@ -144,14 +145,33 @@ const initTheme = ( themeInfo ) => {
const incDirFiles = getAllFiles( getRoot() + '/inc' );
const templatesDirFiles = getAllFiles( getRoot() + '/templates' );
+ // File name to replace in.
+ const fileNameToReplace = {
+ 'class-elementary-theme.php': 'class-' + themeInfo.kebabCase + '.php',
+ };
+
// Concat all files.
const allFiles = files.concat( incDirFiles ).concat( templatesDirFiles );
- console.log( info.success( '\nReplacing theme details in files...' ) );
+ // Replace files contents.
+ console.log( info.success( '\nUpdating theme details in files...' ) );
Object.keys( chunksToReplace ).forEach( ( key ) => {
- replaceContentInFile( allFiles, key, chunksToReplace[ key ] );
+ replaceFileContent( allFiles, key, chunksToReplace[ key ] );
+ } );
+ if ( ! fileContentUpdated ) {
+ console.log( info.error( 'No file content updated.\n' ) );
+ }
+
+ // Replace file names
+ console.log( info.success( '\nUpdating theme bootstrap file name...' ) );
+ Object.keys( fileNameToReplace ).forEach( ( key ) => {
+ replaceFileName( allFiles, key, fileNameToReplace[ key ] );
} );
- if ( isSomethingChanged ) {
+ if ( ! fileNameUpdated ) {
+ console.log( info.error( 'No file name updated.\n' ) );
+ }
+
+ if ( fileContentUpdated || fileNameUpdated ) {
console.log( info.success( '\nYour new theme is ready to go!' ), '✨' );
// Docs link
console.log( info.success( '\nFor more information on how to use this theme, please visit the following link: ' + info.warning( 'https://github.com/rtCamp/theme-elementary/blob/main/README.md' ) ) );
@@ -185,9 +205,9 @@ const getAllFiles = ( dir ) => {
*
* @param {Array} files Files to search
* @param {string} chunksToReplace String to replace
- * @param {Object} newChunk New string to replace with
+ * @param {string} newChunk New string to replace with
*/
-const replaceContentInFile = ( files, chunksToReplace, newChunk ) => {
+const replaceFileContent = ( files, chunksToReplace, newChunk ) => {
files.forEach( ( file ) => {
const filePath = path.resolve( getRoot(), file );
@@ -197,8 +217,8 @@ const replaceContentInFile = ( files, chunksToReplace, newChunk ) => {
content = content.replace( regex, newChunk );
if ( content !== fs.readFileSync( filePath, 'utf8' ) ) {
fs.writeFileSync( filePath, content, 'utf8' );
- console.log( info.success( `Updated file is ${ info.message( path.basename( filePath ) ) }` ) );
- isSomethingChanged = true;
+ console.log( info.success( `Updated [${ info.message( chunksToReplace ) }] ${ info.success( 'to' ) } [${ info.message( newChunk ) }] ${ info.success( 'in file' ) } [${ info.message( path.basename( file ) ) }]` ) );
+ fileContentUpdated = true;
}
} catch ( err ) {
console.log( info.error( `\nError: ${ err }` ) );
@@ -206,6 +226,30 @@ const replaceContentInFile = ( files, chunksToReplace, newChunk ) => {
} );
};
+/**
+ * Change File Name
+ *
+ * @param {Array} files Files to search
+ * @param {string} oldFileName Old file name
+ * @param {string} newFileName New file name
+ */
+const replaceFileName = ( files, oldFileName, newFileName ) => {
+ files.forEach( ( file ) => {
+ if ( oldFileName !== path.basename( file ) ) {
+ return;
+ }
+ const filePath = path.resolve( getRoot(), file );
+ const newFilePath = path.resolve( getRoot(), file.replace( oldFileName, newFileName ) );
+ try {
+ fs.renameSync( filePath, newFilePath );
+ console.log( info.success( `Updated file [${ info.message( path.basename( filePath ) ) }] ${ info.success( 'to' ) } [${ info.message( path.basename( newFilePath ) ) }]` ) );
+ fileNameUpdated = true;
+ } catch ( err ) {
+ console.log( info.error( `\nError: ${ err }` ) );
+ }
+ } );
+};
+
/**
* Generate Theme Info from Theme Name
*
From e887812a0fa7fddfeb294bdaccf74f8f2329bee4 Mon Sep 17 00:00:00 2001
From: thelovekesh
Date: Wed, 13 Apr 2022 17:33:28 +0530
Subject: [PATCH 04/12] Update file permission
---
bin/init.js | 0
1 file changed, 0 insertions(+), 0 deletions(-)
mode change 100644 => 100755 bin/init.js
diff --git a/bin/init.js b/bin/init.js
old mode 100644
new mode 100755
From a107c5dcead4fd194441009223c5fbeb8bf6369e Mon Sep 17 00:00:00 2001
From: thelovekesh
Date: Wed, 13 Apr 2022 17:35:02 +0530
Subject: [PATCH 05/12] Add init scipt to prepare
---
package.json | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/package.json b/package.json
index 1fa4d1fb..0059d617 100644
--- a/package.json
+++ b/package.json
@@ -54,7 +54,7 @@
"lint:php:fix": "./bin/phpcbf.sh",
"lint:pkg-json": "wp-scripts lint-pkg-json --ignorePath .gitignore",
"lint:staged": "lint-staged",
- "prepare": "husky install",
+ "prepare": "husky install && ./bin/init.js",
"start": "wp-scripts start",
"test": "npm-run-all --parallel test:*",
"test:js": "wp-scripts test-unit-js --config=tests/js/jest.config.js",
From 1677d4403054cab2cf1a3d94a20bad562151e84c Mon Sep 17 00:00:00 2001
From: thelovekesh
Date: Wed, 13 Apr 2022 22:37:47 +0530
Subject: [PATCH 06/12] Update filename
---
inc/classes/{class-elementary.php => class-elementary-theme.php} | 0
1 file changed, 0 insertions(+), 0 deletions(-)
rename inc/classes/{class-elementary.php => class-elementary-theme.php} (100%)
diff --git a/inc/classes/class-elementary.php b/inc/classes/class-elementary-theme.php
similarity index 100%
rename from inc/classes/class-elementary.php
rename to inc/classes/class-elementary-theme.php
From 8480c0124269010b990d03bb6b3d1e0b342c405b Mon Sep 17 00:00:00 2001
From: thelovekesh
Date: Wed, 13 Apr 2022 22:52:10 +0530
Subject: [PATCH 07/12] Update themeName, namespace and textdomain
---
composer.json | 4 +--
functions.php | 28 +++++++++----------
inc/classes/class-elementary-theme.php | 16 +++++------
.../patterns/class-block-pattern-base.php | 4 +--
inc/classes/patterns/class-block-patterns.php | 28 +++++++++----------
inc/classes/patterns/content/class-footer.php | 12 ++++----
.../patterns/content/class-hidden-404.php | 14 +++++-----
inc/helpers/custom-functions.php | 2 +-
inc/traits/trait-singleton.php | 4 +--
index.php | 2 +-
package-lock.json | 4 +--
package.json | 2 +-
parts/footer.html | 2 +-
phpcs.xml.dist | 4 +--
style.css | 4 +--
templates/404.html | 2 +-
tests/bootstrap.php | 2 +-
tests/php/TestCase.php | 4 +--
18 files changed, 69 insertions(+), 69 deletions(-)
diff --git a/composer.json b/composer.json
index 4dc4b5cd..16863d23 100644
--- a/composer.json
+++ b/composer.json
@@ -1,5 +1,5 @@
{
- "name": "rtcamp/theme-elementary",
+ "name": "rtcamp/elementary-theme",
"description": "A Block based starter theme.",
"type": "wordpress-theme",
"homepage": "https://rtcamp.com/",
@@ -28,7 +28,7 @@
},
"autoload-dev": {
"psr-4": {
- "Elementary\\Tests\\": "tests/php/"
+ "Elementary_Theme\\Tests\\": "tests/php/"
}
},
"scripts": {
diff --git a/functions.php b/functions.php
index 7e66886f..65fa63dd 100644
--- a/functions.php
+++ b/functions.php
@@ -4,37 +4,37 @@
*
* @link https://developer.wordpress.org/themes/basics/theme-functions/
*
- * @package Elementary
+ * @package Elementary-Theme
*/
-if ( ! defined( 'ELEMENTARY_VERSION' ) ) :
- define( 'ELEMENTARY_VERSION', wp_get_theme()->get( 'Version' ) );
+if ( ! defined( 'ELEMENTARY_THEME_VERSION' ) ) :
+ define( 'ELEMENTARY_THEME_VERSION', wp_get_theme()->get( 'Version' ) );
endif;
-if ( ! defined( 'ELEMENTARY_TEMP_DIR' ) ) :
- define( 'ELEMENTARY_TEMP_DIR', untrailingslashit( get_template_directory() ) );
+if ( ! defined( 'ELEMENTARY_THEME_TEMP_DIR' ) ) :
+ define( 'ELEMENTARY_THEME_TEMP_DIR', untrailingslashit( get_template_directory() ) );
endif;
-if ( ! defined( 'ELEMENTARY_BUILD_URI' ) ) :
- define( 'ELEMENTARY_BUILD_URI', untrailingslashit( get_template_directory_uri() ) . '/assets/build' );
+if ( ! defined( 'ELEMENTARY_THEME_BUILD_URI' ) ) :
+ define( 'ELEMENTARY_THEME_BUILD_URI', untrailingslashit( get_template_directory_uri() ) . '/assets/build' );
endif;
-if ( ! defined( 'ELEMENTARY_BUILD_DIR' ) ) :
- define( 'ELEMENTARY_BUILD_DIR', untrailingslashit( get_template_directory() ) . '/assets/build' );
+if ( ! defined( 'ELEMENTARY_THEME_BUILD_DIR' ) ) :
+ define( 'ELEMENTARY_THEME_BUILD_DIR', untrailingslashit( get_template_directory() ) . '/assets/build' );
endif;
-require_once ELEMENTARY_TEMP_DIR . '/vendor/autoload.php';
+require_once ELEMENTARY_THEME_TEMP_DIR . '/vendor/autoload.php';
/**
* Theme bootstrap instance.
*
* @since 1.0.0
*
- * @return object Elementary
+ * @return object Theme bootstrap instance.
*/
-function elementary_instance() {
- return Elementary\Elementary::get_instance();
+function elementary_theme_instance() {
+ return Elementary_Theme\Elementary_Theme::get_instance();
}
// Instantiate theme.
-elementary_instance();
+elementary_theme_instance();
diff --git a/inc/classes/class-elementary-theme.php b/inc/classes/class-elementary-theme.php
index 955408d6..99633098 100644
--- a/inc/classes/class-elementary-theme.php
+++ b/inc/classes/class-elementary-theme.php
@@ -2,20 +2,20 @@
/**
* Theme bootstrap file.
*
- * @package Elementary
+ * @package Elementary-Theme
*/
-namespace Elementary;
+namespace Elementary_Theme;
-use Elementary\Traits\Singleton;
-use Elementary\Patterns\Block_Patterns;
+use Elementary_Theme\Traits\Singleton;
+use Elementary_Theme\Patterns\Block_Patterns;
/**
- * Class Elementary
+ * Class Elementary_Theme
*
* @since 1.0.0
*/
-class Elementary {
+class Elementary_Theme {
use Singleton;
@@ -36,7 +36,7 @@ protected function __construct() {
* @since 1.0.0
*/
public function setup_hooks() {
- add_action( 'after_setup_theme', array( $this, 'elementary_support' ) );
+ add_action( 'after_setup_theme', array( $this, 'elementary_theme_support' ) );
}
/**
@@ -44,7 +44,7 @@ public function setup_hooks() {
*
* @since 1.0.0
*/
- public function elementary_support() {
+ public function elementary_theme_support() {
// Add support for core block styles.
add_theme_support( 'wp-block-styles' );
}
diff --git a/inc/classes/patterns/class-block-pattern-base.php b/inc/classes/patterns/class-block-pattern-base.php
index a2bb79d2..37f31ca9 100644
--- a/inc/classes/patterns/class-block-pattern-base.php
+++ b/inc/classes/patterns/class-block-pattern-base.php
@@ -2,10 +2,10 @@
/**
* Base class for block patterns.
*
- * @package Elementary
+ * @package Elementary-Theme
*/
-namespace Elementary\Patterns;
+namespace Elementary_Theme\Patterns;
/**
* Class Block_Pattern_Base
diff --git a/inc/classes/patterns/class-block-patterns.php b/inc/classes/patterns/class-block-patterns.php
index 9db5a50a..636078ad 100644
--- a/inc/classes/patterns/class-block-patterns.php
+++ b/inc/classes/patterns/class-block-patterns.php
@@ -2,12 +2,12 @@
/**
* Blocks Patterns handler.
*
- * @package Elementary
+ * @package Elementary-Theme
*/
-namespace Elementary\Patterns;
+namespace Elementary_Theme\Patterns;
-use Elementary\Traits\Singleton;
+use Elementary_Theme\Traits\Singleton;
use WP_Block_Pattern_Categories_Registry;
use WP_Block_Patterns_Registry;
@@ -25,14 +25,14 @@ class Block_Patterns {
*
* @var string
*/
- const PATTERN_NAMESPACE = 'elementary';
+ const PATTERN_NAMESPACE = 'elementary-theme';
/**
* Block Content classes namespace.
*
* @var string
*/
- const BLOCK_CONTENT_NAMESPACE = 'Elementary\\Patterns\\Content\\';
+ const BLOCK_CONTENT_NAMESPACE = 'Elementary_Theme\\Patterns\\Content\\';
/**
* Constructor.
@@ -47,8 +47,8 @@ protected function __construct() {
* @since 1.0.0
*/
public function setup_hooks() {
- add_action( 'init', [ $this, 'elementary_register_block_patterns_categories' ] );
- add_action( 'init', [ $this, 'elementary_register_block_patterns' ] );
+ add_action( 'init', [ $this, 'elementary_theme_register_block_patterns_categories' ] );
+ add_action( 'init', [ $this, 'elementary_theme_register_block_patterns' ] );
}
/**
@@ -56,16 +56,16 @@ public function setup_hooks() {
*
* @since 1.0.0
*/
- public function elementary_register_block_patterns_categories() {
+ public function elementary_theme_register_block_patterns_categories() {
$block_pattern_categories = array(
'featured' => array(
- 'label' => __( 'Featured', 'elementary' ),
+ 'label' => __( 'Featured', 'elementary-theme' ),
),
'footer' => array(
- 'label' => __( 'Footer', 'elementary' ),
+ 'label' => __( 'Footer', 'elementary-theme' ),
),
'query' => array(
- 'label' => __( 'Query', 'elementary' ),
+ 'label' => __( 'Query', 'elementary-theme' ),
),
);
@@ -76,7 +76,7 @@ public function elementary_register_block_patterns_categories() {
*
* @param array $block_pattern_categories Array of block pattern categories.
*/
- $block_pattern_categories = apply_filters( 'elementary_block_patterns_categories', $block_pattern_categories );
+ $block_pattern_categories = apply_filters( 'elementary_theme_block_patterns_categories', $block_pattern_categories );
foreach ( $block_pattern_categories as $name => $properties ) {
if ( ! WP_Block_Pattern_Categories_Registry::get_instance()->is_registered( $name ) ) {
@@ -90,7 +90,7 @@ public function elementary_register_block_patterns_categories() {
*
* @since 1.0.0
*/
- public function elementary_register_block_patterns() {
+ public function elementary_theme_register_block_patterns() {
$block_patterns_classes = array(
'footer' => 'Footer',
'hidden-404' => 'Hidden_404',
@@ -103,7 +103,7 @@ public function elementary_register_block_patterns() {
*
* @param array $block_patterns The theme block patterns.
*/
- $block_patterns = apply_filters( 'elementary_block_patterns', $block_patterns_classes );
+ $block_patterns = apply_filters( 'elementary_theme_block_patterns', $block_patterns_classes );
foreach ( $block_patterns as $name => $class ) {
if ( ! WP_Block_Patterns_Registry::get_instance()->is_registered( $name ) ) {
diff --git a/inc/classes/patterns/content/class-footer.php b/inc/classes/patterns/content/class-footer.php
index 8f8a2719..80f9afc2 100644
--- a/inc/classes/patterns/content/class-footer.php
+++ b/inc/classes/patterns/content/class-footer.php
@@ -2,12 +2,12 @@
/**
* Footer pattern content.
*
- * @package Elementary
+ * @package Elementary-Theme
*/
-namespace Elementary\Patterns\Content;
+namespace Elementary_Theme\Patterns\Content;
-use Elementary\Patterns\Block_Pattern_Base;
+use Elementary_Theme\Patterns\Block_Pattern_Base;
/**
* Class Footer
@@ -23,7 +23,7 @@ final class Footer extends Block_Pattern_Base {
*/
public function block_pattern() {
return array(
- 'title' => __( 'Footer', 'elementary' ),
+ 'title' => __( 'Footer', 'elementary-theme' ),
'categories' => array( 'footer' ),
'blockTypes' => array( 'core/template-part/footer' ),
'content' => $this->block_pattern_content(),
@@ -48,8 +48,8 @@ public function block_pattern_content() {
WordPress'
+ esc_html__( 'Proudly powered by %s', 'elementary-theme' ),
+ 'WordPress'
);
?>
diff --git a/inc/classes/patterns/content/class-hidden-404.php b/inc/classes/patterns/content/class-hidden-404.php
index 867182ef..2e3d9a28 100644
--- a/inc/classes/patterns/content/class-hidden-404.php
+++ b/inc/classes/patterns/content/class-hidden-404.php
@@ -2,12 +2,12 @@
/**
* 404 Pattern content.
*
- * @package Elementary
+ * @package Elementary-Theme
*/
-namespace Elementary\Patterns\Content;
+namespace Elementary_Theme\Patterns\Content;
-use Elementary\Patterns\Block_Pattern_Base;
+use Elementary_Theme\Patterns\Block_Pattern_Base;
/**
* Class Hidden_404
@@ -22,7 +22,7 @@ final class Hidden_404 extends Block_Pattern_Base {
*/
public function block_pattern() {
return array(
- 'title' => __( '404 content', 'elementary' ),
+ 'title' => __( '404 content', 'elementary-theme' ),
'inserter' => false,
'content' => $this->block_pattern_content(),
);
@@ -37,17 +37,17 @@ public function block_pattern_content() {
ob_start();
?>
-
+
-
+
-
+
diff --git a/inc/helpers/custom-functions.php b/inc/helpers/custom-functions.php
index 701cb0db..837f5dfe 100644
--- a/inc/helpers/custom-functions.php
+++ b/inc/helpers/custom-functions.php
@@ -2,7 +2,7 @@
/**
* Define custom functions for the theme.
*
- * @package Elementary
+ * @package Elementary-Theme
*/
// Define custom functions here.
diff --git a/inc/traits/trait-singleton.php b/inc/traits/trait-singleton.php
index d11d7c9f..f7540d02 100644
--- a/inc/traits/trait-singleton.php
+++ b/inc/traits/trait-singleton.php
@@ -22,10 +22,10 @@
*
* If you specifically need multiple objects, then use a normal class.
*
- * @package Blank-Theme
+ * @package Elementary-Theme
*/
-namespace Elementary\Traits;
+namespace Elementary_Theme\Traits;
trait Singleton {
diff --git a/index.php b/index.php
index 792304b4..22ea78c7 100644
--- a/index.php
+++ b/index.php
@@ -2,7 +2,7 @@
/**
* Required template for standalone theme.
*
- * @package Elementary
+ * @package Elementary-Theme
*/
// There is nothing output here because block themes do not use php templates.
diff --git a/package-lock.json b/package-lock.json
index fbd9dcb9..a68e7677 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,11 +1,11 @@
{
- "name": "elementary",
+ "name": "elementary-theme",
"version": "1.0.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
- "name": "elementary",
+ "name": "elementary-theme",
"version": "1.0.0",
"license": "GPL-2.0-or-later",
"devDependencies": {
diff --git a/package.json b/package.json
index 0059d617..bba28332 100644
--- a/package.json
+++ b/package.json
@@ -1,5 +1,5 @@
{
- "name": "elementary",
+ "name": "elementary-theme",
"version": "1.0.0",
"description": "A Block based starter theme.",
"private": true,
diff --git a/parts/footer.html b/parts/footer.html
index cd1d4ddb..c31265d0 100644
--- a/parts/footer.html
+++ b/parts/footer.html
@@ -1 +1 @@
-
+
diff --git a/phpcs.xml.dist b/phpcs.xml.dist
index 6d9cdbff..2cc58f87 100644
--- a/phpcs.xml.dist
+++ b/phpcs.xml.dist
@@ -50,7 +50,7 @@
Multiple valid text domains can be provided as a comma-delimited list. -->
-
+
@@ -82,7 +82,7 @@
Multiple valid prefixes can be provided as a comma-delimited list. -->
-
+
tests/bootstrap.php
diff --git a/style.css b/style.css
index 747d7bc4..866290ed 100644
--- a/style.css
+++ b/style.css
@@ -1,5 +1,5 @@
/*
-Theme Name: Elementary
+Theme Name: Elementary Theme
Theme URI: https://rtcamp.com/
Author: rtCamp
Author URI: https://rtcamp.com/
@@ -7,7 +7,7 @@ Description: Elementary is a FSE based theme,
Version: 1.0.0
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
-Text Domain: elementary
+Text Domain: elementary-theme
Tags: fse
This theme, like WordPress, is licensed under the GPL.
Use it to make something cool, have fun, and share what
diff --git a/templates/404.html b/templates/404.html
index 2db44c7b..34a62422 100644
--- a/templates/404.html
+++ b/templates/404.html
@@ -4,7 +4,7 @@
-
+
diff --git a/tests/bootstrap.php b/tests/bootstrap.php
index 186b5093..12956eb6 100644
--- a/tests/bootstrap.php
+++ b/tests/bootstrap.php
@@ -2,7 +2,7 @@
/**
* PHPUnit bootstrap file
*
- * @package Elementary
+ * @package Elementary-Theme
*/
define( 'TESTS_THEME_DIR', __DIR__ );
diff --git a/tests/php/TestCase.php b/tests/php/TestCase.php
index 9974007d..8b883981 100644
--- a/tests/php/TestCase.php
+++ b/tests/php/TestCase.php
@@ -2,10 +2,10 @@
/**
* Provide a base class for all unit tests by extending WP_UnitTestCase.
*
- * @package Elementary
+ * @package Elementary-Theme
*/
-namespace Elementary\Tests;
+namespace Elementary_Theme\Tests;
use WP_UnitTestCase;
From 476cc2a091b0e640a4e2d3a198a9d21ca2fb4e52 Mon Sep 17 00:00:00 2001
From: thelovekesh
Date: Wed, 13 Apr 2022 23:03:07 +0530
Subject: [PATCH 08/12] Add parts and tests dir in files
---
bin/init.js | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/bin/init.js b/bin/init.js
index 924ef310..19f5cdf6 100755
--- a/bin/init.js
+++ b/bin/init.js
@@ -79,7 +79,7 @@ const setupTheme = ( themeName ) => {
'Theme Name: ': `${ themeInfo.themeName }`,
'Theme Version: ': `1.0.0`,
'Text Domain: ': `${ themeInfo.kebabCase }`,
- 'Package: ': `${ themeInfo.kebabCase }`,
+ 'Package: ': `${ themeInfo.trainCase }`,
'Namespace: ': `${ themeInfo.pascalSnakeCase }`,
'Function Prefix: ': `${ themeInfo.snakeCaseWithUnderscoreSuffix }`,
'CSS Class Prefix: ': `${ themeInfo.kebabCaseWithHyphenSuffix }`,
@@ -142,8 +142,11 @@ const initTheme = ( themeInfo ) => {
'README.md',
'style.css',
];
+
const incDirFiles = getAllFiles( getRoot() + '/inc' );
+ const partsDir = getAllFiles( getRoot() + '/parts' );
const templatesDirFiles = getAllFiles( getRoot() + '/templates' );
+ const testsDir = getAllFiles( getRoot() + '/tests' );
// File name to replace in.
const fileNameToReplace = {
@@ -151,7 +154,7 @@ const initTheme = ( themeInfo ) => {
};
// Concat all files.
- const allFiles = files.concat( incDirFiles ).concat( templatesDirFiles );
+ const allFiles = files.concat( incDirFiles ).concat( partsDir ).concat( templatesDirFiles ).concat( testsDir );
// Replace files contents.
console.log( info.success( '\nUpdating theme details in files...' ) );
From d2938a918659d03868d5fd42a662cdc8a577c053 Mon Sep 17 00:00:00 2001
From: thelovekesh
Date: Thu, 14 Apr 2022 14:51:38 +0530
Subject: [PATCH 09/12] Update theme init script
---
package.json | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/package.json b/package.json
index bba28332..1ccbebe6 100644
--- a/package.json
+++ b/package.json
@@ -44,6 +44,7 @@
"build:prod": "cross-env NODE_ENV=production npm-run-all 'build:!(dev|prod)'",
"build:js": "wp-scripts build",
"dev": "wp-scripts start",
+ "init": "./bin/init.js",
"lint": "npm-run-all --parallel lint:*",
"lint:css": "wp-scripts lint-style ./assets/src",
"lint:css:fix": "npm run lint:css -- --fix ./assets/src",
@@ -54,7 +55,7 @@
"lint:php:fix": "./bin/phpcbf.sh",
"lint:pkg-json": "wp-scripts lint-pkg-json --ignorePath .gitignore",
"lint:staged": "lint-staged",
- "prepare": "husky install && ./bin/init.js",
+ "prepare": "husky install && npm run init",
"start": "wp-scripts start",
"test": "npm-run-all --parallel test:*",
"test:js": "wp-scripts test-unit-js --config=tests/js/jest.config.js",
From f719eec3ca34d3fbf2f3c52d6fccf59bb1250bac Mon Sep 17 00:00:00 2001
From: thelovekesh
Date: Thu, 14 Apr 2022 16:45:42 +0530
Subject: [PATCH 10/12] Update the process of getting file names
---
bin/init.js | 82 +++++++++++++++++++++++++++++------------------------
1 file changed, 45 insertions(+), 37 deletions(-)
diff --git a/bin/init.js b/bin/init.js
index 19f5cdf6..7ccd5f3c 100755
--- a/bin/init.js
+++ b/bin/init.js
@@ -132,43 +132,32 @@ const initTheme = ( themeInfo ) => {
ELEMENTARY_THEME_: themeInfo.macroCaseWithUnderscoreSuffix,
};
- const files = [
- 'composer.json',
- 'functions.php',
- 'index.php',
- 'package.json',
- 'package-lock.json',
- 'phpcs.xml.dist',
- 'README.md',
- 'style.css',
- ];
-
- const incDirFiles = getAllFiles( getRoot() + '/inc' );
- const partsDir = getAllFiles( getRoot() + '/parts' );
- const templatesDirFiles = getAllFiles( getRoot() + '/templates' );
- const testsDir = getAllFiles( getRoot() + '/tests' );
+ const files = getAllFiles( getRoot() );
// File name to replace in.
- const fileNameToReplace = {
- 'class-elementary-theme.php': 'class-' + themeInfo.kebabCase + '.php',
- };
-
- // Concat all files.
- const allFiles = files.concat( incDirFiles ).concat( partsDir ).concat( templatesDirFiles ).concat( testsDir );
+ const fileNameToReplace = {};
+ files.forEach( ( file ) => {
+ const fileName = path.basename( file );
+ Object.keys( chunksToReplace ).forEach( ( key ) => {
+ if ( fileName.includes( key ) ) {
+ fileNameToReplace[ fileName ] = fileName.replace( key, chunksToReplace[ key ] );
+ }
+ } );
+ } );
// Replace files contents.
- console.log( info.success( '\nUpdating theme details in files...' ) );
+ console.log( info.success( '\nUpdating theme details in file(s)...' ) );
Object.keys( chunksToReplace ).forEach( ( key ) => {
- replaceFileContent( allFiles, key, chunksToReplace[ key ] );
+ replaceFileContent( files, key, chunksToReplace[ key ] );
} );
if ( ! fileContentUpdated ) {
console.log( info.error( 'No file content updated.\n' ) );
}
// Replace file names
- console.log( info.success( '\nUpdating theme bootstrap file name...' ) );
+ console.log( info.success( '\nUpdating theme file(s) name...' ) );
Object.keys( fileNameToReplace ).forEach( ( key ) => {
- replaceFileName( allFiles, key, fileNameToReplace[ key ] );
+ replaceFileName( files, key, fileNameToReplace[ key ] );
} );
if ( ! fileNameUpdated ) {
console.log( info.error( 'No file name updated.\n' ) );
@@ -189,18 +178,37 @@ const initTheme = ( themeInfo ) => {
* @param {Array} dir - Directory to search
*/
const getAllFiles = ( dir ) => {
- const files = fs.readdirSync( dir );
- const allFiles = [];
- files.forEach( ( file ) => {
- const filePath = path.join( dir, file );
- const stat = fs.statSync( filePath );
- if ( stat.isDirectory() ) {
- allFiles.push( ...getAllFiles( filePath ) );
- } else {
- allFiles.push( filePath );
- }
- } );
- return allFiles;
+ const dirIgnore = [
+ '.git',
+ '.github',
+ 'node_modules',
+ 'vendor',
+ 'bin',
+ ];
+
+ try {
+ const files = fs.readdirSync( dir );
+
+ files.forEach( ( file, index ) => {
+ if ( dirIgnore.indexOf( file ) !== -1 ) {
+ files.splice( index, 1 );
+ }
+ } );
+
+ const allFiles = [];
+ files.forEach( ( file ) => {
+ const filePath = path.join( dir, file );
+ const stat = fs.statSync( filePath );
+ if ( stat.isDirectory() ) {
+ allFiles.push( ...getAllFiles( filePath ) );
+ } else {
+ allFiles.push( filePath );
+ }
+ } );
+ return allFiles;
+ } catch ( err ) {
+ console.log( info.error( err ) );
+ }
};
/**
From c7904ba527402ade7d81200ca4dc34e7a6631958 Mon Sep 17 00:00:00 2001
From: thelovekesh
Date: Thu, 14 Apr 2022 23:07:19 +0530
Subject: [PATCH 11/12] Update logic of dir or files ignore
Old method was splicing the array incorrectly
---
bin/init.js | 13 ++++---------
1 file changed, 4 insertions(+), 9 deletions(-)
diff --git a/bin/init.js b/bin/init.js
index 7ccd5f3c..fb99fc34 100755
--- a/bin/init.js
+++ b/bin/init.js
@@ -178,22 +178,17 @@ const initTheme = ( themeInfo ) => {
* @param {Array} dir - Directory to search
*/
const getAllFiles = ( dir ) => {
- const dirIgnore = [
+ const dirOrFilesIgnore = [
'.git',
'.github',
'node_modules',
'vendor',
- 'bin',
+ 'init.js',
];
try {
- const files = fs.readdirSync( dir );
-
- files.forEach( ( file, index ) => {
- if ( dirIgnore.indexOf( file ) !== -1 ) {
- files.splice( index, 1 );
- }
- } );
+ let files = fs.readdirSync( dir );
+ files = files.filter( ( fileOrDir ) => ! dirOrFilesIgnore.includes( fileOrDir ) );
const allFiles = [];
files.forEach( ( file ) => {
From bb9dad4367f3bf105a29639f3e7fd49a579e3255 Mon Sep 17 00:00:00 2001
From: RahiDroid
Date: Mon, 18 Apr 2022 15:58:21 +0530
Subject: [PATCH 12/12] Chore: Use lowercase 'y' for confirmation
---
bin/init.js | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/bin/init.js b/bin/init.js
index fb99fc34..fbd387fb 100755
--- a/bin/init.js
+++ b/bin/init.js
@@ -34,14 +34,14 @@ let fileContentUpdated = false;
let fileNameUpdated = false;
// Start with a prompt.
-rl.question( 'Would you like to setup the theme? (Y/n) ', ( answer ) => {
+rl.question( 'Would you like to setup the theme? (y/n) ', ( answer ) => {
if ( 'n' === answer.toLowerCase() ) {
console.log( info.warning( '\nTheme Setup Cancelled.\n' ) );
process.exit( 0 );
}
rl.question( 'Enter theme name (shown in WordPress admin)*: ', ( themeName ) => {
const themeInfo = setupTheme( themeName );
- rl.question( 'Confirm the Theme Details (Y/n) ', ( confirm ) => {
+ rl.question( 'Confirm the Theme Details (y/n) ', ( confirm ) => {
if ( 'n' === confirm.toLowerCase() ) {
console.log( info.warning( '\nTheme Setup Cancelled.\n' ) );
process.exit( 0 );