Using Composer, Laravel and Guzzle in your WordPress Plugin

Enter Composer. Are you still downloading your PHP libraries as .zip’s? Stop! With Composer, your dependencies become easily manageable, updatable and autoloaded. Worried that your favourite PHP library doesn’t support Composer? Chances are it does.

In this tutorial, we’re going to build a simple plugin that utilizes libraries from Laravel and Guzzle. I’ll show you how to autoload these classes, and how to use them inside your plugin.

Before we begin, make sure you have Composer installed and updated. Running composer self-update will ensure you have the latest version.

Let’s start by using the WordPress Plugin Boilerplate.

[code language=”shell”]
cd myproject/wp-content/plugins
git clone https://github.com/tommcfarlin/WordPress-Plugin-Boilerplate.git
mv WordPress-Plugin-Boilerplate/plugin-name plugin-name && rm -rf WordPress-Plugin-Boilerplate && cd plugin-name
[/code]

For brevity’s sake, we’ll leave the plugin name as is. Normally you’d go through and change the name and reference all the @TODOs.

Inside the plugin-name folder, create a file called composer.json:

[code language=”javascript”]
{
“require”: {
“guzzlehttp/guzzle”: “~4.0”,
“illuminate/cache”: “4.x”,
“illuminate/filesystem”: “4.x”,
“illuminate/database”: “4.x”
}
}
[/code]

We’re telling composer that we require the following for our project:

  1. Guzzle: great for working with RESTful APIs
  2. Illuminate Cache: Laravel’s caching library
  3. Illuminate Filesystem: Needed to save the cache to a file
  4. Illuminate Database: Useful for tasks that WP_Query chokes at. Includes the great Eloquent ORM

Next, while in the newly created plugin-name in Terminal, we’ll tell composer to install:

[code language=”shell”]
$ composer install
Loading composer repositories with package information
Installing dependencies (including require-dev)
– Installing guzzlehttp/streams (2.1.0)
Downloading: 100%

– Installing guzzlehttp/guzzle (4.2.1)
Downloading: 100%

– Installing nesbot/carbon (1.11.0)
Downloading: 100%

– Installing illuminate/support (v4.2.8)
Downloading: 100%

– Installing illuminate/cache (v4.2.8)
Downloading: 100%

– Installing symfony/finder (v2.5.3)
Loading from cache

– Installing illuminate/filesystem (v4.2.8)
Downloading: 100%

– Installing illuminate/container (v4.2.8)
Downloading: 100%

– Installing illuminate/events (v4.2.8)
Downloading: 100%

– Installing illuminate/database (v4.2.8)
Downloading: 100%

Writing lock file
Generating autoload files
[/code]

Next, we need to tell our plugin to autoload these classes. Composer includes a handy autoloader, so all you have to do is require it. Place the following in your plugin-name.php file, before any of the other requires:

[code language=”php”]
<?php
// For composer dependencies
require ‘vendor/autoload.php’;
[/code]

Great, now let’s start using these libraries. In plugin-name/public/class-plugin-name.php, we need to add some use as statements before the class is defined (read about namespace importing here):

[code language=”php”]
<?php

use IlluminateFilesystemFilesystem as Filesystem;
use IlluminateCacheFileStore as FileStore;
use IlluminateDatabaseCapsuleManager as Capsule;

class Plugin_Name {
[/code]

Next, let’s add some configuration in our __construct() so we can start using Laravel’s database library (including the Eloquent ORM) inside our plugin:

[code language=”php”]
private function __construct() {
$capsule = new Capsule;

$capsule->addConnection(array(
‘driver’ => ‘mysql’,
‘host’ => DB_HOST,
database’ => DB_NAME,
‘username’ => DB_USER,
‘password’ => DB_PASSWORD,
‘charset’ => ‘utf8’,
‘collation’ => ‘utf8_unicode_ci’,
‘prefix’ => ”
));

$capsule->setAsGlobal();
$capsule->bootEloquent();
[/code]

Our plugin is going to get records from an external API and cache them on the filesystem. To do so, we’ll create a function called getRecords() that will handle this for us:

[code language=”php”]
public function getRecords($id)
{
$cache_name = ‘records_’ . $id;
$cache = new FileStore(new Filesystem($cache_name . ‘.txt’), __DIR__ . ‘/cache’);

// If cache exists
if ($cache->get($cache_name)) {
return $cache->get($cache_name);
} else {
try {
// Try to get records
$client = new GuzzleHttp(‘https://api.hello.com/records/1399394?access_token=w3r2232r’);
$request = $client->get()->send();
$records = json_decode($request->getBody(), true);

// Save records in cache
$cache->put($cache_name, $records, 600);
return $records;
} catch (GuzzleHttpExceptionBadResponseException $e) {
$raw_response = explode(“n”, $e->getResponse());
throw new IDPException(end($raw_response));
}
}
}
[/code]

Instead of hitting the API every single request, we check the cache first and serve that as long as it’s not out of date.

Next, let’s use Laravel’s database library in the most simplest way possible: inserting a bunch of records. We have a database that stores class information, so we need to send the classes function an array of records, and we’ll do a foreach loop to insert these into the database:

[code language=”php”]
public function classes($records)
{
if ($records) {
$results = array();

$i = 0;

foreach ($records as $k => $v) {
$results[$i][‘start_date’] = $v[‘fields’][‘StartDate’];
$results[$i][‘end_date’] = $v[‘fields’][‘EndDate’];
$results[$i][‘program_code’] = $v[‘fields’][‘ProgCode’];
$results[$i][‘program_name’] = $v[‘fields’][‘Program’];

$i++;
}

Capsule::table(‘classes’)->insert($results);
}
}
[/code]

There you have it! Using Composer in your plugin is pretty easy, and opens up a world of libraries for you to use. If you have any questions or comments, please don’t hesitate to leave feedback below.

 

Contact Us