As mentioned here and here, we are using a multi-package-/mono repository that means we can use the advantages of modularized code. What does this exactly mean? Code, which you use more than once, put it into an own package. This works for TypeScript code and also for PHP code.
Beside the packages/utils
package you can create another package. It is recommend to not modify the utils package so you can easier migrate to new versions of WP ReactJS Starter. So, let's start and create a new package:
create-wp-react-app create-package
You will get some prompts and afterwards a new package will be available in packages/
.
The previously created package contains a src
folder. There you can put all your shared PHP classes. Each created plugin can consume local packages from packages/
as the path
is configured already properly.
cd plugins/my-plugin # Navigate to our plugin
composer require "wp-reactjs-multi-starter/mypackage @dev" # @dev is important so symlinking works as expected!
Do not forget navigating to your plugin which consumes the dependency and add the dependency path to .wprjss only changes
.
So, let's imagine we have the file packages/mypackage/src/Example.php
. In your plugins' coding you can access the class simply with the defined PHP namespace:
new \MatthiasWeb\MyPackage\Example();
{% hint style="info" %}
You have to replace wp-reactjs-multi-starter
/ MatthiasWeb\MyPackage
with your names.
{% endhint %}
{% hint style="warning" %} Our predefined GitLab integration does not currently support automatic publish to packagist.org as the local composer dependencies are bundled together with the plugin. {% endhint %}
What? You heard correctly, you can dynamically get plugin data in your composer package. Due to the fact you inject UtilsProvider
in all your classes you can get dynamically plugin data. That means, if you call a method of a modularized composer package in your plugin, you simply can obtain a constant like this in your composer package:
// [...]
trait MyAwesomeTrait
{
public function foo()
{
// Dynamically gets the WPRJSS_PATH variable. See available constant to get more.
$pluginPath = $this->getPluginConstant("PATH");
// Dynamically create a new instance of the plugins class "Activator"
$pluginInstance = $this->getPluginClassInstance("Activator");
// Dynamically get the core instance of the plugin
$pluginCore = $this->getCore();
}
}
{% hint style="warning" %} This only works with traits! {% endhint %}
The previously created package also contains a lib
folder. There you can put all your shared TypeScript files. Due to the fact we are using lerna
using packages as simple as:
yarn lerna add @wp-reactjs-multi-starter/mypackage --scope @wp-reactjs-multi-starter/myplugin
yarn lerna link
Afterwards you need to enqueue that package to your plugin. Why? All packages are so-called externals and WordPress handles it with an own system: Each package / module has an own handle and exposes functionality. Navigate to your Assets.php
and enqueue the package:
$this->enqueueComposerScript('mypackage');
In your plugins' coding you can access the files simply as follow:
import /* [...] */ "@wp-reactjs-multi-starter/mypackage";
Do not forget navigating to your plugin which consumes the dependency and add the dependency path to .wprjss only changes
.
{% hint style="info" %}
You have to replace wp-reactjs-multi-starter
with your names.
{% endhint %}
A package can also be localized with commands yarn i18n:backend
and yarn i18n:generate:frontend
. All .pot
files will be generated to languages
. If you consume a package via enqueueComposerScript
and composer require
all localization files are automatically loaded to the WordPress runtime, you do not have to manually load them!