Skip to content

WP Rocket Development Handbook

Rémy Perona edited this page Dec 13, 2019 · 7 revisions

Introduction

This handbook outlines the necessary knowledge to contribute to WP Rocket code base, for any developer or contributor.

Getting Started

To be able to work on WP Rocket code base, you will need the following:

  • A code editor application. Most of our team uses VS Code currently, as it's modern and offers a lot of useful extensions.
  • Composer installed on your computer, to be able to manage the WP Rocket dependencies.
  • Ideally, a WordPress installation on a local development application like Local. This will make it easier to run tests and control the development environment.

This repository should be cloned in the wp-content/plugins directory of your WP development installation. Once cloned, you will need to execute the following command to install the dependencies composer install.

To activate the plugin, you will also need to add the following constants in the wp-config.php file:

define( 'WP_ROCKET_EMAIL', 'your email' );
define( 'WP_ROCKET_KEY', 'your api key' );

You can get the values from your WP Rocket account.

Once the plugin is up and running, you can start contributing!

VSCode Extensions

Coding Standards

WP Rocket is following the WordPress Coding Standards, with some exceptions configured in the phpcs.xml file found at the root of the project.

The standards are enforced using PHP Code Sniffer and the WP Coding Standards rules, which are both installed by composer for the project.

Your IDE of choice should be warning you when the code you wrote is not complying with the standards, so you can fix it before committing or sending a pull request.

Code Structure

WP Rocket code base is currently a mix between legacy procedural code from the initial development of the plugin, and OOP code aiming to follow modern PHP standards while integrating into the WordPress framework.

PHP classes are contained in the inc/classes folder, following a nested structure to separate each part.

The project is using the root WP_Rocket namespace, with subnamespaces mapping the subfolders structure in the classes folder.

Dependency Injection Container

WP Rocket uses League/Container v2 as its dependency injection container. Each new class added to the project should be registered and instantiated using the container, and its service provider feature if needed. Service Providers can be found in the classes/ServiceProvider folder.

Event Manager & Subscriber System

WordPress uses a way to interact/modify pieces of code, called Hooks. WP Rocket interacts with it via an event manager & subscribers system, which can be found in classes/event-management folder. This interface allows the plugin to decouple from WordPress itself, and make its codebase easier to manage and test.

Classes than need to hook on actions and filters need to:

  • be created in the classes/subscriber folder
  • implement the WP_Rocket\Event_Management\Subscriber_Interface
  • use the get_subscribed_events method to associate callbacks to the actions/filters

To add the subscriber to the event manager, it needs to be listed in the classes/class-plugin.php file, along the other existing subscribers. A boilerplate subscriber is available a starting point.

Testing Suite

WP Rocket testing suite is located in the tests folder at the root of the project.

There is currently two types of testing:

  • Unit tests using PHPUnit and Brain Monkey to easily mock PHP functions and WP plugin API. They are installed for the project via composer.
  • Integration tests, which runs in a WordPress test environment

Both set of tests can be easily run by using the following commands:

  • composer test-unit to run unit tests
  • composer test-integration to run integration tests
  • composer run-tests to run both

More details about how to write tests on the Writing Tests page.

Automated testing

For each new pull request, tests will automatically run using Travis CI, on a matrix of PHP and WP installations, to make sure the code is validated against the PHP & WP versions supported by WP Rocket.

A pull request can be merged only if all tests pass.