Skip to content

Update Project Structure 1. Oct 2022

Brian Henry edited this page Oct 21, 2022 · 2 revisions

The original WPPB directory layout was like:

admin
admin\class-admin.php
includes
includes\class-plugin-name.php
plugin-slug.php
...

Now, the convention is to have most of those files inside a src folder, alongside:

assets\
src\admin
src\includes
templates\
vendor\
plugin-slug.php

The following set of commands does 80% of the moving and renaming. If you have healthy test coverage, run these, then keep running your tests and making changes until they all pass.

plugin_name="Example Plugin"
plugin_package_name="BrianHenryIE\\\\\\Plugin_Name"

plugin_name="BH WC Checkout Rate Limiter";
plugin_package_name="BrianHenryIE\\\\\\WC_Checkout_Rate_Limiter";


plugin_slug=$(echo $plugin_name | tr '[:upper:]' '[:lower:]' | sed 's/ /-/g'); echo $plugin_slug; # example-plugin;

plugin_snake=$(echo $plugin_name | sed 's/ /_/g'); echo $plugin_snake; # Example_Plugin
#plugin_snake_lower=$(echo $plugin_snake | tr '[:upper:]' '[:lower:]'); echo $plugin_snake_lower; # example_plugin
plugin_capitalized=$(echo $plugin_name | tr '[:lower:]' '[:upper:]' | sed 's/ /_/g'); echo $plugin_capitalized; # EXAMPLE_PLUGIN
php_package_name=$(echo "${plugin_package_name%%\\*}"/$plugin_slug | tr '[:upper:]' '[:lower:]'); echo $php_package_name # brianhenryie/example-plugin

php_package_name_escaped=$(echo $php_package_name | sed 's/\//\\\//g');


# Delete the vendor folder so find+replace is faster.
rm -rf vendor;
rm -rf wordpress;


mkdir dist-archive;
mv *.zip dist-archive;


git mv assets .wordpress-org;
find . -depth 1 \( -name 'README.md' \) -exec sed -i '' "s/assets\\//.wordpress-org\\//g" {} +;

# RM the existing symlink to it is rebuilt correctly
rm wp-content/plugins/$plugin_slug;


rm src/license.txt;





# TODO: git rm src/index.php
## git rm -r --cached src/admin/index.php
rm src/index.php
find src -depth \( -name 'index.php' \) -exec git rm -r --cached {} +


git mv src/*.* .;
git mv src/languages .; mv src/languages .;
git mv src/templates .;



rm src/.distignore;
rm -rf src/strauss;

# TODO: Get new .distignore from another project
wget https://raw.githubusercontent.com/BrianHenryIE/WordPress-Plugin-Boilerplate/no-loader/.distignore

git mv src/Includes src/WP_Includes;
git mv tests/wpunit/Includes tests/wpunit/WP_Includes;
git mv tests/unit/Includes tests/unit/WP_Includes;
git mv tests/integration/Includes tests/integration/WP_Includes;

find src -depth \( -name '*.php' \) -exec sed -i '' "s/\Includes/\WP_Includes/g" {} +;
find tests -depth \( -name '*.php' \) -exec sed -i '' "s/\Includes/\WP_Includes/g" {} +;


# TODO: make previous replacement case insensitive
find src -depth \( -name '*.php' \) -exec sed -i '' "s/\includes/\WP_Includes/g" {} +;
find test -depth \( -name '*.php' \) -exec sed -i '' "s/\includes/\WP_Includes/g" {} +;


# For older plugins:::

git mv src/includes src/WP_Includes;
git mv tests/wpunit/includes tests/wpunit/WP_Includes;
git mv tests/unit/includes tests/unit/WP_Includes;
git mv tests/integration/includes tests/integration/WP_Includes;




git mv src/WP_Includes/class-$plugin_slug.php src;
find src -depth 1 \( -name '*.php' \) -exec sed -i '' "s/namespace .*\\\WP_Includes;/namespace $plugin_package_name;/g" {} +; 


git mv tests/unit/WP_Includes/class-$plugin_slug-unit-Test.php tests/unit;


git mv src/API/interface-api.php src;
git mv src/API/interface-settings.php src;
# replace namespace in those three files
find src -depth 1 \( -name '*.php' \) -exec sed -i '' "s/namespace $plugin_package_name\\\\\API;/namespace $plugin_package_name;/g" {} +; 

# replace namespace in files that include them
find . -depth \( -name '*.php' \) -exec sed -i '' "s/\\\API\\\Settings_Interface/\\\Settings_Interface/g" {} +;
find . -depth \( -name '*.php' \) -exec sed -i '' "s/\\\API\\\API_Interface/\\\API_Interface/g" {} +;


# Delete all index.php files. Need to do this after the git mv
find src -depth \( -name 'index.php' \) -exec rm {} +



# TODO: 
# remove from base file:
# use BrianHenryIE\WC_Cash_App_Gateway\WP_Includes\BH_WC_Cash_App_Gateway;
# Not working:
# find $plugin_slug.php -exec sed -i '' "s/use "$plugin_package_name"\\\\\\WP_Includes\\\\\\"$plugin_snake";//g" {} +; 


# Move all JS and CSS to assets folder. Delete all empty folders afterwards.
mkdir assets;
find src -depth \( -name '*.js' -o -name '*.css' \) -exec bash -c 'for f; do git mv "$f" assets; done' _ {} +;
find . -type d -empty -delete


git mv src/Admin/class-admin.php src/Admin/class-admin-assets.php
git mv src/Frontend/class-frontend.php src/Frontend/class-frontend-assets.php

# plugin_dir_url( __FILE__ ) . 'css
# plugin_dir_url( __FILE__ ) . 'js
# to
# plugin_dir_url( plugin_capitalized ) . 'assets

# Close but not working:
#find src/Admin -depth \( -name 'class-admin-assets.php' \) -exec sed -i '' "s/plugin_dir_url( __FILE__ ) . 'js/plugin_dir_url(" $plugin_capitalized"_BASENAME"  ") . 'assets/g" {} +;
#find src/Admin -depth \( -name 'class-admin-assets.php' \) -exec sed -i '' "s/plugin_dir_url( __FILE__ ) . 'css/plugin_dir_url(" $plugin_capitalized"_BASENAME"  ") . 'assets/g" {} +;

# plugin_dir_url( __FILE__ ) . 'js/bh-wp-autologin-urls-woocommerce-admin.js'
# plugin_dir_url( $this->settings->get_plugin_basename() ) . 'assets/$plugin_slug-woocommerce-admin.js'

# todo: Admin to Admin_Assets
"s/new Admin\(/new Admin_Assets(/g "
"s/new Frontend\(/new Frontend_Assets(/g "
# update tests.


# Replace all subpackage lines with blank lines.



# Update autoload.php

require_once DIR . '/vendor-prefixed/autoload.php';

$wpcs_autoloader = new WP_Namespace_Autoloader( array( 'classes_dir' => array( 'src' ) ) ); $wpcs_autoloader->init();



// TODO: wget a blank one... only need to replace the package name later, which should be done anyway.
bootstrap

$GLOBALS['plugin_root_dir'] = $plugin_root_dir = $project_root_dir;


Add secret env loading.


# TODO: find and replace php package name.


class-I18n.php
`dirname( plugin_basename( __FILE__ ), 2 ) . '/languages/'` 
-> 
dirname( plugin_basename( __FILE__ ), 3 ) . '/languages/'




# .gitignore
curl https://raw.githubusercontent.com/BrianHenryIE/WordPress-Plugin-Boilerplate/no-loader/.gitignore > .gitignore;
# phpcs.xml
curl https://raw.githubusercontent.com/BrianHenryIE/WordPress-Plugin-Boilerplate/no-loader/phpcs.xml > phpcs.xml;


# TODO:
replace package
replace subpackage

find src -depth \( -name '*.php' \) -exec sed -i '' 's/ \* @package.*/ \* @package '$(echo $php_package_name_escaped)'/g' {} +;
find tests -depth \( -name '*.php' \) -exec sed -i '' 's/ \* @package.*/ \* @package '$(echo $php_package_name_escaped)'/g' {} +;

find src -depth \( -name '*.php' \) -exec sed -i '' "s/ \* @subpackage.*/ \*/g" {} +;
find tests -depth \( -name '*.php' \) -exec sed -i '' "s/ \* @subpackage.*/ \*/g" {} +;




composer delete-databases;
composer create-databases;


rm composer.lock;

composer install;
vendor/bin/phpbcf;
XDEBUG_MODE=coverage composer run-script coverage-tests;


Clone this wiki locally