Comments (2)
IMO if we're thinking of giving more structure to the PHP side of things and potentially introducing helper classes and utilities it would make sense to make anything reusable in a composer PHP package.
from wp-scaffold.
@tlovett1 I've been thinking about this recently; the way we do this in most projects I've seen so far is the same as this PR: #13.
Whilst most engineers at 10up will be used to this; I wonder if now is an opportune time to re-think the structure.
For example, one of the things I dislike about the current method is that we use Plugin.php
to initialise all of our classes. This seems like an unnecessary step since we're using a PSR-4 autoloader.
Instead, we could have an abstract class used by classes to initialise them. E.G.
/**
* Class InstantiableModule
*
* Used to help register classes that should be instantiated on plugin load.
*/
abstract class InstantiableModule {
/**
* Set up the class and whether the module should be registered or not.
*/
function __construct() {
if ( $this->can_register() ) {
add_action( 'tenup_plugin_loaded', [ $this, 'register' ], 9 );
}
}
/**
* Can the module be registered?
*
* @return boolean
*/
abstract public function can_register();
/**
* Register any hooks/functions for the module.
*/
abstract public function register();
}
It would then be used like this:
/**
* Add a test settings page to the admin area.
*/
class TestSettings extends InstantiableModule {
/**
* Can the module be registered?
*
* @return boolean
*/
public function can_register() {
return is_admin();
}
/**
* Register any hooks/functions for the module.
*/
public function register() {
add_action( 'init', [ $this, 'add_setting_page' ] );
}
}
There would be a few benefits of changing the system to work this way:
- It keeps the same structure to the code that engineers are already familiar with.
- It removes a required step from the setup of a new class.
- It gives engineers an easy way to use the standard setup (via
InstantiableModule
) and allows them to implement custom loading via the constructor should they want that control.
If we were to implement this pattern of instantiating modules, it would also allow us to create a 10up CLI script to help engineers create common classes quickly. For example, say an engineer wanted to add a new settings page, we could have a stub set up that would allow them to do something like:
wp 10up make:setting Settings/SiteSettings --admin=true --fm=true
This CLI command would do something like:
- Create a new class using a 10up stub with the namespace
Settings\SiteSettings
- Make sure that FieldManager is required via Composer.
- Set the
can_register()
method to return tois_admin() && function_exists( 'fm_register_submenu_page' )
- Set the
register()
method to callfm_register_submenu_page()
and all other required functions.
The engineer would only need to set up the fields they want to render, reducing the amount of scaffolding they need to do significantly.
The CLI script is secondary to this issue, but I thought it was worth raising since it'd be significantly harder to implement if we stick with the current loading method via Plugin.php
.
from wp-scaffold.
Related Issues (20)
- Should WP Scaffold use GPLv2 instead of the MIT License? HOT 1
- Update css reset and some base styles
- WordPress 6.3 Compatibility Tracking
- Error: Cannot find module '@10up/babel-preset-default' on clean install HOT 4
- Headless scaffold
- PHPCS does not scan mu-plugins folder
- Add prettier / remove prettier from eslint config HOT 2
- Bump Node versions HOT 1
- Add opinionated code quality tools HOT 2
- Automate the process of renaming the theme and plugin to match the project name HOT 3
- Simplify scaffold HOT 3
- Headless theme
- Float based alignments are causing layout issues for adjacent blocks
- Enhance CSS reset with a custom reset.css file
- RFC: Add JS Module entrypoints to scaffold
- PHPCS Broken HOT 3
- Lint-staged assumes you have 10up-toolkit installed globally HOT 2
- Fix initial linting errors in the scaffold
- Add a standardised way to register post meta and REST fields
- Downstream dependencies require node 20, not 18
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wp-scaffold.