woocommerce/plugins/woocommerce/packages
Vladimir Reznichenko bca89cb14f
[dev] pnpm install: speedup postinstall hooks a bit (#51538)
In this PR, we aim to improve the speed of ppm installs by making changes in ppm configuration and post-install hooks. The main principle behind the changes is to leverage the identified parallelization potential.
2024-09-24 08:34:46 +02:00
..
woocommerce-admin Remove PHP 8.1 warnings and errors (#34336) 2022-08-16 10:22:53 -07:00
README.md [dev] pnpm install: speedup postinstall hooks a bit (#51538) 2024-09-24 08:34:46 +02:00

README.md

External Packages

This directory holds Composer packages containing functionality developed outside of WooCommerce core.

Installing Composer

You need Composer to use the packages. If you don't have it installed, go and check how to install Composer and then continue here.

Developing new packages

To create a package and/or feature plugin for core, you can base your plugin on the example package.

Packages require a Package class which inits the package and returns version information, and Packages also require that you use the jetpack-autoloader package which prevents version conflicts should the same package be used by multiple plugins at once. This is shown in the example package above.

Publishing a package

Your package should be published to Packagist (example). The package name in this case is woocommerce/woocommerce-example-package.

Including packages in core

Edit composer.json in the root directory and add the package and package version under the "require" section. For example:

{
  "name": "woocommerce/woocommerce",
  "description": "An eCommerce toolkit that helps you sell anything. Beautifully.",
  "homepage": "https://woocommerce.com/",
  "type": "wordpress-plugin",
  "license": "GPL-3.0-or-later",
  "prefer-stable": true,
  "minimum-stability": "dev",
  "require": {
    "composer/installers": "1.6.0",
    "woocommerce/woocommerce-rest-api": "dev-test/jetpack-autoloader",
    "woocommerce/woocommerce-blocks": "dev-build/2.2.0-dev",
    "automattic/jetpack-autoloader": "1.2.0",
    "woocommerce/woocommerce-example-package": "1.0.0"
  },
  ...

Finally, you will need to tell core to load your package. Edit src/Packages.php and add your package to the list of packages there:

	protected static $packages = [
		'woocommerce-blocks'          => '\\Automattic\\WooCommerce\\Blocks\\Package',
    'woocommerce-rest-api'        => '\\Automattic\\WooCommerce\\RestApi\\Package',
    'woocommerce-example-package' => '\\Automattic\\WooCommerce\\ExamplePackage\\Package',
	];

You can add tests to ensure your package is loaded to the WooCommerce unit-tests. Some tests exist in unit-tests/tests/packages/packages.php which you can use as an example.

Installing packages

Once you have defined your package requirements, run

composer install

and that will install the required Composer packages.

Using packages

To use something from a package, you have to declare it at the top of the file before any other instruction, and then use it in the code. For example:

use Automattic\WooCommerce\ExamplePackage\ExampleClass;

// other code...

$class = new ExampleClass();

If you need to rule out conflicts, you can alias it:

use Automattic\WooCommerce\ExamplePackage\ExampleClass as Example_Class_Alias;

// other code...

$class = new Example_Class_Alias();