wordpress development: a modern approach
TRANSCRIPT
Wordpress Develompent
A Modern Approach
Who am I ?Backend Developer
@Populis
@whitekross on twitterAlessandro Fiore
The most used PHP software
https://wordpress.org/download/counter/
24,5% (of the Web)
http://w3techs.com/technologies/overview/content_management/all
Innovator’s Dilemma ?
Wordpress, we have some problems
● Poor Core Codebase
● 3th Party Codebase even poorer
● Lack of standards
Use Better Tools!
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": "*"
}
}
Composer\Installer\InstallerInterface
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);
}
A basic Installer Plugin
1. The package file: composer.json
2. The Plugin class, e.g.: My\Project\Composer\Plugin.php, implements
Composer\Plugin\PluginInterface.
3. The Installer class, e.g.: My\Project\Composer\Installer.php, implements
Composer\Installer\InstallerInterface.
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.
Wordpress Core itself is a dependency
use Composer\Installer\LibraryInstaller;use Composer\Package\PackageInterface;
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; }}
Wordpress Core itself is a dependency
"require": {
"johnpbloch/wordpress": "4.3.1"
},
"extra": {
"wordpress-install-dir": "wp"
},
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}
Manage Plugins with Composer
"extra": {
"installer-paths": {
"src/mu-plugins/{$name}/": ["type:wordpress-muplugin"],
"src/plugins/{$name}/": ["type:wordpress-plugin"],
"src/themes/{$name}/": ["type:wordpress-theme"]
},
}
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.
Manage Plugins with Composer
"repositories":[{
"type":"composer",
"url":"http://wpackagist.org"
}
],
"require": {
"aws/aws-sdk-php":"*",
"wpackagist-plugin/akismet":"dev",
"wpackagist-plugin/captcha":"3.9",
"wpackagist-theme/hueman":"*"
},
Wordpress ❤ Composer
├── composer.json├── composer.lock├── index.php├── src/├── vendor/└── wp/
├── index.php
├── wp-activate.php
├── wp-admin/
├── wp-blog-header.php
├── wp-comments-post.php
├── wp-config.php
├── wp-content/
├── wp-cron.php
├── wp-includes/
├── wp-links-opml.php
├── wp-load.php
├── wp-login.php
├── wp-mail.php
├── wp-settings.php
├── wp-signup.php
├── wp-trackback.php
└── xmlrpc.php
Change WP Directory Structure
// INDEX.php (under our new webroot)
/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/wp/wp-blog-header.php' );
/** Change Content dir */
define( 'WP_CONTENT_DIR', dirname(__FILE__) . '/../src' );
define( 'WP_CONTENT_URL', 'http://<domain>//src' );
// WP_CONFIG.php
/** Change WORDPRESS url */
define( 'WP_SITEURL','http://<domain>/wp');
define( 'WP_HOME','http://<domain>/');
A CLI for Wordpress
composer require wp-cli/wp-cli
WP-CLI is a set of command-line tools for managing WordPress installations.
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/
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/
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 ''
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..
Grazie!
Code Samples:
● https://github.com/whitekross/wp-composer● https://github.com/whitekross/wp-badge-poser
Useful Links:
● https://roots.io/bedrock/● http://composer.rarst.net/● http://codesymphony.co/writing-wordpress-plugin-unit-tests/