9. Wordpress ❤ Composer
Custom Installers
At times it may be necessary for a
package to require additional actions
during installation, such as installing
packages outside of the default
vendor library.
{
"name": "my-vendor/my-lib-plugin",
"type": "my-lib-plugin",
"require": {
"my-vendor/my-lib-installer": "*"
}
}
10. ComposerInstallerInstallerInterface
interface InstallerInterface
{
//Decides if the installer supports the given type
public function supports($packageType);
//Checks that provided package is installed.
public function isInstalled(InstalledRepositoryInterface $repo, PackageInterface $package);
//Installs specific package.
public function install(InstalledRepositoryInterface $repo, PackageInterface $package);
//Updates specific package.
public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target);
//Uninstalls specific package.
public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package);
//Returns the installation path of a package
public function getInstallPath(PackageInterface $package);
}
11. A basic Installer Plugin
1. The package file: composer.json
2. The Plugin class, e.g.: MyProjectComposerPlugin.php, implements
ComposerPluginPluginInterface.
3. The Installer class, e.g.: MyProjectComposerInstaller.php, implements
ComposerInstallerInstallerInterface.
12. Wordpress Core itself is a dependency
2) johnpbloch/wordpress
A fork of WordPress with Composer support added. Synced every 15
minutes.
1) johnpbloch/wordpress-core-installer
A custom installer to handle deploying WordPress with composer.
13. Wordpress Core itself is a dependency
use ComposerInstallerLibraryInstaller;
use ComposerPackagePackageInterface;
class WordPressCoreInstaller extends LibraryInstaller {
const TYPE = 'wordpress-core';
public function getInstallPath( PackageInterface $package ) {
$extra = $package->getExtra();
if ( ! $installationDir && ! empty( $extra['wordpress-install-dir'] ) ) {
$installationDir = $extra['wordpress-install-dir'];
}
return $installationDir;
}
public function supports( $packageType ) {
return self::TYPE === $packageType;
}
}
14. Wordpress Core itself is a dependency
"require": {
"johnpbloch/wordpress": "4.3.1"
},
"extra": {
"wordpress-install-dir": "wp"
},
15. Manage Plugins with Composer
composer/installers
A Multi-Framework Composer Library Installer. It will magically install their package to the
correct location based on the specified package type.
● wordpress-plugin => wp-content/plugins/{$name}
● wordpress-theme => wp-content/themes/{$name}
● wordpress-muplugin => wp-content/mu-plugins/{$name}
17. Manage Plugins with Composer
Limited to plugins that already have a
composer.json.
What about 3th party plugins from
https://wordpress.org/plugins/ ?
Wordpress Packagist
Mirrors the WordPress Plugin and
Theme directories as a Composer
repository.
21. A CLI for Wordpress
composer require wp-cli/wp-cli
WP-CLI is a set of command-line tools for managing WordPress installations.
22. Run Commands with WP-CLI
wp <command> <sub-command> <params>
● core: Download, install, update, manage WordPress.
● db: Perform basic database operations.
● plugin: Manage, install, activate, deactivate plugins.
● post: Manage, generate, publish posts.
● scaffold: Generate code for post types, plugins, etc.
● and many more... http://wp-cli.org/commands/
23. Wordpress Automated Testing
Official test suite repository.
https://develop.svn.wordpress.org/trunk/
Time: 4.58 minutes, Memory: 97.75Mb
There was 1 failure:
FAILURES!
Tests: 4251, Assertions: 15938, Failures: 1, Skipped: 65.
https://make.wordpress.org/core/handbook/testing/automated-testing/
24. Test Your Plugins!
$ wp scaffold plugin test_plugin
├──bin/
│ └──install-wp-tests.sh
├──phpunit.xml
├──test_plugin.php
└──tests/
├──bootstrap.php
└──test-sample.php
└──.travis.yml
// This will install WP test suite
// under “/tmp/wordpress”
$ bash bin/install-wp-tests.sh wordpress_test root ''
25. Test Your Plugins!
class FooTest extends WP_UnitTestCase
{
function test_bar() {
// replace this your test
$this->assertTrue( true );
}
}
● Object Factories:
$this->factory->post->create();
● Custom Asserts:
$response = wp_remote_get( $url );
$this->assertWPError( $response );
$this->assertQueryTrue( 'is_tag', 'is_archive' );
● Useful Methods:
setUp() | tearDown() | go_to() |
remove_added_uploads() ecc..